8000 Assignment 3: Implemention the rest of the methods · rpytel1/distributed-algorithms@15eb5fa · GitHub
[go: up one dir, main page]

Skip to content

Commit 15eb5fa

Browse files
author
rpytel1
committed
Assignment 3: Implemention the rest of the methods
1 parent 1412361 commit 15eb5fa

File tree

3 files changed

+71
-18
lines changed

3 files changed

+71
-18
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package assignment3.link;
2+
3+
import java.util.Comparator;
4+
5+
public class LinkComparator implements Comparator<Link> {
6+
@Override
7+
public int compare(Link o1, Link o2) {
8+
return o1.compareTo(o2);
9+
}
10+
11+
}

Assignment3/src/assignment3/message/MessageType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public enum MessageType {
66
ACCEPT,
77
CHANGE_ROOT,
88
CONNECT,
9-
REPORT
9+
REPORT,
10+
REJECT
1011
}

Assignment3/src/assignment3/node/Node.java

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package assignment3.node;
22

33
import assignment3.link.Link;
4+
import assignment3.link.LinkComparator;
45
import assignment3.link.LinkState;
56
import assignment3.message.Message;
67
import assignment3.message.MessageType;
78

89
import java.rmi.RemoteException;
910
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;
1514

1615
public class Node extends UnicastRemoteObject implements IComponent {
1716

@@ -53,6 +52,7 @@ public void wakeUp() throws RemoteException {
5352

5453

5554
public void receive(Message message, Link link) throws RemoteException {
55+
updateLinks(link);
5656
switch (message.getType()) {
5757
case TEST:
5858
receiveTest(message, link);
@@ -69,12 +69,28 @@ public void receive(Message message, Link link) throws RemoteException {
6969
case REPORT:
7070
receiveReport(message, link);
7171
break;
72+
case REJECT:
73+
receiveReject(message, link);
74+
break;
7275
case CHANGE_ROOT:
7376
receiveChangeRoot(message, link);
7477
break;
7578
}
7679
}
7780

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+
7894
public void send(Message message, Link link) {
7995

8096
}
@@ -104,7 +120,7 @@ private void receiveInitiate(Message message, Link link) throws RemoteException
104120
state = message.getSenderState();
105121
inBranch = link;
106122
bestEdge = null;//TODO: NIL
107-
weightBestAdjacent = -10000; // - INFINITY
123+
weightBestAdjacent = Double.POSITIVE_INFINITY;
108124

109125
for (Link adjescentLink : this.links) {//TODO: Not sure if this is the correct one list of links
110126
if (adjescentLink != link && adjescentLink.getState() == LinkState.IN_MST) {
@@ -120,22 +136,47 @@ private void receiveInitiate(Message message, Link link) throws RemoteException
120136
}
121137
}
122138

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();
126147
}
127148
}
128149

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+
}
135172
}
136173

137-
private 1356 void receiveReject(Message message, Link link) {
138174

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();
139180
}
140181

141182
private void receiveAccept(Message message, Link link) throws RemoteException {
@@ -172,7 +213,7 @@ private void receiveReport(Message message, Link link) throws RemoteException {
172213
if (message.getWeight() > weightBestAdjacent)
173214
changeRoot();
174215
else{
175-
if (message.getWeight() == weightBestAdjacent &&
216+
if (message.getWeight() == weightBestAdjacent &&
176217
weightBestAdjacent == Double.POSITIVE_INFINITY){
177218
// TODO: HALT
178219
}

0 commit comments

Comments
 (0)
0