-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrokerServer.ts
93 lines (82 loc) · 3.12 KB
/
BrokerServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Author: Ing. Luca Gian Scaringella
GitHub: LucaCode
Copyright(c) Ing. Luca Gian Scaringella
*/
import {BrokerServer as ZironBrokerServer,BrokerServerOptions} from 'ziron-broker';
import MachineState from "machine-state";
import {version as SERVER_VERSION} from './../package.json';
import * as IP from 'ip';
import {memoResult} from "./utils";
export default class BrokerServer extends ZironBrokerServer {
private launchedTimestamp?: number;
constructor(options: BrokerServerOptions = {}) {
super(options);
this._initStandaloneStateProcedure();
this._startResetCounterInterval();
}
public async listen() {
await super.listen();
if(this.launchedTimestamp == null)
this.launchedTimestamp = Date.now();
}
public async listenAndJoin() {
await this.listen();
await this.join();
}
private _initStandaloneStateProcedure() {
this.procedures['#state'] = async (socket, limitToDynamicInfo, end, reject) => {
try {
if(limitToDynamicInfo) end({
...(await this.getDynamicServerStateInfoCached()),
id: this.id
});
else {
const [staticInfo,dynamicInfo] = await Promise.all([this.getStaticServerStateInfoCached(),
this.getDynamicServerStateInfoCached()]);
end({...staticInfo,...dynamicInfo,id: this.id});
}
}
catch (e) {reject(new Error("Failed to load server state."))}
};
}
private _startResetCounterInterval() {
setInterval( () => {
this.server.resetCounts();
},1000);
}
private readonly getStaticServerStateInfoCached = memoResult(this.getStaticServerStateInfo.bind(this));
public async getStaticServerStateInfo() {
const server = this.server;
return {
type: 1,
port: server.port,
path: server.path,
tls: server.tls,
nodeVersion: process.version,
serverVersion: SERVER_VERSION,
launchedTimestamp: this.launchedTimestamp,
...(await MachineState.getGeneralInfo())
}
}
private _dynamicServerInfoPromise: Promise<any> | null = null;
private async getDynamicServerStateInfoCached() {
if(this._dynamicServerInfoPromise != null) return this._dynamicServerInfoPromise;
const result = await (this._dynamicServerInfoPromise = this.getDynamicServerStateInfo());
this._dynamicServerInfoPromise = null;
return result;
}
private async getDynamicServerStateInfo() {
const server = this.server;
return {
ip: IP.address(),
connectedToState: this.isConnectedToState(),
clientCount: server.clientCount,
resourceUsage: (await MachineState.getResourceUsageInfo()),
httpMessageCount: server.httpMessageCount,
wsMessageCount: server.wsMessageCount,
invokeMessageCount: server.invokeMessageCount,
transmitMessageCount: server.transmitMessageCount
}
}
}