fix(core): update

This commit is contained in:
Philipp Kunz 2019-08-13 11:36:31 +02:00
parent e188b18016
commit 0d94ed9345
7 changed files with 64 additions and 57 deletions

View File

@ -9,8 +9,9 @@ import smartsocket = require('../ts/index');
let testSmartsocket: smartsocket.Smartsocket; let testSmartsocket: smartsocket.Smartsocket;
let testSmartsocketClient: smartsocket.SmartsocketClient; let testSmartsocketClient: smartsocket.SmartsocketClient;
let testSocketConnection: smartsocket.SocketConnection;
let testSocketRole1: smartsocket.SocketRole; let testSocketRole1: smartsocket.SocketRole;
let testSocketFunctionServer: smartsocket.SocketFunction; let testSocketFunctionForServer: smartsocket.SocketFunction;
let testSocketFunctionClient: smartsocket.SocketFunction; let testSocketFunctionClient: smartsocket.SocketFunction;
const testConfig = { const testConfig = {
@ -34,14 +35,14 @@ tap.test('should add a socketrole', async () => {
// class SocketFunction // class SocketFunction
tap.test('should register a new Function', async () => { tap.test('should register a new Function', async () => {
testSocketFunctionServer = new smartsocket.SocketFunction({ testSocketFunctionForServer = new smartsocket.SocketFunction({
allowedRoles: [testSocketRole1], allowedRoles: [testSocketRole1],
funcDef: async (dataArg, socketConnectionArg) => { funcDef: async (dataArg, socketConnectionArg) => {
return dataArg; return dataArg;
}, },
funcName: 'testFunction1' funcName: 'testFunction1'
}); });
testSmartsocket.addSocketFunction(testSocketFunctionServer); testSmartsocket.addSocketFunction(testSocketFunctionForServer);
testSocketFunctionClient = new smartsocket.SocketFunction({ testSocketFunctionClient = new smartsocket.SocketFunction({
allowedRoles: [], allowedRoles: [],
@ -50,7 +51,7 @@ tap.test('should register a new Function', async () => {
}, },
funcName: 'testFunction1' funcName: 'testFunction1'
}); });
testSmartsocket.addSocketFunction(testSocketFunctionServer); testSmartsocket.addSocketFunction(testSocketFunctionForServer);
console.log(testSmartsocket.socketFunctions); console.log(testSmartsocket.socketFunctions);
}); });
@ -72,12 +73,6 @@ tap.test('should react to a new websocket connection from client', async () => {
await testSmartsocketClient.connect(); await testSmartsocketClient.connect();
}); });
tap.test('client should disconnect and reconnect', async tools => {
await testSmartsocketClient.disconnect();
await tools.delayFor(100);
await testSmartsocketClient.connect();
});
tap.test('2 clients should connect in parallel', async () => { tap.test('2 clients should connect in parallel', async () => {
// TODO: implement parallel test // TODO: implement parallel test
}); });
@ -90,12 +85,24 @@ tap.test('should be able to make a functionCall from client to server', async ()
}); });
tap.test('should be able to make a functionCall from server to client', async () => { tap.test('should be able to make a functionCall from server to client', async () => {
const response = await testSmartsocketClient.serverCall('testFunction1', { const response = await testSmartsocket.clientCall(
hi: 'hi there from client' 'testFunction1',
}); {
hi: 'hi there from server'
},
testSmartsocket.socketConnections.find(socketConnection => {
return true;
})
);
console.log(response); console.log(response);
}); });
tap.test('client should disconnect and reconnect', async tools => {
await testSmartsocketClient.disconnect();
await tools.delayFor(100);
await testSmartsocketClient.connect();
});
// terminate // terminate
tap.test('should close the server', async () => { tap.test('should close the server', async () => {
await testSmartsocket.stop(); await testSmartsocket.stop();

View File

@ -1,5 +1,3 @@
import * as plugins from './smartsocket.plugins';
// export main classes // export main classes
export * from './smartsocket.classes.smartsocket'; export * from './smartsocket.classes.smartsocket';
export * from './smartsocket.classes.smartsocketclient'; export * from './smartsocket.classes.smartsocketclient';

View File

@ -18,7 +18,7 @@ export interface ISmartsocketConstructorOptions {
export class Smartsocket { export class Smartsocket {
public options: ISmartsocketConstructorOptions; public options: ISmartsocketConstructorOptions;
public io: SocketIO.Server; public io: SocketIO.Server;
public openSockets = new Objectmap<SocketConnection>(); public socketConnections = new Objectmap<SocketConnection>();
public socketRoles = new Objectmap<SocketRole>(); public socketRoles = new Objectmap<SocketRole>();
public socketFunctions = new Objectmap<SocketFunction>(); public socketFunctions = new Objectmap<SocketFunction>();
public socketRequests = new Objectmap<SocketRequest>(); public socketRequests = new Objectmap<SocketRequest>();
@ -50,14 +50,14 @@ export class Smartsocket {
*/ */
public async stop() { public async stop() {
await plugins.smartdelay.delayFor(1000); await plugins.smartdelay.delayFor(1000);
this.openSockets.forEach((socketObjectArg: SocketConnection) => { this.socketConnections.forEach((socketObjectArg: SocketConnection) => {
plugins.smartlog.defaultLogger.log( plugins.smartlog.defaultLogger.log(
'info', 'info',
`disconnect socket with >>alias ${socketObjectArg.alias}` `disconnect socket with >>alias ${socketObjectArg.alias}`
); );
socketObjectArg.socket.disconnect(); socketObjectArg.socket.disconnect();
}); });
this.openSockets.wipe(); this.socketConnections.wipe();
this.io.close(); this.io.close();
// stop the corresponging server // stop the corresponging server
@ -74,7 +74,6 @@ export class Smartsocket {
dataArg: any, dataArg: any,
targetSocketConnectionArg: SocketConnection targetSocketConnectionArg: SocketConnection
) { ) {
const done = plugins.smartpromise.defer();
const socketRequest = new SocketRequest(this, { const socketRequest = new SocketRequest(this, {
funcCallData: { funcCallData: {
funcDataArg: dataArg, funcDataArg: dataArg,
@ -84,10 +83,8 @@ export class Smartsocket {
shortId: plugins.shortid.generate(), shortId: plugins.shortid.generate(),
side: 'requesting' side: 'requesting'
}); });
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => { const response: ISocketFunctionCall = await socketRequest.dispatch();
done.resolve(dataArg2.funcDataArg); const result = response.funcDataArg;
});
const result = await done.promise;
return result; return result;
} }
@ -108,7 +105,7 @@ export class Smartsocket {
/** /**
* the standard handler for new socket connections * the standard handler for new socket connections
*/ */
private _handleSocketConnection(socketArg) { private async _handleSocketConnection(socketArg: plugins.socketIo.Socket) {
const socketConnection: SocketConnection = new SocketConnection({ const socketConnection: SocketConnection = new SocketConnection({
alias: undefined, alias: undefined,
authenticated: false, authenticated: false,
@ -118,14 +115,8 @@ export class Smartsocket {
socket: socketArg socket: socketArg
}); });
plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...'); plugins.smartlog.defaultLogger.log('info', 'Socket connected. Trying to authenticate...');
this.openSockets.add(socketConnection); this.socketConnections.add(socketConnection);
socketConnection await socketConnection.authenticate();
.authenticate() await socketConnection.listenToFunctionRequests();
.then(() => {
return socketConnection.listenToFunctionRequests();
})
.catch(err => {
console.log(err);
});
} }
} }

View File

@ -18,11 +18,10 @@ export interface ISmartsocketClientOptions {
export class SmartsocketClient { export class SmartsocketClient {
public alias: string; public alias: string;
public role: string; public socketRole: SocketRole;
public socketConnection: SocketConnection; public socketConnection: SocketConnection;
public serverUrl: string; public serverUrl: string;
public serverPort: number; public serverPort: number;
public serverPassword: string;
public socketFunctions = new plugins.lik.Objectmap<SocketFunction>(); public socketFunctions = new plugins.lik.Objectmap<SocketFunction>();
public socketRequests = new plugins.lik.Objectmap<SocketRequest>(); public socketRequests = new plugins.lik.Objectmap<SocketRequest>();
@ -30,14 +29,17 @@ export class SmartsocketClient {
constructor(optionsArg: ISmartsocketClientOptions) { constructor(optionsArg: ISmartsocketClientOptions) {
this.alias = optionsArg.alias; this.alias = optionsArg.alias;
this.role = optionsArg.role;
this.serverUrl = optionsArg.url; this.serverUrl = optionsArg.url;
this.serverPort = optionsArg.port; this.serverPort = optionsArg.port;
this.serverPassword = optionsArg.password; this.socketRole = new SocketRole({
name: optionsArg.role,
passwordHash: optionsArg.password
});
} }
public addSocketFunction(socketFunction: SocketFunction) { public addSocketFunction(socketFunction: SocketFunction) {
this.socketFunctions.add(socketFunction); this.socketFunctions.add(socketFunction);
this.socketRole.allowedFunctions.add(socketFunction);
} }
/** /**
@ -50,7 +52,7 @@ export class SmartsocketClient {
this.socketConnection = new SocketConnection({ this.socketConnection = new SocketConnection({
alias: this.alias, alias: this.alias,
authenticated: false, authenticated: false,
role: undefined, role: this.socketRole,
side: 'client', side: 'client',
smartsocketHost: this, smartsocketHost: this,
socket: plugins.socketIoClient(socketUrl, { multiplex: false }) socket: plugins.socketIoClient(socketUrl, { multiplex: false })
@ -58,8 +60,8 @@ export class SmartsocketClient {
this.socketConnection.socket.on('requestAuth', () => { this.socketConnection.socket.on('requestAuth', () => {
console.log('server requested authentication'); console.log('server requested authentication');
this.socketConnection.socket.emit('dataAuth', { this.socketConnection.socket.emit('dataAuth', {
role: this.role, role: this.socketRole.name,
password: this.serverPassword, password: this.socketRole.passwordHash,
alias: this.alias alias: this.alias
}); });
this.socketConnection.socket.on('authenticated', () => { this.socketConnection.socket.on('authenticated', () => {
@ -72,16 +74,21 @@ export class SmartsocketClient {
return done.promise; return done.promise;
} }
public disconnect() { /**
const done = plugins.smartpromise.defer(); * disconnect from the server
this.socketConnection.socket.disconnect(); */
public async disconnect() {
this.socketConnection.socket.disconnect(true);
this.socketConnection = undefined; this.socketConnection = undefined;
plugins.smartlog.defaultLogger.log('ok', 'disconnected!'); plugins.smartlog.defaultLogger.log('ok', 'disconnected!');
done.resolve();
return done.promise;
} }
public serverCall(functionNameArg: string, dataArg: any) { /**
* dispatches a server call
* @param functionNameArg
* @param dataArg
*/
public async serverCall(functionNameArg: string, dataArg: any): Promise<any> {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
const socketRequest = new SocketRequest(this, { const socketRequest = new SocketRequest(this, {
side: 'requesting', side: 'requesting',
@ -92,9 +99,8 @@ export class SmartsocketClient {
funcDataArg: dataArg funcDataArg: dataArg
} }
}); });
socketRequest.dispatch().then((dataArg2: ISocketFunctionCall) => { const response = await socketRequest.dispatch();
done.resolve(dataArg2.funcDataArg); const result = response.funcDataArg;
}); return result;
return done.promise;
} }
} }

View File

@ -123,7 +123,7 @@ export class SocketConnection {
return socketFunctionArg.name === dataArg.funcCallData.funcName; return socketFunctionArg.name === dataArg.funcCallData.funcName;
} }
); );
if (referencedFunction !== undefined) { if (referencedFunction) {
plugins.smartlog.defaultLogger.log('ok', 'function in access scope'); plugins.smartlog.defaultLogger.log('ok', 'function in access scope');
const localSocketRequest = new SocketRequest(this.smartsocketRef, { const localSocketRequest = new SocketRequest(this.smartsocketRef, {
side: 'responding', side: 'responding',

View File

@ -17,7 +17,7 @@ export type TSocketRequestSide = 'requesting' | 'responding';
/** /**
* interface of constructor of class SocketRequest * interface of constructor of class SocketRequest
*/ */
export interface SocketRequestConstructorOptions { export interface ISocketRequestConstructorOptions {
side: TSocketRequestSide; side: TSocketRequestSide;
originSocketConnection: SocketConnection; originSocketConnection: SocketConnection;
shortId: string; shortId: string;
@ -51,13 +51,13 @@ export class SocketRequest {
public shortid: string; public shortid: string;
public originSocketConnection: SocketConnection; public originSocketConnection: SocketConnection;
public funcCallData: ISocketFunctionCall; public funcCallData: ISocketFunctionCall;
public done = plugins.smartpromise.defer(); public done = plugins.smartpromise.defer<ISocketFunctionCall>();
public smartsocketRef: Smartsocket | SmartsocketClient; public smartsocketRef: Smartsocket | SmartsocketClient;
constructor( constructor(
smartsocketRefArg: Smartsocket | SmartsocketClient, smartsocketRefArg: Smartsocket | SmartsocketClient,
optionsArg: SocketRequestConstructorOptions optionsArg: ISocketRequestConstructorOptions
) { ) {
this.smartsocketRef = smartsocketRefArg; this.smartsocketRef = smartsocketRefArg;
this.side = optionsArg.side; this.side = optionsArg.side;
@ -72,7 +72,7 @@ export class SocketRequest {
/** /**
* dispatches a socketrequest from the requesting to the receiving side * dispatches a socketrequest from the requesting to the receiving side
*/ */
public dispatch() { public dispatch(): Promise<ISocketFunctionCall> {
const requestData: ISocketRequestDataObject = { const requestData: ISocketRequestDataObject = {
funcCallData: this.funcCallData, funcCallData: this.funcCallData,
shortId: this.shortid shortId: this.shortid

View File

@ -10,7 +10,7 @@ import { ISocketConnectionAuthenticationObject } from './smartsocket.classes.soc
/** /**
* interface for class SocketRole * interface for class SocketRole
*/ */
export interface SocketRoleOptions { export interface ISocketRoleOptions {
name: string; name: string;
passwordHash: string; passwordHash: string;
} }
@ -43,10 +43,15 @@ export class SocketRole {
public name: string; public name: string;
public passwordHash: string; public passwordHash: string;
public allowedFunctions = new Objectmap<SocketFunction>(); public allowedFunctions = new Objectmap<SocketFunction>();
constructor(optionsArg: SocketRoleOptions) { constructor(optionsArg: ISocketRoleOptions) {
this.name = optionsArg.name; this.name = optionsArg.name;
this.passwordHash = optionsArg.passwordHash; this.passwordHash = optionsArg.passwordHash;
} }
/**
* adds the socketfunction to the socketrole
* @param socketFunctionArg
*/
public addSocketFunction(socketFunctionArg: SocketFunction) { public addSocketFunction(socketFunctionArg: SocketFunction) {
this.allowedFunctions.add(socketFunctionArg); this.allowedFunctions.add(socketFunctionArg);
} }