-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponent.java
125 lines (107 loc) · 3.7 KB
/
Component.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
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
import java.util.Arrays;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
public class Component implements ComponentIface {
private final int id;
private int[] RN;
private Token token;
private boolean criticalSection;
private int csDelay;
private ComponentIface[] componentList;
public Component(int id, int nComponents) {
this.id = id;
this.RN = new int[nComponents];
this.token = null;
this.criticalSection = false;
this.csDelay = 0;
}
public int getId() {
return this.id;
}
public boolean hasToken() {
if (this.token != null) return true;
else return false;
}
public void broadcastRequest() {
RN[id-1]++;
if (!hasToken()) {
for (ComponentIface c : componentList) { // Broadcast token request
try {
if (c.getId() != this.id) c.onRequest(id, RN[id-1]);
} catch (Exception e) {
System.out.println("Exception @broadcast "+e.toString());
}
}
} else { // Corner case (already possesing token)
token.updateQueue(RN);
onTokenReceive(this.token);
}
}
public void onRequest(int pid, int seq) {
RN[pid-1] = Math.max(RN[pid-1], seq); // Update RN
if (hasToken()) {
token.updateQueue(RN);
if (!criticalSection) sendToken();
else {
if (--csDelay == 0) releaseToken();
}
}
}
public void onTokenReceive(Token t) {
System.out.println("\nP"+id+" TOKEN");
this.token = t;
this.criticalSection = true;
if (csDelay == 0) releaseToken();
}
public void releaseToken() {
criticalSection = false;
token.updateLN(id);
token.popFromQueue();
if (!token.isEmpty()) sendToken();
}
public void sendToken() {
int dst = token.peekQueue();
try {
componentList[dst-1].onTokenReceive(token);
if (dst != id) token = null;
} catch (Exception e) {
System.out.println("Exception @sendToken "+e.toString());
}
}
// Helper functions for simulation ----------------------------------------
public void initNetwork(ComponentIface[] components) {
this.componentList = components;
}
public void initToken() {
this.token = new Token(RN.length);
}
public void setCSDelay(int delay) {
this.csDelay = delay;
}
public void printStatus() {
StringBuilder str = new StringBuilder();
str.append("P"+id+" "+Arrays.toString(RN));
if (token != null) str.append("\ttoken");
if (criticalSection) str.append("\tcritical");
System.out.print(str.toString().indent(2));
}
// RMI stuff --------------------------------------------------------------
public static void main(String[] args) {
// Parse input arguments (PID)
if (args.length != 2) System.exit(1);
int pid = Integer.parseInt(args[0]);
int nComponents = Integer.parseInt(args[1]);
// Create process
Component c = new Component(pid, nComponents);
// Export to RMI registry and set PID as a name
try {
ComponentIface stub = (ComponentIface) UnicastRemoteObject.exportObject(c, 0);
LocateRegistry.getRegistry().rebind(args[0], stub);
System.out.println("Component ready");
} catch (Exception e) {
System.err.println("Component exception: " + e.toString());
e.printStackTrace();
System.exit(1);
}
}
}