some heavy internal refactoring

This commit is contained in:
Philipp Kunz 2016-08-09 18:22:30 +02:00
parent 5109964247
commit 1f8e1fc7cb
4 changed files with 83 additions and 55 deletions

View File

@ -2,11 +2,13 @@ import * as plugins from "./smartsocket.plugins"
// import interfaces // import interfaces
import { ISocketFunctionRequestObject, ISocketFunctionResponseObject } from "./smartsocket.classes.socketfunction"; import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
import { ISocketRequestDataObject } from "./smartsocket.classes.socketrequest"
// import classes // import classes
import { SocketConnection } from "./smartsocket.classes.socketconnection";
import { SocketFunction } from "./smartsocket.classes.socketfunction"; import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketRequest } from "./smartsocket.classes.socketrequest";
/** /**
* interface for class SmartsocketClient * interface for class SmartsocketClient
*/ */
@ -16,13 +18,21 @@ export interface ISmartsocketClientOptions {
} }
export class SmartsocketClient { export class SmartsocketClient {
socketConnection:SocketConnection;
constructor(){ constructor(){
}
dispatchFunctionRequest(dataArg:ISocketFunctionRequestObject): plugins.q.Promise<ISocketFunctionResponseObject> {
let done = plugins.q.defer<ISocketFunctionResponseObject>();
let responseData:ISocketFunctionResponseObject;
done.resolve(responseData);
return done.promise;
}; };
serverCall(functionNameArg:string,dataArg:ISocketFunctionCall){
let socketRequest = new SocketRequest({
side:"requesting",
originSocketConnection:this.socketConnection,
shortId:plugins.shortid.generate(),
funcCallData:{
funcName: functionNameArg,
funcDataArg:dataArg
}
});
}
} }

View File

