Class: NetworkFacade::Base::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/network-facade/base.rb

Direct Known Subclasses

REST::Client, SSL::Client, TCP::Client, Unix::Client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/network-facade/base.rb', line 42

def initialize(options = {})
  @options = options
  if @options[:uri]
    @uri ||= URI.parse(@options[:uri])
  elsif @@klass[self.class]
    @uri ||= @@klass[self.class]
  else
    @uri ||= URI::Generic.new('nf', nil, nil, nil, nil, '/' + self.class.name.downcase, nil, nil, nil)
  end
  @uri.host = @options[:host] if @options[:host]
  @uri.port = @options[:port] if @options[:port]
  @uri.path = @options[:path] if @options[:path]
  @uri.query = @options[:query] if @options[:query]
  @uri.userinfo = @options[:userinfo] if @options[:userinfo]
  require 'zlib' if @options[:compress]
  require 'thread' if @options[:mode] == :thread
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/network-facade/base.rb', line 60

def method_missing(name, *args)
  NetworkFacade.log(:debug, "Method called with #{args.inspect}", "#{self.class}##{name}")
  begin
    __write__([@uri.path[1..-1], name, args])
    result = __read__
    if result.is_a? Exception
      NetworkFacade.log(:info, "Exception occured : #{result.inspect}", "#{self.class}##{name}")
      result.backtrace.collect! do |line|
        @uri.to_s + '/' + line
      end
      raise result
    end
    result
  rescue Errno::EPIPE, EOFError, Errno::EINVAL, Errno::ECONNRESET
    NetworkFacade.log(:warn, "#{$!.inspect} occured", "#{self.class}##{name}")
    # connect
    # retry
  end
end

Class Method Details

.inherited(klass) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/network-facade/base.rb', line 31

def self.inherited(klass)
  @@klass ||= {}
  if defined? @@uri and not @@uri.nil?
    @@klass[klass] = URI.parse(@@uri)
    if @@klass[klass].path.empty? or @@klass[klass].path == '/'
      @@klass[klass].path = '/' + klass.name.downcase
    end
  end
  @@uri = nil
end

.uri=(uri) ⇒ Object



27
28
29
# File 'lib/network-facade/base.rb', line 27

def self.uri=(uri)
  @@uri = uri
end

Instance Method Details

#__read__Object



80
81
82
83
84
85
86
# File 'lib/network-facade/base.rb', line 80

def __read__
  size = @client.read(4).unpack('N').first
  NetworkFacade.log(:debug, "Read #{size + 4} bytes", @uri)
  data = @client.read(size)
  data = Zlib::Inflate.inflate(data) if @options[:compress]
  Marshal.load(data)
end

#__write__(data) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/network-facade/base.rb', line 88

def __write__(data)
  data = Marshal.dump(data)
  data = Zlib::Deflate.deflate(data, Zlib::FINISH) if @options[:compress]
  NetworkFacade.log(:debug, "Write #{data.size + 4} bytes", @uri)
  size = [data.size].pack('N')
  @client.write(size)
  @client.write(data)
  @client.flush
end