@@ -88,7 +88,7 @@ public EWD998(final Map<Integer, Pair> nodes, final int myId, final boolean isIn
88
88
inbox .put (pkt );
89
89
}
90
90
91
- boolean isConclusiveRound = false ;
91
+ boolean terminationDetected = false ;
92
92
93
93
// --------------------------------------------------------------------------------- //
94
94
@@ -141,30 +141,8 @@ public EWD998(final Map<Integer, Pair> nodes, final int myId, final boolean isIn
141
141
142
142
final JsonObject msg = pkt .get (MSG ).getAsJsonObject ();
143
143
144
- int tokenQ = 0 ;
145
- Color tokenColor = null ;
146
-
147
144
// --------------------------------------------------------------------------------- //
148
-
149
- // InitiateToken and PassToken
150
- if (msg .get (TYPE ).equals (TOK )) {
151
- tokenQ = msg .get ("q" ).getAsInt ();
152
- tokenColor = Color .valueOf (msg .get ("color" ).getAsString ());
153
-
154
- if (isInitiator ) {
155
- /*
156
- InitiateProbe ==
157
- /\ token.pos = 0
158
- /\ \* previous round inconclusive:
159
- \/ token.color = "black"
160
- \/ color[0] = "black"
161
- \/ counter[0] + token.q > 0
162
- /\ ...
163
- /\ UNCHANGED <<active, counter, pending>>
164
- */
165
- isConclusiveRound = tokenQ + counter == 0 && color == Color .white && tokenColor == Color .white ;
166
- }
167
- } else if (msg .get (TYPE ).equals (PL )) {
145
+ if (msg .get (TYPE ).equals (PL )) {
168
146
/*
169
147
RecvMsg(i) ==
170
148
/\ pending[i] > 0
@@ -186,7 +164,7 @@ public EWD998(final Map<Integer, Pair> nodes, final int myId, final boolean isIn
186
164
assert !active ;
187
165
assert inbox .isEmpty ();
188
166
return ;
189
- } else {
167
+ } else if (! msg . get ( TYPE ). equals ( TOK )) {
190
168
logline .add ("failure" , new JsonPrimitive (String .format ("Unknown message type: %s" , msg .get (TYPE ).getAsString ())));
191
169
System .out .println (logline );
192
170
throw new IllegalArgumentException ("Unknown message type" );
@@ -244,33 +222,39 @@ public EWD998(final Map<Integer, Pair> nodes, final int myId, final boolean isIn
244
222
// InitiateToken and PassToken actions
245
223
// /\ ...
246
224
// /\ token.pos = i
247
- if (tokenColor != null ) {
248
- if (isInitiator && isConclusiveRound && !active ) {
249
- // Send the trm message to all nodes (us included).
250
- for (Integer n : nodes .keySet ()) {
251
- sendMsg (myId , n , getTrm ());
225
+ if (msg .get (TYPE ).equals (TOK )) {
226
+ final int tokenQ = msg .get ("q" ).getAsInt ();
227
+ final Color tokenColor = Color .valueOf (msg .get ("color" ).getAsString ());
228
+ if (isInitiator ) {
229
+ final boolean isConclusive =
230
+ tokenColor == Color .white &&
231
+ color == Color .white &&
232
+ tokenQ + counter == 0 ;
233
+ if (isConclusive && !active ) {
234
+ // If the previous token round was conclusive and the initiator is inactive,
235
+ // send termination message to all nodes including myself.
236
+ for (Integer n : nodes .keySet ()) {
237
+ sendMsg (myId , n , getTrm ());
238
+ }
239
+ } else if (!isConclusive ) {
240
+ /*
241
+ InitiateProbe ==
242
+ /\ token.pos = 0
243
+ /\ \* previous round inconclusive:
244
+ \/ token.color = "black"
245
+ \/ color[0] = "black"
246
+ \/ counter[0] + token.q > 0
247
+ /\ ...
248
+ /\ UNCHANGED <<active, counter, pending>>
249
+ */
250
+ sendMsg (myId , nodes .size () - 1 , getTok (0 , Color .white ));
251
+ color = Color .white ;
252
+ } else {
253
+ // This UNCHANGED token does not expliclty appear in the EWD998Chan spec, but
254
+ // can be conceptually mapped to the _vars in [Next]_vars ; the InitiateToken
255
+ // action is *not* enabled.
256
+ inbox .add (pkt );
252
257
}
253
- tokenColor = null ;
254
- } else if (isInitiator && !isConclusiveRound ) {
255
- /*
256
- terminationDetected ==
257
- /\ token.pos = 0
258
- /\ token.color = "white"
259
- /\ token.q + counter[0] = 0
260
- /\ color[0] = "white"
261
- /\ ~ active[0]
262
-
263
- */
264
- /*
265
- InitiateProbe ==
266
- /\ ...
267
- /\ token' = [ pos |-> N-1, q |-> 0, color |-> "white"]
268
- /\ color' = [ color EXCEPT ![0] = "white" ]
269
- /\ UNCHANGED <<active, counter, pending>>
270
- */
271
- sendMsg (myId , nodes .size () - 1 , getTok (0 , Color .white ));
272
- color = Color .white ;
273
- tokenColor = null ;
274
258
} else if (!active ) {
275
259
/*
276
260
PassToken(i) ==
@@ -284,8 +268,6 @@ public EWD998(final Map<Integer, Pair> nodes, final int myId, final boolean isIn
284
268
*/
285
269
sendMsg (myId , myId - 1 , getTok (tokenQ + counter , color == Color .black ? Color .black : tokenColor ));
286
270
color = Color .white ;
287
-
288
- tokenColor = null ;
289
271
} else {
290
272
// This node owns the token and is active; keep the unchanged token.
291
273
/*
0 commit comments