2020-08-21 16:38:21 +00:00
|
|
|
import { EventEmitter } from "events";
|
2020-09-17 06:05:45 +00:00
|
|
|
import { Logger } from "winston";
|
|
|
|
import { ICommunicationInterface, requestTaskMsg, responseTaskMsg, availableServices, availableTopics, externalEvent } from "../dispatcher/nopeDispatcher";
|
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-09-13 14:09:01 +00:00
|
|
|
|
2020-09-13 10:35:04 +00:00
|
|
|
emitRpcRequest(name: string, request: requestTaskMsg): void {
|
|
|
|
this._emitter.emit(name, request);
|
|
|
|
}
|
2020-09-13 14:09:01 +00:00
|
|
|
emitRpcResult(name: string, result: responseTaskMsg): void {
|
|
|
|
this._emitter.emit(name,result);
|
2020-09-13 10:35:04 +00:00
|
|
|
}
|
|
|
|
onRpcResult(name: string, cb: (result: responseTaskMsg) => void): void {
|
|
|
|
this._emitter.on(name, cb);
|
|
|
|
}
|
2020-09-13 14:09:01 +00:00
|
|
|
offRpcResponse(name: string, cb: (result: responseTaskMsg) => void): void {
|
2020-09-13 10:35:04 +00:00
|
|
|
this._emitter.off(name, cb);
|
|
|
|
}
|
|
|
|
onRpcRequest(name: string, cb: (data: requestTaskMsg) => void): void {
|
|
|
|
this._emitter.on(name, cb);
|
|
|
|
}
|
|
|
|
offRpcRequest(name: string, cb: (data: requestTaskMsg) => void): void {
|
|
|
|
this._emitter.off(name, cb);
|
|
|
|
}
|
|
|
|
emitNewServicesAvailable(services: availableServices): void {
|
|
|
|
this._emitter.emit('services', services)
|
|
|
|
}
|
|
|
|
onNewServicesAvailable(cb: (services: availableServices) => void) {
|
|
|
|
this._emitter.on('services', cb);
|
2020-09-12 00:59:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 14:09:01 +00:00
|
|
|
constructor(
|
|
|
|
public readonly subscriptionMode: 'individual' | 'generic',
|
|
|
|
public readonly resultSharing: 'individual' | 'generic',
|
|
|
|
protected _logger?: Logger
|
|
|
|
){
|
|
|
|
|
|
|
|
}
|
2020-09-17 06:05:45 +00:00
|
|
|
onBonjour(cb: (dispatcher: string) => void): void {
|
|
|
|
this._emitter.on('bonjour', cb);
|
|
|
|
}
|
|
|
|
emitBonjour(dispatcher: string): void {
|
|
|
|
this._emitter.emit('bonjour', dispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
emitNewTopicsAvailable(topics: availableTopics): void {
|
|
|
|
this._emitter.emit('topics',topics)
|
|
|
|
}
|
|
|
|
onNewTopicsAvailable(cb: (topics: availableTopics) => void) {
|
|
|
|
this._emitter.on('topics',cb)
|
|
|
|
}
|
|
|
|
onEvent(event: string, cb: (data: externalEvent) => void): void {
|
|
|
|
this._emitter.on('event_'+event, cb);
|
|
|
|
}
|
|
|
|
emitEvent(event: string, data: externalEvent): void {
|
|
|
|
this._emitter.emit('event_'+event, data)
|
|
|
|
}
|
|
|
|
offEvent(event: string, cb: (data: externalEvent) => void): void {
|
|
|
|
this._emitter.off('event_'+event, cb);
|
|
|
|
}
|
2020-09-13 14:09:01 +00:00
|
|
|
|
2020-08-21 16:38:21 +00:00
|
|
|
protected _emitter = new EventEmitter();
|
|
|
|
}
|