-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkSimulatorGUI.java
More file actions
437 lines (358 loc) · 16.2 KB
/
NetworkSimulatorGUI.java
File metadata and controls
437 lines (358 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import devices.*;
import layers.*;
import processing.Process;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
public class NetworkSimulatorGUI extends JFrame {
private JPanel controlPanel;
private JComboBox<String> mainDeviceTypeCombo;
private JTextField noOfMainDevicesField;
private JTextField noOfEndDevicesField;
private JPanel addMainDevicePanel;
private JButton addMainDeviceButton;
private JButton removeTopologyButton;
private JPanel addEndDevicesPanel;
private JButton addEndDevicesButton;
private JPanel messagePanel;
private JButton sendMessageButton;
private JTextField senderField;
private JTextField receiverField;
private JTextField messageField;
private JTextArea outputArea;
private TopologyPanel topologyPanel;
private java.util.List<Point> deviceLocations;
private Map<Integer, String> messageHistory;
private java.util.List<EndDevices> devices = new ArrayList<>();
private Map<Integer, EndDevices> deviceMap = new HashMap<>();
private Map<Integer, Hub> hubs = new HashMap<>();
private Map<Integer, Switch> switches = new HashMap<>();
private Map<Integer, Router> routers = new HashMap<>();
private PhysicalLayer physicalLayer = new PhysicalLayer();
private DataLayer dataLayer = new DataLayer();
private NetworkLayer networkLayer = new NetworkLayer();
private Hub hub;
private Switch switchDevice;
private Router router;
public NetworkSimulatorGUI() {
setTitle("Network Simulator");
setSize(1200, 900);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
deviceLocations = new ArrayList<>();
messageHistory = new HashMap<>();
controlPanel = new JPanel(new FlowLayout());
controlPanel.setPreferredSize(new Dimension(300, 900));
controlPanel.setBackground(Color.LIGHT_GRAY);
controlPanel.setBorder(BorderFactory.createTitledBorder("Control Panel"));
// Add Main device Panel
addMainDevicePanel = new JPanel();
addMainDevicePanel.setLayout(new GridBagLayout());
addMainDevicePanel.setBorder(BorderFactory.createTitledBorder("Main Device Panel"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(5, 5, 5, 5);
String[] options = {"Hub", "Switch", "Router"};
mainDeviceTypeCombo = new JComboBox<>(options);
noOfMainDevicesField = new JTextField(5);
addMainDeviceButton = new JButton("Add Main Device");
addMainDeviceButton.addActionListener(new AddMainDeviceButtonListener());
gbc.gridx = 0;
gbc.gridy = 0;
addMainDevicePanel.add(new JLabel("Main Device Type:"), gbc);
gbc.gridx = 1;
addMainDevicePanel.add(mainDeviceTypeCombo, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
addMainDevicePanel.add(new JLabel("Number of Main Devices:"), gbc);
gbc.gridx = 1;
addMainDevicePanel.add(noOfMainDevicesField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 2;
addMainDevicePanel.add(addMainDeviceButton, gbc);
controlPanel.add(addMainDevicePanel);
// End Device Panel
addEndDevicesPanel = new JPanel(new GridBagLayout());
addEndDevicesPanel.setBorder(BorderFactory.createTitledBorder("Add End Devices"));
addEndDevicesPanel.setVisible(true);
GridBagConstraints gbcEndDevices = new GridBagConstraints();
gbcEndDevices.fill = GridBagConstraints.HORIZONTAL;
gbcEndDevices.insets = new Insets(5, 5, 5, 5);
noOfEndDevicesField = new JTextField(5);
addEndDevicesButton = new JButton("Add End Devices");
addEndDevicesButton.addActionListener(new AddEndDevicesButtonListener());
gbcEndDevices.gridx = 0;
gbcEndDevices.gridy = 0;
addEndDevicesPanel.add(new JLabel("Number of End Devices:"), gbcEndDevices);
gbcEndDevices.gridx = 1;
addEndDevicesPanel.add(noOfEndDevicesField, gbcEndDevices);
gbcEndDevices.gridx = 0;
gbcEndDevices.gridy = 1;
gbcEndDevices.gridwidth = 2;
addEndDevicesPanel.add(addEndDevicesButton, gbcEndDevices);
controlPanel.add(addEndDevicesPanel);
// Message Panel
messagePanel = new JPanel(new GridBagLayout());
messagePanel.setBorder(BorderFactory.createTitledBorder("Message Panel"));
messagePanel.setVisible(true);
GridBagConstraints gbcMessage = new GridBagConstraints();
gbcMessage.fill = GridBagConstraints.HORIZONTAL;
gbcMessage.insets = new Insets(5, 5, 5, 5);
senderField = new JTextField();
receiverField = new JTextField();
messageField = new JTextField();
sendMessageButton = new JButton("Send Message");
sendMessageButton.addActionListener(new SendMessageButtonListener());
gbcMessage.gridx = 0;
gbcMessage.gridy = 0;
messagePanel.add(new JLabel("Sender ID:"), gbcMessage);
gbcMessage.gridx = 1;
messagePanel.add(senderField, gbcMessage);
gbcMessage.gridx = 0;
gbcMessage.gridy = 1;
messagePanel.add(new JLabel("Receiver ID:"), gbcMessage);
gbcMessage.gridx = 1;
messagePanel.add(receiverField, gbcMessage);
gbcMessage.gridx = 0;
gbcMessage.gridy = 2;
messagePanel.add(new JLabel("Message:"), gbcMessage);
gbcMessage.gridx = 1;
messagePanel.add(messageField, gbcMessage);
gbcMessage.gridx = 0;
gbcMessage.gridy = 3;
gbcMessage.gridwidth = 2;
messagePanel.add(sendMessageButton, gbcMessage);
controlPanel.add(messagePanel);
// Remove Topology Button
removeTopologyButton = new JButton("Remove Topology");
removeTopologyButton.addActionListener(new RemoveTopologyButtonListener());
controlPanel.add(removeTopologyButton);
outputArea = new JTextArea();
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
topologyPanel = new TopologyPanel();
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(topologyPanel), scrollPane);
splitPane.setResizeWeight(0.7);
add(controlPanel, BorderLayout.WEST);
add(splitPane, BorderLayout.CENTER);
}
private class AddMainDeviceButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String deviceType = (String) mainDeviceTypeCombo.getSelectedItem();
int numDevices = Integer.parseInt(noOfMainDevicesField.getText());
if (deviceType.isEmpty() || numDevices <= 0) {
outputArea.append("Invalid input.\n");
return;
}
for (int i = 0; i < numDevices; i++) {
int id = deviceLocations.size() + 1;
Point point = new Point(deviceLocations.size() * 50 + 20, 50);
deviceLocations.add(point);
switch (deviceType) {
case "Hub":
hub = new Hub(id);
hubs.put(id, hub);
topologyPanel.addDevice(point, "Hub " + id, Color.RED, "H");
break;
case "Switch":
switchDevice = new Switch(id);
switches.put(id, switchDevice);
topologyPanel.addDevice(point, "Switch " + id, Color.GREEN, "S");
break;
case "Router":
router = new Router(id);
routers.put(id, router);
topologyPanel.addDevice(point, "Router " + id, Color.BLUE, "R");
break;
}
}
outputArea.append("Added " + numDevices + " " + deviceType + "(s)\n");
addEndDevicesPanel.setVisible(true);
}
}
private class AddEndDevicesButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int numEndDevices = Integer.parseInt(noOfEndDevicesField.getText());
if (numEndDevices <= 0) {
outputArea.append("Invalid input.\n");
return;
}
for (int i = 0; i < numEndDevices; i++) {
int id = devices.size() + 1;
EndDevices endDevice = new EndDevices(id, physicalLayer.generateMacAddress(), "IP" + id);
devices.add(endDevice);
deviceMap.put(id, endDevice);
Point point = new Point((deviceLocations.size() * 50 + 20), (deviceLocations.size() % 2 == 0) ? 100 : 150);
deviceLocations.add(point);
topologyPanel.addDevice(point, "End " + id, Color.ORANGE, "E");
// Connect end devices to all main devices
for (Hub hub : hubs.values()) {
hub.topology(endDevice);
topologyPanel.addLink(point, deviceLocations.get(hub.getId() - 1));
}
for (Switch switchDevice : switches.values()) {
switchDevice.topology(endDevice);
topologyPanel.addLink(point, deviceLocations.get(switchDevice.getId() - 1));
}
for (Router router : routers.values()) {
router.topology(endDevice);
topologyPanel.addLink(point, deviceLocations.get(router.getId() - 1));
}
}
outputArea.append("Added " + numEndDevices + " End Device(s)\n");
}
}
private class RemoveTopologyButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
devices.clear();
deviceMap.clear();
hubs.clear();
switches.clear();
routers.clear();
deviceLocations.clear();
topologyPanel.clearDevices();
outputArea.append("Topology removed.\n");
}
}
private class SendMessageButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int sender = Integer.parseInt(senderField.getText());
int receiver = Integer.parseInt(receiverField.getText());
String message = messageField.getText();
if (message.isEmpty()) {
outputArea.append("Message cannot be empty.\n");
return;
}
if (deviceMap.containsKey(sender) && deviceMap.containsKey(receiver)) {
EndDevices senderDevice = deviceMap.get(sender);
EndDevices receiverDevice = deviceMap.get(receiver);
// First added hub
if (!hubs.isEmpty()) {
Hub hub = hubs.values().iterator().next();
hub.broadcast(devices, sender);
hub.transmission(sender, receiver);
senderDevice.sendAck(receiver);
hub.broadcastAck(sender, receiver, true);
}
outputArea.append("Message from " + sender + " to " + receiver + ": " + message + "\n");
messageHistory.put(sender, message);
} else {
outputArea.append("Invalid Sender or Receiver ID\n");
}
}
}
private class TopologyPanel extends JPanel {
private java.util.List<Point> devices;
private java.util.List<String> deviceLabels;
private java.util.List<Color> deviceColors;
private java.util.List<String> deviceShapes;
private java.util.List<Line> links;
public TopologyPanel() {
this.devices = new ArrayList<>();
this.deviceLabels = new ArrayList<>();
this.deviceColors = new ArrayList<>();
this.deviceShapes = new ArrayList<>();
this.links = new ArrayList<>();
setPreferredSize(new Dimension(800, 600));
setBackground(Color.WHITE);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Point clickPoint = e.getPoint();
for (int i = 0; i < devices.size(); i++) {
Point devicePoint = devices.get(i);
Rectangle deviceBounds = new Rectangle(devicePoint.x, devicePoint.y, 30, 30);
if (deviceBounds.contains(clickPoint)) {
showDeviceDetails(i + 1);
}
}
}
});
}
public void addDevice(Point point, String label, Color color, String shape) {
devices.add(point);
deviceLabels.add(label);
deviceColors.add(color);
deviceShapes.add(shape);
repaint(); // Trigger repaint to update the topology
}
public void clearDevices() {
devices.clear();
deviceLabels.clear();
deviceColors.clear();
deviceShapes.clear();
links.clear();
repaint();
}
public void addLink(Point from, Point to) {
links.add(new Line(from, to));
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw connections
g.setColor(Color.BLACK);
for (Line link : links) {
g.drawLine(link.from.x + 15, link.from.y + 15, link.to.x + 15, link.to.y + 15); // Adjust for shape center
}
// Draw each device with its shape, color, and label
for (int i = 0; i < devices.size(); i++) {
Point point = devices.get(i);
String label = deviceLabels.get(i);
Color color = deviceColors.get(i);
String shape = deviceShapes.get(i);
g.setColor(color);
if ("H".equals(shape)) {
g.fillRect(point.x, point.y, 30, 30);
} else if ("S".equals(shape)) {
g.fillRoundRect(point.x, point.y, 30, 30, 10, 10);
} else if ("R".equals(shape)) {
g.fillOval(point.x, point.y, 30, 30);
} else if ("E".equals(shape)) {
g.fillOval(point.x, point.y, 20, 20);
}
g.setColor(Color.BLACK);
g.drawString(label, point.x, point.y - 5);
}
}
private void showDeviceDetails(int deviceId) {
EndDevices device = deviceMap.get(deviceId);
if (device != null) {
JOptionPane.showMessageDialog(this, "Device ID: " + deviceId + "\nMAC Address: " + device.getMAC() + "\nIP Address: " + device.getIP(), "Device Details", JOptionPane.INFORMATION_MESSAGE);
} else if (hubs.containsKey(deviceId)) {
JOptionPane.showMessageDialog(this, "Hub ID: " + deviceId, "Device Details", JOptionPane.INFORMATION_MESSAGE);
} else if (switches.containsKey(deviceId)) {
JOptionPane.showMessageDialog(this, "Switch ID: " + deviceId, "Device Details", JOptionPane.INFORMATION_MESSAGE);
} else if (routers.containsKey(deviceId)) {
JOptionPane.showMessageDialog(this, "Router ID: " + deviceId, "Device Details", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Device not found.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private class Line {
Point from, to;
Line(Point from, Point to) {
this.from = from;
this.to = to;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
NetworkSimulatorGUI gui = new NetworkSimulatorGUI();
gui.setVisible(true);
});
}
}