Skip to content

Commit b8b6c73

Browse files
committed
Use 'Map' instead of 'Array' to manage network receiver reactors
1 parent 14965ac commit b8b6c73

File tree

1 file changed

+22
-42
lines changed

1 file changed

+22
-42
lines changed

src/core/federation.ts

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ export class NetworkSender extends Reactor {
295295
* The last reaction of a NetworkSender reactor is 'portAbsentReactor'.
296296
* @returns portAbsentReactor of this NetworkSender reactor
297297
*/
298-
public getLastReactioOrMutation(): Reaction<Variable[]> | undefined {
298+
public getLastReactionOrMutation(): Reaction<Variable[]> | undefined {
299299
return this._getLastReactionOrMutation();
300300
}
301301
}
@@ -309,21 +309,6 @@ export class NetworkReceiver<T> extends Reactor {
309309
*/
310310
private networkInputAction: FederatePortAction<T> | undefined;
311311

312-
// The port ID of networkInputAction.
313-
private readonly portID: number;
314-
315-
constructor(parent: Reactor, portID: number) {
316-
super(parent);
317-
this.portID = portID;
318-
}
319-
320-
/**
321-
* Getter for portID of this NetworkReactor.
322-
*/
323-
public getPortID(): number | undefined {
324-
return this.portID;
325-
}
326-
327312
/**
328313
* Register a federate port's action with the network receiver.
329314
* @param networkInputAction The federate port's action for registration.
@@ -336,18 +321,11 @@ export class NetworkReceiver<T> extends Reactor {
336321

337322
/**
338323
* Handle a message being received from the RTI.
339-
* @param portID The destination port ID of the message.
340324
* @param value The payload of the message.
341325
*/
342-
public handleMessage(portID: number, value: T): void {
326+
public handleMessage(value: T): void {
343327
// Schedule this federate port's action.
344328
// This message is untimed, so schedule it immediately.
345-
if (portID !== this.portID) {
346-
this.util.reportError(
347-
"FederatedApp attempts to pass the tagged message to the wrong port ID"
348-
);
349-
return;
350-
}
351329
if (this.networkInputAction !== undefined) {
352330
this.networkInputAction
353331
.asSchedulable(this._getKey(this.networkInputAction))
@@ -360,7 +338,7 @@ export class NetworkReceiver<T> extends Reactor {
360338
* @param portID The destination port ID of the message.
361339
* @param value The payload of the message.
362340
*/
363-
public handleTimedMessage(portID: number, value: T, intendedTag: Tag): void {
341+
public handleTimedMessage(value: T, intendedTag: Tag): void {
364342
// Schedule this federate port's action.
365343

366344
/**
@@ -382,12 +360,6 @@ export class NetworkReceiver<T> extends Reactor {
382360

383361
// FIXME: implement decentralized control.
384362

385-
if (portID !== this.portID) {
386-
this.util.reportError(
387-
"FederatedApp attempts to pass the tagged message to the wrong port ID"
388-
);
389-
return;
390-
}
391363
if (this.networkInputAction !== undefined) {
392364
if (this.networkInputAction.origin === Origin.logical) {
393365
this.networkInputAction
@@ -1215,7 +1187,10 @@ export class FederatedApp extends App {
12151187
/**
12161188
* An array of network receivers
12171189
*/
1218-
private readonly networkReceivers: Array<NetworkReceiver<unknown>> = [];
1190+
private readonly networkReceivers = new Map<
1191+
number,
1192+
NetworkReceiver<unknown>
1193+
>();
12191194

12201195
/**
12211196
* An array of network senders
@@ -1462,9 +1437,10 @@ export class FederatedApp extends App {
14621437
* @param networkReceiver The designated network receiver reactor.
14631438
*/
14641439
public registerNetworkReceiver(
1440+
portID: number,
14651441
networkReceiver: NetworkReceiver<unknown>
14661442
): void {
1467-
this.networkReceivers.push(networkReceiver);
1443+
this.networkReceivers.set(portID, networkReceiver);
14681444
}
14691445

14701446
/**
@@ -1475,7 +1451,7 @@ export class FederatedApp extends App {
14751451
public registerNetworkSender(networkSender: NetworkSender): void {
14761452
this.networkSenders.push(networkSender);
14771453

1478-
const portAbsentReaction = networkSender.getLastReactioOrMutation();
1454+
const portAbsentReaction = networkSender.getLastReactionOrMutation();
14791455
if (portAbsentReaction !== undefined) {
14801456
this.portAbsentReactions.add(portAbsentReaction);
14811457
}
@@ -1699,10 +1675,12 @@ export class FederatedApp extends App {
16991675
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
17001676
const value: T = JSON.parse(messageBuffer.toString());
17011677

1702-
for (const candidate of this.networkReceivers) {
1703-
if (candidate.getPortID() === destPortID) {
1704-
candidate.handleMessage(destPortID, value);
1705-
}
1678+
try {
1679+
this.networkReceivers.get(destPortID)?.handleMessage(value);
1680+
} catch (e) {
1681+
Log.error(this, () => {
1682+
return `${e}`;
1683+
});
17061684
}
17071685
}
17081686
);
@@ -1717,10 +1695,12 @@ export class FederatedApp extends App {
17171695
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
17181696
const value: T = JSON.parse(messageBuffer.toString());
17191697

1720-
for (const candidate of this.networkReceivers) {
1721-
if (candidate.getPortID() === destPortID) {
1722-
candidate.handleTimedMessage(destPortID, value, tag);
1723-
}
1698+
try {
1699+
this.networkReceivers.get(destPortID)?.handleTimedMessage(value, tag);
1700+
} catch (e) {
1701+
Log.error(this, () => {
1702+
return `${e}`;
1703+
});
17241704
}
17251705
}
17261706
);

0 commit comments

Comments
 (0)