2020-10-13 13:18:25 +00:00
|
|
|
/**
|
|
|
|
* @author Martin Karkowski
|
|
|
|
* @email m.karkowski@zema.de
|
|
|
|
* @create date 2020-10-12 18:52:00
|
2021-10-19 08:01:00 +00:00
|
|
|
* @modify date 2021-10-19 09:15:25
|
2020-10-13 13:18:25 +00:00
|
|
|
* @desc [description]
|
|
|
|
*/
|
|
|
|
|
2022-01-07 17:12:08 +00:00
|
|
|
import { comparePatternAndPath } from "../helpers/pathMatchingMethods";
|
2020-12-04 18:10:33 +00:00
|
|
|
import {
|
2022-01-03 15:44:39 +00:00
|
|
|
IEventAdditionalData,
|
2022-01-10 06:52:05 +00:00
|
|
|
IEventCallback,
|
2022-01-16 19:38:45 +00:00
|
|
|
INopeDispatcher,
|
2022-01-10 06:52:05 +00:00
|
|
|
INopeDispatcherDescription,
|
2022-01-16 19:38:45 +00:00
|
|
|
INopeObserver,
|
2021-11-14 22:16:07 +00:00
|
|
|
} from "../types/nope/index";
|
2022-01-07 17:12:08 +00:00
|
|
|
import { NopeCore } from "./Core";
|
2020-09-12 00:59:30 +00:00
|
|
|
|
2020-08-21 16:38:21 +00:00
|
|
|
/**
|
2020-12-04 18:10:33 +00:00
|
|
|
* A Dispatcher to perform a function on a Remote
|
2020-09-11 07:59:23 +00:00
|
|
|
* Dispatcher. Therefore a Task is created and forwarded
|
|
|
|
* to the remote.
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @class nopeDispatcher
|
2020-08-21 16:38:21 +00:00
|
|
|
*/
|
2022-01-07 17:12:08 +00:00
|
|
|
export class NopeDispatcher extends NopeCore implements INopeDispatcher {
|
2022-01-10 06:52:05 +00:00
|
|
|
public get masterExists(): boolean {
|
2022-10-29 05:52:14 +00:00
|
|
|
return this.connectivityManager.master.isMasterForced;
|
2022-01-10 06:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See interface description
|
2022-01-16 19:38:45 +00:00
|
|
|
public pushData<T = unknown>(
|
2022-01-10 06:52:05 +00:00
|
|
|
path: string,
|
|
|
|
content: T,
|
2022-01-16 19:38:45 +00:00
|
|
|
options: Partial<IEventAdditionalData> = {}
|
2022-01-10 06:52:05 +00:00
|
|
|
): void {
|
|
|
|
return this.dataDistributor.pushData(path, content, options);
|
2021-08-17 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 06:52:05 +00:00
|
|
|
// See interface description
|
2022-01-16 19:38:45 +00:00
|
|
|
public pullData<T = unknown, D = null>(path: string, _default: D = null): T {
|
2022-01-10 06:52:05 +00:00
|
|
|
return this.dataDistributor.pullData<T, D>(path, _default);
|
2021-08-17 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 06:52:05 +00:00
|
|
|
// See interface description
|
2022-01-16 19:38:45 +00:00
|
|
|
public subscribeToEvent<T = unknown>(
|
2022-01-10 06:52:05 +00:00
|
|
|
event: string,
|
|
|
|
subscription: IEventCallback<T>
|
|
|
|
): INopeObserver {
|
|
|
|
return this.eventDistributor.registerSubscription(event, subscription);
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 06:52:05 +00:00
|
|
|
// See interface description
|
2022-01-16 19:38:45 +00:00
|
|
|
public emitEvent<T>(
|
2022-01-10 06:52:05 +00:00
|
|
|
eventName: string,
|
|
|
|
data: T,
|
|
|
|
options: Partial<IEventAdditionalData> = {}
|
|
|
|
) {
|
|
|
|
this.eventDistributor.emit(eventName, data, options);
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 06:52:05 +00:00
|
|
|
// See interface description
|
2022-01-16 19:38:45 +00:00
|
|
|
public query(
|
2022-01-10 06:52:05 +00:00
|
|
|
pattern: string,
|
|
|
|
type: "instances" | "services" | "properties" | "events"
|
|
|
|
): string[] {
|
|
|
|
let items: string[] = [];
|
2022-01-07 17:12:08 +00:00
|
|
|
switch (type) {
|
|
|
|
case "instances":
|
2022-07-23 05:34:38 +00:00
|
|
|
items = this.instanceManager.instances.data.getContent().map((item) => {
|
|
|
|
return item.identifier;
|
|
|
|
});
|
2022-01-07 17:12:08 +00:00
|
|
|
break;
|
|
|
|
case "services":
|
2022-07-07 07:14:22 +00:00
|
|
|
items = Array.from(
|
|
|
|
// Extract the Ids of the Services
|
|
|
|
this.rpcManager.services.simplified.keys()
|
|
|
|
);
|
2022-01-07 17:12:08 +00:00
|
|
|
break;
|
|
|
|
case "properties":
|
2022-01-10 06:52:05 +00:00
|
|
|
items = this.dataDistributor.publishers.data.getContent();
|
2022-01-07 17:12:08 +00:00
|
|
|
break;
|
|
|
|
case "events":
|
|
|
|
items = this.eventDistributor.publishers.data.getContent();
|
|
|
|
break;
|
|
|
|
default:
|
2022-01-10 06:52:05 +00:00
|
|
|
throw Error("Invalid Type-Parameter");
|
|
|
|
}
|
|
|
|
|
2022-08-03 11:18:26 +00:00
|
|
|
return items.filter((item) => {
|
|
|
|
return comparePatternAndPath(pattern, item).affected;
|
|
|
|
});
|
2022-01-10 06:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See interface description
|
|
|
|
public getAllHosts(): string[] {
|
|
|
|
const hosts = new Set<string>();
|
2022-01-16 19:38:45 +00:00
|
|
|
for (const info of this.connectivityManager.dispatchers.originalData.values()) {
|
2022-01-10 06:52:05 +00:00
|
|
|
hosts.add(info.host.name);
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 06:52:05 +00:00
|
|
|
return Array.from(hosts);
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 06:52:05 +00:00
|
|
|
// See interface description
|
|
|
|
public toDescription(): INopeDispatcherDescription {
|
|
|
|
return Object.assign(this.connectivityManager.info, {
|
2022-01-16 19:38:45 +00:00
|
|
|
isMaster: this.connectivityManager.isMaster,
|
2022-01-10 06:52:05 +00:00
|
|
|
instances: this.instanceManager.instances.data.getContent(),
|
2022-10-29 05:52:14 +00:00
|
|
|
services: this.rpcManager.services.data.getContent(),
|
2022-01-10 06:52:05 +00:00
|
|
|
events: this.eventDistributor.emitters,
|
|
|
|
properties: this.dataDistributor.emitters,
|
|
|
|
// Show the data.
|
|
|
|
data: this.dataDistributor.pullData("", {}),
|
|
|
|
});
|
|
|
|
}
|
2020-12-04 18:10:33 +00:00
|
|
|
}
|