nope/lib/communication/IoSocketServer.ts

213 lines
5.8 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-12 13:26:43
2020-11-06 08:10:30 +00:00
* @desc [description]
*/
import { EventEmitter } from "events";
2020-08-23 07:21:03 +00:00
import { Server } from "http";
2020-11-23 06:09:31 +00:00
import * as io from "socket.io";
2020-09-10 16:21:01 +00:00
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;
protected _internalEmitter: EventEmitter;
2020-11-04 16:39:20 +00:00
/**
* 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._internalEmitter = new EventEmitter();
this.connected = new NopeObservable();
this.connected.setContent(false);
2020-11-23 06:09:31 +00:00
this._logger = getNopeLogger("io-socket-server");
this._logger.info("waiting for connection. Listening on port: " + port.toString());
2020-11-04 16:39:20 +00:00
this._socket.listen(port);
const _this = this;
2020-11-23 06:09:31 +00:00
this._socket.on("connection", (client) => {
2020-11-04 16:39:20 +00:00
2020-11-23 06:09:31 +00:00
_this._logger.debug("New Connection established: " + client.id);
2020-11-04 16:39:20 +00:00
// Add the Elements to the Client
_this._sockets.add(client);
// Subscribe to Loosing connection:
2020-11-23 06:09:31 +00:00
client.on("disconnect", () => {
_this._logger.debug("Connection of : " + client.id + " lost.");
2020-11-04 16:39:20 +00:00
_this._sockets.delete(client);
2020-11-23 06:09:31 +00:00
if (_this._sockets.size === 0) {
_this.connected.setContent(false);
2020-11-23 06:09:31 +00:00
_this._logger.warn("All Connections lost");
}
2020-11-04 16:39:20 +00:00
});
for (const [id, _func] of _this._funcs) {
// Subscribe to the Events:
client.on(id, (...args) => {
2020-11-23 06:09:31 +00:00
_this._logger.debug("received content: ", ...args);
2020-11-04 16:39:20 +00:00
// 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);
this._internalEmitter.on(event, cb);
2020-11-04 16:39:20 +00:00
}
emit(event: string, data: any): void {
if (this._logger.isDebugEnabled()) {
2020-11-23 06:09:31 +00:00
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-11-23 06:09:31 +00:00
this._internalEmitter.emit(event, data);
2020-11-04 16:39:20 +00:00
}
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) {
2020-11-23 06:09:31 +00:00
this.on("cancel", cb);
2020-11-04 16:39:20 +00:00
}
async emitTaskCancelation(msg: ITaskCancelationMsg) {
2020-11-23 06:09:31 +00:00
this.emit("cancel", msg);
2020-11-04 16:39:20 +00:00
}
async onAurevoir(cb: (dispatcher: string) => void) {
2020-11-23 06:09:31 +00:00
this.on("aurevoir", cb);
2020-11-04 16:39:20 +00:00
}
async emitAurevoir(dispatcher: string) {
2020-11-23 06:09:31 +00:00
this.emit("aurevoir", dispatcher);
2020-11-04 16:39:20 +00:00
}
async emitNewInstanceGeneratorsAvailable(generators: IAvailableInstanceGeneratorsMsg) {
2020-11-23 06:09:31 +00:00
this.emit("generators", generators);
2020-11-04 16:39:20 +00:00
}
async onNewInstanceGeneratorsAvailable(cb: (generators: IAvailableInstanceGeneratorsMsg) => void) {
2020-11-23 06:09:31 +00:00
this.on("generators", cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async emitRpcRequest(name: string, request: IRequestTaskMsg) {
this.emit(name, request);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async emitRpcResult(name: string, result: IResponseTaskMsg) {
this.emit(name, result);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async onRpcResult(name: string, cb: (result: IResponseTaskMsg) => void) {
this.on(name, cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async offRpcResponse(name: string, cb: (result: IResponseTaskMsg) => void) {
this.off(name, cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async onRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void) {
this.on(name, cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async offRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void) {
this.off(name, cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async emitNewServicesAvailable(services: IAvailableServicesMsg) {
this.emit("services", services);
2020-11-04 16:39:20 +00:00
}
async onNewServicesAvailable(cb: (services: IAvailableServicesMsg) => void) {
2020-11-23 06:09:31 +00:00
this.on("services", cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async onBonjour(cb: (dispatcher: string) => void) {
this.on("bonjour", cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async emitBonjour(dispatcher: string) {
this.emit("bonjour", dispatcher);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async emitNewTopicsAvailable(topics: IAvailableTopicsMsg) {
this.emit("topics", topics);
2020-11-04 16:39:20 +00:00
}
async onNewTopicsAvailable(cb: (topics: IAvailableTopicsMsg) => void) {
2020-11-23 06:09:31 +00:00
this.on("topics", cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async onEvent(event: string, cb: (data: IExternalEventMsg) => void) {
this.on("event_" + event, cb);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async emitEvent(event: string, data: IExternalEventMsg) {
this.emit("event_" + event, data);
2020-11-04 16:39:20 +00:00
}
2020-11-23 06:09:31 +00:00
async offEvent(event: string, cb: (data: IExternalEventMsg) => void) {
this.off("event_" + event, cb);
2020-11-04 16:39:20 +00:00
}
}