Class: PbActor::Proxy

Inherits:
BasicProxy show all
Defined in:
lib/pb_actor/proxy.rb

Instance Method Summary collapse

Methods inherited from BasicProxy

#alive?, #to_s

Constructor Details

#initialize(origin) ⇒ Proxy

Returns a new instance of Proxy.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pb_actor/proxy.rb', line 8

def initialize origin
  @origin = origin
  pr, cw = IO.pipe
  cr, pw = IO.pipe
  @pid = fork do
    [pr, pw].each &:close
    @future_values = {}
    loop do
      type, id, method, *args = begin
                                  Message.recv cr
                                rescue EOFError => e
                                  [:terminate]
                                end
      case type
      when :async_method_call
        @origin.public_send method, *args
      when :future_method_call
        @future_values[id] = @origin.public_send method, *args
      when :future_value_get
        Message.send(if @future_values.has_key? id
          [:future_value, @future_values.delete(id)]
        else
          [:no_value]
        end, cw)
      when :terminate
        exit
      else
        raise "what happend!? receive #{type.inspect}"
      end
    end
  end
  [cr, cw].each &:close
  @rd = pr
  @wr = pw
  @alive = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



45
46
47
48
# File 'lib/pb_actor/proxy.rb', line 45

def method_missing method, *args, &blk
  super
  future.method_missing(method, *args).value
end

Instance Method Details

#asyncObject



50
51
52
# File 'lib/pb_actor/proxy.rb', line 50

def async
  @async ||= AsyncProxy.new @origin, @pid, @wr, @rd
end

#futureObject



54
55
56
# File 'lib/pb_actor/proxy.rb', line 54

def future
  @future ||= FutureProxy.new @origin, @pid, @wr, @rd
end

#terminateObject



58
59
60
61
62
63
64
65
# File 'lib/pb_actor/proxy.rb', line 58

def terminate
  raise DeadActorError, PbActor.dead_actor_msg unless alive?
  Message.send [:terminate], @wr
  Process.wait @pid
  nil
rescue Errno::ECHILD => e
  raise DeadActorError, PbActor.dead_actor_msg
end

#terminate!Object



67
68
69
70
71
72
73
74
# File 'lib/pb_actor/proxy.rb', line 67

def terminate!
  raise DeadActorError, PbActor.dead_actor_msg unless alive?
  Process.kill "KILL", @pid
  Process.wait @pid
  nil
rescue Errno::ECHILD, Errno::ESRCH => e
  raise DeadActorError, PbActor.dead_actor_msg
end