From cb3d7f4d7bae8897f0231b70d5bc1c6c54b68edc Mon Sep 17 00:00:00 2001 From: PhilKunz Date: Fri, 12 Aug 2016 01:32:57 +0200 Subject: [PATCH] update structure --- ts/smartsocket.classes.smartsocket.ts | 9 ++++- ts/smartsocket.classes.smartsocketclient.ts | 41 +++++++++++++++++++-- ts/smartsocket.classes.socketconnection.ts | 8 ++-- ts/smartsocket.classes.socketrequest.ts | 19 ++++++++-- 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/ts/smartsocket.classes.smartsocket.ts b/ts/smartsocket.classes.smartsocket.ts index d7b4cfe..54a4d24 100644 --- a/ts/smartsocket.classes.smartsocket.ts +++ b/ts/smartsocket.classes.smartsocket.ts @@ -23,7 +23,7 @@ export class Smartsocket { /** * the standard handler for new socket connections */ - private _handleSocket(socketArg) { + private _handleSocketConnection(socketArg) { let socketConnection: SocketConnection = new SocketConnection({ authenticated:false, socket:socketArg @@ -41,7 +41,7 @@ export class Smartsocket { startServer = () => { this.io = plugins.socketIo(this.options.port); this.io.on('connection', (socketArg) => { - this._handleSocket(socketArg); + this._handleSocketConnection(socketArg); }); } closeServer = () => { @@ -51,5 +51,10 @@ export class Smartsocket { }); this.openSockets.wipe(); this.io.close(); + }; + + // communication + clientCall(){ + // TODO: target specific client and initiate response } } \ No newline at end of file diff --git a/ts/smartsocket.classes.smartsocketclient.ts b/ts/smartsocket.classes.smartsocketclient.ts index 7e7b0c5..665faa4 100644 --- a/ts/smartsocket.classes.smartsocketclient.ts +++ b/ts/smartsocket.classes.smartsocketclient.ts @@ -15,14 +15,45 @@ import { SocketRequest } from "./smartsocket.classes.socketrequest"; export interface ISmartsocketClientOptions { port: number; url: string; + alias?:string; // an alias makes ir easier to identify this client in a multo client environment + password: string; // by setting a password access to functions can be limited } export class SmartsocketClient { socketConnection:SocketConnection; + serverUrl:string; + serverPort:number; + serverPassword:string; constructor(optionsArg:ISmartsocketClientOptions){ - // TODO: implement Socket init + this.serverUrl = optionsArg.url + this.serverPort = optionsArg.port; + this.serverPassword = optionsArg.password + }; + + // authenticates the socket against the server + private _handleSocketConnection() { + let done = plugins.q.defer(); + let socketUrl = `${this.serverUrl}:${this.serverPort}`; + this.socketConnection = new SocketConnection({ + authenticated:false, + socket: plugins.socketIoClient(this.serverUrl,{}) + }); + this.socketConnection.socket.on("requestAuth", function () { + console.log("server requested authentication"); + this.socketConnection.socket.emit("dataAuth", { + role: "coreflowContainer", + password: "somePassword", + alias: "coreflow1" + }); + this.socketConnection.socket.on("authenticated",() => { + console.log("client is authenticated"); + done.resolve(); + }); + }); + return done.promise; }; serverCall(functionNameArg:string,dataArg:ISocketFunctionCall){ + let done = plugins.q.defer(); let socketRequest = new SocketRequest({ side:"requesting", originSocketConnection:this.socketConnection, @@ -32,7 +63,11 @@ export class SmartsocketClient { funcDataArg:dataArg } }); - socketRequest.dispatch(); - } + socketRequest.dispatch() + .then(() => { + done.resolve(); + }); + return done.promise; + }; } \ No newline at end of file diff --git a/ts/smartsocket.classes.socketconnection.ts b/ts/smartsocket.classes.socketconnection.ts index 0f394eb..2e7556f 100644 --- a/ts/smartsocket.classes.socketconnection.ts +++ b/ts/smartsocket.classes.socketconnection.ts @@ -11,11 +11,11 @@ import { SocketRole } from "./smartsocket.classes.socketrole"; /** * interface for constructor of class SocketConnection */ -export interface ISocketConnectionOptions { +export interface ISocketConnectionConstructorOptions { alias?: string; authenticated: boolean; role?: SocketRole; - socket: SocketIO.Socket; + socket?: SocketIO.Socket | SocketIOClient.Socket; }; /** @@ -36,8 +36,8 @@ export class SocketConnection { alias?: string; authenticated: boolean; role?: SocketRole; - socket: SocketIO.Socket; - constructor(optionsArg: ISocketConnectionOptions) { + socket: SocketIO.Socket | SocketIOClient.Socket; + constructor(optionsArg: ISocketConnectionConstructorOptions) { this.alias = optionsArg.alias; this.authenticated = optionsArg.authenticated; this.role = optionsArg.role; diff --git a/ts/smartsocket.classes.socketrequest.ts b/ts/smartsocket.classes.socketrequest.ts index ba99de5..616fc54 100644 --- a/ts/smartsocket.classes.socketrequest.ts +++ b/ts/smartsocket.classes.socketrequest.ts @@ -57,17 +57,30 @@ export class SocketRequest { shortId:optionsArg.shortId } }; + + // requesting -------------------------- + /** - * + * dispatches a socketrequest from the requesting to the receiving side */ dispatch(){ this.originSocketConnection.socket.emit("function",this.requestData); return this.done.promise; }; - handleResponse(responseDataArg:ISocketRequestDataObject){ + + /** + * handles the response that is received by the requesting side + */ + private _handleResponse(responseDataArg:ISocketRequestDataObject){ this.done.resolve(responseDataArg); } - createResponse(){ + // responding -------------------------- + + /** + * creates the response on the responding side + */ + createResponse(){ + } };