Skip to content
remeniuk edited this page Oct 28, 2010 · 6 revisions

##Source code

Node.js side

// Create new proxy for the remote actor
var actorProxy = actors.createActorProxy('10.6.122.29', 12345, 'server')

// Define handler to receive message from the actor asynchronously. Handler is a function that
// maps FQCN of the protocol stub used by the actor and function that handles serialized response
actorProxy.receive({
    'com.vasilrem.remote.protocol.RemoteActorProtocol$StringMessage': function(data){
        console.log('Remote actor responded with: ' + StringMessage.parse(data).message)
        actorProxy.end()
        actorProxy.destroy()
    }
})

// When proxy is loaded, start sending messages
actorProxy.on('ready', function(){
    actorProxy.send(
        'com.vasilrem.remote.protocol.RemoteActorProtocol$StringMessage',
        StringMessage.serialize({
            message: 'Message from node.js!'
        }))
})

Scala side

object TestRemoteProtoActor{
  scala.actors.Debug.level_=(100)
  println("Starting dummy remote actor...")

  actor{
    RemoteProtoActor.alive(12345)
    RemoteProtoActor.register('server, self)
    loop {
      react {
        case msg:StringMessage => println(msg.getMessage)
          reply(StringMessage.newBuilder.setMessage("Server replied to [%s]" format(msg.getMessage)))
        case unknown => throw new Exception("Unknown message %s was received by remote actor!" format(unknown))
      }
    }
  }

  println("Started.")
}

##Running the example ###Running the actor

  1. >sbt
  2. update
  3. test
[info]   + Message serialized into protobuf is sent directly via socket to remote actor
...
[info]   + Deserialization of a serialized object returns original object
...
[info] Passed: : Total 2, Failed 0, Errors 0, Passed 2, Skipped 0
  1. console
  2. scala> com.vasilrem.remote.TestRemoteProtoActor
Info: initializing scala.actors.Scheduler$@a368a4...
Info: scala.actors.scheduler.ThreadPoolConfig$@2b050f: java.version = 1.6.0_17
Info: scala.actors.scheduler.ThreadPoolConfig$@2b050f: java.vm.vendor = Sun Microsystems Inc.
Info: scala.actors.scheduler.ForkJoinScheduler@a817f3: parallelism 4
Info: scala.actors.scheduler.ForkJoinScheduler@a817f3: max pool size 256
Info: scala.actors.Scheduler$@a368a4: starting new scala.actors.scheduler.ForkJoinScheduler@a817f3 [class scala.actors.scheduler.ForkJoinSch
eduler]
Started.
Info: created service at Node(10.6.122.29,12345)
Info: Thread[Thread-41,5,trap.exit]: caught java.net.BindException: Address already in use: JVM_Bind
Info: Thread[Thread-41,5,trap.exit]: shutting down...
res0: com.vasilrem.remote.TestRemoteProtoActor.type = com.vasilrem.remote.TestRemoteProtoActor$@3c270c

##Running actor proxy ./node-run.test.sh

Connecting to 10.6.122.29:12345
Successfully connected to 10.6.122.29:12345
Sent greeting to the actor node.
Sending message:
Message from node.js!
Received message is to short!
Remote actor responded with: Server replied to [Message from node.js!]

##Remote Actor output

Adding connection to Node(10.6.122.26,2428)
Info: Thread[Thread-22,5,main]: waiting for new connection on port 12345...
Info: scala.actors.remote.NetKernel@1637f9a: processing NamedSend(Locator(Node(10.6.122.29,12345),'nodejs),Locator(Node(10.6.122.29,12345),'
server),[B@1c97c99,'nosession)
Message from node.js!
Info: Thread[Thread-43,5,trap.exit]: transmitting data...
Info: Thread[Thread-43,5,trap.exit]: caught java.io.EOFException
Info: Thread[Thread-43,5,trap.exit]: service terminated at Node(10.6.122.29,12345)
Clone this wiki locally