smartsocket/ts/smartsocket.classes.socketconnection.ts

110 lines
3.7 KiB
TypeScript
Raw Normal View History

2016-08-08 16:20:00 +00:00
import * as plugins from "./smartsocket.plugins";
import * as helpers from "./smartsocket.helpers";
// import classes
2016-08-09 16:22:30 +00:00
import { SocketFunction } from "./smartsocket.classes.socketfunction";
import { SocketRequest, ISocketRequestDataObject } from "./smartsocket.classes.socketrequest";
2016-08-08 16:20:00 +00:00
import { SocketRole } from "./smartsocket.classes.socketrole";
2016-08-09 09:42:21 +00:00
// export interfaces
/**
* interface for constructor of class SocketConnection
*/
2016-08-11 23:32:57 +00:00
export interface ISocketConnectionConstructorOptions {
2016-08-08 16:20:00 +00:00
alias?: string;
authenticated: boolean;
role?: SocketRole;
2016-08-11 23:32:57 +00:00
socket?: SocketIO.Socket | SocketIOClient.Socket;
2016-08-08 16:20:00 +00:00
};
2016-08-09 09:42:21 +00:00
/**
* interface for authentication data
*/
export interface ISocketConnectionAuthenticationObject {
role: "coreflowContainer",
password: "somePassword",
alias: "coreflow1"
}
// export classes
/**
* class SocketConnection represents a websocket connection
*/
2016-08-08 16:20:00 +00:00
export class SocketConnection {
alias?: string;
authenticated: boolean;
role?: SocketRole;
2016-08-11 23:32:57 +00:00
socket: SocketIO.Socket | SocketIOClient.Socket;
constructor(optionsArg: ISocketConnectionConstructorOptions) {
2016-08-08 16:20:00 +00:00
this.alias = optionsArg.alias;
this.authenticated = optionsArg.authenticated;
this.role = optionsArg.role;
this.socket = optionsArg.socket;
}
2016-08-12 01:22:36 +00:00
// authenticating --------------------------
2016-08-08 16:20:00 +00:00
/**
* authenticate the socket
*/
authenticate() {
let done = plugins.q.defer();
this.socket.on("dataAuth", dataArg => {
plugins.beautylog.log("received authentication data. now hashing and comparing...");
this.socket.removeListener("dataAuth", () => { });
if ((true)) { // TODO: authenticate password
this.alias = dataArg.alias
this.authenticated = true;
2016-08-12 01:22:36 +00:00
this.role = helpers.getSocketRoleByName(dataArg.role);
2016-08-08 16:20:00 +00:00
this.socket.emit("authenticated");
plugins.beautylog.ok(`socket with >>alias ${this.alias} >>role ${this.role} is authenticated!`);
done.resolve(this);
} else {
this.socket.disconnect();
done.reject("not authenticated");
};
});
this.socket.emit("requestAuth");
return done.promise;
};
2016-08-12 01:22:36 +00:00
// listening -------------------------------
2016-08-08 16:20:00 +00:00
/**
* listen to function requests
*/
listenToFunctionRequests() {
let done = plugins.q.defer();
if(this.authenticated){
2016-08-09 16:22:30 +00:00
this.socket.on("function", (dataArg:ISocketRequestDataObject) => {
2016-08-09 21:37:25 +00:00
// check if requested function is available to the socket's scope
2016-08-09 09:42:21 +00:00
let referencedFunction:SocketFunction = this.role.allowedFunctions.find((socketFunctionArg) => {
2016-08-09 21:37:25 +00:00
return socketFunctionArg.name === dataArg.funcCallData.funcName;
2016-08-09 09:42:21 +00:00
});
if(referencedFunction !== undefined){
let localSocketRequest = new SocketRequest({
side:"responding",
2016-08-09 16:22:30 +00:00
originSocketConnection:this,
shortId:dataArg.shortId,
2016-08-09 21:37:25 +00:00
funcCallData:dataArg.funcCallData
});
2016-08-12 01:22:36 +00:00
localSocketRequest.createResponse(); // takes care of creating response and sending it back
2016-08-09 09:42:21 +00:00
} else {
plugins.beautylog.warn("function not existent or out of access scope");
};
});
2016-08-09 16:22:30 +00:00
this.socket.on("functionResponse", (dataArg:ISocketRequestDataObject) => {
2016-08-08 16:20:00 +00:00
})
} else {
done.reject("socket needs to be authenticated first");
};
return done.promise;
2016-08-12 01:22:36 +00:00
};
// sending ----------------------
2016-08-08 16:20:00 +00:00
};