Class: UState::Client
- Inherits:
-
Object
- Object
- UState::Client
- Defined in:
- lib/ustate/client.rb
Defined Under Namespace
Classes: Error, InvalidResponse, Query, ServerError
Constant Summary collapse
- HOST =
'127.0.0.1'
- PORT =
55956
- TYPE_STATE =
1
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.
Instance Method Summary collapse
-
#<<(state_opts) ⇒ Object
Send a state.
-
#[](query) ⇒ Object
Returns an array of states matching query.
- #close ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#query(string = nil) ⇒ Object
Ask for states.
-
#read_message(s) ⇒ Object
Read a message from a stream.
-
#with_connection ⇒ Object
Yields a connection in the block.
Constructor Details
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
15 16 17 |
# File 'lib/ustate/client.rb', line 15 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
15 16 17 |
# File 'lib/ustate/client.rb', line 15 def port @port end |
#socket ⇒ Object
Returns the value of attribute socket.
15 16 17 |
# File 'lib/ustate/client.rb', line 15 def socket @socket end |
Instance Method Details
#<<(state_opts) ⇒ Object
Send a state
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ustate/client.rb', line 24 def <<(state_opts) # Create state case state_opts when UState::State state = state_opts else unless state_opts.include? :host state_opts[:host] = Socket.gethostname end state = UState::State.new(state_opts) end = UState::Message.new :states => [state] # Transmit with_connection do |s| s << .encode_with_length s end end |
#[](query) ⇒ Object
Returns an array of states matching query.
46 47 48 |
# File 'lib/ustate/client.rb', line 46 def [](query) query(query).states || [] end |
#close ⇒ Object
54 55 56 57 58 |
# File 'lib/ustate/client.rb', line 54 def close @locket.synchronize do @socket.close end end |
#connect ⇒ Object
50 51 52 |
# File 'lib/ustate/client.rb', line 50 def connect @socket = TCPSocket.new(@host, @port) end |
#connected? ⇒ Boolean
60 61 62 |
# File 'lib/ustate/client.rb', line 60 def connected? not @socket.closed? end |
#query(string = nil) ⇒ Object
Ask for states
65 66 67 68 69 70 71 |
# File 'lib/ustate/client.rb', line 65 def query(string = nil) = UState::Message.new query: UState::Query.new(string: string) with_connection do |s| s << .encode_with_length s end end |
#read_message(s) ⇒ Object
Read a message from a stream
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ustate/client.rb', line 74 def (s) if buffer = s.read(4) and buffer.size == 4 length = buffer.unpack('N').first begin str = s.read length = UState::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 |
#with_connection ⇒ Object
Yields a connection in the block.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ustate/client.rb', line 97 def with_connection tries = 0 @locket.synchronize do begin tries += 1 yield (@socket || connect) rescue IOError => e raise if tries > 3 connect and retry rescue Errno::EPIPE => e raise if tries > 3 connect and retry rescue Errno::ECONNREFUSED => e raise if tries > 3 connect and retry rescue Errno::ECONNRESET => e raise if tries > 3 connect and retry rescue InvalidResponse => e raise if tries > 3 connect and retry end end end |