Skip to content

Commit d075ac5

Browse files
committed
Toueg Checkpoint
1 parent 199b587 commit d075ac5

File tree

1 file changed

+42
-88
lines changed

1 file changed

+42
-88
lines changed

src/main/scala/com/distcomp/routing/Toueg.scala

Lines changed: 42 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -41,138 +41,92 @@ object Toueg {
4141
Behaviors.receiveMessage {
4242
case SetAllNodes(allnodesnew)=>
4343
context.log.info(s"Set all nodes received by initializer ${context.self.path.name}.")
44-
// set all nodes in distanceesp to inf except self and edges for allnode.size -1 rounds
45-
val updatedDistancesp = (0 until allnodesnew.size).map { i =>
46-
i -> Map.empty[ActorRef[Message], Int]
47-
}.toMap
48-
// log edges and weight
49-
// context.log.info(s"Edges and weights for node ${node.path.name} are $edges.")
50-
val updatedDistp = allnodesnew.map { q =>
51-
if (q == context.self) {
52-
q -> 0
53-
} else if (edges.contains(q)) {
54-
// context.log.info(s"Setting distance for node ${q.path.name} to ${edges(q)}")
55-
q -> edges(q)
56-
}
57-
else {
58-
q -> Int.MaxValue
59-
}
60-
}.toMap
44+
val updatedDistancesp = (0 until allnodesnew.size).map { i => i -> Map.empty[ActorRef[Message], Int]}.toMap
6145

62-
// log updatedDistp of edges
63-
context.log.info(s"Updated distance for node ${node.path.name} is $updatedDistp.")
46+
val updatedDistp = allnodesnew.map { q => if (q == context.self) {q -> 0} else if (edges.contains(q)) {q -> edges(q)} else {q -> Int.MaxValue}}.toMap
6447

65-
val updatedParentp = allnodesnew.map { node =>
66-
if (edges.contains(node)) {
67-
node -> Some(node)
68-
}
69-
else {
70-
node -> None
71-
}
72-
}.toMap
48+
val UpdatedDistancesp_1 = updatedDistancesp.updated(0, updatedDistp)
7349

74-
active(node, allnodesnew, edges, updatedDistp, updatedParentp, forwardp, updatedDistancesp, S, pivots, roundp, counter, numNodes, context, simulator)
50+
val updatedParentp = allnodesnew.map { q => if (edges.contains(q)) {q -> Some(q)} else {q -> None}}.toMap
7551

76-
case StartRoutingT(allnodes, pivot) => // add all nodes
77-
context.log.info(s"Routing started by initializer ${context.self.path.name}.")
78-
// set pivot as init
79-
val updatedPivots = pivots.updated(roundp, pivot)
52+
active(node, allnodesnew, edges, updatedDistp, updatedParentp, forwardp, UpdatedDistancesp_1, S, pivots, 0, counter, numNodes, context, simulator)
8053

81-
// log updated pivot and roundp for each node
82-
context.log.info(s"Round $roundp pivot is ${pivot.path.name} for node ${node.path.name}.")
83-
if (node == updatedPivots(roundp) ){
84-
context.log.info(s"Node ${node.path.name} is the pivot for round $roundp. || $forwardp")
85-
// send to all ref in forwardp
54+
case StartRoutingT(allnodes, pivots) =>
55+
context.log.info(s"Round $roundp for node ${node.path.name}.")
56+
if (node == pivots(roundp)){
57+
context.log.info(s"${node.path.name} == pivot for round $roundp. || ${forwardp(roundp)}")
8658
forwardp(roundp).foreach { q =>
87-
val distancesToSend = allnodes.filter(r => distp(r) < Int.MaxValue).map(r => r -> distp(r)).toMap
88-
context.log.info(s"Sending distances to ${q.path.name} for round $roundp. || $distancesToSend")
89-
q ! ProvideRoutingInfo(distancesToSend, context.self)
59+
q ! ProvideRoutingInfo(allnodes.filter(r => distp(r) < Int.MaxValue).map(r => r -> distp(r)).toMap, context.self)
9060
}
91-
// Perform NextRound
92-
context.self ! InitiateNextRoundT //check 1
93-
active(node, allnodes, edges, distp, parentp, forwardp, distancesp, S, updatedPivots, roundp, counter, allnodes.size, context, simulator)
61+
context.self ! InitiateNextRoundT
62+
active(node, allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp, counter, allnodes.size, context, simulator)
9463
}
95-
// check parentp[pivots[round]] != none
96-
else if (parentp(updatedPivots(roundp)).isDefined) {
97-
context.log.info(s"Parent of pivot for round $roundp is present for node ${node.path.name}.")
98-
// send request to parent of pivot
99-
parentp(updatedPivots(roundp)).get ! RequestRouting(roundp, node)
100-
active(node, allnodes, edges, distp, parentp, forwardp, distancesp, S, updatedPivots, roundp, counter, allnodes.size, context, simulator)
64+
else if (parentp(pivots(roundp)).isDefined) {
65+
context.log.info(s"Parent ${parentp(pivots(roundp)).get.path.name} of pivot: ${pivots(roundp).path.name} for round $roundp is present for ${node.path.name}.")
66+
parentp(pivots(roundp)).get ! RequestRouting(roundp, node)
67+
active(node, allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp, counter, allnodes.size, context, simulator)
10168
}
10269
else {
103-
//next round
10470
context.self ! InitiateNextRoundT
105-
active(node, allnodes, edges, distp, parentp, forwardp, distancesp, S, updatedPivots, roundp, counter, numNodes, context, simulator)
71+
active(node, allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp, counter, numNodes, context, simulator)
10672
}
10773

10874

10975
case RequestRouting(round, requester) =>
11076
if (round < roundp) {
11177
requester ! ProvideRoutingInfo(distancesp(round), context.self)
112-
context.log.info(s"Routing info for round $round provided to ${requester.path.name}")
113-
active(node,allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp+1, counter, numNodes, context, simulator)
78+
context.log.info(s"Routing info for round $round provided to ${requester.path.name} from ${node.path.name} || ${distancesp(round)}")
79+
active(node,allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp, counter, numNodes, context, simulator)
11480
} else {
11581
val updatedForwardp = forwardp.updated(round, forwardp(round) + requester)
116-
context.log.info(s"Request from ${requester.path.name} added to forward list for round $round || ${updatedForwardp(round)} || $updatedForwardp ")
82+
context.log.info(s"Request from ${requester.path.name} added to forward list node ${node.path.name} for round $round || ${updatedForwardp(round)} || $updatedForwardp ")
11783
active(node,allnodes, edges, distp, parentp, updatedForwardp, distancesp, S, pivots, roundp, counter, numNodes, context, simulator)
11884
}
11985

12086
case ProvideRoutingInfo(receivedDistances, from) =>
121-
//log
122-
context.log.info(s"Routing info received from ${from.path.name} for round $roundp.")
87+
context.log.info(s"Routing info received from ${from.path.name} to ${node.path.name} for round $roundp.")
12388
var updateDistances = receivedDistances
12489
var updatedParentp = parentp
12590
var updatedDistp = distp
126-
// for all distance keys
91+
var updatedDistancesp = distancesp
12792
allnodes.foreach { q =>
128-
// check if receivedDistances has distance key
129-
// log q
130-
context.log.info(s"Checking distance for node ${q.path.name} in round $roundp. || receivedDistances: $receivedDistances || ${receivedDistances.contains(q)}")
131-
if (receivedDistances.contains(q)) {
93+
if (receivedDistances.contains(q) ) {
94+
context.log.info(s"Distance for node ${q.path.name} to pivot is ${distp(pivots(roundp))}.")
13295
val newDist = receivedDistances(q) + distp(pivots(roundp))
133-
context.log.info(s"New distance for node ${q.path.name} is $newDist, Orig ${distp(q)}.")
13496
if (newDist < distp(q)) {
135-
//log
136-
context.log.info(s"Updating distance for node ${q.path.name} from ${from.path.name} to $newDist. | ${distp(q)}")
137-
// update distp, parentp of ref
97+
context.log.info(s"Updating distance for node ${q.path.name} from ${context.self.path.name} to $newDist. with pivot ${pivots(roundp).path.name} and round ${roundp}| ${distp(q)}")
13898
updatedDistp = distp.updated(q, newDist)
139-
updatedParentp = parentp.updated(q, parentp(pivots(roundp)))
99+
updatedParentp = parentp.updated(q, Some(pivots(roundp)))
140100
}
141101
else {
142102
context.log.info(s"Removing distance for node ${q.path.name} from round $roundp.")
143103
updateDistances = updateDistances - q
144104
}
145105
}
146106
}
147-
148-
// send distances to all ref in forwardp
149-
//log forwardp values
150-
context.log.info(s"Forwarding ${context.self.path.name} distances to all nodes in round $roundp. || ${forwardp(roundp-1)} || $updateDistances")
107+
context.log.info(s"Forwarding ${context.self.path.name} distances to all nodes in round $roundp. || ${forwardp(roundp)} || $updateDistances")
151108
forwardp(roundp).foreach(r => r ! ProvideRoutingInfo(updateDistances, context.self))
152-
// add distances to distancesp for round k
153-
val updatedDistancesp = distancesp.updated(roundp, updateDistances)
154-
// Perform NextRound
109+
updatedDistancesp = distancesp.updated(roundp, updateDistances)
110+
context.log.info(s"Updated distances for round $roundp. || $updatedDistancesp")
155111
context.self ! InitiateNextRoundT
156112
active(node,allnodes, edges, updatedDistp, updatedParentp, forwardp, updatedDistancesp, S, pivots, roundp, counter, numNodes, context, simulator)
157113

158114
case InitiateNextRoundT =>
159-
if (counter < numNodes) {
160-
val updatedCounter = counter + 1
161-
context.log.info(s"Initiating round $updatedCounter for node ${node.path.name}.")
162-
// send message to for next round
163-
context.self ! StartRoutingT(allnodes, pivots(roundp))
164-
active(node,allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp, updatedCounter, numNodes, context, simulator)
165-
} else
166-
{
167-
// print distances
168-
context.log.info(s"Distances for node ${node.path.name} are ${distp.values}.")
169-
context.log.info(s"Round $roundp for ${node.path.name} completed, terminating.")
170-
simulator ! SimulatorProtocol.AlgorithmDone
171-
active(node,allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp+1, counter, numNodes, context, simulator)
172-
}
115+
if (roundp >= allnodes.size-1)
116+
{
117+
context.log.info(s"Distances for node ${node.path.name} are ${distp}.")
118+
context.log.info(s"Round $roundp for ${node.path.name} completed, terminating.")
119+
simulator ! SimulatorProtocol.AlgorithmDone
120+
active(node,allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp, counter, numNodes, context, simulator)
121+
}
122+
else{
123+
context.self ! StartRoutingT(allnodes, pivots)
124+
active(node,allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp+1, counter, numNodes, context, simulator)
125+
}
173126

174127
case _ =>
175-
active(node,allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp, counter, numNodes, context, simulator)
128+
context.log.info(s"Unhandled message received by ${context.self.path.name}.")
129+
active(node, allnodes, edges, distp, parentp, forwardp, distancesp, S, pivots, roundp, counter, numNodes, context, simulator)
176130

177131
}
178132
}

0 commit comments

Comments
 (0)