-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStpSwitch.java
More file actions
41 lines (34 loc) · 1.17 KB
/
StpSwitch.java
File metadata and controls
41 lines (34 loc) · 1.17 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
/*This class defines a Spanning Tree Switch that servers as a parent class to the Switch class.
* It abstracts details of sending messages and verifying topologies.
*/
package stp;
import java.util.ArrayList;
public class StpSwitch {
protected int switchId;
protected Topology topolink;
protected ArrayList<Integer> neighbors;
public StpSwitch(int switchId, Topology topolink, ArrayList<Integer> neighbors) {
this.switchId = switchId;
this.topolink = topolink;
this.neighbors = neighbors;
}
//Invoked at initialization of topology of switches
//Verify that all your neighbors has a backlink to you
public void verifyNeighbors() throws Exception {
for(Integer neighbor : neighbors) {
Switch sw = this.topolink.getSwitch(neighbor);
if(sw == null) {
throw new Exception("Topology does not have switch of id " + neighbor);
}
if(!sw.hasNeighbor(this.switchId)) {
throw new Exception(neighbor + " does not have link to " + this.switchId);
}
}
}
public boolean hasNeighbor(int id) {
return this.neighbors.contains(id);
}
public void sendMessage(Message msg) {
this.topolink.sendMessage(msg);
}
}