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-02-05 10:53:33 +00:00
|
|
|
* @modify date 2021-02-05 09:49:00
|
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.
|
|
|
|
* Here, only a Events are used.
|
|
|
|
*
|
|
|
|
* @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 {
|
2020-11-11 17:07:50 +00:00
|
|
|
connected: INopeObservable<boolean>;
|
2021-02-05 10:53:33 +00:00
|
|
|
considerConnection = true;
|
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();
|
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
|
|
|
|
2020-12-30 18:53:48 +00:00
|
|
|
protected _on(event: string, cb) {
|
|
|
|
if (typeof this._emitter.getMaxListeners === "function") {
|
|
|
|
this._emitter.setMaxListeners(this._emitter.getMaxListeners() + 1);
|
|
|
|
}
|
|
|
|
this._emitter.on(event, cb);
|
|
|
|
}
|
|
|
|
|
2020-12-05 01:28:33 +00:00
|
|
|
async emitStatusUpdate(status: IDispatcherInfo): Promise<void> {
|
2020-12-30 18:53:48 +00:00
|
|
|
this._emitter.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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
this._on("newInstancesAvailable", cb);
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewInstancesAvailable(
|
|
|
|
instances: IAvailableInstancesMsg
|
|
|
|
): Promise<void> {
|
|
|
|
this._emitter.emit("newInstancesAvailable", instances);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onTaskCancelation(
|
|
|
|
cb: (msg: ITaskCancelationMsg) => void
|
|
|
|
): Promise<void> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this._emitter.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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this._emitter.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> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this._emitter.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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-09-13 10:35:04 +00:00
|
|
|
this._emitter.emit(name, request);
|
|
|
|
}
|
2020-09-28 16:38:35 +00:00
|
|
|
|
2020-12-05 01:28:33 +00:00
|
|
|
async emitRpcResponse(name: string, result: IResponseTaskMsg): Promise<void> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this._emitter.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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-09-13 10:35:04 +00:00
|
|
|
this._emitter.off(name, cb);
|
|
|
|
}
|
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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-09-13 10:35:04 +00:00
|
|
|
this._emitter.off(name, cb);
|
|
|
|
}
|
2020-09-28 16:38:35 +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._emitter.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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
|
|
|
this._emitter.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> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this._emitter.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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-12-30 18:53:48 +00:00
|
|
|
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> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this._emitter.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> {
|
2020-11-23 06:09:31 +00:00
|
|
|
this._emitter.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
|
|
|
}
|