Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jboner/akka
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorklang committed Oct 8, 2010
2 parents 848a69d + e809d98 commit d165f14
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
39 changes: 27 additions & 12 deletions akka-remote/src/main/scala/remote/RemoteServer.scala
Expand Up @@ -572,6 +572,29 @@ class RemoteServerHandler(
server.typedActorsByUuid().get(uuid)
}

private def findActorByIdOrUuid(id: String, uuid: String) : ActorRef = {
var actorRefOrNull = if (id.startsWith(UUID_PREFIX)) {
findActorByUuid(id.substring(UUID_PREFIX.length))
} else {
findActorById(id)
}
if (actorRefOrNull eq null) {
actorRefOrNull = findActorByUuid(uuid)
}
actorRefOrNull
}

private def findTypedActorByIdOrUuid(id: String, uuid: String) : AnyRef = {
var actorRefOrNull = if (id.startsWith(UUID_PREFIX)) {
findTypedActorByUuid(id.substring(UUID_PREFIX.length))
} else {
findTypedActorById(id)
}
if (actorRefOrNull eq null) {
actorRefOrNull = findTypedActorByUuid(uuid)
}
actorRefOrNull
}

/**
* Creates a new instance of the actor with name, uuid and timeout specified as arguments.
Expand All @@ -587,11 +610,7 @@ class RemoteServerHandler(
val name = actorInfo.getTarget
val timeout = actorInfo.getTimeout

val actorRefOrNull = if (id.startsWith(UUID_PREFIX)) {
findActorByUuid(id.substring(UUID_PREFIX.length))
} else {
findActorById(id)
}
val actorRefOrNull = findActorByIdOrUuid(id, uuidFrom(uuid.getHigh,uuid.getLow).toString)

if (actorRefOrNull eq null) {
try {
Expand All @@ -603,7 +622,7 @@ class RemoteServerHandler(
actorRef.id = id
actorRef.timeout = timeout
actorRef.remoteAddress = None
server.actors.put(id, actorRef) // register by id
server.actorsByUuid.put(actorRef.uuid.toString, actorRef) // register by uuid
actorRef
} catch {
case e =>
Expand All @@ -618,11 +637,7 @@ class RemoteServerHandler(
val uuid = actorInfo.getUuid
val id = actorInfo.getId

val typedActorOrNull = if (id.startsWith(UUID_PREFIX)) {
findTypedActorByUuid(id.substring(UUID_PREFIX.length))
} else {
findTypedActorById(id)
}
val typedActorOrNull = findTypedActorByIdOrUuid(id, uuidFrom(uuid.getHigh,uuid.getLow).toString)

if (typedActorOrNull eq null) {
val typedActorInfo = actorInfo.getTypedActorInfo
Expand All @@ -639,7 +654,7 @@ class RemoteServerHandler(

val newInstance = TypedActor.newInstance(
interfaceClass, targetClass.asInstanceOf[Class[_ <: TypedActor]], actorInfo.getTimeout).asInstanceOf[AnyRef]
server.typedActors.put(id, newInstance) // register by id
server.typedActors.put(uuidFrom(uuid.getHigh,uuid.getLow).toString, newInstance) // register by uuid
newInstance
} catch {
case e =>
Expand Down
Expand Up @@ -56,6 +56,15 @@ object ClientInitiatedRemoteActorSpec {
SendOneWayAndReplySenderActor.latch.countDown
}
}

class MyActorCustomConstructor extends Actor {
var prefix = "default-"
var count = 0
def receive = {
case "incrPrefix" => count += 1; prefix = "" + count + "-"
case msg: String => self.reply(prefix + msg)
}
}
}

class ClientInitiatedRemoteActorSpec extends JUnitSuite {
Expand Down Expand Up @@ -150,6 +159,26 @@ class ClientInitiatedRemoteActorSpec extends JUnitSuite {
assert("Expected exception; to test fault-tolerance" === e.getMessage())
}
actor.stop
}
}

@Test
def shouldRegisterActorByUuid {
val actor1 = actorOf[MyActorCustomConstructor]
actor1.makeRemote(HOSTNAME, PORT1)
actor1.start
actor1 ! "incrPrefix"
assert((actor1 !! "test").get === "1-test")
actor1 ! "incrPrefix"
assert((actor1 !! "test").get === "2-test")

val actor2 = actorOf[MyActorCustomConstructor]
actor2.makeRemote(HOSTNAME, PORT1)
actor2.start

assert((actor2 !! "test").get === "default-test")

actor1.stop
actor2.stop
}
}

0 comments on commit d165f14

Please sign in to comment.