Class: Riemann::Client::TCP
- Inherits:
-
Riemann::Client
- Object
- Riemann::Client
- Riemann::Client::TCP
- Defined in:
- lib/riemann/client/tcp.rb
Constant Summary
Constants inherited from Riemann::Client
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#socket ⇒ Object
Returns the value of attribute socket.
Attributes inherited from Riemann::Client
Class Method Summary collapse
-
.socket_factory ⇒ Object
Public: Return a socket factory.
-
.socket_factory=(factory) ⇒ Object
Public: Set a socket factory – an object responding to #call(options) that returns a Socket object.
Instance Method Summary collapse
- #close ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(options = {}) ⇒ TCP
constructor
A new instance of TCP.
-
#read_message(s) ⇒ Object
Read a message from a stream.
- #send_recv(message) ⇒ Object (also: #send_maybe_recv)
-
#with_connection ⇒ Object
Yields a connection in the block.
Methods inherited from Riemann::Client
#<<, #[], #connect, #query, #timeout
Constructor Details
#initialize(options = {}) ⇒ TCP
Returns a new instance of TCP.
20 21 22 23 |
# File 'lib/riemann/client/tcp.rb', line 20 def initialize( = {}) @options = @locket = Monitor.new end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
7 8 9 |
# File 'lib/riemann/client/tcp.rb', line 7 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
7 8 9 |
# File 'lib/riemann/client/tcp.rb', line 7 def port @port end |
#socket ⇒ Object
Returns the value of attribute socket.
7 8 9 |
# File 'lib/riemann/client/tcp.rb', line 7 def socket @socket end |
Class Method Details
.socket_factory ⇒ Object
Public: Return a socket factory
16 17 18 |
# File 'lib/riemann/client/tcp.rb', line 16 def self.socket_factory @socket_factory || proc { || TcpSocket.connect() } end |
.socket_factory=(factory) ⇒ Object
Public: Set a socket factory – an object responding to #call(options) that returns a Socket object
11 12 13 |
# File 'lib/riemann/client/tcp.rb', line 11 def self.socket_factory=(factory) @socket_factory = factory end |
Instance Method Details
#close ⇒ Object
40 41 42 43 44 45 |
# File 'lib/riemann/client/tcp.rb', line 40 def close @locket.synchronize do @socket.close if connected? @socket = nil end end |
#connected? ⇒ Boolean
47 48 49 50 51 |
# File 'lib/riemann/client/tcp.rb', line 47 def connected? @locket.synchronize do !@socket.nil? && !@socket.closed? end end |
#read_message(s) ⇒ Object
Read a message from a stream
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/riemann/client/tcp.rb', line 54 def (s) if buffer = s.read(4) and buffer.size == 4 length = buffer.unpack('N').first begin str = s.read length = Riemann::Message.decode str rescue => e puts "Message was #{str.inspect}" raise end unless .ok puts "Failed" raise ServerError, .error end else raise InvalidResponse, "unexpected EOF" end end |
#send_recv(message) ⇒ Object Also known as: send_maybe_recv
76 77 78 79 80 81 |
# File 'lib/riemann/client/tcp.rb', line 76 def send_recv() with_connection do |s| s.write(.encode_with_length) (s) end end |
#with_connection ⇒ Object
Yields a connection in the block.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/riemann/client/tcp.rb', line 86 def with_connection tries = 0 @locket.synchronize do begin tries += 1 yield(socket) rescue IOError, Errno::EPIPE, Errno::ECONNREFUSED, InvalidResponse, Timeout::Error, Riemann::Client::TcpSocket::Error close raise if tries > 3 retry rescue Exception close raise end end end |