Class: Nobject

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

Overview

this class wraps a local object, listens for method invocations over the network, and sends the method invocations onwards to the local object.

Defined Under Namespace

Classes: NoMethodError

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Nobject

Returns a new instance of Nobject.



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

def initialize(socket)
  @socket = socket
  obj_size = @socket.recv(8).unpack('Q>').first
  @obj = Marshal.load(@socket.recv(obj_size))
end

Instance Method Details

#network_return(data) ⇒ Object



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

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



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

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]) #local_method, *msg[:args])
      network_return([
        :ok,
        result
      ])
    end
  end
end