Class: Nobject::Local

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

Overview

this class is used by the client application, wraps a local object, pushes it to a Nobject::Serve4r, which will then send method calls to a matching Nobject::Remote object

Defined Under Namespace

Classes: InvalidMethod, MethodFailure, UnknownReturnDataType

Instance Method Summary collapse

Constructor Details

#initialize(host, port, obj) ⇒ Local

host: the hostname of the server to push obj to port: the port number of the server to push obj to obj: the obj to store over the network

ex:

# this will create a new Nobject::Local, then push it to the specified
server Nobject::Local.new('localhost', 1234, <object>)


15
16
17
18
19
20
21
# File 'lib/nobject/local.rb', line 15

def initialize(host, port, obj)
  @socket = TCPSocket.new(host, port)
  obj_bytes = Marshal.dump(obj)

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nobject/local.rb', line 23

def method_missing(method, *args, **kwargs, &block)
  msg = { method: method, args: args }
  msg_bytes = Marshal.dump(msg)

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

  return_size = begin
                  @socket.recv(8).unpack('Q>').first
                rescue Exception
                  raise Local::MethodFailure.new("failed to call method `#{method}' over the network")
                end

  return_data = Marshal.load(@socket.recv(return_size))

  case return_data.first
  when :ok then return_data.last
  when :raise then raise return_data.last
  else
    raise Local::UnknownReturnDataType.new("unknown data type '#{return_data.first}' within Nobject::Local (Nobject::Local::UnknownReturnDataType)")
  end
end

Instance Method Details

#!~(other) ⇒ Object

method overridden from Object class



49
# File 'lib/nobject/local.rb', line 49

def !~(other);      method_missing(:is_a?, other);  end

#<=>(other) ⇒ Object



50
# File 'lib/nobject/local.rb', line 50

def <=>(other);     method_missing(:<=>, other);    end

#===(other) ⇒ Object



51
# File 'lib/nobject/local.rb', line 51

def ===(other);     method_missing(:<=>, other);    end

#inspectObject



53
# File 'lib/nobject/local.rb', line 53

def inspect;        method_missing(:inspect);       end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


52
# File 'lib/nobject/local.rb', line 52

def is_a?(klass);   method_missing(:is_a?, klass);  end

#to_sObject



54
# File 'lib/nobject/local.rb', line 54

def to_s;           method_missing(:to_s);          end