more structure updates
This commit is contained in:
		| @@ -2,3 +2,5 @@ import * as plugins from "./smartsocket.plugins"; | |||||||
|  |  | ||||||
| export * from "./smartsocket.classes.smartsocket"; | export * from "./smartsocket.classes.smartsocket"; | ||||||
| export * from "./smartsocket.classes.smartsocketclient"; | export * from "./smartsocket.classes.smartsocketclient"; | ||||||
|  |  | ||||||
|  | // need something more exposed? Create an issue on GitLab! | ||||||
| @@ -43,6 +43,9 @@ export class SocketConnection { | |||||||
|         this.role = optionsArg.role; |         this.role = optionsArg.role; | ||||||
|         this.socket = optionsArg.socket; |         this.socket = optionsArg.socket; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // authenticating -------------------------- | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * authenticate the socket |      * authenticate the socket | ||||||
|      */ |      */ | ||||||
| @@ -54,7 +57,7 @@ export class SocketConnection { | |||||||
|             if ((true)) { // TODO: authenticate password |             if ((true)) { // TODO: authenticate password | ||||||
|                 this.alias = dataArg.alias |                 this.alias = dataArg.alias | ||||||
|                 this.authenticated = true; |                 this.authenticated = true; | ||||||
|                 this.role = helpers.findSocketRoleByString(dataArg.role); |                 this.role = helpers.getSocketRoleByName(dataArg.role); | ||||||
|                 this.socket.emit("authenticated"); |                 this.socket.emit("authenticated"); | ||||||
|                 plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`); |                 plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`); | ||||||
|                 done.resolve(this); |                 done.resolve(this); | ||||||
| @@ -67,6 +70,8 @@ export class SocketConnection { | |||||||
|         return done.promise; |         return done.promise; | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|  |     // listening ------------------------------- | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * listen to function requests |      * listen to function requests | ||||||
|      */ |      */ | ||||||
| @@ -85,7 +90,7 @@ export class SocketConnection { | |||||||
|                         shortId:dataArg.shortId, |                         shortId:dataArg.shortId, | ||||||
|                         funcCallData:dataArg.funcCallData |                         funcCallData:dataArg.funcCallData | ||||||
|                     }); |                     }); | ||||||
|                     localSocketRequest |                     localSocketRequest.createResponse(); // takes care of creating response and sending it back  | ||||||
|                 } else { |                 } else { | ||||||
|                     plugins.beautylog.warn("function not existent or out of access scope"); |                     plugins.beautylog.warn("function not existent or out of access scope"); | ||||||
|                 }; |                 }; | ||||||
| @@ -97,5 +102,8 @@ export class SocketConnection { | |||||||
|             done.reject("socket needs to be authenticated first"); |             done.reject("socket needs to be authenticated first"); | ||||||
|         }; |         }; | ||||||
|         return done.promise; |         return done.promise; | ||||||
|     } |     }; | ||||||
|  |  | ||||||
|  |     // sending ---------------------- | ||||||
|  |      | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| import * as plugins from "./smartsocket.plugins"; | import * as plugins from "./smartsocket.plugins"; | ||||||
|  |  | ||||||
| // import classes | // import classes | ||||||
| import { Stringmap } from "lik"; | import { Objectmap } from "lik"; | ||||||
| import { SocketRole } from "./smartsocket.classes.socketrole"; | import { SocketRole } from "./smartsocket.classes.socketrole"; | ||||||
|  |  | ||||||
| // export interfaces | // export interfaces | ||||||
| @@ -12,20 +12,23 @@ import { SocketRole } from "./smartsocket.classes.socketrole"; | |||||||
| /** | /** | ||||||
|  * interface of the contructor options of class SocketFunction |  * interface of the contructor options of class SocketFunction | ||||||
|  */ |  */ | ||||||
| export interface ISocketFunctionOptions { | export interface ISocketFunctionConstructorOptions { | ||||||
|     funcName: string; |     funcName: string; | ||||||
|     funcDef: any; |     funcDef: any; | ||||||
|     allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction |     allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * interface of the Socket Function call  |  * interface of the Socket Function call, in other words the object that routes a call to a function | ||||||
|  */ |  */ | ||||||
| export interface ISocketFunctionCall { | export interface ISocketFunctionCall { | ||||||
|     funcName:string; |     funcName:string; | ||||||
|     funcDataArg:any; |     funcDataArg:any; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // export objects | ||||||
|  | export let allSocketFunctions = new Objectmap<SocketFunction>(); | ||||||
|  |  | ||||||
| // export classes | // export classes | ||||||
|  |  | ||||||
| /** | /** | ||||||
| @@ -39,13 +42,14 @@ export class SocketFunction { | |||||||
|     /** |     /** | ||||||
|      * the constructor for SocketFunction |      * the constructor for SocketFunction | ||||||
|      */ |      */ | ||||||
|     constructor(optionsArg: ISocketFunctionOptions) { |     constructor(optionsArg: ISocketFunctionConstructorOptions) { | ||||||
|         this.name = optionsArg.funcName; |         this.name = optionsArg.funcName; | ||||||
|         this.func = optionsArg.funcDef; |         this.func = optionsArg.funcDef; | ||||||
|         this.roles = optionsArg.allowedRoles; |         this.roles = optionsArg.allowedRoles; | ||||||
|         for (let socketRoleArg of this.roles){ |         for (let socketRoleArg of this.roles){ | ||||||
|             this._notifyRole(socketRoleArg); |             this._notifyRole(socketRoleArg); | ||||||
|         } |         }; | ||||||
|  |         allSocketFunctions.add(this); // map instance with Objectmap | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     /**  |     /**  | ||||||
| @@ -60,6 +64,7 @@ export class SocketFunction { | |||||||
|      */ |      */ | ||||||
|     invoke(dataArg:any):plugins.q.Promise<any> { |     invoke(dataArg:any):plugins.q.Promise<any> { | ||||||
|         let done = plugins.q.defer(); |         let done = plugins.q.defer(); | ||||||
|  |  | ||||||
|         return done.promise;         |         return done.promise;         | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -41,21 +41,17 @@ export class SocketRequest { | |||||||
|     side: TSocketRequestSide; |     side: TSocketRequestSide; | ||||||
|     shortid: string; |     shortid: string; | ||||||
|     originSocketConnection:SocketConnection; |     originSocketConnection:SocketConnection; | ||||||
|     requestData: ISocketRequestDataObject; |     funcCallData: ISocketFunctionCall | ||||||
|     done = plugins.q.defer(); |     done = plugins.q.defer(); | ||||||
|     constructor(optionsArg: SocketRequestConstructorOptions) { |     constructor(optionsArg: SocketRequestConstructorOptions) { | ||||||
|         this.side = optionsArg.side; |         this.side = optionsArg.side; | ||||||
|         this.shortid = optionsArg.shortId; |         this.shortid = optionsArg.shortId; | ||||||
|  |         this.funcCallData = optionsArg.funcCallData; | ||||||
|         if(this.side === "requesting"){ |         if(this.side === "requesting"){ | ||||||
|             allRequestingSocketRequests.add(this); |             allRequestingSocketRequests.add(this); | ||||||
|         } else { |         } else { | ||||||
|             allRespondingSocketRequests.add(this); |             allRespondingSocketRequests.add(this); | ||||||
|         }; |         }; | ||||||
|         // build request and response dataArg |  | ||||||
|         this.requestData = { |  | ||||||
|             funcCallData:optionsArg.funcCallData, |  | ||||||
|             shortId:optionsArg.shortId |  | ||||||
|         } |  | ||||||
|     }; |     }; | ||||||
|      |      | ||||||
|     // requesting -------------------------- |     // requesting -------------------------- | ||||||
| @@ -64,7 +60,11 @@ export class SocketRequest { | |||||||
|      * dispatches a socketrequest from the requesting to the receiving side |      * dispatches a socketrequest from the requesting to the receiving side | ||||||
|      */ |      */ | ||||||
|     dispatch(){ |     dispatch(){ | ||||||
|         this.originSocketConnection.socket.emit("function",this.requestData); |         let requestData:ISocketRequestDataObject = { | ||||||
|  |             funcCallData:this.funcCallData, | ||||||
|  |             shortId:this.shortid | ||||||
|  |         } | ||||||
|  |         this.originSocketConnection.socket.emit("function",requestData); | ||||||
|         return this.done.promise; |         return this.done.promise; | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,13 +2,22 @@ import * as plugins from "./smartsocket.plugins"; | |||||||
|  |  | ||||||
| // classes | // classes | ||||||
| import { Smartsocket } from "./smartsocket.classes.smartsocket"; | import { Smartsocket } from "./smartsocket.classes.smartsocket"; | ||||||
| import { SocketFunction } from "./smartsocket.classes.socketfunction"; | import { SocketFunction, allSocketFunctions } from "./smartsocket.classes.socketfunction"; | ||||||
| import { SocketConnection } from "./smartsocket.classes.socketconnection"; | import { SocketConnection } from "./smartsocket.classes.socketconnection"; | ||||||
| import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole"; | import { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole"; | ||||||
|  |  | ||||||
|  | // SocketFunction helpers | ||||||
|  | export let getSocketFunctionByName = (functionNameArg:string) => { | ||||||
|  |     return allSocketFunctions.find((socketFunctionArg) => { return socketFunctionArg.name === functionNameArg}); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| // SocketRole helpers | // SocketRole helpers | ||||||
| export let findSocketRoleByString = (socketRoleNameArg: string): SocketRole => { |  | ||||||
|  | /** | ||||||
|  |  * get corresponding SocketRequest instance by name | ||||||
|  |  */ | ||||||
|  | export let getSocketRoleByName = (socketRoleNameArg: string): SocketRole => { | ||||||
|     return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg }) |     return allSocketRoles.find((socketRoleArg) => { return socketRoleArg.name === socketRoleNameArg }) | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user