2020-11-06 08:10:30 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-06 08:52:42
|
2020-11-11 16:08:11 +00:00
|
|
|
* @modify date 2020-11-11 16:29:07
|
2020-11-06 08:10:30 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
2020-08-23 07:21:03 +00:00
|
|
|
import { Server } from "http";
|
2020-09-10 16:21:01 +00:00
|
|
|
import * as io from 'socket.io';
|
|
|
|
import { Logger } from "winston";
|
2020-11-11 16:08:11 +00:00
|
|
|
import { getNopeLogger } from "../logger/getLogger";
|
2020-11-11 17:07:50 +00:00
|
|
|
import { NopeObservable } from "../observables/nopeObservable";
|
2020-11-09 06:42:24 +00:00
|
|
|
import { IAvailableInstanceGeneratorsMsg, IAvailableServicesMsg, IAvailableTopicsMsg, ICommunicationInterface, IExternalEventMsg, IRequestTaskMsg, IResponseTaskMsg, ITaskCancelationMsg } from "../types/nope/nopeCommunication.interface";
|
2020-11-11 17:07:50 +00:00
|
|
|
import { INopeObservable } from "../types/nope/nopeObservable.interface";
|
2020-08-23 07:21:03 +00:00
|
|
|
|
2020-09-11 12:07:31 +00:00
|
|
|
/**
|
2020-10-24 05:57:48 +00:00
|
|
|
* Wrapper Interface.
|
|
|
|
* Creates an Interface for a Socket server
|
2020-09-11 12:07:31 +00:00
|
|
|
*/
|
2020-10-24 05:57:48 +00:00
|
|
|
export class IoSocketSeverEventEmitter {
|
2020-11-11 17:07:50 +00:00
|
|
|
connected: INopeObservable<boolean>;
|
2020-11-04 16:39:20 +00:00
|
|
|
protected _socket: io.Server
|
|
|
|
protected _sockets: Set<io.Socket>;
|
|
|
|
protected _funcs: Map<string, (...args: any[]) => void>;
|
|
|
|
protected _logger: Logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of IoSocketServer.
|
|
|
|
* @param {number} port The Port, on which the Server should be hosted
|
|
|
|
* @param {Server} [server] A Server shich should be used. (Otherwise a Server is created)
|
|
|
|
* @memberof IoSocketServer
|
|
|
|
*/
|
|
|
|
constructor(public port: number, server?: Server) {
|
|
|
|
if (server) {
|
|
|
|
this._socket = (io as any)(server);
|
|
|
|
} else {
|
|
|
|
this._socket = (io as any)();
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:07:50 +00:00
|
|
|
this.connected = new NopeObservable();
|
|
|
|
this.connected.setContent(false);
|
|
|
|
|
2020-11-09 06:42:24 +00:00
|
|
|
this._logger = getNopeLogger('io-socket');
|
2020-11-04 16:39:20 +00:00
|
|
|
|
|
|
|
this._socket.listen(port);
|
|
|
|
|
|
|
|
const _this = this;
|
|
|
|
this._socket.on('connection', (client) => {
|
|
|
|
|
|
|
|
_this._logger.debug('New Connection established: ' + client.id);
|
|
|
|
|
|
|
|
// Add the Elements to the Client
|
|
|
|
_this._sockets.add(client);
|
|
|
|
|
|
|
|
// Subscribe to Loosing connection:
|
|
|
|
client.on('disconnect', () => {
|
|
|
|
_this._logger.debug('Connection of : ' + client.id + ' lost.');
|
|
|
|
_this._sockets.delete(client);
|
2020-11-11 17:07:50 +00:00
|
|
|
|
|
|
|
if (_this._sockets.size === 0){
|
|
|
|
_this.connected.setContent(false);
|
|
|
|
}
|
2020-11-04 16:39:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const [id, _func] of _this._funcs) {
|
|
|
|
// Subscribe to the Events:
|
|
|
|
client.on(id, (...args) => {
|
|
|
|
_this._logger.debug('received content: ', ...args);
|
|
|
|
|
|
|
|
// Forward the Message.
|
|
|
|
_func(...args);
|
2020-09-12 00:59:30 +00:00
|
|
|
});
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
2020-11-11 17:07:50 +00:00
|
|
|
|
|
|
|
_this.connected.setContent(true);
|
2020-11-04 16:39:20 +00:00
|
|
|
});
|
2020-09-10 16:21:01 +00:00
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
this._sockets = new Set();
|
|
|
|
this._funcs = new Map<string, (...args: any[]) => void>();
|
|
|
|
}
|
|
|
|
off(event: string, cb: (...args: any[]) => void) {
|
|
|
|
// Remove the Subscription from the Socket.
|
|
|
|
for (const socket of this._sockets) {
|
|
|
|
socket.off(event, cb);
|
2020-09-12 00:59:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
// Remove the Function.
|
|
|
|
this._funcs.delete(event);
|
|
|
|
}
|
2020-08-23 07:21:03 +00:00
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
on(event: string, cb: (...args: any[]) => void) {
|
|
|
|
// Add the Subscription to the Sockets.
|
|
|
|
for (const socket of this._sockets) {
|
|
|
|
socket.on(event, cb);
|
2020-08-23 07:21:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
// Store the Function
|
|
|
|
this._funcs.set(event, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(event: string, data: any): void {
|
|
|
|
if (this._logger.isDebugEnabled()) {
|
|
|
|
this._logger.debug('sending data on: ' + event);
|
2020-08-23 07:21:03 +00:00
|
|
|
}
|
2020-11-04 16:39:20 +00:00
|
|
|
this._socket.emit(event, data);
|
|
|
|
}
|
2020-10-24 05:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Communication Layer using IO-Sockets.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @class IoSocketServer
|
|
|
|
* @implements {ICommunicationInterface}
|
|
|
|
*/
|
2020-11-11 17:07:50 +00:00
|
|
|
export class IoSocketServer extends IoSocketSeverEventEmitter implements ICommunicationInterface {
|
2020-10-24 05:57:48 +00:00
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
async onTaskCancelation(cb: (msg: ITaskCancelationMsg) => void) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on('cancel',cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitTaskCancelation(msg: ITaskCancelationMsg) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit('cancel', msg);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onAurevoir(cb: (dispatcher: string) => void) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on('aurevoir', cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitAurevoir(dispatcher: string) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit('aurevoir', dispatcher);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewInstanceGeneratorsAvailable(generators: IAvailableInstanceGeneratorsMsg) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit('generators',generators);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onNewInstanceGeneratorsAvailable(cb: (generators: IAvailableInstanceGeneratorsMsg) => void) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on('generators',cb)
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitRpcRequest(name: string, request: IRequestTaskMsg){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit(name, request);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitRpcResult(name: string, result: IResponseTaskMsg){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit(name,result);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onRpcResult(name: string, cb: (result: IResponseTaskMsg) => void){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on(name, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async offRpcResponse(name: string, cb: (result: IResponseTaskMsg) => void){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.off(name, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on(name, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async offRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.off(name, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewServicesAvailable(services: IAvailableServicesMsg){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit('services', services)
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onNewServicesAvailable(cb: (services: IAvailableServicesMsg) => void) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on('services', cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onBonjour(cb: (dispatcher: string) => void){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on('bonjour', cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitBonjour(dispatcher: string){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit('bonjour', dispatcher);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewTopicsAvailable(topics: IAvailableTopicsMsg){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit('topics',topics)
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onNewTopicsAvailable(cb: (topics: IAvailableTopicsMsg) => void) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on('topics',cb)
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onEvent(event: string, cb: (data: IExternalEventMsg) => void){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on('event_'+event, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitEvent(event: string, data: IExternalEventMsg){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit('event_'+event, data)
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async offEvent(event: string, cb: (data: IExternalEventMsg) => void){
|
2020-11-11 17:07:50 +00:00
|
|
|
this.off('event_'+event, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
}
|