Skip to content

Commit 9eb4c74

Browse files
authored
Dynamic ARP table (#232)
This pull request introduces a mechanism to dynamically update the ARP table in the UI when changes occur in the underlying data model. The key changes include adding a listener for ARP table updates, implementing a refresh method in the ARP table UI component, and integrating these updates into the `DeviceInfo` class.
1 parent b132b62 commit 9eb4c74

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/graphics/renderables/arp_table.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ export class ArpTable {
173173

174174
return { onEdit, onRegenerate, onDelete };
175175
}
176+
177+
refreshTable(): void {
178+
const updatedEntries = this.props.viewgraph
179+
.getDataGraph()
180+
.getArpTable(this.props.deviceId);
181+
182+
const updatedRows = updatedEntries.map((entry) => [entry.ip, entry.mac]);
183+
184+
this.updateRows(updatedRows);
185+
}
176186
}
177187

178188
function isValidMAC(mac: string): boolean {

src/graphics/renderables/device_info.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ProgressBar } from "../basic_components/progress_bar";
1616
import { LabeledProgressBar } from "../components/labeled_progress_bar";
1717
import { ArpTable } from "./arp_table";
1818
import { Layer } from "../../types/layer";
19+
import { DataNetworkDevice } from "../../types/data-devices";
1920

2021
export class DeviceInfo extends BaseInfo {
2122
readonly device: ViewDevice;
@@ -157,6 +158,18 @@ export class DeviceInfo extends BaseInfo {
157158
});
158159

159160
this.inputFields.push(arpTable.toHTML());
161+
162+
const dataDevice = viewgraph.getDataGraph().getDevice(deviceId);
163+
164+
if (dataDevice instanceof DataNetworkDevice) {
165+
// Suscribe to ARP table changes
166+
dataDevice.setArpTableChangeListener(() => {
167+
// update the ARP table in the UI
168+
arpTable.refreshTable();
169+
});
170+
} else {
171+
console.warn(`Device with ID ${deviceId} is not a DataNetworkDevice.`);
172+
}
160173
}
161174
}
162175

src/types/data-devices/dNetworkDevice.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export abstract class DataNetworkDevice extends DataDevice {
77
ip: IpAddress;
88
ipMask: IpAddress;
99
arpTable: Map<string, string>;
10+
private arpTableChangeListener: (() => void) | null = null;
1011

1112
constructor(graphData: NetworkDataNode, datagraph: DataGraph) {
1213
super(graphData, datagraph);
@@ -17,8 +18,21 @@ export abstract class DataNetworkDevice extends DataDevice {
1718

1819
abstract receiveDatagram(datagram: IPv4Packet): void;
1920

20-
updateArpTable(mac: MacAddress, ip: IpAddress) {
21+
/**
22+
* Update the ARP table and notify the listener.
23+
*/
24+
updateArpTable(mac: MacAddress, ip: IpAddress): void {
2125
this.arpTable.set(ip.toString(), mac.toString());
26+
if (this.arpTableChangeListener) {
27+
this.arpTableChangeListener(); // Notify the listener
28+
}
29+
}
30+
31+
/**
32+
* Set the listener for ARP table changes.
33+
*/
34+
setArpTableChangeListener(listener: () => void): void {
35+
this.arpTableChangeListener = listener;
2236
}
2337

2438
resolveAddress(ip: IpAddress): MacAddress {

0 commit comments

Comments
 (0)