@aj-shadow/z-abs-corelayer-server 0.0.0-aj-beta.221
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +26 -0
- package/LICENSE.txt +96 -0
- package/README.md +5 -0
- package/npm-shrinkwrap.json +13 -0
- package/package.json +10 -0
- package/project/server/_build/Server-CoreLayer-server.bld +12 -0
- package/project/server/_build/z-abs-corelayer-server.prj +12 -0
- package/project/server/communication/core-protocol/decoder.js +404 -0
- package/project/server/communication/core-protocol/encoder.js +503 -0
- package/project/server/communication/messages/messages-s-to-c/message-persistent-init-response.js +14 -0
- package/project/server/communication/messages/messages-s-to-c/message-persistent-publish.js +16 -0
- package/project/server/communication/messages/messages-s-to-c/message-ws-init.js +14 -0
- package/project/server/communication/messages/messages-s-to-s/service-init-request.js +17 -0
- package/project/server/communication/messages/messages-s-to-s/service-init-response.js +18 -0
- package/project/server/communication/messages/messages-s-to-s/service-offer-off.js +15 -0
- package/project/server/communication/messages/messages-s-to-s/service-offer-on.js +15 -0
- package/project/server/communication/messages/messages-s-to-s/worker-init-request.js +15 -0
- package/project/server/data-response.js +588 -0
- package/project/server/high-resolution-timestamp.js +36 -0
- package/project/server/log/log-config.js +107 -0
- package/project/server/log/log-types.js +14 -0
- package/project/server/log/logger-console.js +36 -0
- package/project/server/log/logger.js +114 -0
- package/project/server/node/channel-input.js +65 -0
- package/project/server/node/channel-output.js +39 -0
- package/project/server/node/clients.js +80 -0
- package/project/server/node/connection-address.js +24 -0
- package/project/server/node/connection-data-ip.js +82 -0
- package/project/server/node/connection-data.js +25 -0
- package/project/server/node/const.js +44 -0
- package/project/server/node/external-services.js +209 -0
- package/project/server/node/http-cache.js +109 -0
- package/project/server/node/http-proxy.js +97 -0
- package/project/server/node/http-request-handler.js +219 -0
- package/project/server/node/http-server.js +36 -0
- package/project/server/node/http2-server.js +67 -0
- package/project/server/node/https-server.js +63 -0
- package/project/server/node/ip-client.js +98 -0
- package/project/server/node/ip-server.js +63 -0
- package/project/server/node/ip-subscription.js +46 -0
- package/project/server/node/message-channel.js +98 -0
- package/project/server/node/node-admin.js +513 -0
- package/project/server/node/node-data.js +25 -0
- package/project/server/node/node-settings.js +352 -0
- package/project/server/node/node-worker.js +76 -0
- package/project/server/node/node.js +156 -0
- package/project/server/node/servers.js +168 -0
- package/project/server/node/ws-client.js +12 -0
- package/project/server/node/ws-server.js +20 -0
- package/project/server/node/ws-web-server.js +52 -0
- package/project/server/node/wss-server.js +20 -0
- package/project/server/node/wss-web-server.js +82 -0
- package/project/server/path/actor-path-build.js +29 -0
- package/project/server/path/actor-path-content.js +51 -0
- package/project/server/path/actor-path-creator.js +90 -0
- package/project/server/path/actor-path-data.js +487 -0
- package/project/server/path/actor-path-dist.js +132 -0
- package/project/server/path/actor-path-generated.js +202 -0
- package/project/server/path/actor-path-project.js +181 -0
- package/project/server/path/actor-path.js +57 -0
- package/project/server/path/paths/actor-content-paths.js +34 -0
- package/project/server/path/paths/actor-data-paths.js +302 -0
- package/project/server/path/paths/actor-generated-paths.js +99 -0
- package/project/server/path/paths/actor-paths.js +22 -0
- package/project/server/plugin-base-multi.js +484 -0
- package/project/server/plugin-base.js +233 -0
- package/project/server/plugin-component.js +92 -0
- package/project/server/plugin-data/client/plugin_data_ClientGet.js +18 -0
- package/project/server/plugin-data/dialog-file/plugin_data_DialogFileGet.js +197 -0
- package/project/server/plugin-data/platform/plugin_data_PlatformPing.js +19 -0
- package/project/server/plugin-factor-protocol.js +35 -0
- package/project/server/plugin-factory.js +127 -0
- package/project/server/plugin-lock.js +164 -0
- package/project/server/response-queue.js +46 -0
- package/project/server/responses.js +20 -0
- package/project/server/service/plugin-service.js +264 -0
- package/project/server/service/service-export.js +47 -0
- package/project/server/service/service-manager.js +270 -0
- package/project/server/service/service-requests.js +49 -0
- package/project/server/session-cache.js +39 -0
- package/project/server/session.js +23 -0
- package/project/server/worker/worker-channel.js +175 -0
- package/project/server/worker/worker-core.js +58 -0
- package/project/server/worker/worker-main.js +165 -0
- package/project/server/worker/worker-pool.js +94 -0
- package/project/server/worker/worker-thread.js +128 -0
- package/project/z-abs-corelayer-server.tree +99 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const HttpRequestHandler = require('./http-request-handler');
|
|
5
|
+
const Fs = require('fs');
|
|
6
|
+
const Http2 = require('http2');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Http2Server {
|
|
10
|
+
constructor(serverData, nodeName, nodeId, httpCache, dataResponse, defaultSite) {
|
|
11
|
+
this.serverData = serverData;
|
|
12
|
+
this.nodeName = nodeName;
|
|
13
|
+
this.nodeId = nodeId;
|
|
14
|
+
this.httpRequestHandler = new HttpRequestHandler(httpCache, dataResponse, defaultSite);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
start(http2Data, cb) {
|
|
18
|
+
this.httpRequestHandler.init(http2Data, true);
|
|
19
|
+
const options = this._readKeys(http2Data);
|
|
20
|
+
if(options) {
|
|
21
|
+
const server = Http2.createSecureServer({
|
|
22
|
+
cert: options.certFile,
|
|
23
|
+
key: options.keyFile,
|
|
24
|
+
allowHTTP1: false
|
|
25
|
+
}, (req, res) => {
|
|
26
|
+
this.httpRequestHandler.handle(req, res);
|
|
27
|
+
});
|
|
28
|
+
server.on('listening', () => {
|
|
29
|
+
ddb.writelnTime(ddb.cyan('Server: '), `${this.nodeName}-${http2Data.name} `, ddb.green('started at '), `http://${http2Data.host}:${http2Data.port}`);
|
|
30
|
+
cb();
|
|
31
|
+
});
|
|
32
|
+
server.on('error', (err) => {
|
|
33
|
+
ddb.writelnTimeError(err, ddb.red('Server:'), ` '${this.nodeName}-${http2Data.name}'`, ddb.red(' did not start at'), ` http://${http2Data.host}:${http2Data.port}` + ddb.red('. Error: '), err.message);
|
|
34
|
+
cb(err);
|
|
35
|
+
});
|
|
36
|
+
server.listen(http2Data.port, http2Data.host);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const err = new Error('-----');
|
|
40
|
+
ddb.writelnTimeError(err, ddb.red('Server:'), ` '${this.nodeName}-${http2Data.name}'`, ddb.red(' did not start at '), ` http://${http2Data.host}:${http2Data.port}` + ddb.red('. No SSL keys.'));
|
|
41
|
+
cb(err);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
_readKeys(http2Data) {
|
|
46
|
+
if(http2Data.keyFile && http2Data.certFile) {
|
|
47
|
+
try {
|
|
48
|
+
const keyFile = Fs.readFileSync(http2Data.keyFile);
|
|
49
|
+
const certFile = Fs.readFileSync(http2Data.certFile);
|
|
50
|
+
return {
|
|
51
|
+
key: keyFile,
|
|
52
|
+
cert: certFile
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
catch(err) {
|
|
56
|
+
ddb.writelnTimeError(ddb.red(`Could not get cert and key file, error:`), err);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
module.exports = Http2Server;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const HttpRequestHandler = require('./http-request-handler');
|
|
5
|
+
const Fs = require('fs');
|
|
6
|
+
const Https = require('https');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class HttpsServer {
|
|
10
|
+
constructor(serverData, nodeName, nodeId, httpCache, dataResponse, defaultSite) {
|
|
11
|
+
this.serverData = serverData;
|
|
12
|
+
this.nodeName = nodeName;
|
|
13
|
+
this.nodeId = nodeId;
|
|
14
|
+
this.httpRequestHandler = new HttpRequestHandler(httpCache, dataResponse, defaultSite);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
start(httpsData, cb) {
|
|
18
|
+
this.httpRequestHandler.init(httpsData, true);
|
|
19
|
+
const options = this._readKeys(httpsData);
|
|
20
|
+
if(options) {
|
|
21
|
+
const server = Https.createServer(options, (req, res) => {
|
|
22
|
+
this.httpRequestHandler.handle(req, res);
|
|
23
|
+
});
|
|
24
|
+
server.on('listening', () => {
|
|
25
|
+
ddb.writelnTime(ddb.cyan('Server: '), ` ${this.nodeName}-${httpsData.name}`, ddb.green(' started at'), ` http://${httpsData.host}:${httpsData.port}`);
|
|
26
|
+
cb();
|
|
27
|
+
});
|
|
28
|
+
server.on('error', (err) => {
|
|
29
|
+
ddb.writelnTimeError(err, ddb.red('Server:'), ` '${this.nodeName}-${httpsData.name}'`, ddb.red(' did not start at'), ` http://${httpsData.host}:${httpsData.port}` + ddb.red('. Error: '), err.message);
|
|
30
|
+
cb(err);
|
|
31
|
+
});
|
|
32
|
+
server.listen(httpsData.port, httpsData.host);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const err = new Error('-----');
|
|
36
|
+
ddb.writelnTimeError(err, ddb.red('Server:'), ` '${this.nodeName}-${httpsData.name}'`, ddb.red(' did not start at'), ` http://${httpsData.host}:${httpsData.port}` + ddb.red('. No SSL keys.'));
|
|
37
|
+
cb(err);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_readKeys(httpsData) {
|
|
42
|
+
if(httpsData.keyFile && httpsData.certFile) {
|
|
43
|
+
try {
|
|
44
|
+
const keyFile = Fs.readFileSync(httpsData.keyFile);
|
|
45
|
+
const certFile = Fs.readFileSync(httpsData.certFile);
|
|
46
|
+
return {
|
|
47
|
+
key: keyFile,
|
|
48
|
+
cert: certFile
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
catch(err) {
|
|
52
|
+
ddb.writelnTimeError(ddb.red(`Could not get cert and key file, error:`), err);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
module.exports = HttpsServer;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const Const = require('./const');
|
|
5
|
+
const DataResponse = require('../data-response');
|
|
6
|
+
const PluginFactoryProtocol = require('../plugin-factor-protocol');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class IpClient {
|
|
10
|
+
static STATE_CONNECTING = 1;
|
|
11
|
+
static STATE_CONNECTED = 2;
|
|
12
|
+
static STATE_NOT_CONNECTED = 3;
|
|
13
|
+
|
|
14
|
+
constructor(client, nodeName, nodeId, dataResponse, protocols) {
|
|
15
|
+
this.client = client;
|
|
16
|
+
this.nodeName = nodeName;
|
|
17
|
+
this.nodeId = nodeId;
|
|
18
|
+
this.dataResponse = dataResponse;
|
|
19
|
+
this.protocols = protocols ? protocols : [];
|
|
20
|
+
this.pluginFactoryProtocol = new PluginFactoryProtocol();
|
|
21
|
+
this.data = null;
|
|
22
|
+
this.state = IpClient.STATE_NOT_CONNECTED;
|
|
23
|
+
this.timerId = -1;
|
|
24
|
+
this.downTicks = 0;
|
|
25
|
+
this.connectionData = null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
start(data, cb) {
|
|
29
|
+
this.data = data;
|
|
30
|
+
this.pluginFactoryProtocol.load(this.protocols, () => {
|
|
31
|
+
this._connectionHandler(cb);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
stop(cb) {
|
|
36
|
+
if(-1 !== this.timerId) {
|
|
37
|
+
clearTimeout(this.timerId);
|
|
38
|
+
this.timerId = -1;
|
|
39
|
+
}
|
|
40
|
+
this.data = null;
|
|
41
|
+
process.nextTick(cb);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_connectionHandler(cb) {
|
|
45
|
+
if(IpClient.STATE_NOT_CONNECTED === this.state) {
|
|
46
|
+
this.state = IpClient.STATE_CONNECTING;
|
|
47
|
+
this._connect((connected) => {
|
|
48
|
+
if(!connected) {
|
|
49
|
+
this.state = IpClient.STATE_NOT_CONNECTED;
|
|
50
|
+
this.downTicks += 1;
|
|
51
|
+
this.timerId = setTimeout(() => {
|
|
52
|
+
this.timerId = -1;
|
|
53
|
+
this._connectionHandler();
|
|
54
|
+
}, 1000);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this.state = IpClient.STATE_CONNECTED;
|
|
58
|
+
this.downTicks = 0;
|
|
59
|
+
}
|
|
60
|
+
if(cb) {
|
|
61
|
+
cb();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_connect(cbConnected) {
|
|
68
|
+
this.client.onConnect(this.data, cbConnected);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onConnected(connectionAddress) {
|
|
72
|
+
//ddb.writelnTime(ddb.blueBright('Client: '), ddb.green('connected '), this.nodeName, ddb.yellow('::'), this.nodeId, ddb.green(' to '), this.data.name, ddb.green(' at'), ` tcp://${this.data.host}:${this.data.port}`);
|
|
73
|
+
this.connectionData = this.dataResponse.onConnectionConnected(Const.CONNECTION_TYPE_CLIENT_IP, (buffer) => {
|
|
74
|
+
this.client.onSend(buffer);
|
|
75
|
+
});
|
|
76
|
+
connectionAddress.setData(this.connectionData.id, this.client.transport);
|
|
77
|
+
this.connectionData.setConnectionInit(this.client.name, connectionAddress);
|
|
78
|
+
this.pluginFactoryProtocol.register(this.connectionData.channelInput, this.connectionData.channelOutput);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
onData(msgIn) {
|
|
82
|
+
this.dataResponse.onMessage(msgIn, this.connectionData);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
onClose() {
|
|
86
|
+
//ddb.writelnTime(ddb.blueBright('Client: '), ddb.red('disconnected '), this.nodeName, ddb.yellow('::'), this.nodeId, ddb.red(' from '), this.data.name, ddb.red(' at'), ` tcp://${this.data.host}:${this.data.port}`);
|
|
87
|
+
this.connectionData.closed();
|
|
88
|
+
this.dataResponse.onConnectionClose(Const.CONNECTION_TYPE_CLIENT_IP, this.connectionData);
|
|
89
|
+
this.state = IpClient.STATE_NOT_CONNECTED;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
onError(err) {
|
|
93
|
+
this.state = IpClient.STATE_NOT_CONNECTED;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
module.exports = IpClient;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const Const = require('./const');
|
|
5
|
+
const DataResponse = require('../data-response');
|
|
6
|
+
const PluginFactoryProtocol = require('../plugin-factor-protocol');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class IpServer {
|
|
10
|
+
constructor(server, nodeName, nodeId, dataResponse, protocols) {
|
|
11
|
+
this.server = server;
|
|
12
|
+
this.nodeName = nodeName;
|
|
13
|
+
this.nodeId = nodeId;
|
|
14
|
+
this.dataResponse = dataResponse;
|
|
15
|
+
this.protocols = protocols ? protocols : [];
|
|
16
|
+
this.pluginFactoryProtocol = new PluginFactoryProtocol();
|
|
17
|
+
this.data = null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
start(data, cb) {
|
|
21
|
+
this.data = data;
|
|
22
|
+
this.pluginFactoryProtocol.load(this.protocols, () => {
|
|
23
|
+
this.server.onListen(this.data, cb);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
stop(cb) {
|
|
28
|
+
this.data = null;
|
|
29
|
+
process.nextTick(cb);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
onListen(transport, err) {
|
|
33
|
+
if(!err) {
|
|
34
|
+
ddb.writelnTime(ddb.cyan('Server: '), this.nodeName, ddb.yellow('::'), this.nodeId, ddb.yellow('::'), this.data.name, ddb.green(' started at '), `${Const.TRANSPORT_AS_TEXT[transport]}://${this.data.host}:${this.data.port}`);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
ddb.writelnTimeError(err, ddb.cyan('Server: '), this.nodeName, ddb.yellow('::'), this.nodeId, ddb.yellow('::'), this.data.name, ddb.red(' did not start at'), ` ${Const.TRANSPORT_AS_TEXT[transport]}://${this.data.host}:${this.data.port}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
onConnected(connectionAddress, cbMessage) {
|
|
42
|
+
const connectionData = this.dataResponse.onConnectionConnected(Const.CONNECTION_TYPE_SERVER_IP, (buffer) => {
|
|
43
|
+
cbMessage(buffer);
|
|
44
|
+
});
|
|
45
|
+
connectionAddress.setData(connectionData.id, this.server.transport);
|
|
46
|
+
connectionData.setConnectionInit(this.server.name, connectionAddress);
|
|
47
|
+
this.pluginFactoryProtocol.register(connectionData.channelInput, connectionData.channelOutput);
|
|
48
|
+
this.dataResponse.sendServiceInitRequest(connectionData);
|
|
49
|
+
return connectionData;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
onClose(connectionData) {
|
|
53
|
+
connectionData.closed();
|
|
54
|
+
this.dataResponse.onConnectionClose(Const.CONNECTION_TYPE_SERVER_IP, connectionData);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
onData(msgIn, connectionData) {
|
|
58
|
+
this.dataResponse.onMessage(msgIn, connectionData);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
module.exports = IpServer;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class IpSubscription {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.subscribers = new Map();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
subscribeOnConnectionEvents(guid, eventType, name, cb) {
|
|
11
|
+
const key = `${eventType}_${name}`;
|
|
12
|
+
if(!this.subscribers.has(key)) {
|
|
13
|
+
this.subscribers.set(key, new Map([[guid, cb]]));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
const subscribers = this.subscribers.get(key);
|
|
17
|
+
if(subscribers) {
|
|
18
|
+
subscribers.set(guid, cb);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
unsubscribeOnConnectionEvents(guid, eventType, name) {
|
|
24
|
+
const key = `${eventType}_${name}`;
|
|
25
|
+
const subscribers = this.subscribers.get(key);
|
|
26
|
+
if(subscribers) {
|
|
27
|
+
subscribers.delete(guid);
|
|
28
|
+
if(0 === subscribers.size) {
|
|
29
|
+
this.subscribers.delete(key);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
event(eventType, name, eventId, data) {
|
|
35
|
+
const key = `${eventType}_${name}`;
|
|
36
|
+
const subscribers = this.subscribers.get(key);
|
|
37
|
+
if(subscribers) {
|
|
38
|
+
subscribers.forEach((cb) => {
|
|
39
|
+
cb(eventId, data);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
module.exports = IpSubscription;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const CoreProtocolConst = require('z-abs-corelayer-cs/clientServer/communication/core-protocol/core-protocol-const');
|
|
5
|
+
const ActionRequest = require('z-abs-corelayer-cs/clientServer/communication/action-request');
|
|
6
|
+
const GuidGenerator = require('z-abs-corelayer-cs/clientServer/guid-generator');
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MessageChannel {
|
|
10
|
+
constructor(socket, dataResponse, BufferHandler, name) {
|
|
11
|
+
this.socket = socket;
|
|
12
|
+
this.dataResponse = dataResponse;
|
|
13
|
+
this.BufferHandler = BufferHandler;
|
|
14
|
+
this.name = name;
|
|
15
|
+
this.initialized = false;
|
|
16
|
+
this.requests = new Map();
|
|
17
|
+
this.connectionSessionDataCache = new Map();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
request(serviceAction, sessionId, cb) {
|
|
21
|
+
const guid = GuidGenerator.create();
|
|
22
|
+
serviceAction.setIds(guid, sessionId);
|
|
23
|
+
const request = {
|
|
24
|
+
type: 'DP_REQ',
|
|
25
|
+
sessionId: sessionId,
|
|
26
|
+
data: serviceAction.actionRequest
|
|
27
|
+
};
|
|
28
|
+
this.requests.set(guid, {
|
|
29
|
+
serviceAction: serviceAction,
|
|
30
|
+
cb: cb
|
|
31
|
+
});
|
|
32
|
+
this.socket.write(this.BufferHandler.convertToBuffer(request));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
onMessage(msg, cbInit) {
|
|
36
|
+
if(CoreProtocolConst.INIT === msg.msgId) {
|
|
37
|
+
this.initialized = true;
|
|
38
|
+
cbInit(msg);
|
|
39
|
+
}
|
|
40
|
+
else if(CoreProtocolConst.RESPONSE === msg.msgId) {
|
|
41
|
+
this._response(msg);
|
|
42
|
+
}
|
|
43
|
+
else if('DP_REQ' === msg.type || 0 !== msg.msgId) {
|
|
44
|
+
if(this.initialized) {
|
|
45
|
+
this.dataResponse.onMessage(msg, this.connectionSessionDataCache, (msgOut) => {
|
|
46
|
+
if('object' === typeof msgOut) {
|
|
47
|
+
this.socket.write(this.BufferHandler.convertToBuffer(msgOut), (err) => {});
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
this.socket.write(this.BufferHandler.convertToBufferWithoutStringify(msgOut));
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
if('DP_REQ' === msg.type) {
|
|
56
|
+
// TODO: make an error response.
|
|
57
|
+
ddb.warning(ddb.red('Server: '), this.name, ddb.red(` DROPPED message, received a request when the connection where not initialized. `), this._jsonStringify(msg));
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
ddb.warning(ddb.red('Server: '), this.name, ddb.red(` DROPPED message, received a message when the connection where not initialized. `), this._jsonStringify(msg));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
ddb.warning(ddb.red('Server: '), this.name, ddb.red(' DROPPED message, received a message of unknown type: '), this._jsonStringify(msg));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
_response(response) {
|
|
70
|
+
const serviceActionData = this.requests.get(response.id);
|
|
71
|
+
this.requests.delete(response.id);
|
|
72
|
+
serviceActionData.serviceAction.setResponses(response.responses);
|
|
73
|
+
const responses = serviceActionData.serviceAction.responses;
|
|
74
|
+
if(1 === responses.length) {
|
|
75
|
+
if('success' === responses[0].result.code) {
|
|
76
|
+
serviceActionData.cb(null, responses[0].data);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
serviceActionData.cb(new Error(responses[0].result.msg));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
serviceActionData.cb(new Error('Wrong response format'));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
_jsonStringify(msg) {
|
|
88
|
+
try {
|
|
89
|
+
return JSON.stringify(msg);
|
|
90
|
+
}
|
|
91
|
+
catch(err) {
|
|
92
|
+
return ddb.red('Could not stringify msg');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
module.exports = MessageChannel;
|