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 { ISocketFunctionRequestObject, ISocketFunctionResponseObject } from "./smartsocket.classes.socketfunction";
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
import { ISocketRequestDataObject } from "./smartsocket.classes.socketrequest"
// import classes
import { SocketConnection } from "./smartsocket.classes.socketconnection";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketRequest } from "./smartsocket.classes.socketrequest";
/**
* interface for class SmartsocketClient
*/
@ -16,13 +18,21 @@ export interface ISmartsocketClientOptions {
}
export class SmartsocketClient {
constructor() {
socketConnection:SocketConnection;
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 classes
import { SocketFunction, ISocketFunctionRequestObject } from "./smartsocket.classes.socketfunction";
import { SocketRequest } from "./smartsocket.classes.socketrequest";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketRequest, ISocketRequestDataObject } from "./smartsocket.classes.socketrequest";
import { SocketRole } from "./smartsocket.classes.socketrole";
// export interfaces
@ -73,21 +73,22 @@ export class SocketConnection {
listenToFunctionRequests() {
let done = plugins.q.defer();
if(this.authenticated){
this.socket.on("function", (dataArg:ISocketFunctionRequestObject) => {
this.socket.on("function", (dataArg:ISocketRequestDataObject) => {
let referencedFunction:SocketFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
return socketFunctionArg.name === dataArg.functionName
return socketFunctionArg.name === dataArg.funcName
});
if(referencedFunction !== undefined){
let localSocketRequest = new SocketRequest({
side:"responding",
shortid:dataArg.shortId,
originSocketConnection:this,
shortId:dataArg.shortId,
requestData:dataArg
});
} else {
plugins.beautylog.warn("function not existent or out of access scope");
};
});
this.socket.on("functionResponse", (dataArg:ISocketFunctionRequestObject) => {
this.socket.on("functionResponse", (dataArg:ISocketRequestDataObject) => {
})
} else {

View File

@ -6,28 +6,30 @@ import { SocketRole } from "./smartsocket.classes.socketrole";
// 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;
argumentObject:any;
};
export interface SocketFunctionOptions {
name: string;
func: any;
roles: SocketRole[]; // all roles that are allowed to execute a SocketFunction
};
/**
* interface of the Socket Function call
*/
export interface ISocketFunctionCall {
funcName:string;
funcDataArg:any;
}
// 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 {
name: string;
@ -37,10 +39,10 @@ export class SocketFunction {
/**
* the constructor for SocketFunction
*/
constructor(optionsArg: SocketFunctionOptions) {
this.name = optionsArg.name;
this.func = optionsArg.func;
this.roles = optionsArg.roles;
constructor(optionsArg: ISocketFunctionOptions) {
this.name = optionsArg.funcName;
this.func = optionsArg.funcDef;
this.roles = optionsArg.allowedRoles;
for (let socketRoleArg of this.roles){
this._notifyRole(socketRoleArg);
}
@ -56,8 +58,9 @@ export class 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 interfaces
import { ISocketFunctionRequestObject, ISocketFunctionResponseObject } from "./smartsocket.classes.socketfunction";
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
// import classes
import { Objectmap } from "lik";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketConnection } from "./smartsocket.classes.socketconnection";
// export interfaces
export type TSocketRequestStatus = "new" | "pending" | "finished";
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 {
side: TSocketRequestSide;
shortid: string;
requestData?: ISocketFunctionRequestObject;
responseData?:ISocketFunctionResponseObject;
originSocketConnection:SocketConnection;
shortId: string;
funcCallData?: ISocketFunctionCall;
};
//export objects
@ -27,13 +38,12 @@ export class SocketRequest {
status: TSocketRequestStatus = "new";
side: TSocketRequestSide;
shortid: string;
requestData: ISocketFunctionRequestObject;
responseData: ISocketFunctionResponseObject;
originSocketConnection:SocketConnection;
requestData: ISocketRequestDataObject;
responseData: ISocketRequestDataObject;
constructor(optionsArg: SocketRequestConstructorOptions) {
this.side = optionsArg.side;
this.shortid = optionsArg.shortid;
this.requestData = optionsArg.requestData;
this.responseData = optionsArg.responseData;
this.shortid = optionsArg.shortId;
if(this.side === "requesting"){
allRequestingSocketRequests.add(this);
} else {
@ -41,19 +51,23 @@ export class SocketRequest {
};
};
private _sendRequest(dataArg:ISocketFunctionRequestObject){
};
private _receiveRequest(dataArg:ISocketFunctionRequestObject){
};
private _sendResponse(dataArg:ISocketFunctionResponseObject){
respond(dataArg){
}
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
};
};