Skip to content
Browse files

get prefix and default gateway working

  • Loading branch information...
1 parent 5c29fd8 commit 0003dc1b97d14fb4de0fd48454881caed9a1af66 @mental committed Dec 31, 2010
Showing with 74 additions and 3 deletions.
  1. +54 −0 spec/ActorSpec.coffee
  2. +20 −3 src/Actor.coffee
View
54 spec/ActorSpec.coffee
@@ -449,13 +449,67 @@ describe "WebActors routing", ->
runs -> expect(received).toEqual([["foo:0", "link", actor_id],
["foo:0", "unlink", actor_id]])
+describe "WebActors.setDefaultGateway", ->
+ afterEach ->
+ WebActors.setDefaultGateway null
+ WebActors.unregisterGateway "foo"
+
+ it "should affect routing for unknown prefixes", ->
+ received = []
+
+ WebActors.registerGateway "foo", (args...) ->
+ received.push ["gateway"].concat(args)
+ WebActors.setDefaultGateway (args...) ->
+ received.push ["default"].concat(args)
+
+ WebActors.send "#{WebActors.getLocalPrefix()}:bogus", "abc"
+ WebActors.send "foo:0", "def"
+ WebActors.send "bar:0", "ghi"
+
+ waitsFor -> received.length > 1
+
+ runs ->
+ expect(received).toEqual([["gateway", "foo:0", "send", "def"],
+ ["default", "bar:0", "send", "ghi"]])
+
+ it "should restore default behavior when null is passed", ->
+ WebActors.setDefaultGateway (args...) ->
+ WebActors.setDefaultGateway null
+
+ passed = false
+
+ WebActors.spawn ->
+ WebActors.trapKill WebActors.sendback()
+ WebActors.link "junk:bogus"
+ WebActors.receive ["junk:bogus", WebActors.ANY], ->
+ passed = true
+
+ waitsFor -> passed
+
describe "WebActors.getLocalPrefix", ->
it "should return the prefix being used for local actors", ->
actor_id = WebActors.spawn ->
local_prefix = WebActors.getLocalPrefix()
local_prefix = "#{local_prefix}:"
expect(actor_id.substr(0, local_prefix.length)).toEqual(local_prefix)
+describe "WebActors.setLocalPrefix", ->
+ afterEach ->
+ WebActors.setLocalPrefix "actor"
+
+ it "should change the prefix used for local actors", ->
+ ready = false
+ local_prefix = "foo"
+ WebActors.setLocalPrefix local_prefix
+ actor_id = WebActors.spawn ->
+ ready = true
+
+ waitsFor -> ready
+
+ runs ->
+ local_prefix = "#{local_prefix}:"
+ expect(actor_id.substr(0, local_prefix.length)).toEqual(local_prefix)
+
describe "WebActors.allocateChildPrefix", ->
it "should return a prefix based on a key and the local prefix", ->
key = "zorg"
View
23 src/Actor.coffee
@@ -140,10 +140,14 @@ class ForwardingActor
next_actor_serial = 0
actors_by_id = {}
local_prefix = "actor:"
+default_gateway = null
gateways_by_prefix = {}
getLocalPrefix = -> local_prefix.substr(0, local_prefix.length-1)
+setLocalPrefix = (prefix) ->
+ local_prefix = "#{prefix}:"
+
alloc_actor_id = ->
"#{local_prefix}#{next_actor_serial++}"
@@ -154,16 +158,24 @@ lookup_actor = (actor_id) ->
actor = actors_by_id[actor_id]
return actor if actor
longest_prefix = ""
+ if actor_id.substr(0, local_prefix.length) is local_prefix
+ longest_prefix = local_prefix
for prefix, callback of gateways_by_prefix
# prefixes in the map include the trailing ':' separator
if actor_id.substr(0, prefix.length) is prefix
if prefix.length > longest_prefix.length
longest_prefix = prefix
if longest_prefix.length > 0
- callback = gateways_by_prefix[longest_prefix]
- return new ForwardingActor(actor_id, callback)
+ if longest_prefix is local_prefix
+ return new DeadActor(actor_id)
+ else
+ callback = gateways_by_prefix[longest_prefix]
+ return new ForwardingActor(actor_id, callback)
else
- return new DeadActor(actor_id)
+ if default_gateway
+ return new ForwardingActor(actor_id, default_gateway)
+ else
+ return new DeadActor(actor_id)
register_actor = (actor_id, actor) ->
actors_by_id[actor_id] = actor
@@ -180,6 +192,9 @@ unregisterGateway = (prefix) ->
prefix = "#{prefix}:"
delete gateways_by_prefix[prefix]
+setDefaultGateway = (callback) ->
+ default_gateway = callback
+
spawn = (body) ->
actor_id = alloc_actor_id()
actor = new LocalActor(actor_id)
@@ -258,5 +273,7 @@ injectEvent = (actor_id, verb, args...) ->
@WebActors.injectEvent = injectEvent
@WebActors.registerGateway = registerGateway
@WebActors.unregisterGateway = unregisterGateway
+@WebActors.setDefaultGateway = setDefaultGateway
@WebActors.getLocalPrefix = getLocalPrefix
+@WebActors.setLocalPrefix = setLocalPrefix
@WebActors.allocateChildPrefix = allocateChildPrefix

0 comments on commit 0003dc1

Please sign in to comment.
Something went wrong with that request. Please try again.