Skip to content

Commit

Permalink
Giraffe finds loops :)
Browse files Browse the repository at this point in the history
  • Loading branch information
michielbdejong committed Apr 19, 2024
1 parent d811996 commit d15ab0a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
6 changes: 5 additions & 1 deletion __tests__/fixtures/batched-giraffe-hourglass.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@
"[TraceEngine] handling trace message from Dave: trace genRanHex0 genRanHex3 genRanHex5",
"loop-found genRanHex0 genRanHex3"
],
"loopsFound": []
"loopsFound": [
"genRanHex0 genRanHex1",
"genRanHex0 genRanHex3",
"genRanHex0 genRanHex3"
]
},
"bob": {
"debugLog": [
Expand Down
9 changes: 7 additions & 2 deletions __tests__/fixtures/batched-giraffe-triangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@
"[TracesEngine] sending message to Charlie: trace genRanHex2 genRanHex8 genRanHex10",
"[Node#sendTraceMessage] Alice sends trace message to Charlie: trace genRanHex2 genRanHex8 genRanHex10"
],
"loopsFound": []
"loopsFound": [
"genRanHex0 genRanHex3",
"genRanHex1 genRanHex5"
]
},
"bob": {
"debugLog": [
Expand Down Expand Up @@ -197,7 +200,9 @@
"[TraceEngine] handling trace message from Charlie: trace genRanHex2 genRanHex8 genRanHex10",
"loop-found genRanHex2 genRanHex8"
],
"loopsFound": []
"loopsFound": [
"genRanHex2 genRanHex8"
]
},
"charlie": {
"debugLog": [
Expand Down
15 changes: 15 additions & 0 deletions src/engine/loopsengine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import EventEmitter from "events";

export class LoopsEngine extends EventEmitter {
loops: string[];
constructor() {
super();
this.loops = [];
}
handleLoopFound(probeId: string, traceId: string): void {
this.loops.push(`${probeId} ${traceId}`);
}
getLoops(): string[] {
return this.loops;
}
}
47 changes: 30 additions & 17 deletions src/giraffe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import { getMessageType } from "./messages.js";
import { ProbesEngine } from "./engine/probesengine.js";
import { FriendsEngine } from "./engine/friendsengine.js";
import { TracesEngine } from "./engine/tracesengine.js";
import { LoopsEngine } from "./engine/loopsengine.js";

export class Giraffe extends EventEmitter implements NetworkNode {
protected friendsengine: FriendsEngine;
protected probesengine: ProbesEngine;
protected tracesengine: TracesEngine;
protected friendsEngine: FriendsEngine;
protected probesEngine: ProbesEngine;
protected tracesEngine: TracesEngine;
protected loopsEngine: LoopsEngine;
protected debugLog: string[] = [];
protected name: string;

constructor(name: string) {
super();
this.name = name;
this.friendsengine = new FriendsEngine(name);
this.probesengine = this.connectProbesEngine();
this.tracesengine = this.connectTracesEngine(this.probesengine);
this.friendsEngine = new FriendsEngine(name);
this.probesEngine = this.connectProbesEngine();
this.tracesEngine = this.connectTracesEngine(this.probesEngine);
this.loopsEngine = this.connectLoopsEngine(this.tracesEngine);
}
protected connectProbesEngine(): ProbesEngine {
const probesengine = new ProbesEngine(this.name);
Expand Down Expand Up @@ -48,33 +51,43 @@ export class Giraffe extends EventEmitter implements NetworkNode {
});
return tracesengine;
}
protected connectLoopsEngine(traceEngine: TracesEngine): LoopsEngine {
const loopsEngine = new LoopsEngine();
traceEngine.on('loop-found', (probeId: string, traceId: string) => {
loopsEngine.handleLoopFound(probeId, traceId);
});
loopsEngine.on('debug', (message: string) => {
this.debugLog.push(message);
});
return loopsEngine;
}
process(sender: string, message: string): void {
this.debugLog.push(`[Node#receiveMessage] ${this.name} receives message from ${sender}`);
// console.log(`${this.name} receives message from ${sender}`, message);
switch(getMessageType(message)) {
case `meet`: return this.handleMeetMessage(sender);
case `probe`: return this.probesengine.handleProbeMessage(sender, message);
case `trace`: return this.tracesengine.handleTraceMessage(sender, message);
case `probe`: return this.probesEngine.handleProbeMessage(sender, message);
case `trace`: return this.tracesEngine.handleTraceMessage(sender, message);
// case `loop`: return this.probesengine.handleTraceMessage(sender, message);
case `have-probes`: return this.probesengine.handleHaveProbesMessage(sender);
case `okay-to-send-probes`: return this.probesengine.handleOkayToSendProbesMessage(sender);
case `have-probes`: return this.probesEngine.handleHaveProbesMessage(sender);
case `okay-to-send-probes`: return this.probesEngine.handleOkayToSendProbesMessage(sender);
}
}
meet(other: string, createProbe: boolean = true): void {
this.friendsengine.addFriend(other);
this.friendsEngine.addFriend(other);
this.debugLog.push(`I meet ${other} [1/4]`);
// this is safe to because it will just queue them for the next message round
this.emit('message', other, 'meet');
this.debugLog.push(`I queue ${other} all my flood probes [2/4]`);
this.probesengine.addFriend(other, true, createProbe);
this.probesEngine.addFriend(other, true, createProbe);
this.debugLog.push(`Done onMeet ${other} [4/4]`);
}

// when this node has received a `meet` message
handleMeetMessage(sender: string): void {
this.friendsengine.addFriend(sender);
this.friendsEngine.addFriend(sender);
this.debugLog.push(`MEET MESSAGE FROM ${sender}, queueing all flood probes`);
this.probesengine.addFriend(sender, false, false);
this.probesEngine.addFriend(sender, false, false);
}
getProbes(): {
[id: string]: {
Expand All @@ -88,7 +101,7 @@ export class Giraffe extends EventEmitter implements NetworkNode {
}[]
}
} {
return this.probesengine.getProbes();
return this.probesEngine.getProbes();
}
getName(): string {
return this.name;
Expand All @@ -97,9 +110,9 @@ export class Giraffe extends EventEmitter implements NetworkNode {
return this.debugLog;
}
getFriends(): string[] {
return Object.keys(this.friendsengine.getFriends());
return Object.keys(this.friendsEngine.getFriends());
}
getLoops(): string[] {
return [];
return this.loopsEngine.getLoops();
}
}

0 comments on commit d15ab0a

Please sign in to comment.