import akka.actor.{Actor, ActorRef, Props} import domain._ import scala.io.Source /** * Created by sri on 1/6/17. */ case class Master(fileName: String) extends Actor { var totalLines = 0 var totalWords = 0 var linesSoFar = 0 private var senderRef: Option[ActorRef] = None def receive={ case Start => { senderRef = Some(sender) Source.fromFile(fileName).getLines().foreach( line => { context.actorOf(Props[Slave]) ! DoWork(line) totalLines+=1 }) } case Done(cnt) =>{ totalWords += cnt linesSoFar += 1 if(linesSoFar == totalLines) { senderRef.map(_ ! totalWords) //Don't use sender, we don't want to send to the Slave...... } } case _ => "What Do I do?" } }