1
1
package assignment3 .node ;
2
2
3
3
import assignment3 .link .Link ;
4
+ import assignment3 .link .LinkComparator ;
4
5
import assignment3 .link .LinkState ;
5
6
import assignment3 .message .Message ;
6
7
import assignment3 .message .MessageType ;
7
8
8
9
import java .rmi .RemoteException ;
9
10
import java .rmi .server .UnicastRemoteObject ;
10
- import java .util .List ;
11
- import java .util .PriorityQueue ;
12
- import java .util .Queue ;
13
- import java .util .Set ;
14
- import java .util .TreeSet ;
11
+ import java .util .*;
12
+ import java .util .stream .Collector ;
13
+ import java .util .stream .Collectors ;
15
14
16
15
public class Node extends UnicastRemoteObject implements IComponent {
17
16
@@ -53,6 +52,7 @@ public void wakeUp() throws RemoteException {
53
52
54
53
55
54
public void receive (Message message , Link link ) throws RemoteException {
55
+ updateLinks (link );
56
56
switch (message .getType ()) {
57
57
case TEST :
58
58
receiveTest (message , link );
@@ -69,12 +69,28 @@ public void receive(Message message, Link link) throws RemoteException {
69
69
case REPORT :
70
70
receiveReport (message , link );
71
71
break ;
72
+ case REJECT :
73
+ receiveReject (message , link );
74
+ break ;
72
75
case CHANGE_ROOT :
73
76
receiveChangeRoot (message , link );
74
77
break ;
75
78
}
76
79
}
77
80
81
+ public void updateLinks (Link link ) {
82
+ List <Link > linkLists = new ArrayList <>(links );
83
+ Link linkToUpdate = new Link ();
84
+ for (Link linkEntry : linkLists ) {
85
+ if (linkEntry .compareTo (link ) == 0 ) {
86
+ linkToUpdate = linkEntry ;
87
+ }
88
+ }
89
+ linkLists .remove (linkToUpdate );
90
+ linkLists .add (link );
91
+ this .links = new PriorityQueue <>(linkLists );
92
+ }
93
+
78
94
public void send (Message message , Link link ) {
79
95
80
96
}
@@ -104,7 +120,7 @@ private void receiveInitiate(Message message, Link link) throws RemoteException
104
120
state = message .getSenderState ();
105
121
inBranch = link ;
106
122
bestEdge = null ;//TODO: NIL
107
- weightBestAdjacent = - 10000 ; // - INFINITY
123
+ weightBestAdjacent = Double . POSITIVE_INFINITY ;
108
124
109
125
for (Link adjescentLink : this .links ) {//TODO: Not sure if this is the correct one list of links
110
126
if (adjescentLink != link && adjescentLink .getState () == LinkState .IN_MST ) {
@@ -120,22 +136,47 @@ private void receiveInitiate(Message message, Link link) throws RemoteException
120
136
}
121
137
}
122
138
123
- private void test () {
124
- if (links .stream ().anyMatch (p ->p .getState ()==LinkState .CANDIDATE_IN_MST )){//TODO: check if adjecent links are the ones
125
-
139
+ private void test () throws RemoteException {
140
+ if (links .stream ().anyMatch (p -> p .getState () == LinkState .CANDIDATE_IN_MST )) {//TODO: check if adjecent links are the ones
141
+ testEdge = links .stream ().min (new LinkComparator ()).get ();
142
+ Message msg = new Message (MessageType .TEST , level , fragmentName );
143
+ nodes [testEdge .getReceiver (id )].receive (msg , testEdge );
144
+ } else {
145
+ testEdge = null ;
146
+ report ();
126
147
}
127
148
}
128
149
129
- private void receiveTest (Message message , Link link ) {
130
-
131
- }
132
-
133
- private void reject (Link link ) {
134
-
150
+ private void receiveTest (Message message , Link link ) throws RemoteException {
151
+ if (state == NodeState .SLEEPING ) {
152
+ wakeUp ();
153
+ }
154
+ if (message .getLevel () > level ) {
155
+ //TODO: append message to the queue
156
+ } else {
157
+ if (message .getfName () != fragmentName ) {
158
+ Message msg = new Message (MessageType .ACCEPT );
159
+ nodes [link .getReceiver (id )].receive (msg , link );
160
+ } else {
161
+ if (link .getState () == LinkState .CANDIDATE_IN_MST ) {
162
+ link .setState (LinkState .NOT_IN_MST );//TODO: think about local copies
163
+ }
164
+ if (link .getWeight () == testEdge .getWeight ()) {//TODO: think if Link does not require some id
165
+ Message msg = new Message (MessageType .REJECT );
166
+ nodes [link .getReceiver (id )].receive (msg , link );
167
+ } else {
168
+ test ();
169
+ }
170
+ }
171
+ }
135
172
}
136
173
137
- private
1356
void receiveReject (Message message , Link link ) {
138
174
175
+ private void receiveReject (Message message , Link link ) throws RemoteException {
176
+ if (link .getState () == LinkState .CANDIDATE_IN_MST ) {
177
+ link .setState (LinkState .NOT_IN_MST );
178
+ }
179
+ test ();
139
180
}
140
181
141
182
private void receiveAccept (Message message , Link link ) throws RemoteException {
@@ -172,7 +213,7 @@ private void receiveReport(Message message, Link link) throws RemoteException {
172
213
if (message .getWeight () > weightBestAdjacent )
173
214
changeRoot ();
174
215
else {
175
- if (message .getWeight () == weightBestAdjacent &&
216
+ if (message .getWeight () == weightBestAdjacent &&
176
217
weightBestAdjacent == Double .POSITIVE_INFINITY ){
177
218
// TODO: HALT
178
219
}
0 commit comments