2022-03-14 21:40:55 +00:00
|
|
|
import * as plugins from './smartsocket.plugins.js';
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// import interfaces
|
2020-09-24 18:04:11 +00:00
|
|
|
import {
|
|
|
|
SocketFunction,
|
2023-07-21 01:53:41 +00:00
|
|
|
type ISocketFunctionCallDataRequest,
|
|
|
|
type ISocketFunctionCallDataResponse,
|
2022-03-14 21:40:55 +00:00
|
|
|
} from './smartsocket.classes.socketfunction.js';
|
2016-08-09 09:42:21 +00:00
|
|
|
|
|
|
|
// import classes
|
2022-03-14 21:40:55 +00:00
|
|
|
import { SocketConnection } from './smartsocket.classes.socketconnection.js';
|
|
|
|
import { logger } from './smartsocket.logging.js';
|
|
|
|
import { Smartsocket } from './smartsocket.classes.smartsocket.js';
|
|
|
|
import { SmartsocketClient } from './smartsocket.classes.smartsocketclient.js';
|
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
|
|
|
*/
|
2020-09-24 18:04:11 +00:00
|
|
|
export interface ISocketRequestConstructorOptions<
|
|
|
|
T extends plugins.typedrequestInterfaces.ITypedRequest
|
|
|
|
> {
|
2018-03-15 01:29:40 +00:00
|
|
|
side: TSocketRequestSide;
|
|
|
|
originSocketConnection: SocketConnection;
|
|
|
|
shortId: string;
|
2019-09-09 21:58:32 +00:00
|
|
|
funcCallData?: ISocketFunctionCallDataRequest<T>;
|
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
|
|
|
|
*/
|
2019-09-09 21:58:32 +00:00
|
|
|
export interface ISocketRequestDataObject<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
|
|
|
funcCallData: ISocketFunctionCallDataRequest<T> | ISocketFunctionCallDataResponse<T>;
|
2018-03-15 01:29:40 +00:00
|
|
|
shortId: string;
|
|
|
|
responseTimeout?: number;
|
2017-07-07 20:02:19 +00:00
|
|
|
}
|
2016-08-09 21:37:25 +00:00
|
|
|
|
2016-08-09 09:42:21 +00:00
|
|
|
// export classes
|
2019-09-09 21:58:32 +00:00
|
|
|
export class SocketRequest<T extends plugins.typedrequestInterfaces.ITypedRequest> {
|
2019-08-12 20:31:40 +00:00
|
|
|
// STATIC
|
|
|
|
public static getSocketRequestById(
|
|
|
|
smartsocketRef: Smartsocket | SmartsocketClient,
|
|
|
|
shortIdArg: string
|
2019-09-09 21:58:32 +00:00
|
|
|
): SocketRequest<any> {
|
2022-01-18 16:10:46 +00:00
|
|
|
return smartsocketRef.socketRequests.findSync((socketRequestArg) => {
|
2019-08-12 20:31:40 +00:00
|
|
|
return socketRequestArg.shortid === shortIdArg;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
2019-05-02 09:46:36 +00:00
|
|
|
public status: TSocketRequestStatus = 'new';
|
|
|
|
public side: TSocketRequestSide;
|
|
|
|
public shortid: string;
|
|
|
|
public originSocketConnection: SocketConnection;
|
2019-09-09 21:58:32 +00:00
|
|
|
public funcCallData: ISocketFunctionCallDataRequest<T>;
|
|
|
|
public done = plugins.smartpromise.defer<ISocketFunctionCallDataResponse<T>>();
|
2019-08-12 20:31:40 +00:00
|
|
|
|
|
|
|
public smartsocketRef: Smartsocket | SmartsocketClient;
|
|
|
|
|
2019-08-12 20:46:57 +00:00
|
|
|
constructor(
|
|
|
|
smartsocketRefArg: Smartsocket | SmartsocketClient,
|
2019-09-09 21:58:32 +00:00
|
|
|
optionsArg: ISocketRequestConstructorOptions<T>
|
2019-08-12 20:46:57 +00:00
|
|
|
) {
|
2019-08-12 20:31:40 +00:00
|
|
|
this.smartsocketRef = smartsocketRefArg;
|
2018-03-15 01:29:40 +00:00
|
|
|
this.side = optionsArg.side;
|
|
|
|
this.shortid = optionsArg.shortId;
|
|
|
|
this.funcCallData = optionsArg.funcCallData;
|
|
|
|
this.originSocketConnection = optionsArg.originSocketConnection;
|
2019-08-12 20:31:40 +00:00
|
|
|
this.smartsocketRef.socketRequests.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
|
|
|
|
*/
|
2019-09-09 21:58:32 +00:00
|
|
|
public dispatch(): Promise<ISocketFunctionCallDataResponse<T>> {
|
|
|
|
const requestData: ISocketRequestDataObject<T> = {
|
2017-07-07 20:02:19 +00:00
|
|
|
funcCallData: this.funcCallData,
|
2020-09-24 18:04:11 +00:00
|
|
|
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
|
|
|
|
*/
|
2019-09-09 21:58:32 +00:00
|
|
|
public async handleResponse(responseDataArg: ISocketRequestDataObject<T>) {
|
2020-12-16 01:38:57 +00:00
|
|
|
// logger.log('info', 'handling response!');
|
2018-03-15 01:29:40 +00:00
|
|
|
this.done.resolve(responseDataArg.funcCallData);
|
2019-08-12 20:31:40 +00:00
|
|
|
this.smartsocketRef.socketRequests.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> {
|
2019-09-09 21:58:32 +00:00
|
|
|
const targetSocketFunction: SocketFunction<T> = SocketFunction.getSocketFunctionByName(
|
2019-08-12 20:31:40 +00:00
|
|
|
this.smartsocketRef,
|
2018-03-15 01:29:40 +00:00
|
|
|
this.funcCallData.funcName
|
|
|
|
);
|
2019-08-12 20:31:40 +00:00
|
|
|
|
2019-04-26 15:35:15 +00:00
|
|
|
if (!targetSocketFunction) {
|
2020-09-29 17:21:08 +00:00
|
|
|
logger.log('error', `There is no SocketFunction defined for ${this.funcCallData.funcName}`);
|
2019-04-26 15:35:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-16 01:38:57 +00:00
|
|
|
// logger.log('info', `invoking ${targetSocketFunction.name}`);
|
2020-09-24 18:04:11 +00:00
|
|
|
targetSocketFunction
|
|
|
|
.invoke(this.funcCallData, this.originSocketConnection)
|
|
|
|
.then((resultData) => {
|
2020-12-16 01:38:57 +00:00
|
|
|
// logger.log('info', 'got resultData. Sending it to requesting party.');
|
2020-09-24 18:04:11 +00:00
|
|
|
const responseData: ISocketRequestDataObject<T> = {
|
|
|
|
funcCallData: resultData,
|
|
|
|
shortId: this.shortid,
|
|
|
|
};
|
|
|
|
this.originSocketConnection.socket.emit('functionResponse', responseData);
|
|
|
|
this.smartsocketRef.socketRequests.remove(this);
|
|
|
|
});
|
2017-07-07 20:02:19 +00:00
|
|
|
}
|
|
|
|
}
|