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
# 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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Instance Method Details

#asyncObject



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

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

#futureObject



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

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

#terminateObject



57
58
59
60
61
# File 'lib/pb_actor/proxy.rb', line 57

def terminate
  Message.send [:terminate], @wr
  Process.wait @pid
  nil
end

#terminate!Object



63
64
65
66
67
# File 'lib/pb_actor/proxy.rb', line 63

def terminate!
  Process.kill "KILL", @pid
  Process.wait @pid
  nil
end