@ti-engine/core 1.6.1 → 1.7.2
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/CHANGELOG.md +383 -364
- package/LICENSE.md +321 -321
- package/README.md +597 -548
- package/bin/localization/labels.json +122 -122
- package/bin/settings.json +41 -41
- package/bin/start-instance.js +164 -156
- package/components/auditing.js +191 -191
- package/components/connection-observer.js +72 -72
- package/components/definitions.types.js +248 -248
- package/components/exchange/default/default-message-exchange.js +136 -136
- package/components/exchange/default/default-message-receiver.js +101 -101
- package/components/exchange/default/default-message-sender.js +100 -100
- package/components/exchange/message-dispatcher.js +168 -168
- package/components/exchange/message-exchange.js +449 -449
- package/components/exchange/message-handler.js +235 -234
- package/components/exchange/message-memory-cache.js +190 -190
- package/components/exchange/message-observer.js +126 -126
- package/components/exchange/message-receiver.js +181 -181
- package/components/exchange/message-sender.js +143 -143
- package/components/exchange/message-tracer.js +212 -212
- package/components/service-caller.js +370 -370
- package/components/service-consumer.js +131 -131
- package/components/service-executor.js +278 -278
- package/components/service-instance.js +316 -316
- package/components/service-provider.js +251 -251
- package/integrations/redis-integration.js +591 -591
- package/package.json +89 -90
- package/utils/cache.js +772 -772
- package/utils/config.js +103 -103
- package/utils/exceptions.js +368 -368
- package/utils/localization.js +298 -298
- package/utils/logger.js +82 -82
- package/utils/tools.js +632 -632
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
-
* Copyright © 2021-2025 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
-
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
-
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
-
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const MessageSender = require( "#message-sender" );
|
|
10
|
-
const _ = require( "lodash" );
|
|
11
|
-
const config = require( "#config" );
|
|
12
|
-
const exceptions = require( "#exceptions" );
|
|
13
|
-
const memoryCache = require( "#message-memory-cache" );
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The default {@link MessageSender} behavior for the Ti Engine using Redis for message exchange.
|
|
17
|
-
*
|
|
18
|
-
* @class DefaultMessageSender
|
|
19
|
-
* @extends MessageSender
|
|
20
|
-
* @public
|
|
21
|
-
*/
|
|
22
|
-
class DefaultMessageSender extends MessageSender {
|
|
23
|
-
|
|
24
|
-
#memoryCache;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @constructor
|
|
28
|
-
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
29
|
-
*/
|
|
30
|
-
constructor( identifier ) {
|
|
31
|
-
super( identifier );
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Used to perform the actual sending of a message.
|
|
36
|
-
* <br/>
|
|
37
|
-
* NOTE: The default message exchange works with lightweight messages (i.e., will keep the payloads stored in Redis while exchanging).
|
|
38
|
-
*
|
|
39
|
-
* @method
|
|
40
|
-
* @param {Message} message The message to send.
|
|
41
|
-
* @param {string} queue The route to destination (queue) for the message as recognized by the {@link MessageExchange} implementation.
|
|
42
|
-
* @returns {Promise}
|
|
43
|
-
* @override
|
|
44
|
-
* @public
|
|
45
|
-
*/
|
|
46
|
-
onSend( message, queue ) {
|
|
47
|
-
return new Promise( ( resolve, reject ) => {
|
|
48
|
-
this.#memoryCache.storeMessagePayload( message.payload, config.getSetting( config.setting.MESSAGE_EXCHANGE_MESSAGE_STORE ) ).then( ( storeID ) => {
|
|
49
|
-
let lightweightMessage = _.cloneDeep( message );
|
|
50
|
-
lightweightMessage.payload = storeID;
|
|
51
|
-
return this.#memoryCache.sendMessage( lightweightMessage, queue );
|
|
52
|
-
} ).then( () => {
|
|
53
|
-
resolve();
|
|
54
|
-
} ).catch( ( error ) => {
|
|
55
|
-
reject( exceptions.raise( error ) );
|
|
56
|
-
} );
|
|
57
|
-
} );
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Used to initialize and enable the communication capabilities of the handler.
|
|
62
|
-
*
|
|
63
|
-
* @method
|
|
64
|
-
* @returns {Promise}
|
|
65
|
-
* @override
|
|
66
|
-
* @public
|
|
67
|
-
*/
|
|
68
|
-
enable() {
|
|
69
|
-
return new Promise( ( resolve, reject ) => {
|
|
70
|
-
this.#memoryCache = memoryCache.create( this.connectionIdentifier );
|
|
71
|
-
this.#memoryCache.addConnectionObserver( this );
|
|
72
|
-
this.#memoryCache.initialize().then( () => {
|
|
73
|
-
resolve();
|
|
74
|
-
} ).catch( ( error ) => {
|
|
75
|
-
reject( exceptions.raise( error ) );
|
|
76
|
-
} );
|
|
77
|
-
} );
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Used to shut down and disable the communication behavior of the handler.
|
|
82
|
-
*
|
|
83
|
-
* @method
|
|
84
|
-
* @returns {Promise}
|
|
85
|
-
* @override
|
|
86
|
-
* @public
|
|
87
|
-
*/
|
|
88
|
-
disable() {
|
|
89
|
-
return new Promise( ( resolve, reject ) => {
|
|
90
|
-
this.isAvailable = false;
|
|
91
|
-
this.#memoryCache.shutDown().then( () => {
|
|
92
|
-
this.#memoryCache = null;
|
|
93
|
-
resolve();
|
|
94
|
-
} ).catch( ( error ) => {
|
|
95
|
-
reject( exceptions.raise( error ) );
|
|
96
|
-
} );
|
|
97
|
-
} );
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
1
|
+
/*
|
|
2
|
+
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
+
* Copyright © 2021-2025 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
+
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const MessageSender = require( "#message-sender" );
|
|
10
|
+
const _ = require( "lodash" );
|
|
11
|
+
const config = require( "#config" );
|
|
12
|
+
const exceptions = require( "#exceptions" );
|
|
13
|
+
const memoryCache = require( "#message-memory-cache" );
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The default {@link MessageSender} behavior for the Ti Engine using Redis for message exchange.
|
|
17
|
+
*
|
|
18
|
+
* @class DefaultMessageSender
|
|
19
|
+
* @extends MessageSender
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
class DefaultMessageSender extends MessageSender {
|
|
23
|
+
|
|
24
|
+
#memoryCache;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @constructor
|
|
28
|
+
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
29
|
+
*/
|
|
30
|
+
constructor( identifier ) {
|
|
31
|
+
super( identifier );
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Used to perform the actual sending of a message.
|
|
36
|
+
* <br/>
|
|
37
|
+
* NOTE: The default message exchange works with lightweight messages (i.e., will keep the payloads stored in Redis while exchanging).
|
|
38
|
+
*
|
|
39
|
+
* @method
|
|
40
|
+
* @param {Message} message The message to send.
|
|
41
|
+
* @param {string} queue The route to destination (queue) for the message as recognized by the {@link MessageExchange} implementation.
|
|
42
|
+
* @returns {Promise}
|
|
43
|
+
* @override
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
onSend( message, queue ) {
|
|
47
|
+
return new Promise( ( resolve, reject ) => {
|
|
48
|
+
this.#memoryCache.storeMessagePayload( message.payload, config.getSetting( config.setting.MESSAGE_EXCHANGE_MESSAGE_STORE ) ).then( ( storeID ) => {
|
|
49
|
+
let lightweightMessage = _.cloneDeep( message );
|
|
50
|
+
lightweightMessage.payload = storeID;
|
|
51
|
+
return this.#memoryCache.sendMessage( lightweightMessage, queue );
|
|
52
|
+
} ).then( () => {
|
|
53
|
+
resolve();
|
|
54
|
+
} ).catch( ( error ) => {
|
|
55
|
+
reject( exceptions.raise( error ) );
|
|
56
|
+
} );
|
|
57
|
+
} );
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Used to initialize and enable the communication capabilities of the handler.
|
|
62
|
+
*
|
|
63
|
+
* @method
|
|
64
|
+
* @returns {Promise}
|
|
65
|
+
* @override
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
enable() {
|
|
69
|
+
return new Promise( ( resolve, reject ) => {
|
|
70
|
+
this.#memoryCache = memoryCache.create( this.connectionIdentifier );
|
|
71
|
+
this.#memoryCache.addConnectionObserver( this );
|
|
72
|
+
this.#memoryCache.initialize().then( () => {
|
|
73
|
+
resolve();
|
|
74
|
+
} ).catch( ( error ) => {
|
|
75
|
+
reject( exceptions.raise( error ) );
|
|
76
|
+
} );
|
|
77
|
+
} );
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Used to shut down and disable the communication behavior of the handler.
|
|
82
|
+
*
|
|
83
|
+
* @method
|
|
84
|
+
* @returns {Promise}
|
|
85
|
+
* @override
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
disable() {
|
|
89
|
+
return new Promise( ( resolve, reject ) => {
|
|
90
|
+
this.isAvailable = false;
|
|
91
|
+
this.#memoryCache.shutDown().then( () => {
|
|
92
|
+
this.#memoryCache = null;
|
|
93
|
+
resolve();
|
|
94
|
+
} ).catch( ( error ) => {
|
|
95
|
+
reject( exceptions.raise( error ) );
|
|
96
|
+
} );
|
|
97
|
+
} );
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
101
|
module.exports = DefaultMessageSender;
|
|
@@ -1,169 +1,169 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
-
* Copyright © 2021-2026 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
-
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
-
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
-
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const tools = require( "#tools" );
|
|
10
|
-
const exceptions = require( "#exceptions" );
|
|
11
|
-
const logger = require( "#logger" );
|
|
12
|
-
const messageTracer = require( "#message-tracer" );
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Used to create and/or return a Message Dispatcher singleton instance.
|
|
16
|
-
* This class handles the internal message dispatching between the microservices.
|
|
17
|
-
*
|
|
18
|
-
* @class MessageDispatcher
|
|
19
|
-
* @singleton
|
|
20
|
-
* @public
|
|
21
|
-
*/
|
|
22
|
-
class MessageDispatcher {
|
|
23
|
-
|
|
24
|
-
static #instance = null;
|
|
25
|
-
#messageExchange;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @constructor
|
|
29
|
-
* @returns {MessageDispatcher}
|
|
30
|
-
*/
|
|
31
|
-
constructor() {
|
|
32
|
-
if ( !MessageDispatcher.#instance ) {
|
|
33
|
-
MessageDispatcher.#instance = this;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return MessageDispatcher.#instance;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/* Public interface */
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Used to initialize the message dispatcher and enable the message exchange.
|
|
43
|
-
*
|
|
44
|
-
* @method
|
|
45
|
-
* @param {MessageExchange} messageExchange The message exchange instance to be used by the dispatcher.
|
|
46
|
-
* @param {boolean} configureInbound If set to 'true' it tells the message exchange to set up inbound messaging.
|
|
47
|
-
* @param {boolean} configureOutbound If set to 'true' it tells the message exchange to set up outbound messaging.
|
|
48
|
-
* @returns {Promise}
|
|
49
|
-
* @public
|
|
50
|
-
*/
|
|
51
|
-
initialize( messageExchange, configureInbound, configureOutbound ) {
|
|
52
|
-
return new Promise( ( resolve, reject ) => {
|
|
53
|
-
this.#messageExchange = messageExchange;
|
|
54
|
-
|
|
55
|
-
// Initialize the message tracer before enabling the message exchange:
|
|
56
|
-
messageTracer.instance.initialize().then( () => {
|
|
57
|
-
return this.#messageExchange.enableMessaging( configureInbound, configureOutbound );
|
|
58
|
-
} ).then( () => {
|
|
59
|
-
resolve();
|
|
60
|
-
} ).catch( ( error ) => {
|
|
61
|
-
reject( exceptions.raise( error ) );
|
|
62
|
-
} );
|
|
63
|
-
} );
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Used to shut down the message dispatcher and disable the message exchange.
|
|
68
|
-
*
|
|
69
|
-
* @method
|
|
70
|
-
* @returns {Promise}
|
|
71
|
-
* @public
|
|
72
|
-
*/
|
|
73
|
-
shutDown() {
|
|
74
|
-
return new Promise( ( resolve, reject ) => {
|
|
75
|
-
this.#messageExchange.disableMessaging().then( () => {
|
|
76
|
-
this.#messageExchange = null;
|
|
77
|
-
resolve();
|
|
78
|
-
} ).catch( ( error ) => {
|
|
79
|
-
reject( exceptions.raise( error ) );
|
|
80
|
-
} );
|
|
81
|
-
} );
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Used to send a message request via the message exchange system.
|
|
86
|
-
*
|
|
87
|
-
* @method
|
|
88
|
-
* @param {Message} message The message to send. This can also be a subclass of {@link Message}.
|
|
89
|
-
* @returns {Promise<string>}
|
|
90
|
-
* @public
|
|
91
|
-
*/
|
|
92
|
-
sendRequest( message ) {
|
|
93
|
-
return new Promise( ( resolve, reject ) => {
|
|
94
|
-
let retry = new tools.RetryPolicy( 3 );
|
|
95
|
-
retry.onFailedAttempt( ( error ) => {
|
|
96
|
-
logger.log( `Failed to send message request with chain ID: ${ message.chainID }`, logger.logSeverity.WARNING, error );
|
|
97
|
-
} );
|
|
98
|
-
retry.onRetry( ( attempt, error ) => {
|
|
99
|
-
logger.log( `Retrying to send message response with chain ID: ${ message.chainID }. This is attempt ${ attempt }...`, logger.logSeverity.NOTICE, ( error ) ? { error: tools.errorToJSON( error ) } : undefined );
|
|
100
|
-
} );
|
|
101
|
-
|
|
102
|
-
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_REQUEST, messageTracer.dispatchEvent.SENT, messageTracer.messageState.PENDING );
|
|
103
|
-
|
|
104
|
-
retry.execute( this.#messageExchange, this.#messageExchange.sendMessageRequest, [ message ] ).then( () => {
|
|
105
|
-
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_REQUEST, messageTracer.dispatchEvent.DELIVERED, messageTracer.messageState.PENDING );
|
|
106
|
-
resolve( message.messageID );
|
|
107
|
-
} ).catch( ( error ) => {
|
|
108
|
-
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_REQUEST, messageTracer.dispatchEvent.FAILED, messageTracer.messageState.PENDING );
|
|
109
|
-
reject( exceptions.raise( error ) );
|
|
110
|
-
} );
|
|
111
|
-
} );
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Used to send a message response via the message exchange system.
|
|
116
|
-
*
|
|
117
|
-
* @method
|
|
118
|
-
* @param {Message} message The message to send. This can also be a subclass of {@link Message}.
|
|
119
|
-
* @returns {Promise}
|
|
120
|
-
* @public
|
|
121
|
-
*/
|
|
122
|
-
sendResponse( message ) {
|
|
123
|
-
return new Promise( ( resolve, reject ) => {
|
|
124
|
-
let retry = new tools.RetryPolicy( 3 );
|
|
125
|
-
retry.onFailedAttempt( ( error ) => {
|
|
126
|
-
logger.log( `Failed to send message response with chain ID: ${ message.chainID }`, logger.logSeverity.WARNING, error );
|
|
127
|
-
} );
|
|
128
|
-
retry.onRetry( ( attempt, error ) => {
|
|
129
|
-
logger.log( `Retrying to send message response with chain ID: ${ message.chainID }. This is attempt ${ attempt }...`, logger.logSeverity.NOTICE, ( error ) ? { error: tools.errorToJSON( error ) } : undefined );
|
|
130
|
-
} );
|
|
131
|
-
|
|
132
|
-
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_RESPONSE, messageTracer.dispatchEvent.SENT, messageTracer.messageState.PROCESSED );
|
|
133
|
-
|
|
134
|
-
retry.execute( this.#messageExchange, this.#messageExchange.sendMessageResponse, [ message ] ).then( () => {
|
|
135
|
-
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_RESPONSE, messageTracer.dispatchEvent.DELIVERED, messageTracer.messageState.PROCESSED );
|
|
136
|
-
resolve();
|
|
137
|
-
} ).catch( ( error ) => {
|
|
138
|
-
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_RESPONSE, messageTracer.dispatchEvent.FAILED, messageTracer.messageState.PROCESSED );
|
|
139
|
-
reject( exceptions.raise( error ) );
|
|
140
|
-
} );
|
|
141
|
-
} );
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Used to add an additional {@link MessageObserver} to the connection for the incoming message requests.
|
|
146
|
-
*
|
|
147
|
-
* @method
|
|
148
|
-
* @param {MessageObserver} messageObserver
|
|
149
|
-
* @public
|
|
150
|
-
*/
|
|
151
|
-
addMessageObserverRequestsIn( messageObserver ) {
|
|
152
|
-
this.#messageExchange.addMessageObserverRequestsIn( messageObserver );
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Used to add an additional {@link MessageObserver} to the connection for the incoming message responses.
|
|
157
|
-
*
|
|
158
|
-
* @method
|
|
159
|
-
* @param {MessageObserver} messageObserver
|
|
160
|
-
* @public
|
|
161
|
-
*/
|
|
162
|
-
addMessageObserverResponsesIn( messageObserver ) {
|
|
163
|
-
this.#messageExchange.addMessageObserverResponsesIn( messageObserver );
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const instance = new MessageDispatcher();
|
|
1
|
+
/*
|
|
2
|
+
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
+
* Copyright © 2021-2026 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
+
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const tools = require( "#tools" );
|
|
10
|
+
const exceptions = require( "#exceptions" );
|
|
11
|
+
const logger = require( "#logger" );
|
|
12
|
+
const messageTracer = require( "#message-tracer" );
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Used to create and/or return a Message Dispatcher singleton instance.
|
|
16
|
+
* This class handles the internal message dispatching between the microservices.
|
|
17
|
+
*
|
|
18
|
+
* @class MessageDispatcher
|
|
19
|
+
* @singleton
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
class MessageDispatcher {
|
|
23
|
+
|
|
24
|
+
static #instance = null;
|
|
25
|
+
#messageExchange;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @constructor
|
|
29
|
+
* @returns {MessageDispatcher}
|
|
30
|
+
*/
|
|
31
|
+
constructor() {
|
|
32
|
+
if ( !MessageDispatcher.#instance ) {
|
|
33
|
+
MessageDispatcher.#instance = this;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return MessageDispatcher.#instance;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/* Public interface */
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Used to initialize the message dispatcher and enable the message exchange.
|
|
43
|
+
*
|
|
44
|
+
* @method
|
|
45
|
+
* @param {MessageExchange} messageExchange The message exchange instance to be used by the dispatcher.
|
|
46
|
+
* @param {boolean} configureInbound If set to 'true' it tells the message exchange to set up inbound messaging.
|
|
47
|
+
* @param {boolean} configureOutbound If set to 'true' it tells the message exchange to set up outbound messaging.
|
|
48
|
+
* @returns {Promise}
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
initialize( messageExchange, configureInbound, configureOutbound ) {
|
|
52
|
+
return new Promise( ( resolve, reject ) => {
|
|
53
|
+
this.#messageExchange = messageExchange;
|
|
54
|
+
|
|
55
|
+
// Initialize the message tracer before enabling the message exchange:
|
|
56
|
+
messageTracer.instance.initialize().then( () => {
|
|
57
|
+
return this.#messageExchange.enableMessaging( configureInbound, configureOutbound );
|
|
58
|
+
} ).then( () => {
|
|
59
|
+
resolve();
|
|
60
|
+
} ).catch( ( error ) => {
|
|
61
|
+
reject( exceptions.raise( error ) );
|
|
62
|
+
} );
|
|
63
|
+
} );
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Used to shut down the message dispatcher and disable the message exchange.
|
|
68
|
+
*
|
|
69
|
+
* @method
|
|
70
|
+
* @returns {Promise}
|
|
71
|
+
* @public
|
|
72
|
+
*/
|
|
73
|
+
shutDown() {
|
|
74
|
+
return new Promise( ( resolve, reject ) => {
|
|
75
|
+
this.#messageExchange.disableMessaging().then( () => {
|
|
76
|
+
this.#messageExchange = null;
|
|
77
|
+
resolve();
|
|
78
|
+
} ).catch( ( error ) => {
|
|
79
|
+
reject( exceptions.raise( error ) );
|
|
80
|
+
} );
|
|
81
|
+
} );
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Used to send a message request via the message exchange system.
|
|
86
|
+
*
|
|
87
|
+
* @method
|
|
88
|
+
* @param {Message} message The message to send. This can also be a subclass of {@link Message}.
|
|
89
|
+
* @returns {Promise<string>}
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
sendRequest( message ) {
|
|
93
|
+
return new Promise( ( resolve, reject ) => {
|
|
94
|
+
let retry = new tools.RetryPolicy( 3 );
|
|
95
|
+
retry.onFailedAttempt( ( error ) => {
|
|
96
|
+
logger.log( `Failed to send message request with chain ID: ${ message.chainID }`, logger.logSeverity.WARNING, error );
|
|
97
|
+
} );
|
|
98
|
+
retry.onRetry( ( attempt, error ) => {
|
|
99
|
+
logger.log( `Retrying to send message response with chain ID: ${ message.chainID }. This is attempt ${ attempt }...`, logger.logSeverity.NOTICE, ( error ) ? { error: tools.errorToJSON( error ) } : undefined );
|
|
100
|
+
} );
|
|
101
|
+
|
|
102
|
+
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_REQUEST, messageTracer.dispatchEvent.SENT, messageTracer.messageState.PENDING );
|
|
103
|
+
|
|
104
|
+
retry.execute( this.#messageExchange, this.#messageExchange.sendMessageRequest, [ message ] ).then( () => {
|
|
105
|
+
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_REQUEST, messageTracer.dispatchEvent.DELIVERED, messageTracer.messageState.PENDING );
|
|
106
|
+
resolve( message.messageID );
|
|
107
|
+
} ).catch( ( error ) => {
|
|
108
|
+
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_REQUEST, messageTracer.dispatchEvent.FAILED, messageTracer.messageState.PENDING );
|
|
109
|
+
reject( exceptions.raise( error ) );
|
|
110
|
+
} );
|
|
111
|
+
} );
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Used to send a message response via the message exchange system.
|
|
116
|
+
*
|
|
117
|
+
* @method
|
|
118
|
+
* @param {Message} message The message to send. This can also be a subclass of {@link Message}.
|
|
119
|
+
* @returns {Promise}
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
sendResponse( message ) {
|
|
123
|
+
return new Promise( ( resolve, reject ) => {
|
|
124
|
+
let retry = new tools.RetryPolicy( 3 );
|
|
125
|
+
retry.onFailedAttempt( ( error ) => {
|
|
126
|
+
logger.log( `Failed to send message response with chain ID: ${ message.chainID }`, logger.logSeverity.WARNING, error );
|
|
127
|
+
} );
|
|
128
|
+
retry.onRetry( ( attempt, error ) => {
|
|
129
|
+
logger.log( `Retrying to send message response with chain ID: ${ message.chainID }. This is attempt ${ attempt }...`, logger.logSeverity.NOTICE, ( error ) ? { error: tools.errorToJSON( error ) } : undefined );
|
|
130
|
+
} );
|
|
131
|
+
|
|
132
|
+
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_RESPONSE, messageTracer.dispatchEvent.SENT, messageTracer.messageState.PROCESSED );
|
|
133
|
+
|
|
134
|
+
retry.execute( this.#messageExchange, this.#messageExchange.sendMessageResponse, [ message ] ).then( () => {
|
|
135
|
+
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_RESPONSE, messageTracer.dispatchEvent.DELIVERED, messageTracer.messageState.PROCESSED );
|
|
136
|
+
resolve();
|
|
137
|
+
} ).catch( ( error ) => {
|
|
138
|
+
messageTracer.instance.recordTraceEntry( message, messageTracer.messageType.MESSAGE_RESPONSE, messageTracer.dispatchEvent.FAILED, messageTracer.messageState.PROCESSED );
|
|
139
|
+
reject( exceptions.raise( error ) );
|
|
140
|
+
} );
|
|
141
|
+
} );
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Used to add an additional {@link MessageObserver} to the connection for the incoming message requests.
|
|
146
|
+
*
|
|
147
|
+
* @method
|
|
148
|
+
* @param {MessageObserver} messageObserver
|
|
149
|
+
* @public
|
|
150
|
+
*/
|
|
151
|
+
addMessageObserverRequestsIn( messageObserver ) {
|
|
152
|
+
this.#messageExchange.addMessageObserverRequestsIn( messageObserver );
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Used to add an additional {@link MessageObserver} to the connection for the incoming message responses.
|
|
157
|
+
*
|
|
158
|
+
* @method
|
|
159
|
+
* @param {MessageObserver} messageObserver
|
|
160
|
+
* @public
|
|
161
|
+
*/
|
|
162
|
+
addMessageObserverResponsesIn( messageObserver ) {
|
|
163
|
+
this.#messageExchange.addMessageObserverResponsesIn( messageObserver );
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const instance = new MessageDispatcher();
|
|
169
169
|
module.exports.instance = Object.freeze( instance );
|