Skip to content

Commit 2adca71

Browse files
committed
Tweeks
1 parent 0b88b7c commit 2adca71

File tree

2 files changed

+163
-59
lines changed

2 files changed

+163
-59
lines changed

WaveAlgorithm/src/WaveMain.java

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ public static void main(String args[])
1919
ProcessNode n6=new ProcessNode(6);
2020
ProcessNode n7=new ProcessNode(7);
2121

22+
treenodes.add(n0);
23+
treenodes.add(n1);
24+
treenodes.add(n2);
25+
treenodes.add(n3);
26+
treenodes.add(n4);
27+
treenodes.add(n5);
28+
treenodes.add(n6);
29+
treenodes.add(n7);
30+
2231
n0.addNeigh(n1);
2332
n1.addNeigh(n2);
2433
n1.addNeigh(n3);
@@ -29,36 +38,62 @@ public static void main(String args[])
2938

3039

3140

32-
n0.init();
33-
n1.init();
34-
n2.init();
35-
n3.init();
36-
n4.init();
37-
n5.init();
38-
n6.init();
39-
n7.init();
41+
n0.init(true);
42+
n1.init(true);
43+
n2.init(true);
44+
n3.init(true);
45+
n4.init(true);
46+
n5.init(true);
47+
n6.init(true);
48+
n7.init(true);
49+
50+
// n0.executeStep();
51+
// n1.executeStep();
52+
//n2.executeStep();
53+
//n4.executeStep();
54+
// n5.executeStep();
55+
// n6.executeStep();
56+
// n2.executeStep();
57+
// n1.executeStep();
58+
// n3.executeStep();
59+
// n7.executeStep();
60+
// n6.executeStep();
61+
// n6.executeStep();
62+
// n3.executeStep();
63+
// n1.executeStep();
64+
// n0.executeStep();
65+
// n1.executeStep();
66+
// n7.executeStep();
67+
// n2.executeStep();
68+
// n4.executeStep();
69+
// n5.executeStep();
70+
// n6.executeStep();
71+
72+
n0.doStep();
73+
n1.doStep();
74+
n2.doStep();
75+
n4.doStep();
76+
n5.doStep();
77+
n6.doStep();
78+
n2.doStep();
79+
n1.doStep();
80+
n3.doStep();
81+
n7.doStep();
82+
n6.doStep();
83+
n6.doStep();
84+
n3.doStep();
85+
n1.doStep();
86+
n0.doStep();
87+
n1.doStep();
88+
n7.doStep();
89+
n2.doStep();
90+
n4.doStep();
91+
n5.doStep();
92+
n6.doStep();
93+
94+
95+
4096

41-
n0.executeStep();
42-
n1.executeStep();
43-
n2.executeStep();
44-
n4.executeStep();
45-
n5.executeStep();
46-
n6.executeStep();
47-
n2.executeStep();
48-
n1.executeStep();
49-
n3.executeStep();
50-
n7.executeStep();
51-
n6.executeStep();
52-
n6.executeStep();
53-
n3.executeStep();
54-
n1.executeStep();
55-
n0.executeStep();
56-
n1.executeStep();
57-
n7.executeStep();
58-
n2.executeStep();
59-
n4.executeStep();
60-
n5.executeStep();
61-
n6.executeStep();
6297

6398
}
6499

WaveAlgorithm/src/com/technipun/dc/algorithm/ProcessNode.java

Lines changed: 99 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)