2020-11-04 16:39:20 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-11-04 17:36:04
|
2020-11-06 08:10:30 +00:00
|
|
|
* @modify date 2020-11-06 08:46:24
|
2020-11-04 16:39:20 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
2020-09-10 16:21:01 +00:00
|
|
|
import { connect, Socket } from 'socket.io-client';
|
2020-11-11 17:24:21 +00:00
|
|
|
import { getCentralNopeLogger, getNopeLogger } from "../logger/getLogger";
|
2020-11-11 17:07:50 +00:00
|
|
|
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";
|
2020-11-11 17:07:50 +00:00
|
|
|
import { INopeObservable } from '../types/nope/nopeObservable.interface';
|
2020-11-11 17:24:21 +00:00
|
|
|
import { Logger } from "winston";
|
2020-08-23 07:21:03 +00:00
|
|
|
|
2020-09-11 12:07:26 +00:00
|
|
|
export class IoSocketClient implements ICommunicationInterface {
|
2020-08-23 07:21:03 +00:00
|
|
|
|
2020-11-11 17:07:50 +00:00
|
|
|
connected: INopeObservable<boolean>;
|
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
protected _emitter: typeof Socket;
|
2020-11-11 17:24:21 +00:00
|
|
|
protected _logger: Logger;
|
2020-08-23 07:21:03 +00:00
|
|
|
|
2020-09-10 16:21:01 +00:00
|
|
|
constructor(public uri: string) {
|
2020-11-11 17:07:50 +00:00
|
|
|
const _this = this;
|
|
|
|
|
|
|
|
this.connected = new NopeObservable();
|
|
|
|
this.connected.setContent(false);
|
|
|
|
|
2020-11-11 17:24:21 +00:00
|
|
|
// Make shure we use the http before connecting.
|
|
|
|
this.uri = this.uri.startsWith('http://') ? this.uri : 'http://' + this.uri;
|
|
|
|
|
|
|
|
// Create a Logger
|
|
|
|
this._logger = getNopeLogger('io-socket-client');
|
|
|
|
this._logger.info('connecting to: ' + uri);
|
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
this._emitter = connect(uri);
|
2020-11-11 17:24:21 +00:00
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
this._emitter.on('connect', (...args) => {
|
2020-09-12 00:59:30 +00:00
|
|
|
// Element is connected
|
2020-11-11 17:24:21 +00:00
|
|
|
_this._logger.info('connected');
|
2020-11-11 17:07:50 +00:00
|
|
|
_this.connected.setContent(true);
|
2020-09-12 00:59:30 +00:00
|
|
|
});
|
2020-11-11 17:07:50 +00:00
|
|
|
this._emitter.on('disconnect', () => {
|
|
|
|
// Connection Lost.
|
2020-11-11 17:24:21 +00:00
|
|
|
_this._logger.error('Connection lost!');
|
2020-11-11 17:07:50 +00:00
|
|
|
_this.connected.setContent(false);
|
2020-11-11 17:24:21 +00:00
|
|
|
});
|
2020-08-23 07:21:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
async onTaskCancelation(cb: (msg: ITaskCancelationMsg) => void) {
|
|
|
|
this._emitter.on('cancel', cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitTaskCancelation(msg: ITaskCancelationMsg) {
|
|
|
|
this._emitter.emit('cancel', msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onAurevoir(cb: (dispatcher: string) => void) {
|
|
|
|
this._emitter.on('aurevoir', cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitAurevoir(dispatcher: string) {
|
|
|
|
this._emitter.emit('aurevoir', dispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewInstanceGeneratorsAvailable(generators: IAvailableInstanceGeneratorsMsg) {
|
|
|
|
this._emitter.emit('generators', generators);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onNewInstanceGeneratorsAvailable(cb: (generators: IAvailableInstanceGeneratorsMsg) => void) {
|
|
|
|
this._emitter.on('generators', cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitRpcRequest(name: string, request: IRequestTaskMsg) {
|
|
|
|
this._emitter.emit(name, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitRpcResult(name: string, result: IResponseTaskMsg) {
|
|
|
|
this._emitter.emit(name, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onRpcResult(name: string, cb: (result: IResponseTaskMsg) => void) {
|
|
|
|
this._emitter.on(name, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async offRpcResponse(name: string, cb: (result: IResponseTaskMsg) => void) {
|
|
|
|
this._emitter.off(name, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void) {
|
|
|
|
this._emitter.on(name, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async offRpcRequest(name: string, cb: (data: IRequestTaskMsg) => void) {
|
|
|
|
this._emitter.off(name, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewServicesAvailable(services: IAvailableServicesMsg) {
|
|
|
|
this._emitter.emit('services', services)
|
|
|
|
}
|
|
|
|
|
|
|
|
async onNewServicesAvailable(cb: (services: IAvailableServicesMsg) => void) {
|
|
|
|
this._emitter.on('services', cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onBonjour(cb: (dispatcher: string) => void) {
|
|
|
|
this._emitter.on('bonjour', cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitBonjour(dispatcher: string) {
|
|
|
|
this._emitter.emit('bonjour', dispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitNewTopicsAvailable(topics: IAvailableTopicsMsg) {
|
|
|
|
this._emitter.emit('topics', topics)
|
|
|
|
}
|
|
|
|
|
|
|
|
async onNewTopicsAvailable(cb: (topics: IAvailableTopicsMsg) => void) {
|
|
|
|
this._emitter.on('topics', cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
async onEvent(event: string, cb: (data: IExternalEventMsg) => void) {
|
|
|
|
this._emitter.on('event_' + event, cb);
|
2020-08-23 07:21:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
async emitEvent(event: string, data: IExternalEventMsg) {
|
|
|
|
this._emitter.emit('event_' + event, data)
|
2020-09-12 00:59:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 16:39:20 +00:00
|
|
|
async offEvent(event: string, cb: (data: IExternalEventMsg) => void) {
|
|
|
|
this._emitter.off('event_' + event, cb);
|
2020-08-23 07:21:03 +00:00
|
|
|
}
|
|
|
|
}
|