@ -2,8 +2,8 @@ import * as plugins from "./smartsocket.plugins";
import * as helpers from "./smartsocket.helpers"; import * as helpers from "./smartsocket.helpers";
// import classes // import classes
import { SocketFunction, ISocketFunctionRequestObject } from "./smartsocket.classes.socketfunction"; import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketRequest } from "./smartsocket.classes.socketrequest"; import { SocketRequest, ISocketRequestDataObject } from "./smartsocket.classes.socketrequest";
import { SocketRole } from "./smartsocket.classes.socketrole"; import { SocketRole } from "./smartsocket.classes.socketrole";
// export interfaces // export interfaces
@ -73,21 +73,22 @@ export class SocketConnection {
listenToFunctionRequests() { listenToFunctionRequests() {
let done = plugins.q.defer(); let done = plugins.q.defer();
if(this.authenticated){ if(this.authenticated){
this.socket.on("function", (dataArg:ISocketFunctionRequestObject) => { this.socket.on("function", (dataArg:ISocketRequestDataObject) => {
let referencedFunction:SocketFunction = this.role.allowedFunctions.find((socketFunctionArg) => { let referencedFunction:SocketFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
return socketFunctionArg.name === dataArg.functionName return socketFunctionArg.name === dataArg.funcName
}); });
if(referencedFunction !== undefined){ if(referencedFunction !== undefined){
let localSocketRequest = new SocketRequest({ let localSocketRequest = new SocketRequest({
side:"responding", side:"responding",
shortid:dataArg.shortId, originSocketConnection:this,
shortId:dataArg.shortId,
requestData:dataArg requestData:dataArg
}); });
} else { } else {
plugins.beautylog.warn("function not existent or out of access scope"); plugins.beautylog.warn("function not existent or out of access scope");
}; };
}); });
this.socket.on("functionResponse", (dataArg:ISocketFunctionRequestObject) => { this.socket.on("functionResponse", (dataArg:ISocketRequestDataObject) => {
}) })
} else { } else {

View File

@ -6,28 +6,30 @@ import { SocketRole } from "./smartsocket.classes.socketrole";
// export interfaces // export interfaces
export interface ISocketFunctionRequestObject {
functionName:string,
argumentObject:any,
shortId:string, /**
responseTimeout?:number * interface of the contructor options of class SocketFunction
*/
export interface ISocketFunctionOptions {
funcName: string;
funcDef: any;
allowedRoles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
}; };
export interface ISocketFunctionResponseObject { /**
shortId:string; * interface of the Socket Function call
argumentObject:any; */
}; export interface ISocketFunctionCall {
funcName:string;
export interface SocketFunctionOptions { funcDataArg:any;
name: string; }
func: any;
roles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
};
// export classes // export classes
/** /**
* class SocketFunction respresents a function that can be transparently called using a SocketConnection * class that respresents a function that can be transparently called using a SocketConnection
*/ */
export class SocketFunction { export class SocketFunction {
name: string; name: string;
@ -37,10 +39,10 @@ export class SocketFunction {
/** /**
* the constructor for SocketFunction * the constructor for SocketFunction
*/ */
constructor(optionsArg: SocketFunctionOptions) { constructor(optionsArg: ISocketFunctionOptions) {
this.name = optionsArg.name; this.name = optionsArg.funcName;
this.func = optionsArg.func; this.func = optionsArg.funcDef;
this.roles = optionsArg.roles; this.roles = optionsArg.allowedRoles;
for (let socketRoleArg of this.roles){ for (let socketRoleArg of this.roles){
this._notifyRole(socketRoleArg); this._notifyRole(socketRoleArg);
} }
@ -56,8 +58,9 @@ export class SocketFunction {
/** /**
* invokes the function of this SocketFunction * invokes the function of this SocketFunction
*/ */
invoke(dataArg:ISocketFunctionRequestObject){ invoke(dataArg:any):plugins.q.Promise<any> {
let done = plugins.q.defer();
return done.promise;
}; };
} }

View File

@ -1,21 +1,32 @@
import * as plugins from "./smartsocket.plugins"; import * as plugins from "./smartsocket.plugins";
// import interfaces // import interfaces
import { ISocketFunctionRequestObject, ISocketFunctionResponseObject } from "./smartsocket.classes.socketfunction"; import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
// import classes // import classes
import { Objectmap } from "lik"; import { Objectmap } from "lik";
import { SocketFunction } from "./smartsocket.classes.socketfunction"; import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
// export interfaces // export interfaces
export type TSocketRequestStatus = "new" | "pending" | "finished"; export type TSocketRequestStatus = "new" | "pending" | "finished";
export type TSocketRequestSide = "requesting" | "responding"; export type TSocketRequestSide = "requesting" | "responding";
/**
* request object that is sent initially and may or may not receive a response
*/
export interface ISocketRequestDataObject {
funcName:string,
funcDataArg:any,
shortId:string,
responseTimeout?:number
};
export interface SocketRequestConstructorOptions { export interface SocketRequestConstructorOptions {
side: TSocketRequestSide; side: TSocketRequestSide;
shortid: string; originSocketConnection:SocketConnection;
requestData?: ISocketFunctionRequestObject; shortId: string;
responseData?:ISocketFunctionResponseObject; funcCallData?: ISocketFunctionCall;
}; };
//export objects //export objects
@ -27,13 +38,12 @@ export class SocketRequest {
status: TSocketRequestStatus = "new"; status: TSocketRequestStatus = "new";
side: TSocketRequestSide; side: TSocketRequestSide;
shortid: string; shortid: string;
requestData: ISocketFunctionRequestObject; originSocketConnection:SocketConnection;
responseData: ISocketFunctionResponseObject; requestData: ISocketRequestDataObject;
responseData: ISocketRequestDataObject;
constructor(optionsArg: SocketRequestConstructorOptions) { constructor(optionsArg: SocketRequestConstructorOptions) {
this.side = optionsArg.side; this.side = optionsArg.side;
this.shortid = optionsArg.shortid; this.shortid = optionsArg.shortId;
this.requestData = optionsArg.requestData;
this.responseData = optionsArg.responseData;
if(this.side === "requesting"){ if(this.side === "requesting"){
allRequestingSocketRequests.add(this); allRequestingSocketRequests.add(this);
} else { } else {
@ -41,19 +51,23 @@ export class SocketRequest {
}; };
}; };
private _sendRequest(dataArg:ISocketFunctionRequestObject){ respond(dataArg){
};
private _receiveRequest(dataArg:ISocketFunctionRequestObject){
};
private _sendResponse(dataArg:ISocketFunctionResponseObject){
} }
private _receiveResponse(dataArg:ISocketFunctionResponseObject){ // private functions
private _sendRequest(dataArg:ISocketRequestDataObject){
}; };
private _dispatch(dataArg:ISocketFunctionRequestObject){ // note: dispatch is private as it will be fired from the constructor private _receiveRequest(dataArg:ISocketRequestDataObject){
};
private _sendResponse(dataArg:ISocketRequestDataObject){
}
private _receiveResponse(dataArg:ISocketRequestDataObject){
};
private _dispatch(dataArg:ISocketRequestDataObject){ // note: dispatch is private as it will be fired from the constructor
}; };
}; };