@@ -19,9 +19,25 @@ public ProcessNode(int nodeID) {
1919
2020 private MessageQueue messageQueue ;
2121 private ProcessNode silentNeigh ;
22+ private boolean runDiffusion ;
23+
24+ /**
25+ * @return the runDiffusion
26+ */
27+ public boolean isRunDiffusion () {
28+ return runDiffusion ;
29+ }
30+
31+ /**
32+ * @param runDiffusion
33+ * the runDiffusion to set
34+ */
35+ public void setRunDiffusion (boolean runDiffusion ) {
36+ this .runDiffusion = runDiffusion ;
37+ }
2238
2339 private enum Status {
24- WAITING , HAS_SILENT_NEIGH , DECIDED , TOKEN_SENT
40+ INIT , WAITING , DECIDED , TOKEN_SENT
2541 };
2642
2743 private Status status ;
@@ -30,8 +46,9 @@ public void addMessage(Message msg) {
3046 messageQueue .add (msg );
3147 }
3248
33- public void init () {
34-
49+ public void init (boolean runDiffusion ) {
50+ this .status =Status .INIT ;
51+ this .runDiffusion = runDiffusion ;
3552 Iterator <Node > neighItr = neigh .iterator ();
3653 while (neighItr .hasNext ())
3754 receiveVector .add (new ReceiveIndicator ((ProcessNode ) neighItr
@@ -49,6 +66,21 @@ private ProcessNode receiveToken() {
4966 ReceiveIndicator recnode = recItr .next ();
5067 if (msg .header .getSender () == recnode .getNode ()) {
5168 recnode .setReceived (true );
69+
70+ if (msg .header .getSender () == silentNeigh ) {
71+
72+ System .out
73+ .println ("Process Node["
74+ + this .nodeID
75+ + "] Received Token from Silent Neighbour Process Node["
76+ + msg .header .getSender ().nodeID + "]" );
77+
78+ } else {
79+ System .out .println ("Process Node[" + this .nodeID
80+ + "] Received Token from Process Node["
81+ + msg .header .getSender ().nodeID + "]" );
82+ }
83+
5284 return msg .header .getSender ();
5385 }
5486 }
@@ -63,6 +95,11 @@ private void send(ProcessNode recepient, MessageType messageType) {
6395 newMsg .header .setReceiver (recepient );
6496 newMsg .send ();
6597
98+ if (recepient == silentNeigh ) {
99+ System .out .println ("Process Node[" + this .nodeID
100+ + "] sent token to its silent neighbour:Process Node["
101+ + silentNeigh .nodeID + "]" );
102+ }
66103 }
67104
68105 private int getNonRecCount () {
@@ -84,7 +121,10 @@ private ProcessNode findSilentNeighbour() {
84121 while (recItr .hasNext ()) {
85122 ReceiveIndicator recnode = recItr .next ();
86123 if (!recnode .isReceived ())
87- return recnode .getNode ();
124+ System .out .println ("Process Node[" + this .nodeID
125+ + "] found its silent neighbour:Process Node["
126+ + recnode .getNode ().nodeID + "]" );
127+ return recnode .getNode ();
88128 }
89129 return null ;
90130 }
@@ -129,9 +169,9 @@ public void executeStep() {
129169 this .status = Status .WAITING ;
130170 ProcessNode sender = receiveToken ();
131171 if (sender != null ) {
132- System .out .println ("Process Node[" + this .nodeID
133- + "] Received Token from Process Node["
134- + sender .nodeID + "]" );
172+ // System.out.println("Process Node[" + this.nodeID
173+ // + "] Received Token from Process Node["
174+ // + sender.nodeID + "]");
135175
136176 }
137177
@@ -142,45 +182,74 @@ public void executeStep() {
142182 this .silentNeigh = findSilentNeighbour ();
143183 if (this .status != Status .TOKEN_SENT ) {
144184 if (silentNeigh != null ) {
145- this .status = Status .HAS_SILENT_NEIGH ;
146185 if (this .status != Status .TOKEN_SENT ) {
147- System .out .println ("Process Node[" + this .nodeID
148- + "] found its silent neighbour:Process Node["
149- + silentNeigh .nodeID + "]" );
186+ // System.out.println("Process Node[" + this.nodeID
187+ // + "] found its silent neighbour:Process Node["
188+ // + silentNeigh.nodeID + "]");
150189 send (silentNeigh , MessageType .TOKEN );
151190 this .status = Status .TOKEN_SENT ;
152- System .out
153- .println ("Process Node["
154- + this .nodeID
155- + "] sent token to its silent neighbour:Process Node["
156- + silentNeigh .nodeID + "]" );
191+ // System.out
192+ // .println("Process Node["
193+ // + this.nodeID
194+ // + "] sent token to its silent neighbour:Process Node["
195+ // + silentNeigh.nodeID + "]");
157196 }
158197 }
159198 }
160199 ProcessNode sender = receiveToken ();
161200 if (sender != null && silentNeigh != null && sender == silentNeigh ) {
162- System .out .println ("Process Node[" + this .nodeID
163- + "] Received Token from Silent Neighbour Process Node["
164- + sender .nodeID + "]" );
201+ // System.out.println("Process Node[" + this.nodeID
202+ // + "] Received Token from Silent Neighbour Process Node["
203+ // + sender.nodeID + "]");
165204 System .out .println ("Process Node[" + this .nodeID + "] Decides" );
166205 this .status = Status .DECIDED ;
167- defuse ();
206+ deffuse ();
168207 }
169208 }
170-
171- public void defuse ()
172- {
209+
210+ public void deffuse () {
173211 Iterator <Node > neighItr = neigh .iterator ();
174- while (neighItr .hasNext ())
175- {
176- ProcessNode node =(ProcessNode ) neighItr .next ();
177- if (node !=silentNeigh )
178- {
212+ while (neighItr .hasNext ()) {
213+ ProcessNode node = (ProcessNode ) neighItr .next ();
214+ if (node != silentNeigh ) {
179215 this .send (node , MessageType .TOKEN );
180216 }
181-
217+
218+ }
219+ }
220+
221+ public void doStep () {
222+ switch (this .status ) {
223+ case DECIDED :
224+ break ;
225+ case TOKEN_SENT :
226+ ProcessNode sender = receiveToken ();
227+ if (sender != null && sender == silentNeigh )
228+ decide ();
229+ break ;
230+ default :
231+ while (!messageQueue .isEmpty () && getNonRecCount () > 1 ) {
232+ this .status = Status .WAITING ;
233+ receiveToken ();
234+ }
235+ this .silentNeigh = findSilentNeighbour ();
236+ if (this .silentNeigh != null && this .status != Status .TOKEN_SENT ) {
237+ send (this .silentNeigh , MessageType .TOKEN );
238+ this .status = Status .TOKEN_SENT ;
239+ }
240+ break ;
241+
242+ }
243+
244+ }
245+
246+ public void decide () {
247+ this .status = Status .DECIDED ;
248+ System .out .println ("Process Node[" + this .nodeID + "] Decides" );
249+ if (runDiffusion )
250+ {
251+ deffuse ();
182252 }
183253 }
184-
185254
186255}
0 commit comments