Skip to content

Commit

Permalink
Start work on NetworkSimulator
Browse files Browse the repository at this point in the history
  • Loading branch information
michielbdejong committed Apr 18, 2024
1 parent 49b4170 commit 618fa31
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ export class PauzeMessage extends Message {
toString(): string {
return `pauze ${this.pauze}`;
}
}
}

export function getMessageType(message: Message | string): string {
if (typeof message === 'string') {
const parts = message.split(' ');
return parts[0];
}
return message.getMessageType();
}
4 changes: 2 additions & 2 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, MeetMessage, ProbeMessage, TraceMessage } from "./messages.js";
import { Message, MeetMessage, ProbeMessage, TraceMessage, getMessageType } from "./messages.js";
import { Entry, createPlantUml } from "./util.js";

export class BasicMessageForwarder {
Expand Down Expand Up @@ -42,7 +42,7 @@ export class BasicMessageForwarder {
[text: string]: string[]
} = {};
// console.log(this.log);
this.log.filter(entry => entry.message.getMessageType() === 'probe').map(entry => {
this.log.filter(entry => getMessageType(entry.message) === 'probe').map(entry => {
if (typeof probeLogs[entry.message.toString()] === 'undefined') {
probeLogs[entry.message.toString()] = [];
}
Expand Down
79 changes: 79 additions & 0 deletions src/simulator/networksimulator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import EventEmitter from "node:events";
import { Message, getMessageType } from "../messages.js";
import { Node } from "../node.js";
import { Entry, createPlantUml } from "../util.js";

export abstract class NetworkNode extends EventEmitter {
abstract process(from: string, message: string);
}
export class NetworkSimulator {
protected nodes: { [name: string]: NetworkNode } = {};
addNode(name: string, node: NetworkNode): void {
this.nodes[name] = node;
}
}

export class LoggingNetworkSimulator extends NetworkSimulator {
private log: Entry[] = [];
logMessageSent(sender: string, receiver: string, message: string): void {
this.log.push(new Entry(sender, receiver, message, 'sent'));
}
logMessageReceived(sender: string, receiver: string, message: Message): void {
this.log.push(new Entry(sender, receiver, message, 'received'));
}
forwardMessage(sender: Node, receiver: Node, message: Message): void {
this.logMessageSent(sender.getName(), receiver.getName(), message.toString());
receiver.receiveMessage(sender, message);
}
getLocalLog(name: string): string[] {
return this.log.filter(entry => {
if (entry.sender === name) {
return (entry.event === 'sent');
}
if (entry.receiver === name) {
return (entry.event === 'received');
}
// istanbul ignore next
return false;
}).map(entry => {
if (entry.event === 'sent') {
return `TO[${entry.receiver}] ${entry.message.toString()}`;
} else {
return `FROM[${entry.sender}] ${entry.message.toString()}`;
}
});
}
getFullLog(includeEachMessageTwice: boolean = false): string[] {
const filtered = (includeEachMessageTwice) ? this.log : this.log.filter(entry => entry.event === 'sent');
return filtered.map(entry => `${entry.describePath()} ${entry.message.toString()}`);
}
getProbeLogs(): {
[text: string]: string[]
} {
const probeLogs: {
[text: string]: string[]
} = {};
// console.log(this.log);
this.log.filter(entry => (getMessageType(entry.message) === 'probe')).map(entry => {
if (typeof probeLogs[entry.message.toString()] === 'undefined') {
probeLogs[entry.message.toString()] = [];
}
probeLogs[entry.message.toString()].push(entry.describePath());
});
return probeLogs;
}
getPlantUml(): string {
return createPlantUml(this.log);
}
}

export class BasicMessageSimulator extends LoggingNetworkSimulator {
addNode(name: string, node: NetworkNode): void {
super.addNode(name, node);
node.on('message', (to: string, message: string) => {
if (typeof this.nodes[to] !== 'undefined') {
this.nodes[to].process(name, message);
}
});
}
}
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export function objectMap(object, mapFn): object {
export class Entry {
sender: string;
receiver: string;
message: Message;
message: Message | string;
event: string;
constructor(sender: string, receiver: string, message: Message, event: string) {
constructor(sender: string, receiver: string, message: Message | string, event: string) {
this.sender = sender;
this.receiver = receiver;
this.message = message;
Expand Down

0 comments on commit 618fa31

Please sign in to comment.