#1567 Remote actors

Xan Wed 29 Jun 2011

Hi,

Is there a Fantom equivalent as Scala Remote actors (Scala native or Akka I don't mind)?

If not, is it planned to code?

Thanks a lot, Xan.

brian Thu 30 Jun 2011

It is not available unless someone has done an implementation. I'm sure I might add it to the core library at some point, but to be honest I find it is typically the wrong way to do a network protocol. It suffers all the same problems as traditional RPC like solutions like RMI - remote programming is rarely like local programming.

Xan Sun 17 Jul 2011

Well, it's a pain. I'm obligated to code with scala (?). How can I "simulate" some sort of remote actos?

Thanks, Xan.

brian Mon 18 Jul 2011

Hi Xan,

Distributed systems can often be very complex beasts, but I think you will find that Fantom libraries make it pretty easy to roll your own custom solutions. In the cast of Actors, I think anything over the network would be strictly limited to asynchronously messaging. In this case you can write a fully functional client and server actor that communicate asynchronously over HTTP in a couple of dozen lines of code:

using concurrent
using web
using wisp

class HttpActorTest
{
  Void main(Str[] args)
  {
    pool := ActorPool()
    if (args.first == "server")
    {
      WispService { port = 8080; root = ServerActorMod(ServerActor(pool)) }.start
      Actor.sleep(Duration.maxVal)
    }
    else
    {
      c := ClientActor(ActorPool(), `http://localhost:8080/`)
      c.send(123)
      c.send("hello string")
      c.send(["list", "of", "strings"])
      c.send(["ten":10, "twenty":20])
    }
  }
}

const class ServerActorMod : WebMod
{
  new make(Actor actor) { this.actor = actor }
  const Actor actor
  override Void onPost()
  {
    obj := req.in.readObj
    actor.send(obj)
    res.statusCode = 200
  }
}

const class ServerActor : Actor
{
  new make(ActorPool pool) : super(pool) {}
  override Obj? receive(Obj? msg)
  {
    echo("ServerActor received: $msg [$msg.typeof]")
    return null
  }
}

const class ClientActor : Actor
{
  new make(ActorPool pool, Uri uri) : super(pool) { this.uri = uri }
  const Uri uri
  override Obj? receive(Obj? msg)
  {
    WebClient(uri).postStr(StrBuf() { out.writeObj(msg) }.toStr)
    return null
  }
}

Login or Signup to reply.