lib/game_machine/actor/base.rb in game_machine-0.0.11 vs lib/game_machine/actor/base.rb in game_machine-1.0.2
- old
+ new
@@ -3,11 +3,11 @@
class DuplicateHashringError < StandardError;end
class MissingHashringError < StandardError;end
# @abstract All game actors inherit fromm this class
- class Base < JavaLib::UntypedActor
+ class Base < JavaLib::GameActor
ON_RECEIVE_HOOKS = {}
@@player_controller = nil
@@ -47,29 +47,14 @@
unless Application.registered.include?(self)
Application.register(self)
end
end
- def reset_hashrings
- @@hashrings = nil
- end
-
- def hashrings
- @@hashrings ||= java.util.concurrent.ConcurrentHashMap.new
- end
-
def hashring(name)
- hashrings.fetch(name,nil)
+ JavaLib::Hashring.get_hashring(name)
end
- def add_hashring(name,hashring)
- if hashring(name)
- raise DuplicateHashringError, "name=#{name}"
- end
- hashrings[name] = hashring
- end
-
# Find a local actor by name
# @return [Actor::Ref]
def find(name=self.name)
Actor::Ref.new(local_path(name),name)
end
@@ -88,20 +73,18 @@
# Returns a local actor ref from the distributed ring of actors based
# on a consistent hashing of the id.
# @return [Actor::Ref]
def find_distributed_local(id,name=self.name)
- ensure_hashring(name)
Actor::Ref.new(local_distributed_path(id, name),name)
end
# Returns an actor ref from the distributed ring of actors based
# on a consistent hashing of the id. The actor returned can be from
# any server in the cluster
# @return [Actor::Ref]
def find_distributed(id,name=self.name)
- ensure_hashring(name)
Actor::Ref.new(distributed_path(id, name),name)
end
def local_path(name)
"/user/#{name}"
@@ -123,28 +106,22 @@
message
end
private
- def ensure_hashring(name)
- unless hashring(name)
- raise MissingHashringError
- end
- end
-
def remote_path(server,name)
- "#{Akka.address_for(server)}/user/#{name}"
+ "#{server}/user/#{name}"
end
def local_distributed_path(id,name)
- bucket = hashring(name).bucket_for(id)
- "/user/#{bucket}"
+ node = hashring(name).node_for(id)
+ "/user/#{node}"
end
def distributed_path(id,name)
- server = Akka.instance.hashring.bucket_for(id)
- bucket = hashring(name).bucket_for(id)
- "#{server}/user/#{bucket}"
+ server = Akka.instance.hashring.node_for(id)
+ node = hashring(name).node_for(id)
+ "#{server}/user/#{node}"
end
end
# This indirection is primarily because Akka's test actors