2016-08-09 09:42:21 +00:00
|
|
|
import * as plugins from "./smartsocket.plugins";
|
2016-08-12 03:17:13 +00:00
|
|
|
import * as helpers from "./smartsocket.helpers";
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// import interfaces
|
2016-08-09 16:22:30 +00:00
|
|
|
import { ISocketFunctionCall } from "./smartsocket.classes.socketfunction";
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// import classes
|
|
|
|
import { Objectmap } from "lik";
|
|
|
|
import { SocketFunction } from "./smartsocket.classes.socketfunction";
|
2016-08-09 16:22:30 +00:00
|
|
|
import { SocketConnection } from "./smartsocket.classes.socketconnection";
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// export interfaces
|
|
|
|
export type TSocketRequestStatus = "new" | "pending" | "finished";
|
|
|
|
export type TSocketRequestSide = "requesting" | "responding";
|
|
|
|
|
2016-08-09 16:22:30 +00:00
|
|
|
/**
|
2016-08-09 21:37:25 +00:00
|
|
|
* interface of constructor of class SocketRequest
|
2016-08-09 16:22:30 +00:00
|
|
|
*/
|
2016-08-09 09:42:21 +00:00
|
|
|
export interface SocketRequestConstructorOptions {
|
|
|
|
side: TSocketRequestSide;
|
2016-08-12 03:17:13 +00:00
|
|
|
originSocketConnection: SocketConnection;
|
2016-08-09 16:22:30 +00:00
|
|
|
shortId: string;
|
|
|
|
funcCallData?: ISocketFunctionCall;
|
2016-08-09 09:42:21 +00:00
|
|
|
};
|
|
|
|
|
2016-08-09 21:37:25 +00:00
|
|
|
/**
|
|
|
|
* request object that is sent initially and may or may not receive a response
|
|
|
|
*/
|
|
|
|
export interface ISocketRequestDataObject {
|
2016-08-12 03:17:13 +00:00
|
|
|
funcCallData: ISocketFunctionCall;
|
|
|
|
shortId: string;
|
|
|
|
responseTimeout?: number;
|
2016-08-09 21:37:25 +00:00
|
|
|
};
|
|
|
|
|
2016-08-09 09:42:21 +00:00
|
|
|
//export objects
|
2016-08-12 03:17:13 +00:00
|
|
|
export let allSocketRequests = new Objectmap<SocketRequest>();
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// export classes
|
|
|
|
export class SocketRequest {
|
|
|
|
status: TSocketRequestStatus = "new";
|
|
|
|
side: TSocketRequestSide;
|
|
|
|
shortid: string;
|
2016-08-12 03:17:13 +00:00
|
|
|
originSocketConnection: SocketConnection;
|
2016-08-12 01:22:36 +00:00
|
|
|
funcCallData: ISocketFunctionCall
|
2016-08-09 21:37:25 +00:00
|
|
|
done = plugins.q.defer();
|
2016-08-09 09:42:21 +00:00
|
|
|
constructor(optionsArg: SocketRequestConstructorOptions) {
|
|
|
|
this.side = optionsArg.side;
|
2016-08-09 16:22:30 +00:00
|
|
|
this.shortid = optionsArg.shortId;
|
2016-08-12 01:22:36 +00:00
|
|
|
this.funcCallData = optionsArg.funcCallData;
|
2016-08-12 03:17:13 +00:00
|
|
|
allSocketRequests.add(this);
|
2016-08-09 09:42:21 +00:00
|
|
|
};
|
2016-08-12 03:17:13 +00:00
|
|
|
|
2016-08-11 23:32:57 +00:00
|
|
|
// requesting --------------------------
|
2016-08-12 03:17:13 +00:00
|
|
|
|
2016-08-09 21:37:25 +00:00
|
|
|
/**
|
2016-08-11 23:32:57 +00:00
|
|
|
* dispatches a socketrequest from the requesting to the receiving side
|
2016-08-09 21:37:25 +00:00
|
|
|
*/
|
2016-08-12 03:17:13 +00:00
|
|
|
dispatch() {
|
|
|
|
let requestData: ISocketRequestDataObject = {
|
|
|
|
funcCallData: this.funcCallData,
|
|
|
|
shortId: this.shortid
|
2016-08-12 01:22:36 +00:00
|
|
|
}
|
2016-08-12 03:17:13 +00:00
|
|
|
this.originSocketConnection.socket.emit("function", requestData);
|
2016-08-09 21:37:25 +00:00
|
|
|
return this.done.promise;
|
2016-08-09 14:33:56 +00:00
|
|
|
};
|
2016-08-11 23:32:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* handles the response that is received by the requesting side
|
|
|
|
*/
|
2016-08-12 03:17:13 +00:00
|
|
|
handleResponse(responseDataArg: ISocketRequestDataObject) {
|
2016-08-09 21:37:25 +00:00
|
|
|
this.done.resolve(responseDataArg);
|
2016-08-12 03:56:40 +00:00
|
|
|
allSocketRequests.remove(this);
|
2016-08-09 09:42:21 +00:00
|
|
|
}
|
2016-08-09 14:33:56 +00:00
|
|
|
|
2016-08-11 23:32:57 +00:00
|
|
|
// responding --------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates the response on the responding side
|
|
|
|
*/
|
2016-08-12 03:17:13 +00:00
|
|
|
createResponse() {
|
|
|
|
let targetSocketFunction: SocketFunction = helpers.getSocketFunctionByName(this.funcCallData.funcName);
|
|
|
|
targetSocketFunction.invoke(this.funcCallData)
|
|
|
|
.then((resultData) => {
|
|
|
|
let requestData: ISocketRequestDataObject = {
|
|
|
|
funcCallData: resultData,
|
|
|
|
shortId: this.shortid
|
|
|
|
}
|
|
|
|
this.originSocketConnection.socket.emit("functionResponse",requestData);
|
2016-08-12 03:56:40 +00:00
|
|
|
allSocketRequests.remove(this);
|
2016-08-12 03:17:13 +00:00
|
|
|
});
|
2016-08-09 21:37:25 +00:00
|
|
|
}
|
2016-08-09 09:42:21 +00:00
|
|
|
};
|