more structure updates

This commit is contained in:
Philipp Kunz 2016-08-12 03:22:36 +02:00
parent cb3d7f4d7b
commit da510eb87a
5 changed files with 41 additions and 17 deletions

View File

@ -2,3 +2,5 @@ import * as plugins from "./smartsocket.plugins";
export * from "./smartsocket.classes.smartsocket";
export * from "./smartsocket.classes.smartsocketclient";
// need something more exposed? Create an issue on GitLab!

View File

@ -43,6 +43,9 @@ export class SocketConnection {
this.role = optionsArg.role;
this.socket = optionsArg.socket;
}
// authenticating --------------------------
/**
* authenticate the socket
*/
@ -54,7 +57,7 @@ export class SocketConnection {
if ((true)) { // TODO: authenticate password
this.alias = dataArg.alias
this.authenticated = true;
this.role = helpers.findSocketRoleByString(dataArg.role);
this.role = helpers.getSocketRoleByName(dataArg.role);
this.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
done.resolve(this);
@ -67,6 +70,8 @@ export class SocketConnection {
return done.promise;
};
// listening -------------------------------
/**
* listen to function requests
*/
@ -85,7 +90,7 @@ export class SocketConnection {
shortId:dataArg.shortId,
funcCallData:dataArg.funcCallData
});
localSocketRequest
localSocketRequest.createResponse(); // takes care of creating response and sending it back
} else {
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");
};
return done.promise;
}
};
// sending ----------------------
};

View File

@ -1,7 +1,7 @@
import * as plugins from "./smartsocket.plugins";
// import classes
import { Stringmap } from "lik";
import { Objectmap } from "lik";
import { SocketRole } from "./smartsocket.classes.socketrole";
// export interfaces
@ -12,20 +12,23 @@ import { SocketRole } from "./smartsocket.classes.socketrole";
/**
* interface of the contructor options of class SocketFunction
*/
export interface ISocketFunctionOptions {
export interface ISocketFunctionConstructorOptions {
funcName: string;
funcDef: any;
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 {
funcName:string;
funcDataArg:any;
}
// export objects
export let allSocketFunctions = new Objectmap<SocketFunction>();
// export classes
/**
@ -39,13 +42,14 @@ export class SocketFunction {
/**
* the constructor for SocketFunction
*/
constructor(optionsArg: ISocketFunctionOptions) {
constructor(optionsArg: ISocketFunctionConstructorOptions) {
this.name = optionsArg.funcName;
this.func = optionsArg.funcDef;
this.roles = optionsArg.allowedRoles;
for (let socketRoleArg of this.roles){
this._notifyRole(socketRoleArg);
}
};
allSocketFunctions.add(this); // map instance with Objectmap
};
/**
@ -60,6 +64,7 @@ export class SocketFunction {
*/
invoke(dataArg:any):plugins.q.Promise<any> {
let done = plugins.q.defer();
return done.promise;
};

View File

@ -41,21 +41,17 @@ export class SocketRequest {
side: TSocketRequestSide;
shortid: string;
originSocketConnection:SocketConnection;
requestData: ISocketRequestDataObject;
funcCallData: ISocketFunctionCall
done = plugins.q.defer();
constructor(optionsArg: SocketRequestConstructorOptions) {
this.side = optionsArg.side;
this.shortid = optionsArg.shortId;
this.funcCallData = optionsArg.funcCallData;
if(this.side === "requesting"){
allRequestingSocketRequests.add(this);
} else {
allRespondingSocketRequests.add(this);
};
// build request and response dataArg
this.requestData = {
funcCallData:optionsArg.funcCallData,
shortId:optionsArg.shortId
}
};
// requesting --------------------------
@ -64,7 +60,11 @@ export class SocketRequest {
* dispatches a socketrequest from the requesting to the receiving side
*/
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;
};

View File

@ -2,13 +2,22 @@ import * as plugins from "./smartsocket.plugins";
// classes
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 { SocketRole, allSocketRoles } from "./smartsocket.classes.socketrole";
// SocketFunction helpers
export let getSocketFunctionByName = (functionNameArg:string) => {
return allSocketFunctions.find((socketFunctionArg) => { return socketFunctionArg.name === functionNameArg});
}
// 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 })
};