Class: GameMachine::Actor::Ref

Inherits:
Object
  • Object
show all
Defined in:
lib/game_machine/actor/ref.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_or_actor_ref, metric_name = nil) ⇒ Ref

Returns a new instance of Ref.



6
7
8
9
# File 'lib/game_machine/actor/ref.rb', line 6

def initialize(path_or_actor_ref,metric_name=nil)
  @path_or_actor_ref = path_or_actor_ref
  @metric_name = metric_name
end

Instance Method Details

#actorObject



23
24
25
# File 'lib/game_machine/actor/ref.rb', line 23

def actor
  @actor ||= @path_or_actor_ref.is_a?(JavaLib::ActorRef) ? @path_or_actor_ref : actor_selection
end

#ask(message, timeout) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/game_machine/actor/ref.rb', line 37

def ask(message,timeout)
  message = convert_if_model(message)
  duration = duration_in_ms(timeout)
  t = JavaLib::Timeout.new(duration)
  if actor.is_a?(JavaLib::ActorSelection)
    sel = JavaLib::AskableActorSelection.new(actor)
    future = sel.ask(message,t)
  else
    future = JavaLib::Patterns::ask(actor,message,t)
  end
  JavaLib::Await.result(future, duration)
rescue Java::JavaUtilConcurrent::TimeoutException => e
  GameMachine.logger.debug("TimeoutException caught in ask (timeout = #{timeout})")
  false
end

#pathObject



27
28
29
# File 'lib/game_machine/actor/ref.rb', line 27

def path
  @path_or_actor_ref.is_a?(String) ? @path_or_actor_ref : nil
end

#send_message(message, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/game_machine/actor/ref.rb', line 11

def send_message(message,options={})
  options = default_options.merge(options)
  sender = sender_for(options[:sender])

  if options[:blocking]
    ask(message,options[:timeout])
  else
    tell(message,sender)
    true
  end
end

#tell(message, sender = nil) ⇒ Object



31
32
33
34
35
# File 'lib/game_machine/actor/ref.rb', line 31

def tell(message,sender=nil)
  message = convert_if_model(message)
  actor.tell(message,sender_for(sender))
  true
end