Skip to content

Commit a0e3c19

Browse files
authored
feat: invert router processing speed units (#260)
Closes #234 This PR inverts the units that the router processing speed is represented in.
1 parent 74d3616 commit a0e3c19

File tree

6 files changed

+33
-34
lines changed

6 files changed

+33
-34
lines changed

src/types/data-devices/dRouter.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@ import { DataNetworkDevice } from "./dNetworkDevice";
1111
import { ROUTER_CONSTANTS } from "../../utils/constants/router_constants";
1212

1313
export class DataRouter extends DataNetworkDevice {
14-
packetQueueSize: number;
14+
private packetQueueSize: number;
1515
private packetQueue: PacketQueue;
16-
// Time in ms to process a single byte
17-
timePerByte: number;
16+
// Number of bytes to process per second
17+
private bytesPerSecond: number;
1818
// Number of bytes processed
1919
private processingProgress = 0;
20+
2021
routingTable: RoutingTableEntry[];
2122

2223
constructor(graphData: RouterDataNode, datagraph: DataGraph) {
2324
super(graphData, datagraph);
2425
this.packetQueueSize =
2526
graphData.packetQueueSize ?? ROUTER_CONSTANTS.PACKET_QUEUE_MAX_SIZE;
2627
this.packetQueue = new PacketQueue(this.packetQueueSize);
27-
this.timePerByte =
28-
graphData.timePerByte ?? ROUTER_CONSTANTS.PROCESSING_SPEED;
28+
this.bytesPerSecond =
29+
graphData.bytesPerSecond ?? ROUTER_CONSTANTS.PROCESSING_SPEED;
2930
this.routingTable = graphData.routingTable ?? [];
3031
console.log("packetQueueSize Dr", this.packetQueueSize);
31-
console.log("processingSpeed Dr", this.timePerByte);
32+
console.log("processingSpeed Dr", this.bytesPerSecond);
3233
}
3334

3435
setMaxQueueSize(newSize: number) {
@@ -37,8 +38,8 @@ export class DataRouter extends DataNetworkDevice {
3738
this.packetQueueSize = newSize;
3839
}
3940

40-
setTimePerByte(newTime: number) {
41-
this.timePerByte = newTime;
41+
setBytesPerSecond(newTime: number) {
42+
this.bytesPerSecond = newTime;
4243
console.log("Time per byte set to Dr", newTime);
4344
}
4445

@@ -48,7 +49,7 @@ export class DataRouter extends DataNetworkDevice {
4849
type: DeviceType.Router,
4950
routingTable: this.routingTable,
5051
packetQueueSize: this.packetQueue.getMaxQueueSize(),
51-
timePerByte: this.timePerByte,
52+
bytesPerSecond: this.bytesPerSecond,
5253
};
5354
}
5455

@@ -87,13 +88,12 @@ export class DataRouter extends DataNetworkDevice {
8788
}
8889

8990
getPacketsToProcess(timeMs: number): IPv4Packet | null {
90-
this.processingProgress += timeMs;
91+
this.processingProgress += (this.bytesPerSecond * timeMs) / 1000;
9192
const packetLength = this.packetQueue.getHead()?.totalLength;
92-
const progressNeeded = this.timePerByte * packetLength;
93-
if (this.processingProgress < progressNeeded) {
93+
if (this.processingProgress < packetLength) {
9494
return null;
9595
}
96-
this.processingProgress -= progressNeeded;
96+
this.processingProgress -= packetLength;
9797
return this.packetQueue.dequeue();
9898
}
9999

src/types/graphs/datagraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface RouterDataNode extends NetworkDataNode {
6161
type: DeviceType.Router;
6262
routingTable?: RoutingTableEntry[];
6363
packetQueueSize: number;
64-
timePerByte: number;
64+
bytesPerSecond: number;
6565
}
6666

6767
export interface RoutingTableEntry {

src/types/view-devices/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export function createViewDevice(
2222
}
2323

2424
let packetQueueSize: number;
25-
let timePerByte: number;
25+
let bytesPerSecond: number;
2626

2727
if (isRouter(deviceInfo)) {
2828
packetQueueSize = deviceInfo.packetQueueSize;
29-
timePerByte = deviceInfo.timePerByte;
29+
bytesPerSecond = deviceInfo.bytesPerSecond;
3030
}
3131
const { id, interfaces, tag } = deviceInfo;
3232

@@ -41,7 +41,7 @@ export function createViewDevice(
4141
tag,
4242
mask,
4343
packetQueueSize,
44-
timePerByte,
44+
bytesPerSecond,
4545
);
4646
case DeviceType.Host:
4747
return new ViewHost(id, viewgraph, ctx, position, interfaces, tag, mask);

src/types/view-devices/vRouter.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export class ViewRouter extends ViewNetworkDevice {
2121

2222
private packetQueueSize: number;
2323
private packetQueue: PacketQueue;
24-
// Time in ms to process a single byte
25-
private timePerByte: number;
24+
// Number of bytes to process per second
25+
private bytesPerSecond: number;
2626
// Number of bytes processed
2727
private processingProgress = 0;
2828

@@ -42,7 +42,7 @@ export class ViewRouter extends ViewNetworkDevice {
4242
tag: string,
4343
mask: IpAddress,
4444
packetQueueSize: number = ROUTER_CONSTANTS.PACKET_QUEUE_MAX_SIZE,
45-
timePerByte: number = ROUTER_CONSTANTS.PROCESSING_SPEED,
45+
bytesPerSecond: number = ROUTER_CONSTANTS.PROCESSING_SPEED,
4646
) {
4747
super(
4848
id,
@@ -56,7 +56,7 @@ export class ViewRouter extends ViewNetworkDevice {
5656
);
5757
this.packetQueueSize = packetQueueSize;
5858
this.packetQueue = new PacketQueue(this.packetQueueSize);
59-
this.timePerByte = timePerByte;
59+
this.bytesPerSecond = bytesPerSecond;
6060
}
6161

6262
showInfo(): void {
@@ -90,7 +90,7 @@ export class ViewRouter extends ViewNetworkDevice {
9090
},
9191
{
9292
label: TOOLTIP_KEYS.PROCESSING_SPEED_PARAMETER,
93-
initialValue: this.timePerByte,
93+
initialValue: this.bytesPerSecond,
9494
onChange: (newSpeed: number) => {
9595
this.modifyProcessingSpeed(newSpeed);
9696
},
@@ -118,8 +118,8 @@ export class ViewRouter extends ViewNetworkDevice {
118118
this.packetQueueSize = newSize;
119119
}
120120

121-
setTimePerByte(newTime: number) {
122-
this.timePerByte = newTime;
121+
setBytesPerSecond(newTime: number) {
122+
this.bytesPerSecond = newTime;
123123
}
124124

125125
/**
@@ -150,16 +150,16 @@ export class ViewRouter extends ViewNetworkDevice {
150150
* @param newSpeed - The new processing speed to set, represented as the time per byte.
151151
*
152152
* This method updates the internal processing speed of the device by calling
153-
* `setTimePerByte` with the provided `newSpeed`. It also ensures that the
153+
* `setBytesPerSecond` with the provided `newSpeed`. It also ensures that the
154154
* corresponding device in the data graph is updated with the same processing speed,
155155
* but only if the device is an instance of `DataRouter`. If the device is not a
156156
* `DataRouter`, a warning is logged to the console.
157157
*/
158158
modifyProcessingSpeed(newSpeed: number): void {
159-
this.setTimePerByte(newSpeed);
159+
this.setBytesPerSecond(newSpeed);
160160
this.viewgraph.getDataGraph().modifyDevice(this.id, (device) => {
161161
if (device instanceof DataRouter) {
162-
device.setTimePerByte(newSpeed);
162+
device.setBytesPerSecond(newSpeed);
163163
} else {
164164
console.warn("Device is not a DataRouter, cannot set time per byte");
165165
}
@@ -225,13 +225,12 @@ export class ViewRouter extends ViewNetworkDevice {
225225
}
226226

227227
getPacketsToProcess(timeMs: number): IPv4Packet | null {
228-
this.processingProgress += timeMs;
228+
this.processingProgress += (this.bytesPerSecond * timeMs) / 1000;
229229
const packetLength = this.packetQueue.getHead()?.totalLength;
230-
const progressNeeded = this.timePerByte * packetLength;
231-
if (this.processingProgress < progressNeeded) {
230+
if (this.processingProgress < packetLength) {
232231
return null;
233232
}
234-
this.processingProgress -= progressNeeded;
233+
this.processingProgress -= packetLength;
235234
return this.packetQueue.dequeue();
236235
}
237236

src/utils/constants/router_constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Router Constants
22
export const ROUTER_CONSTANTS = {
3-
PACKET_QUEUE_MAX_SIZE: 1024,
4-
PROCESSING_SPEED: 8,
3+
PACKET_QUEUE_MAX_SIZE: 4096,
4+
PROCESSING_SPEED: 1024,
55
IP_COL_INDEX: 0,
66
MASK_COL_INDEX: 1,
77
INTERFACE_COL_INDEX: 2,

src/utils/constants/tooltips_constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const TOOLTIP_KEYS = {
4747
SWITCH_PARAMETERS: "Switch Parameters",
4848
HOST_PARAMETERS: "Host Parameters",
4949
PACKET_QUEUE_SIZE_PARAMETER: "Packet queue size (bytes)",
50-
PROCESSING_SPEED_PARAMETER: "Processing speed (ms/byte)",
50+
PROCESSING_SPEED_PARAMETER: "Processing speed (bytes/s)",
5151
REGENERATE: "Regenerate",
5252
DELETE_EDGE_BUTTON: "Delete Edge",
5353
CONNECTION: "Connection",

0 commit comments

Comments
 (0)