Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Store names in actors when they're registered

  • Loading branch information...
commit 62f219250e082b4f8969730811f42002f12d2b7e 1 parent 02d7525
@tarcieri tarcieri authored
View
13 lib/celluloid/actor.rb
@@ -20,7 +20,7 @@ def initialize(cause)
# messages.
class Actor
extend Registry
- attr_reader :proxy, :tasks, :links, :mailbox
+ attr_reader :proxy, :tasks, :links, :mailbox, :name
class << self
# Obtain the current actor
@@ -30,6 +30,13 @@ def current
actor.proxy
end
+ # Obtain the name of the current actor
+ def name
+ actor = Thread.current[:actor]
+ raise NotActorError, "not in actor scope" unless actor
+ actor.name
+ end
+
# Invoke a method on the given actor via its mailbox
def call(mailbox, meth, *args, &block)
call = SyncCall.new(Thread.mailbox, meth, args, block)
@@ -95,6 +102,7 @@ def initialize(subject)
@timers = Timers.new
@running = true
@exclusive = false
+ @name = nil
@thread = InternalPool.get do
Thread.current[:actor] = self
@@ -145,6 +153,9 @@ def run
rescue ExitEvent => exit_event
Task.new(:exit_handler) { handle_exit_event exit_event }.resume
retry
+ rescue NamingRequest => ex
+ @name = ex.name
+ retry
rescue TerminationRequest
break
end
View
9 lib/celluloid/events.rb
@@ -12,6 +12,15 @@ def initialize(actor, reason = nil)
end
end
+ # Name an actor at the time it's registered
+ class NamingRequest < SystemEvent
+ attr_reader :name
+
+ def initialize(name)
+ @name = name
+ end
+ end
+
# Request for an actor to terminate
class TerminationRequest < SystemEvent; end
end
View
2  lib/celluloid/registry.rb
@@ -16,6 +16,8 @@ def []=(name, actor)
@@registry_lock.synchronize do
@@registry[name.to_sym] = actor
end
+
+ actor.mailbox << NamingRequest.new(name.to_sym)
end
# Retrieve an actor by name
View
11 spec/celluloid/registry_spec.rb
@@ -7,6 +7,10 @@ class Marilyn
def sing_for(person)
"o/~ Happy birthday, #{person}"
end
+
+ def whats_your_name
+ Actor.name
+ end
end
it "registers Actors" do
@@ -21,7 +25,12 @@ def sing_for(person)
end
it "lists all registered actors" do
- Celluloid::Actor[:marilyn] = Marilyn.spawn
+ Celluloid::Actor[:marilyn] = Marilyn.new
Celluloid::Actor.registered.should include :marilyn
end
+
+ it "knows its name once registered" do
+ Celluloid::Actor[:marilyn] = Marilyn.new
+ Celluloid::Actor[:marilyn].whats_your_name.should == :marilyn
+ end
end

5 comments on commit 62f2192

@benlangfeld
Collaborator

Is it really necessary to use exceptions for this sort of thing? It's not exactly exceptional...

@tarcieri
Owner

Exceptions are how system messages are implemented at present, for better or for worse...

@tarcieri
Owner

It might be a good idea, prior to Celluloid 1.0, to look for a way to implement system messages such that they aren't exceptions

@benlangfeld
Collaborator

I'll do that some time in the next few days :)

@tarcieri
Owner

Cool

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