2020-11-06 08:10:30 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-06 08:52:36
|
2021-04-12 05:09:47 +00:00
|
|
|
* @modify date 2021-04-09 08:36:53
|
2020-11-06 08:10:30 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
2020-08-21 16:38:21 +00:00
|
|
|
import { EventEmitter } from "events";
|
2020-12-04 18:10:33 +00:00
|
|
|
import { ILogger } from "js-logger";
|
2021-03-22 19:24:15 +00:00
|
|
|
import { generateId } from "../../helpers/idMethods";
|
|
|
|
import { NopeObservable } from "../../observables/nopeObservable";
|
2020-12-04 18:10:33 +00:00
|
|
|
import {
|
|
|
|
IAvailableInstanceGeneratorsMsg,
|
|
|
|
IAvailableInstancesMsg,
|
|
|
|
IAvailableServicesMsg,
|
|
|
|
IAvailableTopicsMsg,
|
|
|
|
ICommunicationInterface,
|
2021-03-19 13:26:38 +00:00
|
|
|
IEmitter,
|
2020-12-04 18:10:33 +00:00
|
|
|
IExternalEventMsg,
|
|
|
|
IRequestTaskMsg,
|
|
|
|
IResponseTaskMsg,
|
|
|
|
ITaskCancelationMsg
|
2021-03-22 19:24:15 +00:00
|
|
|
} from "../../types/nope/nopeCommunication.interface";
|
|
|
|
import { IDispatcherInfo } from "../../types/nope/nopeDispatcher.interface";
|
|
|
|
import { INopeObservable } from "../../types/nope/nopeObservable.interface";
|
2020-09-12 00:59:30 +00:00
|
|
|
|
2020-08-23 07:21:03 +00:00
|
|
|
/**
|
2020-09-11 07:59:23 +00:00
|
|
|
* A Communication Layer for the Dispatchers.
|
2021-04-12 05:09:47 +00:00
|
|
|
* Here, only a Events are used. This layer represents
|
|
|
|
* the basic layer. If transmits the events using
|
|
|
|
* internal emitters.
|
2020-09-11 07:59:23 +00:00
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @class EventLayer
|
|
|
|
* @implements {ICommunicationInterface}
|
2020-08-23 07:21:03 +00:00
|
|
|
*/
|
2020-08-21 16:38:21 +00:00
|
|
|
export class EventLayer implements ICommunicationInterface {
|
2021-04-12 05:09:47 +00:00
|
|
|
/**
|
|
|
|
* Flag, showing whether the Mirror is connected or not.
|
|
|
|
*
|
|
|
|
* @type {INopeObservable<boolean>}
|
|
|
|
* @memberof EventLayer
|
|
|
|
*/
|
2020-11-11 17:07:50 +00:00
|
|
|
connected: INopeObservable<boolean>;
|
2021-04-12 05:09:47 +00:00
|
|
|
/**
|
|
|
|
* Flag, to enable, that this layer will be used in a bridge,
|
|
|
|
* to show, whether the bridge is connected or not.
|
|
|
|
*
|
|
|
|
* @memberof EventLayer
|
|
|
|
*/
|
2021-02-05 10:53:33 +00:00
|
|
|
considerConnection = true;
|
2021-04-12 05:09:47 +00:00
|
|
|
|
2021-03-19 13:26:38 +00:00
|
|
|
allowServiceRedundancy = false;
|
2020-09-13 14:09:01 +00:00
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
/**
|
|
|
|
* Creaetes an Event Emitter-based Communication Layer:
|
2020-12-30 18:53:48 +00:00
|
|
|
* @param _emitter
|
|
|
|
* @param _logger
|
2020-12-04 20:39:18 +00:00
|
|
|
*/
|
2020-09-28 16:38:35 +00:00
|
|
|
constructor(
|
2020-12-05 01:28:33 +00:00
|
|
|
protected _emitter: IEmitter = new EventEmitter(),
|
2020-12-04 18:10:33 +00:00
|
|
|
protected _logger?: ILogger
|
2020-11-23 06:09:31 +00:00
|
|
|
) {
|
2020-11-11 17:07:50 +00:00
|
|
|
this.connected = new NopeObservable();
|
|
|
|
this.connected.setContent(true);
|
2021-03-12 07:47:30 +00:00
|
|
|
this.id = generateId();
|
2021-04-12 05:09:47 +00:00
|
|
|
this._subscribing = new Map();
|
2020-09-28 16:38:35 +00:00
|
|
|
}
|
2021-03-19 13:26:38 +00:00
|
|
|
|
2021-03-12 07:47:30 +00:00
|
|
|
id: string;
|
2020-11-23 06:09:31 +00:00
|
|
|
|
2021-04-12 05:09:47 +00:00
|
|
|
protected _subscribing: Map<string, Set<(...args) => any>>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper-Function, which will publish the subescribe events
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @memberof EventLayer
|
|
|
|
*/
|
|
|
|
protected _publishSubscriptions(): void {
|
|
|
|
const activeSubscriptions = Array.from(this._subscribing.entries())
|
|
|
|
.filter((entry) => {
|
|
|
|
return entry[1].size > 0;
|
|
|
|
})
|
|
|
|
.map((entry) => entry[0]);
|
|
|
|
|
|
|
|
this._emitter.emit("subscribing", activeSubscriptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async _on(event: string, cb): Promise<void> {
|
|
|
|
if (typeof (await this._emitter.getMaxListeners) === "function") {
|
|
|
|
await this._emitter.setMaxListeners(
|
|
|
|
(await this._emitter.getMaxListeners()) + 1
|
|
|
|
);
|
2020-12-30 18:53:48 +00:00
|
|
|
}
|
2021-04-12 05:09:47 +00:00
|
|
|
|
|
|
|
// Store the Event we are publishing
|
|
|
|
if (!this._subscribing.has(event)) {
|
|
|
|
this._subscribing.set(event, new Set());
|
|
|
|
}
|
|
|
|
this._subscribing.get(event).add(cb);
|
|
|
|
|
|
|
|
this._publishSubscriptions();
|
2020-12-30 18:53:48 +00:00
|
|
|
this._emitter.on(event, cb);
|
|
|
|
}
|
|
|
|
|
2021-04-12 05:09:47 +00:00
|
|
|
protected async _emit(name: string, data: any): Promise<void> {
|
|
|
|
this._emitter.emit("emitted", { name, data });
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async _off(name, cb): Promise<void> {
|
|
|
|
this._off(name, cb);
|
|
|
|
|
|
|
|
// Store the Event we are publishing
|
|
|
|
if (!this._subscribing.has(name)) {
|
|
|
|
this._subscribing.set(name, new Set());
|
|
|
|
}
|
|
|
|
this._subscribing.get(name).delete(cb);
|
|
|
|
|
|
|
|
this._publishSubscriptions();
|
|
|
|
}
|
|
|
|
|
2020-12-05 01:28:33 +00:00
|
|
|
async emitStatusUpdate(status: IDispatcherInfo): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("statusUdpate", status);
|
2020-12-05 01:28:33 +00:00
|
|
|
}
|
2020-12-30 18:53:48 +00:00
|
|
|
|
2020-12-05 01:28:33 +00:00
|
|
|
async onStatusUpdate(cb: (status: IDispatcherInfo) => void): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("statusUdpate", cb);
|
2020-12-05 01:28:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onNewInstancesAvailable(
|
|
|
|
cb: (instances: IAvailableInstancesMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("newInstancesAvailable", cb);
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewInstancesAvailable(
|
|
|
|
instances: IAvailableInstancesMsg
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("newInstancesAvailable", instances);
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async onTaskCancelation(
|
|
|
|
cb: (msg: ITaskCancelationMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("cancel", cb);
|
2020-11-04 16:33:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitTaskCancelation(msg: ITaskCancelationMsg): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("cancel", msg);
|
2020-11-04 16:33:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onAurevoir(cb: (dispatcher: string) => void): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("aurevoir", cb);
|
2020-11-04 16:33:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitAurevoir(dispatcher: string): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("aurevoir", dispatcher);
|
2020-11-04 16:33:25 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitNewInstanceGeneratorsAvailable(
|
|
|
|
generators: IAvailableInstanceGeneratorsMsg
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("generators", generators);
|
2020-09-28 16:38:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onNewInstanceGeneratorsAvailable(
|
|
|
|
cb: (generators: IAvailableInstanceGeneratorsMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("generators", cb);
|
2020-09-28 16:38:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitRpcRequest(name: string, request: IRequestTaskMsg): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit(name, request);
|
2020-09-13 10:35:04 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-05 01:28:33 +00:00
|
|
|
async emitRpcResponse(name: string, result: IResponseTaskMsg): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit(name, result);
|
2020-09-13 10:35:04 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 20:39:18 +00:00
|
|
|
async onRpcResponse(
|
2020-12-04 18:10:33 +00:00
|
|
|
name: string,
|
|
|
|
cb: (result: IResponseTaskMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on(name, cb);
|
2020-09-13 10:35:04 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async offRpcResponse(
|
|
|
|
name: string,
|
|
|
|
cb: (result: IResponseTaskMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._off(name, cb);
|
2020-09-13 10:35:04 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onRpcRequest(
|
|
|
|
name: string,
|
|
|
|
cb: (data: IRequestTaskMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on(name, cb);
|
2020-09-13 10:35:04 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async offRpcRequest(
|
|
|
|
name: string,
|
|
|
|
cb: (data: IRequestTaskMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._off(name, cb);
|
2020-09-13 10:35:04 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitNewServicesAvailable(
|
|
|
|
services: IAvailableServicesMsg
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("services", services);
|
2020-09-13 10:35:04 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onNewServicesAvailable(
|
|
|
|
cb: (services: IAvailableServicesMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("services", cb);
|
2020-09-12 00:59:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onBonjour(cb): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("bonjour", cb);
|
2020-09-17 06:05:45 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitBonjour(msg): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("bonjour", msg);
|
2020-09-17 06:05:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitNewObersvablesAvailable(
|
|
|
|
topics: IAvailableTopicsMsg
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("topics", topics);
|
2020-09-17 06:05:45 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onNewObservablesAvailable(
|
|
|
|
cb: (topics: IAvailableTopicsMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("topics", cb);
|
2020-09-17 06:05:45 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async onEvent(
|
|
|
|
event: string,
|
|
|
|
cb: (data: IExternalEventMsg) => void
|
|
|
|
): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._on("event_" + event, cb);
|
2020-09-17 06:05:45 +00:00
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-04 18:10:33 +00:00
|
|
|
async emitEvent(event: string, data: IExternalEventMsg): Promise<void> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._emit("event_" + event, data);
|
2020-09-17 06:05:45 +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> {
|
2021-04-12 05:09:47 +00:00
|
|
|
await this._off("event_" + event, cb);
|
2020-09-17 06:05:45 +00:00
|
|
|
}
|
2021-02-05 10:53:33 +00:00
|
|
|
|
2021-03-19 13:26:38 +00:00
|
|
|
async dispose(): Promise<void> {
|
2021-02-05 10:53:33 +00:00
|
|
|
this.connected.dispose();
|
|
|
|
}
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|