-
Notifications
You must be signed in to change notification settings - Fork 0
/
project2-bonus.scala
334 lines (256 loc) · 8.03 KB
/
project2-bonus.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package slash
import scala.math._
import scala.util.Random
import akka.actor.Actor
import akka.actor.Terminated
import akka.actor.Props
import akka.actor._
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.actor.Props
import scala.reflect._
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy._
import akka.util.duration._
import scala.collection.immutable.HashMap
//sealed trait Message //to ensure message are passed in a controlled fashion
case class NodeInitialize(number:Int,nodes:List[ActorRef],neighbors:List[Int],tracker:ActorRef,stopRumor:Int,distribute:Int)
case class TrackerInitialize(system:ActorSystem, numNodes:Int, stopRumor:Int, algo:String)
case class Update(id:Int,Counter:Int)
case class PushSumalgo(sum:Double, weight:Double)
case class PushSumUpdate(number:Int)
case class initWorker(numNodes:Int,topo:String,algo:String) extends message
case class RegisterWorker(val worker: ActorRef, val supervisor: ActorRef)
case class DeadWorker()
case class init(k:Int)
sealed trait message
case object SpreadRumor extends message
object project2bonus {
def main(args: Array[String]) : Unit = {
println("object intitialized")
val system = ActorSystem("MessageSpreading")
var numNodes:Int = 0;
var topology:String = null
var algo:String = null
var stopRumor:Int = 0;
var distribute:Int = 0;
if(args.length == 0 || args.length != 3){
println("Arguments are not proper.")
}
else if(args.length == 3){
numNodes = args(0).toInt
topology = args(1)
algo = args(2)
stopRumor = 10
distribute = 2
}
val supervisor = system.actorOf(Props[SupervisorActor], name = "supervisor")
supervisor ! initWorker(numNodes,topology,algo)
}
}
class Master extends Actor
{
var localStopRumor:Int = 0
var time:Long = 0
var localNumNodes = 0
var localSys:ActorSystem = null
var tracking:List[Update] = Nil
time = System.currentTimeMillis
def receive =
{
case TrackerInitialize(system:ActorSystem, numNodes:Int, stopRumor:Int, algo:String) =>
{
time = System.currentTimeMillis
localSys = system;
localNumNodes = numNodes;
localStopRumor = stopRumor;
//if(algo.equalsIgnoreCase("gossip")) shouldWork = true;
}
case Update(id:Int, counter:Int) =>
{
var temp:List[Update] = Nil
var i:Int = 0
while(i<tracking.length){
if(tracking(i).id!=id)
{
temp ::= tracking(i)
}
i += 1
}
tracking = temp
tracking = tracking ::: List(new Update(id:Int, counter:Int))
if((tracking.length.toDouble/localNumNodes.toDouble)>0.90){
localSys.shutdown;
}
}
case PushSumUpdate(number:Int) => {
localSys.shutdown;
}
}
override def postStop(){
time = System.currentTimeMillis-time
println("\nTotal time = "+time)
}
}
class Worker extends Actor
{
var id:Int = 0
var localStopRumor:Int = 0
var counter:Int=0
var localNodes:List[ActorRef] = Nil
var localNeighbors:List[Int] = Nil
var localTracker:ActorRef = null
var dist:Int = 0
var random:Int = 0
var PushSumCounter:Int = 0;
var s:Double=0;
var w:Double=0;
def getId():Int = {return id}
def receive =
{
case NodeInitialize(number:Int,nodes:List[ActorRef],neighbors:List[Int],tracker:ActorRef,stopRumor:Int,distribute:Int) =>
{
localNeighbors = localNeighbors ::: neighbors
localNodes = nodes
id = number
localTracker = tracker
localStopRumor = stopRumor
dist = distribute
s=number
}
case SpreadRumor =>
{
if(counter==2){
counter+=1;
var temp= new index()
temp.init(id)
context.stop(self)
println("here")
}
if(counter<localStopRumor)
{
counter += 1
localTracker ! Update(id,counter)
for(i <- 0 until dist)
{
random = Random.nextInt(localNeighbors.length)
localNodes(localNeighbors(random)) ! SpreadRumor
}
}
}
case PushSumalgo(sum:Double, weight:Double) => {
counter += 1;
dist = 2
var oldTemp:Double = s/w;
s+=sum;
w+=weight;
s = s/dist;
w = w/dist;
var newTemp:Double = s/w;
if(counter==1 || Math.abs((oldTemp-newTemp))>localStopRumor) {
PushSumCounter=0;
for(i<- 1 until dist){
var randomPlayer = Random.nextInt(localNeighbors.length);
localNodes(localNeighbors(randomPlayer)) ! PushSumalgo(s,w)
}
}
else{
PushSumCounter+=1;
if(PushSumCounter>3) {
localTracker ! PushSumUpdate(id);
self ! PoisonPill
}
else{
for(i<- 1 until dist){
var randomPlayer = Random.nextInt(localNeighbors.length);
localNodes(localNeighbors(randomPlayer)) ! PushSumalgo(s,w)
}
}
}
}
}
}
class SupervisorActor extends Actor with ActorLogging {
var system= ActorSystem("BonusProblem");
var nodes:List[ActorRef] = Nil
val tracker:ActorRef = system.actorOf(Props[Master])
val monitor = context.system.actorOf(Props[MonitorActor], name = "monitor")
val stopRumor:Int=10
val distribute:Int=2
def makeNeighborList(index:Int):List[Int] ={
var neighbors:List[Int] = Nil //initialising neighbors of each element
for(j <- 0 until nodes.length)
{
if(j!=index)
{
neighbors ::= j
}
}
return neighbors
}
def initialize(numNodes:Int,topology:String,algo:String) ={
var i:Int = 0
while(i<numNodes){
nodes ::= system.actorOf(Props[Worker])
i += 1
}
println("intitialized finished")
println("neighbor list starts here")
if(topology.equalsIgnoreCase("full"))
{
var i:Int = 0
var neighbors:List[Int] = Nil
while(i<nodes.length)
{
//initialising neighbors of each element
neighbors=makeNeighborList(i)
nodes(i) ! NodeInitialize(i,nodes,neighbors,tracker,stopRumor,distribute)
monitor ! new RegisterWorker(nodes(i), self)
i+=1;
}
println("topology built --------->")
tracker ! TrackerInitialize(system,numNodes,stopRumor,algo)
if(algo.equalsIgnoreCase("gossip")){
nodes(0) ! SpreadRumor
}
}
}
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 10 seconds) {
case _: ArithmeticException => Resume
case _: IllegalArgumentException => Stop
}
def receive = {
case initWorker(numNodes:Int,topology:String,algo:String) =>initialize(numNodes,topology,algo)
case DeadWorker() =>
log.info("Got a DeadWorker message, restarting the worker")
println("actor restart")
var y= new index()
var temp1= system.actorOf(Props[Worker])
var neighbors:List[Int] = makeNeighborList(y.get)
temp1 ! NodeInitialize(y.get,nodes,neighbors,tracker,stopRumor,distribute)
monitor ! new RegisterWorker(temp1, self)
}
}
class MonitorActor extends Actor with ActorLogging {
var monitoredActors = new HashMap[ActorRef, ActorRef]
def receive: Receive = {
case t: Terminated =>
println("actor terminate new")
if (monitoredActors.contains(t.actor)) {
log.info("Received Worker Actor Termination Message -> "
+ t.actor.path)
log.info("Sending message to Supervisor")
val value: Option[ActorRef] = monitoredActors.get(t.actor)
value.get ! new DeadWorker()
}
case msg: RegisterWorker =>
println("actor registered")
context.watch(msg.worker)
monitoredActors += msg.worker -> msg.supervisor
}
}
class index {
var temp:Int =0
def init(k:Int)= temp=k
def get() :Int= return temp
}