add some more logic

This commit is contained in:
2016-08-09 11:42:21 +02:00
parent 8d58e0b2f3
commit fee54dfb95
21 changed files with 315 additions and 74 deletions

View File

@ -1,15 +1,28 @@
import * as plugins from "./smartsocket.plugins"
// import interfaces
import { ISocketFunctionRequestObject, ISocketFunctionResponseObject } from "./smartsocket.classes.socketfunction";
// import classes
import { SocketFunction } from "./smartsocket.classes.socketfunction";
/**
* interface for class SmartsocketClient
*/
export interface ISmartsocketClientOptions {
port:number;
url:string;
port: number;
url: string;
}
export class SmartsocketClient {
constructor(){
constructor() {
}
dispatchFunctionRequest(dataArg:ISocketFunctionRequestObject): plugins.q.Promise<ISocketFunctionResponseObject> {
let done = plugins.q.defer<ISocketFunctionResponseObject>();
let responseData:ISocketFunctionResponseObject;
done.resolve(responseData);
return done.promise;
};
}

View File

@ -2,9 +2,15 @@ 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 { SocketRole } from "./smartsocket.classes.socketrole";
import { SocketFunction, ISocketFunctionData } from "./smartsocket.classes.socketfunction";
// export interfaces
/**
* interface for constructor of class SocketConnection
*/
export interface ISocketConnectionOptions {
alias?: string;
authenticated: boolean;
@ -12,6 +18,20 @@ export interface ISocketConnectionOptions {
socket: SocketIO.Socket;
};
/**
* interface for authentication data
*/
export interface ISocketConnectionAuthenticationObject {
role: "coreflowContainer",
password: "somePassword",
alias: "coreflow1"
}
// export classes
/**
* class SocketConnection represents a websocket connection
*/
export class SocketConnection {
alias?: string;
authenticated: boolean;
@ -53,8 +73,15 @@ export class SocketConnection {
listenToFunctionRequests() {
let done = plugins.q.defer();
if(this.authenticated){
this.socket.on("function", (dataArg:ISocketFunctionData) => {
this.role.allowedFunctions
this.socket.on("function", (dataArg:ISocketFunctionRequestObject) => {
let referencedFunction:SocketFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
return socketFunctionArg.name === dataArg.functionName
});
if(referencedFunction !== undefined){
referencedFunction.invoke(dataArg);
} else {
plugins.beautylog.warn("function not existent or out of access scope");
};
})
} else {
done.reject("socket needs to be authenticated first");

View File

@ -4,18 +4,31 @@ import * as plugins from "./smartsocket.plugins";
import { Stringmap } from "lik";
import { SocketRole } from "./smartsocket.classes.socketrole";
export interface ISocketFunctionData {
// export interfaces
export interface ISocketFunctionRequestObject {
functionName:string,
functionData:any,
argumentObject:any,
shortId:string,
responseTimeout?:number
};
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
};
// export classes
/**
* class SocketFunction respresents a function that can be transparently called using a SocketConnection
*/
export class SocketFunction {
name: string;
func: any;
@ -41,9 +54,9 @@ export class SocketFunction {
}
/**
* handles a function request to this SocketFunction
* invokes the function of this SocketFunction
*/
functionRequest(dataArg:ISocketFunctionData){
invoke(dataArg:ISocketFunctionRequestObject){
};

View File

@ -0,0 +1,43 @@
import * as plugins from "./smartsocket.plugins";
// import interfaces
import { ISocketFunctionRequestObject, ISocketFunctionResponseObject } from "./smartsocket.classes.socketfunction";
// import classes
import { Objectmap } from "lik";
import { SocketFunction } from "./smartsocket.classes.socketfunction";
// export interfaces
export type TSocketRequestStatus = "new" | "pending" | "finished";
export type TSocketRequestSide = "requesting" | "responding";
export interface SocketRequestConstructorOptions {
side: TSocketRequestSide;
shortid: string;
};
//export objects
export let allRequestingSocketRequests = new Objectmap<SocketRequest>();
export let allRespondingSocketRequests = new Objectmap<SocketRequest>();
// export classes
export class SocketRequest {
status: TSocketRequestStatus = "new";
side: TSocketRequestSide;
shortid: string;
constructor(optionsArg: SocketRequestConstructorOptions) {
this.side = optionsArg.side;
this.shortid = optionsArg.shortid;
if(this.side === "requesting"){
allRequestingSocketRequests.add(this);
} else {
allRespondingSocketRequests.add(this);
};
};
respond(dataArg:ISocketFunctionResponseObject){
}
private _dispatch(){ // note: dispatch is private as it will be fired from the constructor
}
};

View File

@ -2,6 +2,7 @@ import "typings-global";
export import beautylog = require("beautylog");
export import lik = require("lik");
export import q = require("q");
export import shortid = require("shortid");
export import socketIo = require("socket.io");
export import socketIoClient = require("socket.io-client");
export import taskbuffer = require("taskbuffer");