user3091280
user3091280

Reputation: 19

Actors example not compiling

I am trying to run this code to learn actors (using Scala on Eclipse) but it is telling me that values Ping and Pong are not found.

Any idea of what I am doing wrong?

I installed akka. Any help is appreciated.

Thanks


import scala.actors.Actor
import scala.actors.Actor._

class Ping(count: int, pong:Actor) extends Actor{ // type int here is not found as well
   def act(){
     var pingsLeft= count-1
     pong! Ping
      while(true){
        receive {
         case Pong =>
           if (pingsLeft % 1000 ==0)
             Console.println("Ping : pong ")
         if (pingsLeft > 0){
           pong ! Ping
           pingsleft -=1
         } else {
          Console.println("Ping : stop")
          pong ! Stop
          exit()
        }
      }
    }
  }
}

class Pong extends Actor {
  def act(){
    var pongCount =0
    while (true){
      receive {
        case Ping =>
          if(pongCount % 1000 ==0)
            Console.println("Pong : ping " + pongCount)
           sender ! Pong
           pongCount = pongCount + 1
        case Stop =>
          Console.println("Pong : stop")
          exit()
      }
    }
  }
 }

 object pingpong extends Application {
  val pong = new Pong
  val ping = new Ping(100000, pong)
  ping.start
  pong.start
}

Upvotes: 0

Views: 822

Answers (1)

cmbaxter
cmbaxter

Reputation: 35443

As I stated in my comment, you should switch your example over to Akka. Here is a rough approximation of your example refactored to using Akka:

import akka.actor._

class Ping(count: Int, pong:ActorRef) extends Actor{ // type int here is not found as well
  pong! Ping
  var pingsLeft = count - 1

  def receive = {
    case Pong =>
      if (pingsLeft % 1000 ==0)
        Console.println("Ping : pong ")
      if (pingsLeft > 0){
        pong ! Ping
        pingsLeft -=1
      } else {
        Console.println("Ping : stop")
        pong ! Stop
        context stop self    
    }
  }
}

class Pong extends Actor {
  var pongCount =0

  def receive = {
        case Ping =>
          if(pongCount % 1000 ==0)
            Console.println("Pong : ping " + pongCount)
           sender ! Pong
           pongCount = pongCount + 1
        case Stop =>
          Console.println("Pong : stop")
          exit()    
  }
 }

case object Ping
case object Pong
case object Stop

object pingpong {
  def main(args: Array[String]) {
    val system = ActorSystem("pingpong")
    val pong = system.actorOf(Props[Pong])
    val ping = system.actorOf(Props(classOf[Ping], 100000, pong))  
  }

}

And he's a slightly refactored version, cleaning up some mutable state and also setting up the Pong instance as a child of the Ping instance so that when Ping stops, it also automatically stops the Pong instance:

import akka.actor._

class Ping(count: Int) extends Actor{ // type int here is not found as well
  val pong = context.actorOf(Props[Pong])
  pong! Ping

  def receive = pingReceive(count - 1)

  def pingReceive(pingsLeft:Int):Receive = {
    case Pong =>
      if (pingsLeft % 1000 ==0)
        Console.println("Ping : pong ")
      if (pingsLeft > 0){
        pong ! Ping
        context.become(pingReceive(pingsLeft - 1))
      } 
      else {
        Console.println("Ping : stop")
        context stop self    
      }        
  }
}

class Pong extends Actor {
  override def postStop{
    Console.println("Pong : stop") 
  }

  def receive = pongReceive(0)

  def pongReceive(pongCount:Int):Receive = {
    case Ping =>
      if(pongCount % 1000 ==0) 
        Console.println("Pong : ping " + pongCount)

      sender ! Pong
      context.become(pongReceive(pongCount + 1))        
  }
}

case object Ping
case object Pong

object PingPong {
  def main(args: Array[String]) {
    val system = ActorSystem("pingpong")
    val ping = system.actorOf(Props(classOf[Ping], 100000))  
  }    
}

Upvotes: 2

Related Questions