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.



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

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]
	@mutex = Mutex.new
	require 'zlib' if @options[:compress]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



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

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, re-connecting...", "#{self.class}##{name}")
		connect
		retry
	end
end

Class Method Details

.inherited(klass) ⇒ Object



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

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



25
26
27
# File 'lib/network-facade/base.rb', line 25

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

Instance Method Details

#__read__Object



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

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



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

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