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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pb_actor/proxy.rb', line 51

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



87
88
89
90
# File 'lib/pb_actor/proxy.rb', line 87

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

Instance Method Details

#asyncObject



92
93
94
# File 'lib/pb_actor/proxy.rb', line 92

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

#futureObject



96
97
98
# File 'lib/pb_actor/proxy.rb', line 96

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

#terminateObject



100
101
102
103
104
# File 'lib/pb_actor/proxy.rb', line 100

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

#terminate!Object



106
107
108
109
110
# File 'lib/pb_actor/proxy.rb', line 106

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