forked from mfucci/node-matter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageExchange.ts
292 lines (261 loc) · 12.7 KB
/
MessageExchange.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
* @license
* Copyright 2022 The node-matter Authors
* SPDX-License-Identifier: Apache-2.0
*/
import { Message, MessageCodec, SessionType } from "../../codec/MessageCodec";
import { Queue } from "../../util/Queue";
import {Session, SLEEPY_ACTIVE_INTERVAL_MS, SLEEPY_IDLE_INTERVAL_MS} from "../session/Session";
import { MessageType, SECURE_CHANNEL_PROTOCOL_ID } from "../session/secure/SecureChannelMessages";
import { MessageChannel, MessageCounter } from "./ExchangeManager";
import { getPromiseResolver } from "../../util/Promises";
import { Time, Timer } from "../../time/Time";
import { Logger } from "../../log/Logger";
import { NodeId } from "./NodeId";
import { ByteArray } from "@project-chip/matter.js";
import { SecureChannelProtocol } from "../session/secure/SecureChannelProtocol";
import { MatterError } from "../../error/MatterError";
const logger = Logger.get("MessageExchange");
export class UnexpectedMessageError extends MatterError {
public constructor(
message: string,
public readonly receivedMessage: Message,
) {
super(`(${MessageCodec.messageToString(receivedMessage)}) ${message}`);
}
}
/** The base number for the exponential backoff equation. */
const MRP_BACKOFF_BASE = 1.6;
/** The scaler for random jitter in the backoff equation. */
const MRP_BACKOFF_JITTER = 0.25;
/** The scaler margin increase to backoff over the peer sleepy interval. */
const MRP_BACKOFF_MARGIN = 1.1;
/** The number of retransmissions before transitioning from linear to exponential backoff. */
const MRP_BACKOFF_THRESHOLD = 1;
/**
* Amount of time to wait for an opportunity to piggyback an acknowledgement on an outbound message before
* falling back to sending a standalone acknowledgement.
*/
const MRP_STANDALONE_ACK_TIMEOUT = 200;
/** @see {@link MatterCoreSpecificationV1_0}, section 4.11.2.1 */
const MAXIMUM_TRANSMISSION_TIME_MS = 9495; // 413 + 825 + 1485 + 2541 + 4231 ms as per specs
export class MessageExchange<ContextT> {
static fromInitialMessage<ContextT>(
channel: MessageChannel<ContextT>,
messageCounter: MessageCounter,
initialMessage: Message,
closeCallback: () => void,
) {
const { session } = channel;
const exchange = new MessageExchange<ContextT>(
session,
channel,
messageCounter,
false,
session.getId(),
initialMessage.packetHeader.destNodeId,
initialMessage.packetHeader.sourceNodeId,
initialMessage.payloadHeader.exchangeId,
initialMessage.payloadHeader.protocolId,
closeCallback,
)
return exchange;
}
static initiate<ContextT>(
channel: MessageChannel<ContextT>,
exchangeId: number,
protocolId: number,
messageCounter: MessageCounter,
closeCallback: () => void,
) {
const { session } = channel;
return new MessageExchange(
session,
channel,
messageCounter,
true,
session.getPeerSessionId(),
session.getNodeId(),
session.getPeerNodeId(),
exchangeId,
protocolId,
closeCallback,
);
}
private readonly activeRetransmissionTimeoutMs: number;
private readonly idleRetransmissionTimeoutMs: number;
private readonly retransmissionRetries: number;
private readonly messagesQueue = new Queue<Message>();
private receivedMessageToAck: Message | undefined;
private sentMessageToAck: Message | undefined;
private sentMessageAckSuccess: ((...args: any[]) => void) | undefined;
private sentMessageAckFailure: ((error?: Error) => void) | undefined;
private retransmissionTimer: Timer | undefined;
constructor(
readonly session: Session<ContextT>,
readonly channel: MessageChannel<ContextT>,
private readonly messageCounter: MessageCounter,
private readonly isInitiator: boolean,
private readonly peerSessionId: number,
private readonly nodeId: NodeId | undefined,
private readonly peerNodeId: NodeId | undefined,
private readonly exchangeId: number,
private readonly protocolId: number,
private readonly closeCallback: () => void,
) {
const { activeRetransmissionTimeoutMs, idleRetransmissionTimeoutMs , retransmissionRetries } = session.getMrpParameters();
this.activeRetransmissionTimeoutMs = activeRetransmissionTimeoutMs ?? SLEEPY_ACTIVE_INTERVAL_MS;
this.idleRetransmissionTimeoutMs = idleRetransmissionTimeoutMs ?? SLEEPY_IDLE_INTERVAL_MS;
this.retransmissionRetries = retransmissionRetries;
logger.debug("new MessageExchange", this.protocolId, this.exchangeId, this.activeRetransmissionTimeoutMs, this.idleRetransmissionTimeoutMs, this.retransmissionRetries);
}
async onMessageReceived(message: Message) {
const { packetHeader: { messageId }, payloadHeader: { requiresAck, ackedMessageId, protocolId, messageType } } = message;
logger.debug("onMessageReceived", this.protocolId, MessageCodec.messageToString(message));
this.session.notifyActivity(true);
if (messageId === this.receivedMessageToAck?.packetHeader.messageId) {
// Received a message retransmission but the reply is not ready yet, ignoring
if (requiresAck) {
await this.send(MessageType.StandaloneAck, new ByteArray(0));
}
return;
}
if (messageId === this.sentMessageToAck?.payloadHeader.ackedMessageId) {
// Received a message retransmission, this means that the other side didn't get our ack
// Resending the previous reply message which contains the ack
await this.channel.send(this.sentMessageToAck);
return;
}
const sentMessageIdToAck = this.sentMessageToAck?.packetHeader.messageId;
if (sentMessageIdToAck !== undefined) {
if (ackedMessageId === undefined) {
// The message has no ack, but one previous message sent still needs to be acked.
throw new Error("Previous message ack is missing");
} else if (ackedMessageId !== sentMessageIdToAck) {
// The message has an ack for another message.
if (SecureChannelProtocol.isStandaloneAck(protocolId, messageType)) {
// Ignore if this is a standalone ack, probably this was a retransmission.
} else {
throw new Error(`Incorrect ack received. Expected ${sentMessageIdToAck}, received: ${ackedMessageId}`);
}
} else {
// The other side has received our previous message
this.retransmissionTimer?.stop();
this.sentMessageAckSuccess?.(message);
this.sentMessageAckSuccess = undefined;
this.sentMessageAckFailure = undefined;
this.sentMessageToAck = undefined;
}
}
if (SecureChannelProtocol.isStandaloneAck(protocolId, messageType)) {
// Don't include standalone acks in the message stream
return;
}
if (protocolId !== this.protocolId) {
throw new Error(`Received a message for an unexpected protocol. Expected: ${this.protocolId}, received: ${protocolId}`);
}
if (requiresAck) {
this.receivedMessageToAck = message;
}
await this.messagesQueue.write(message);
}
async send(messageType: number, payload: ByteArray, expectAckOnly: boolean = false) {
if (this.sentMessageToAck !== undefined) throw new Error("The previous message has not been acked yet, cannot send a new message");
this.session.notifyActivity(false);
const message = {
packetHeader: {
sessionId: this.peerSessionId,
sessionType: SessionType.Unicast, // TODO: support multicast
messageId: this.messageCounter.getIncrementedCounter(),
destNodeId: this.peerNodeId,
sourceNodeId: this.nodeId,
},
payloadHeader: {
exchangeId: this.exchangeId,
protocolId: messageType === MessageType.StandaloneAck ? SECURE_CHANNEL_PROTOCOL_ID : this.protocolId,
messageType,
isInitiatorMessage: this.isInitiator,
requiresAck: messageType !== MessageType.StandaloneAck,
ackedMessageId: this.receivedMessageToAck?.packetHeader.messageId,
},
payload,
};
if (messageType !== MessageType.StandaloneAck) {
this.receivedMessageToAck = undefined;
}
let ackPromise: Promise<Message> | undefined;
if (message.payloadHeader.requiresAck) {
this.sentMessageToAck = message;
this.retransmissionTimer = Time.getTimer(this.getResubmissionBackOffTime(0), () => this.retransmitMessage(message, 0));
const { promise, resolver, rejecter } = await getPromiseResolver<Message>();
ackPromise = promise;
this.sentMessageAckSuccess = resolver;
this.sentMessageAckFailure = rejecter;
}
await this.channel.send(message);
if (ackPromise !== undefined) {
this.retransmissionTimer?.start();
// Await Response to be received (or Message retransmit limit reached which rejects the promise)
const responseMessage = await ackPromise;
this.sentMessageAckSuccess = undefined;
this.sentMessageAckFailure = undefined;
// If we only expect an Ack without data but got data, throw an error
const { payloadHeader: { protocolId, messageType } } = responseMessage;
if (expectAckOnly && !SecureChannelProtocol.isStandaloneAck(protocolId, messageType)) {
throw new UnexpectedMessageError("Expected ack only", responseMessage);
}
}
}
nextMessage() {
return this.messagesQueue.read();
}
async waitFor(messageType: number) {
const message = await this.messagesQueue.read();
const { payloadHeader: { messageType: receivedMessageType } } = message;
if (receivedMessageType !== messageType)
throw new Error(`Received unexpected message type ${receivedMessageType.toString(16)}. Expected ${messageType.toString(16)}`);
return message;
}
/** @see {@link MatterCoreSpecificationV1_0}, section 4.11.2.1 */
private getResubmissionBackOffTime(retransmissionCount: number) {
const baseInterval = this.session.isPeerActive() ? this.activeRetransmissionTimeoutMs : this.idleRetransmissionTimeoutMs;
return Math.floor(MRP_BACKOFF_MARGIN * baseInterval * Math.pow(MRP_BACKOFF_BASE, Math.max(0, retransmissionCount - MRP_BACKOFF_THRESHOLD)) * (1 + Math.random() * MRP_BACKOFF_JITTER));
}
private retransmitMessage(message: Message, retransmissionCount: number) {
retransmissionCount++;
if (retransmissionCount === this.retransmissionRetries) {
if (this.sentMessageToAck !== undefined && this.sentMessageAckFailure !== undefined) {
this.receivedMessageToAck = undefined;
this.sentMessageAckFailure(new Error("Message retransmission limit reached"));
this.sentMessageAckFailure = undefined;
this.sentMessageAckSuccess = undefined;
}
return;
}
this.session.notifyActivity(false);
if (retransmissionCount === 1) {
// this.session.getContext().announce(); // TODO: announce
}
const resubmissionBackoffTime = this.getResubmissionBackOffTime(retransmissionCount);
logger.debug(`Resubmit message ${message.packetHeader.messageId} (attempt ${retransmissionCount}, next backoff time ${resubmissionBackoffTime}ms))`);
this.channel.send(message)
.then(() => {
this.retransmissionTimer = Time.getTimer(resubmissionBackoffTime, () => this.retransmitMessage(message, retransmissionCount))
.start();
})
.catch(error => logger.error("An error happened when retransmitting a message", error));
}
close() {
if (this.receivedMessageToAck !== undefined) {
this.send(MessageType.StandaloneAck, new ByteArray(0))
.catch(error => logger.error("An error happened when closing the exchange", error));
}
// Wait until all potential Resubmissions are done, also for Standalone-Acks
Time.getTimer(MAXIMUM_TRANSMISSION_TIME_MS, () => this.closeInternal()).start();
}
private closeInternal() {
this.retransmissionTimer?.stop();
this.messagesQueue.close();
this.closeCallback();
}
}