Class: UState::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
# File 'lib/ustate/client.rb', line 17

def initialize(opts = {})
  @host = opts[:host] || HOST
  @port = opts[:port] || PORT
  @locket = Mutex.new
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



15
16
17
# File 'lib/ustate/client.rb', line 15

def host
  @host
end

#portObject

Returns the value of attribute port.



15
16
17
# File 'lib/ustate/client.rb', line 15

def port
  @port
end

#socketObject

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

  message = UState::Message.new :states => [state]

  # Transmit
  with_connection do |s|
    s << message.encode_with_length
    read_message 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

#closeObject



54
55
56
57
58
# File 'lib/ustate/client.rb', line 54

def close
  @locket.synchronize do
    @socket.close
  end
end

#connectObject



50
51
52
# File 'lib/ustate/client.rb', line 50

def connect
  @socket = TCPSocket.new(@host, @port)
end

#connected?Boolean

Returns:

  • (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)
  message = UState::Message.new query: UState::Query.new(string: string)
  with_connection do |s|
    s << message.encode_with_length
    read_message 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 read_message(s)
  if buffer = s.read(4) and buffer.size == 4
    length = buffer.unpack('N').first
    begin
      str = s.read length
      message = UState::Message.decode str
    rescue => e
      puts "Message was #{str.inspect}"
      raise
    end
    
    unless message.ok
      puts "Failed"
      raise ServerError, message.error
    end
    
    message
  else
    raise InvalidResponse, "unexpected EOF"
  end
end

#with_connectionObject

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