2018-03-15 01:29:40 +00:00
|
|
|
import * as plugins from './smartsocket.plugins';
|
|
|
|
import * as helpers from './smartsocket.helpers';
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// import interfaces
|
2018-03-15 01:29:40 +00:00
|
|
|
import { ISocketFunctionCall } from './smartsocket.classes.socketfunction';
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// import classes
|
2019-01-30 02:14:02 +00:00
|
|
|
import { Objectmap } from '@pushrocks/lik';
|
2018-03-15 01:29:40 +00:00
|
|
|
import { SocketFunction } from './smartsocket.classes.socketfunction';
|
|
|
|
import { SocketConnection } from './smartsocket.classes.socketconnection';
|
2019-04-26 15:35:15 +00:00
|
|
|
import { defaultLogger } from '@pushrocks/smartlog';
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// export interfaces
|
2018-03-15 01:29:40 +00:00
|
|
|
export type TSocketRequestStatus = 'new' | 'pending' | 'finished';
|
|
|
|
export type TSocketRequestSide = 'requesting' | 'responding';
|
2016-08-09 09:42:21 +00:00
|
|
|
|
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 {
|
2018-03-15 01:29:40 +00:00
|
|
|
side: TSocketRequestSide;
|
|
|
|
originSocketConnection: SocketConnection;
|
|
|
|
shortId: string;
|
|
|
|
funcCallData?: ISocketFunctionCall;
|
2017-07-07 20:02:19 +00:00
|
|
|
}
|
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 {
|
2018-03-15 01:29:40 +00:00
|
|
|
funcCallData: ISocketFunctionCall;
|
|
|
|
shortId: string;
|
|
|
|
responseTimeout?: number;
|
2017-07-07 20:02:19 +00:00
|
|
|
}
|
2016-08-09 21:37:25 +00:00
|
|
|
|
2017-07-07 20:02:19 +00:00
|
|
|
// export objects
|
2018-03-15 01:29:40 +00:00
|
|
|
export let allSocketRequests = new Objectmap<SocketRequest>();
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// export classes
|
|
|
|
export class SocketRequest {
|
2018-03-15 01:29:40 +00:00
|
|
|
status: TSocketRequestStatus = 'new';
|
|
|
|
side: TSocketRequestSide;
|
|
|
|
shortid: string;
|
|
|
|
originSocketConnection: SocketConnection;
|
|
|
|
funcCallData: ISocketFunctionCall;
|
2019-01-30 02:14:02 +00:00
|
|
|
done = plugins.smartpromise.defer();
|
2018-03-15 01:29:40 +00:00
|
|
|
constructor(optionsArg: SocketRequestConstructorOptions) {
|
|
|
|
this.side = optionsArg.side;
|
|
|
|
this.shortid = optionsArg.shortId;
|
|
|
|
this.funcCallData = optionsArg.funcCallData;
|
|
|
|
this.originSocketConnection = optionsArg.originSocketConnection;
|
|
|
|
allSocketRequests.add(this);
|
2017-07-07 20:02:19 +00:00
|
|
|
}
|
2016-08-12 03:17:13 +00:00
|
|
|
|
2017-07-07 20:02:19 +00:00
|
|
|
// requesting --------------------------
|
2016-08-12 03:17:13 +00:00
|
|
|
|
2017-07-07 20:02:19 +00:00
|
|
|
/**
|
|
|
|
* dispatches a socketrequest from the requesting to the receiving side
|
|
|
|
*/
|
2018-03-15 01:29:40 +00:00
|
|
|
dispatch() {
|
2017-07-07 20:02:19 +00:00
|
|
|
let requestData: ISocketRequestDataObject = {
|
|
|
|
funcCallData: this.funcCallData,
|
|
|
|
shortId: this.shortid
|
2018-03-15 01:29:40 +00:00
|
|
|
};
|
|
|
|
this.originSocketConnection.socket.emit('function', requestData);
|
|
|
|
return this.done.promise;
|
2017-07-07 20:02:19 +00:00
|
|
|
}
|
2016-08-09 14:33:56 +00:00
|
|
|
|
2017-07-07 20:02:19 +00:00
|
|
|
/**
|
|
|
|
* handles the response that is received by the requesting side
|
|
|
|
*/
|
2018-03-15 01:29:40 +00:00
|
|
|
handleResponse(responseDataArg: ISocketRequestDataObject) {
|
2019-01-30 02:14:02 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('info', 'handling response!');
|
2018-03-15 01:29:40 +00:00
|
|
|
this.done.resolve(responseDataArg.funcCallData);
|
|
|
|
allSocketRequests.remove(this);
|
2017-07-07 20:02:19 +00:00
|
|
|
}
|
2016-08-11 23:32:57 +00:00
|
|
|
|
2017-07-07 20:02:19 +00:00
|
|
|
// responding --------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates the response on the responding side
|
|
|
|
*/
|
2019-04-26 15:35:15 +00:00
|
|
|
public async createResponse(): Promise<void> {
|
|
|
|
const targetSocketFunction: SocketFunction = helpers.getSocketFunctionByName(
|
2018-03-15 01:29:40 +00:00
|
|
|
this.funcCallData.funcName
|
|
|
|
);
|
2019-04-26 15:35:15 +00:00
|
|
|
if (!targetSocketFunction) {
|
|
|
|
defaultLogger.log('warn', `There is no SocketFunction defined for ${this.funcCallData.funcName}`);
|
|
|
|
defaultLogger.log('warn', `So now response is being sent.`);
|
|
|
|
return;
|
|
|
|
}
|
2019-01-30 02:14:02 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('info', `invoking ${targetSocketFunction.name}`);
|
2018-03-15 01:29:40 +00:00
|
|
|
targetSocketFunction.invoke(this.funcCallData).then(resultData => {
|
2019-01-30 02:14:02 +00:00
|
|
|
plugins.smartlog.defaultLogger.log('info', 'got resultData. Sending it to requesting party.');
|
2018-03-15 01:29:40 +00:00
|
|
|
let requestData: ISocketRequestDataObject = {
|
|
|
|
funcCallData: resultData,
|
|
|
|
shortId: this.shortid
|
|
|
|
};
|
|
|
|
this.originSocketConnection.socket.emit('functionResponse', requestData);
|
|
|
|
allSocketRequests.remove(this);
|
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
}
|
|
|
|
}
|