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
12
# File 'lib/nobject/remote.rb', line 6

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

Instance Method Details

#network_return(data) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/nobject/remote.rb', line 30

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

  @socket.send([data_bytes.length].pack('Q>'), 0)
  File.open('/tmp/nobject.log', 'a') {|f| f.puts "    RMResult:##{@msg_counter += 1} sz#{data_bytes.length}"; f.flush }
  @socket.send(data_bytes, 0)
  @socket.flush
end

#serve!Object



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

def serve!
  Thread.new do
    loop do
      msg_size = @socket.recv(8).unpack('Q>').first
      File.open('/tmp/nobject.log', 'a') {|f| f.puts "  RMR:##{@msg_counter += 1} sz#{msg_size}"; f.flush }
      msg = Marshal.load(@socket.recv(msg_size))

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