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-12-04 18:10:33 +00:00
|
|
|
* @modify date 2020-12-03 19:03:41
|
2020-11-06 08:10:30 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
2020-11-12 16:07:05 +00:00
|
|
|
import { EventEmitter } from "events";
|
2020-08-23 07:21:03 +00:00
|
|
|
import { Server } from "http";
|
2020-12-04 18:10:33 +00:00
|
|
|
import * as Logger from "js-logger";
|
|
|
|
import { ILogger } from "js-logger";
|
2020-11-23 06:09:31 +00:00
|
|
|
import * as io from "socket.io";
|
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-12-04 18:10:33 +00:00
|
|
|
import {
|
|
|
|
IAvailableInstanceGeneratorsMsg,
|
|
|
|
IAvailableInstancesMsg,
|
|
|
|
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-12-04 18:10:33 +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-12-04 18:10:33 +00:00
|
|
|
protected _socket: io.Server;
|
2020-11-04 16:39:20 +00:00
|
|
|
protected _sockets: Set<io.Socket>;
|
|
|
|
protected _funcs: Map<string, (...args: any[]) => void>;
|
2020-12-04 18:10:33 +00:00
|
|
|
protected _logger: ILogger;
|
2020-11-12 16:07:05 +00:00
|
|
|
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)();
|
|
|
|
}
|
|
|
|
|
2020-11-12 16:07:05 +00:00
|
|
|
this._internalEmitter = new EventEmitter();
|
|
|
|
|
2020-11-11 17:07:50 +00:00
|
|
|
this.connected = new NopeObservable();
|
|
|
|
this.connected.setContent(false);
|
|
|
|
|
2020-11-23 06:09:31 +00:00
|
|
|
this._logger = getNopeLogger("io-socket-server");
|
2020-12-04 18:10:33 +00:00
|
|
|
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) => {
|
|
|
|
_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-11 17:07:50 +00:00
|
|
|
|
2020-11-23 06:09:31 +00:00
|
|
|
if (_this._sockets.size === 0) {
|
2020-11-11 17:07:50 +00:00
|
|
|
_this.connected.setContent(false);
|
2020-11-23 06:09:31 +00:00
|
|
|
_this._logger.warn("All Connections lost");
|
2020-11-11 17:07:50 +00:00
|
|
|
}
|
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-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>();
|
|
|
|
}
|
2020-12-04 18:10:33 +00:00
|
|
|
off(event: string, cb: (...args: any[]) => void): void {
|
2020-11-04 16:39:20 +00:00
|
|
|
// 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-12-04 18:10:33 +00:00
|
|
|
on(event: string, cb: (...args: any[]) => void): void {
|
2020-11-04 16:39:20 +00:00
|
|
|
// Add the Subscription to the Sockets.
|
2020-12-04 18:10:33 +00:00
|
|
|
const _this = this;
|
2020-11-04 16:39:20 +00:00
|
|
|
for (const socket of this._sockets) {
|
|
|
|
socket.on(event, cb);
|
2020-12-04 18:10:33 +00:00
|
|
|
socket.on(event, (...args) => {
|
|
|
|
_this._socket.emit(event, ...args);
|
|
|
|
});
|
2020-08-23 07:21:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
// Store the Function
|
|
|
|
this._funcs.set(event, cb);
|
2020-11-12 16:07:05 +00:00
|
|
|
this._internalEmitter.on(event, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit(event: string, data: any): void {
|
2020-12-04 18:10:33 +00:00
|
|
|
if (this._logger.enabledFor(Logger.DEBUG)) {
|
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}
|
|
|
|
*/
|
2020-12-04 18:10:33 +00:00
|
|
|
export class IoSocketServer
|
|
|
|
extends IoSocketSeverEventEmitter
|
|
|
|
implements ICommunicationInterface {
|
|
|
|
subscriptionMode?: "individual" | "generic";
|
|
|
|
resultSharing?: "individual" | "generic";
|
|
|
|
async onTaskCancelation(
|
|
|
|
cb: (msg: ITaskCancelationMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.on("cancel", cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitTaskCancelation(msg: ITaskCancelationMsg): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.emit("cancel", msg);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onAurevoir(cb: (dispatcher: string) => void): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.on("aurevoir", cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitAurevoir(dispatcher: string): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.emit("aurevoir", dispatcher);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitNewInstanceGeneratorsAvailable(
|
|
|
|
generators: IAvailableInstanceGeneratorsMsg
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.emit("generators", generators);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onNewInstanceGeneratorsAvailable(
|
|
|
|
cb: (generators: IAvailableInstanceGeneratorsMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.on("generators", cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitRpcRequest(name: string, request: IRequestTaskMsg): Promise<void> {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.emit(name, request);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitRpcResult(name: string, result: IResponseTaskMsg): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.emit(name, result);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onRpcResult(
|
|
|
|
name: string,
|
|
|
|
cb: (result: IResponseTaskMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on(name, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async offRpcResponse(
|
|
|
|
name: string,
|
|
|
|
cb: (result: IResponseTaskMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.off(name, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onRpcRequest(
|
|
|
|
name: string,
|
|
|
|
cb: (data: IRequestTaskMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.on(name, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async offRpcRequest(
|
|
|
|
name: string,
|
|
|
|
cb: (data: IRequestTaskMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.off(name, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitNewServicesAvailable(
|
|
|
|
services: IAvailableServicesMsg
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.emit("services", services);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onNewServicesAvailable(
|
|
|
|
cb: (services: IAvailableServicesMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.on("services", cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onBonjour(cb): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.on("bonjour", cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitBonjour(msg): Promise<void> {
|
|
|
|
this.emit("bonjour", msg);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitNewObersvablesAvailable(
|
|
|
|
topics: IAvailableTopicsMsg
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.emit("topics", topics);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onNewObservablesAvailable(
|
|
|
|
cb: (topics: IAvailableTopicsMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.on("topics", cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onEvent(
|
|
|
|
event: string,
|
|
|
|
cb: (data: IExternalEventMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.on("event_" + event, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitEvent(event: string, data: IExternalEventMsg): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.emit("event_" + event, data);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
2020-11-23 06:09:31 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async offEvent(
|
|
|
|
event: string,
|
|
|
|
cb: (data: IExternalEventMsg) => void
|
|
|
|
): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this.off("event_" + event, cb);
|
2020-11-04 16:39:20 +00:00
|
|
|
}
|
2020-12-04 18:10:33 +00:00
|
|
|
|
|
|
|
async onNewInstancesAvailable(
|
|
|
|
cb: (instances: IAvailableInstancesMsg) => void
|
|
|
|
): Promise<void> {
|
|
|
|
this.on("newInstancesAvailable", cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewInstancesAvailable(
|
|
|
|
instances: IAvailableInstancesMsg
|
|
|
|
): Promise<void> {
|
|
|
|
this.emit("newInstancesAvailable", instances);
|
|
|
|
}
|
|
|
|
}
|