nope/lib/communication/IoSocketServer.ts

205 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-11-06 08:10:30 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:52:42
* @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";
import { getNopeLogger } from "../logger/getLogger";
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";
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 {
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)();
}
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);
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-11-04 16:39:20 +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-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}
*/
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) {
this.on('cancel',cb);
2020-11-04 16:39:20 +00:00
}
async emitTaskCancelation(msg: ITaskCancelationMsg) {
this.emit('cancel', msg);
2020-11-04 16:39:20 +00:00
}
async onAurevoir(cb: (dispatcher: string) => void) {
this.on('aurevoir', cb);
2020-11-04 16:39:20 +00:00
}
async emitAurevoir(dispatcher: string) {
this.emit('aurevoir', dispatcher);
2020-11-04 16:39:20 +00:00
}
async emitNewInstanceGeneratorsAvailable(generators: IAvailableInstanceGeneratorsMsg) {
this.emit('generators',generators);
2020-11-04 16:39:20 +00:00
}
async onNewInstanceGeneratorsAvailable(cb: (generators: IAvailableInstanceGeneratorsMsg) => void) {
this.on('generators',cb)
2020-11-04 16:39:20 +00:00
}
async emitRpcRequest(name: string, request: IRequestTaskMsg){
this.emit(name, request);
2020-11-04 16:39:20 +00:00
}
async emitRpcResult(name: string, result: IResponseTaskMsg){
this.emit(name,result);
2020-11-04 16:39:20 +00:00
}
async onRpcResult(name: string, cb: (result: IResponseTaskMsg) => void){
this.on(name, cb);
2020-11-04 16:39:20 +00:00
}
async offRpcResponse(name: string, cb: (result: IResponseTaskMsg) => void){
this.off(name, cb);
2020-11-04 16:39:20 +00:00
}
async onRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void){
this.on(name, cb);
2020-11-04 16:39:20 +00:00
}
async offRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void){
this.off(name, cb);
2020-11-04 16:39:20 +00:00
}
async emitNewServicesAvailable(services: IAvailableServicesMsg){
this.emit('services', services)
2020-11-04 16:39:20 +00:00
}
async onNewServicesAvailable(cb: (services: IAvailableServicesMsg) => void) {
this.on('services', cb);
2020-11-04 16:39:20 +00:00
}
async onBonjour(cb: (dispatcher: string) => void){
this.on('bonjour', cb);
2020-11-04 16:39:20 +00:00
}
async emitBonjour(dispatcher: string){
this.emit('bonjour', dispatcher);
2020-11-04 16:39:20 +00:00
}
async emitNewTopicsAvailable(topics: IAvailableTopicsMsg){
this.emit('topics',topics)
2020-11-04 16:39:20 +00:00
}
async onNewTopicsAvailable(cb: (topics: IAvailableTopicsMsg) => void) {
this.on('topics',cb)
2020-11-04 16:39:20 +00:00
}
async onEvent(event: string, cb: (data: IExternalEventMsg) => void){
this.on('event_'+event, cb);
2020-11-04 16:39:20 +00:00
}
async emitEvent(event: string, data: IExternalEventMsg){
this.emit('event_'+event, data)
2020-11-04 16:39:20 +00:00
}
async offEvent(event: string, cb: (data: IExternalEventMsg) => void){
this.off('event_'+event, cb);
2020-11-04 16:39:20 +00:00
}
}