@@ -41,138 +41,92 @@ object Toueg {
41
41
Behaviors .receiveMessage {
42
42
case SetAllNodes (allnodesnew)=>
43
43
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
61
45
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
64
47
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)
73
49
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
75
51
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)
80
53
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)}" )
86
58
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)
90
60
}
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)
94
63
}
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)
101
68
}
102
69
else {
103
- // next round
104
70
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)
106
72
}
107
73
108
74
109
75
case RequestRouting (round, requester) =>
110
76
if (round < roundp) {
111
77
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)
114
80
} else {
115
81
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 " )
117
83
active(node,allnodes, edges, distp, parentp, updatedForwardp, distancesp, S , pivots, roundp, counter, numNodes, context, simulator)
118
84
}
119
85
120
86
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. " )
123
88
var updateDistances = receivedDistances
124
89
var updatedParentp = parentp
125
90
var updatedDistp = distp
126
- // for all distance keys
91
+ var updatedDistancesp = distancesp
127
92
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))}. " )
132
95
val newDist = receivedDistances(q) + distp(pivots(roundp))
133
- context.log.info(s " New distance for node ${q.path.name} is $newDist, Orig ${distp(q)}. " )
134
96
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)}" )
138
98
updatedDistp = distp.updated(q, newDist)
139
- updatedParentp = parentp.updated(q, parentp (pivots(roundp)))
99
+ updatedParentp = parentp.updated(q, Some (pivots(roundp)))
140
100
}
141
101
else {
142
102
context.log.info(s " Removing distance for node ${q.path.name} from round $roundp. " )
143
103
updateDistances = updateDistances - q
144
104
}
145
105
}
146
106
}
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" )
151
108
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" )
155
111
context.self ! InitiateNextRoundT
156
112
active(node,allnodes, edges, updatedDistp, updatedParentp, forwardp, updatedDistancesp, S , pivots, roundp, counter, numNodes, context, simulator)
157
113
158
114
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
+ }
173
126
174
127
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)
176
130
177
131
}
178
132
}
0 commit comments