Class: Nobject::Remote

Inherits:
Object
  • Object
show all
Defined in:
lib/nobject/remote.rb

Overview

this class is used by the Nobject::Server to receive objects pushed to the Nobject::Server, listens for method invocations over the network, and sends the method invocations onwards to this object

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Remote

Returns a new instance of Remote.



6
7
8
9
10
11
# File 'lib/nobject/remote.rb', line 6

def initialize(socket)
  @socket = socket
  obj_size = @socket.recv(8).unpack('Q>').first
  File.open('/tmp/nobject.log', 'w') {|f| f.puts "R:#{obj_size}"; f.flush }
  @obj = Marshal.load(@socket.recv(obj_size))
end

Instance Method Details

#network_return(data) ⇒ Object



28
29
30
31
32
33
# File 'lib/nobject/remote.rb', line 28

def network_return(data)
  data_bytes = Marshal.dump(data)

  @socket.send([data_bytes.length].pack('Q>'), 0)
  @socket.send(data_bytes, 0)
end

#serve!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nobject/remote.rb', line 13

def serve!
  Thread.new do
    loop do
      msg_size = @socket.recv(8).unpack('Q>').first
      msg = Marshal.load(@socket.recv(msg_size))

      result = @obj.send(msg[:method], *msg[:args])
      network_return([
        :ok,
        result
      ])
    end
  end
end