@@ -31,17 +31,17 @@ protected RemoteComponentImpl() throws RemoteException {
31
31
32
32
@ Override
33
33
public void init (int n ) {
34
- this . N = new int [n ];
35
- this . States = new String [n ];
36
- this . tk = new Token (n );
34
+ N = new int [n ];
35
+ States = new String [n ];
36
+ tk = new Token (n );
37
37
for (int i = 0 ; i < n ; i ++) N [i ] = 0 ;
38
38
// different type of initialization for process 0 and the others
39
- if (this . id == 0 ) {
40
- this . States [0 ] = "H" ;
41
- for (int i = 1 ; i < n ; i ++) this . States [i ] = "O" ;
39
+ if (id == 0 ) {
40
+ States [0 ] = "H" ;
41
+ for (int i = 1 ; i < n ; i ++) States [i ] = "O" ;
42
42
} else {
43
- for (int i = 0 ; i < this . id ; i ++) this . States [i ] = "R" ;
44
- for (int i = this . id ; i < n ; i ++) this . States [i ] = "O" ;
43
+ for (int i = 0 ; i < id ; i ++) States [i ] = "R" ;
44
+ for (int i = id ; i < n ; i ++) States [i ] = "O" ;
45
45
}
46
46
index = 100 * id ;
47
47
}
@@ -53,8 +53,8 @@ public void init(int n) {
53
53
public void sendRequest () throws RemoteException {
54
54
System .out .println (id + " " + "Started sending request" );
55
55
56
- if (this . States [this . id ].equals ("H" )) {
57
- System .out .println (getStateStamp () + this . id + ":Sending request to itself" );
56
+ if (States [id ].equals ("H" )) {
57
+ System .out .println (getStateStamp () + id + ":Sending request to itself" );
58
58
59
59
new java .util .Timer ().schedule (
60
60
new java .util .TimerTask () {
@@ -70,13 +70,13 @@ public void run() {
70
70
200 // and the delivery delay is set
71
71
);
72
72
}
73
- this . States [this . id ] = "R" ;
74
- this . N [ this . id ] += 1 ;
73
+ States [id ] = "R" ;
74
+ N [ id ] += 1 ;
75
75
int i = 0 ;
76
- while (i < this . numProc ) {
77
- if (i != this . id ) {
78
- if (this . States [i ].equals ("R" )) {
79
- System .out .println (getStateStamp () + this . id + ": Sending request to process " + i );
76
+ while (i < numProc ) {
77
+ if (i != id ) {
78
+ if (States [i ].equals ("R" )) {
79
+ System .out .println (getStateStamp () + id + ": Sending request to process " + i );
80
80
int finalI = i ;
81
81
new java .util .Timer ().schedule (
82
82
new java .util .TimerTask () {
@@ -108,17 +108,17 @@ public void receiveRequest(int reqId, int numReq) throws RemoteException {
108
108
System .out .println (id + " " + index + "Started receiving request" + reqId );
109
109
// Update requests
110
110
System .out .println (getStateStamp () + id + ":Receiving request from process " + reqId );
111
- this . N [reqId ] = numReq ;
111
+ N [reqId ] = numReq ;
112
112
113
113
if (id == reqId ) {
114
114
States [id ] = "H" ;
115
115
}
116
116
117
- if (this . States [this . id ].equals ("O" ) || this . States [this . id ].equals ("E" ))
118
- this . States [reqId ] = "R" ;
119
- else if (this . States [this . id ].equals ("R" )) {
120
- if (!this . States [reqId ].equals ("R" )) {
121
- this . States [reqId ] = "R" ;
117
+ if (States [id ].equals ("O" ) || States [id ].equals ("E" ))
118
+ States [reqId ] = "R" ;
119
+ else if (States [id ].equals ("R" )) {
120
+ if (!States [reqId ].equals ("R" )) {
121
+ States [reqId ] = "R" ;
122
122
new java .util .Timer ().schedule (
123
123
new java .util .TimerTask () {
124
124
@ Override
@@ -134,14 +134,24 @@ public void run() {
134
134
135
135
}
136
136
} else {
137
- this . States [reqId ] = "R" ;
138
- this . States [this . id ] = "O" ;
139
- this . tk .setTS (reqId , "R" );
140
- this . tk .setTN (reqId , numReq );
141
- System .out .println (getStateStamp () + this . id + ": Sending token to process " + reqId );
137
+ States [reqId ] = "R" ;
138
+ States [id ] = "O" ;
139
+ tk .setTS (reqId , "R" );
140
+ tk .setTN (reqId , numReq );
141
+ System .out .println (getStateStamp () + id + ": Sending token to process " + reqId );
142
142
143
-
144
- RD [reqId ].receiveToken ();
143
+ new java .util .Timer ().schedule (
144
+ new java .util .TimerTask () {
145
+ @ Override
146
+ public void run () {
147
+ try {
148
+ RD [reqId ].receiveToken ();
149
+ } catch (RemoteException e ) {
150
+ e .printStackTrace ();
151
+ }
152
+ }
153
+ },
154
+ 200 ); // and the delivery delay is set
145
155
146
156
147
157
}
@@ -156,37 +166,36 @@ public void run() {
156
166
public void receiveToken () throws RemoteException {
157
167
System .out .println (id + "Started receiving tokken" );
158
168
System .out .println (getStateStamp () + id + ":Token received " );
159
- this . States [this . id ] = "E" ;
169
+ States [id ] = "E" ;
160
170
criticalSection ();
161
- this . States [this . id ] = "O" ;
162
- this . tk .setTS (this . id , "O" );
163
- for (int i = 0 ; i < this . numProc ; i ++) {
171
+ States [id ] = "O" ;
172
+ tk .setTS (id , "O" );
173
+ for (int i = 0 ; i < numProc ; i ++) {
164
174
System .out .println ("Executions" + N [i ] + " " + tk .getTN (i ));
165
175
System .out .println ("States" + States [i ] + " " + tk .getTS (i ));
166
- if (this . N [i ] > this . tk .getTN (i )) {
167
- this . tk .setTN (i , this . N [i ]);
168
- this . tk .setTS (i , this . States [i ]);
176
+ if (N [i ] > tk .getTN (i )) {
177
+ tk .setTN (i , N [i ]);
178
+ tk .setTS (i , States [i ]);
169
179
} else {
170
- this . N [i ] = this . tk .getTN (i );
171
- this . States [i ] = this . tk .getTS (i );
180
+ N [i ] = tk .getTN (i );
181
+ States [i ] = tk .getTS (i );
172
182
}
173
183
}
174
- System .out .println (getStateStamp ());
175
184
boolean flag = true ;
176
- for (int i = 0 ; i < this . numProc ; i ++) {
177
- if (!this . States [i ].equals ("O" )) {
185
+ for (int i = 0 ; i < numProc ; i ++) {
186
+ if (!States [i ].equals ("O" )) {
178
187
flag = false ;
179
188
}
180
189
}
181
190
if (flag ) {
182
- this . States [this . id ] = "H" ;
191
+ States [id ] = "H" ;
183
192
} else {
184
- for (int i = 1 ; i < this . numProc + 1 ; i ++) {
193
+ for (int i = 1 ; i < numProc + 1 ; i ++) {
185
194
// Cyclic check
186
- int reqID = (this . id + i ) % this . numProc ;
195
+ int reqID = (id + i ) % numProc ;
187
196
// Send to first process with outstanding request
188
- if (this . States [reqID ].equals ("R" )) {
189
- System .out .println (getStateStamp () + this . id + " sending token to process " + reqID );
197
+ if (States [reqID ].equals ("R" )) {
198
+ System .out .println (getStateStamp () + id + " sending token to process " + reqID );
190
199
new java .util .Timer ().schedule (
191
200
new java .util .TimerTask () {
192
201
@ Override
@@ -212,46 +221,46 @@ public void run() {
212
221
/* Method implementing the critical section of each process
213
222
*/
214
223
private void criticalSection () {
215
- //this. States[this. id] = "E";
216
- System .out .println (getStateStamp () + this . id + " Entering critical section" );
224
+ //States[id] = "E";
225
+ System .out .println (getStateStamp () + id + " Entering critical section" );
217
226
try {
218
227
Thread .sleep (2000 );
219
228
} catch (InterruptedException e ) {
220
229
System .out .println ("Critical section interrupted" );
221
230
}
222
- System .out .println (getStateStamp () + this . id + " leaving critical section" );
223
- //this. States[this. id] = "O";
224
- //this. tk.setTS(this. id, "O");
231
+ System .out .println (getStateStamp () + id + " leaving critical section" );
232
+ //States[id] = "O";
233
+ //tk.setTS(id, "O");
225
234
}
226
235
227
236
@ Override
228
237
public int [] getListOfRequests () throws RemoteException {
229
- return this . N ;
238
+ return N ;
230
239
}
231
240
232
241
@ Override
233
242
public String [] getListOfStates () throws RemoteException {
234
- return this . States ;
243
+ return States ;
235
244
}
236
245
237
246
@ Override
238
247
public void setListOfRequests (int [] arr ) throws RemoteException {
239
- this . N = Arrays .copyOf (arr , this . numProc );
248
+ N = Arrays .copyOf (arr , numProc );
240
249
}
241
250
242
251
@ Override
243
252
public void setListOfStates (String [] arr ) throws RemoteException {
244
- this . States = Arrays .copyOf (arr , this . numProc );
253
+ States = Arrays .copyOf (arr , numProc );
245
254
}
246
255
247
256
@ Override
248
257
public void setEntities (IComponent [] entities ) throws RemoteException {
249
- this . RD = entities ;
258
+ RD = entities ;
250
259
}
251
260
252
261
@ Override
253
262
public void setName (String name ) throws RemoteException {
254
- this . name = name ;
263
+ name = name ;
255
264
}
256
265
257
266
@ Override
@@ -272,13 +281,13 @@ public int getId() throws RemoteException {
272
281
273
282
@ Override
274
283
public void setNumProc (int n ) throws RemoteException {
275
- this . numProc = n ;
284
+ numProc = n ;
276
285
}
277
286
278
287
private String getStateStamp () {
279
288
String stateStamp = "[" ;
280
289
for (int i = 0 ; i < States .length ; i ++) {
281
- stateStamp += this . States [i ];
290
+ stateStamp += States [i ];
282
291
}
283
292
stateStamp += "]" ;
284
293
return stateStamp ;
0 commit comments