-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.java
72 lines (62 loc) · 2.1 KB
/
Simulator.java
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
import java.rmi.registry.LocateRegistry;
public class Simulator {
public ComponentIface[] components;
public Simulator(int nComponents) {
// Get Component stubs, distribute list of all Components to everyone, init token
try {
this.components = new ComponentIface[nComponents];
for (int i = 0; i < nComponents; i++) {
this.components[i] = (ComponentIface) LocateRegistry.getRegistry().lookup(Integer.toString(i+1));
}
for (ComponentIface c : components) c.initNetwork(this.components);
this.components[0].initToken();
} catch (Exception e) {
System.out.println("Simulator exception "+e.toString());
}
}
// csDelay = number of request events
public void request(int pid, int csDelay) {
System.out.println();
System.out.println("P"+pid+" REQUEST");
try {
components[pid-1].setCSDelay(csDelay);
components[pid-1].broadcastRequest();
} catch (Exception e) {
System.out.println("Simulator exception @request "+e.toString());
}
}
public void request(int pid) { // Convenience wrapper (for csDelay=0)
request(pid, 0);
}
public void example1() {
request(1, 2);
request(2);
request(3);
}
public void example2() {
request(2, 2);
request(1, 2);
request(3);
request(4);
request(5);
}
public void printState() {
System.out.println();
try {
for (ComponentIface c : components) c.printStatus();
} catch (Exception e) {
System.out.println("Simulator exception @printState "+e.toString());
}
System.out.println();
System.out.println();
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java Simulator <num components>");
System.exit(1);
}
Simulator sim = new Simulator(Integer.parseInt(args[0]));
// sim.example1();
sim.example2();
}
}