Class: Vorhees::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/vorhees/client.rb

Defined Under Namespace

Modules: MessageList, MessageString

Constant Summary collapse

SystemTimer =
Timeout
GOT_NOTHING =
nil
GOT_DATA =
1
GOT_MESSAGE =
2
@@defaults =
{
  :timeout => 0.06,
  :eof     => "\n",
  :key     => 'command',
  :host    => 'localhost',
  :port    => 80,
  :bufsize => 1024
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



61
62
63
64
65
66
67
# File 'lib/vorhees/client.rb', line 61

def initialize(options={})    
  @options  = Client.defaults.merge(options.symbolize_keys!)
  @socket   = TCPSocket.new(options[:host], options[:port])
  @env      = {}
  @buffer   = ''
  clear
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



23
24
25
# File 'lib/vorhees/client.rb', line 23

def buffer
  @buffer
end

#envObject

Returns the value of attribute env.



23
24
25
# File 'lib/vorhees/client.rb', line 23

def env
  @env
end

#optionsObject

Returns the value of attribute options.



23
24
25
# File 'lib/vorhees/client.rb', line 23

def options
  @options
end

#receivedObject Also known as: messages

Returns the value of attribute received.



23
24
25
# File 'lib/vorhees/client.rb', line 23

def received
  @received
end

#sentObject

Returns the value of attribute sent.



23
24
25
# File 'lib/vorhees/client.rb', line 23

def sent
  @sent
end

#socketObject

Returns the value of attribute socket.



23
24
25
# File 'lib/vorhees/client.rb', line 23

def socket
  @socket
end

Class Method Details

.const_missing(k) ⇒ Object



50
51
52
53
54
55
# File 'lib/vorhees/client.rb', line 50

def self.const_missing k
  if k =~ /^DEFAULT_(.*)$/
    @@defaults[$1.to_s.downcase.to_sym]
  else super
  end
end

.defaultsObject

cattr_accessor :defaults



37
38
39
# File 'lib/vorhees/client.rb', line 37

def self.defaults
  @@defaults
end

.set_defaults(options = {}) ⇒ Object



57
58
59
# File 'lib/vorhees/client.rb', line 57

def self.set_defaults(options={})    
  defaults.merge! options.symbolize_keys!
end

Instance Method Details

#clearObject



177
178
179
180
181
# File 'lib/vorhees/client.rb', line 177

def clear
  @sent     = [].extend MessageList
  @received = [].extend MessageList
  # @buffer   = ""
end

#connected?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/vorhees/client.rb', line 183

def connected?
  !disconnected?
end

#consumeObject



171
172
173
174
175
# File 'lib/vorhees/client.rb', line 171

def consume
  v = received.parse
  @received = [].extend MessageList
  v
end

#consume_messageObject



167
168
169
# File 'lib/vorhees/client.rb', line 167

def consume_message
  received.shift.parse rescue nil
end

#discard_responses_until(value, opts = {}) ⇒ Object

FIXME use wait_for, clean this up



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/vorhees/client.rb', line 127

def discard_responses_until(value, opts={})
  opts = options.merge(opts)
  SystemTimer.timeout(opts[:timeout]) do
    loop do
      wait_for_response opts do |msg|
        if msg && msg[Client::DEFAULT_KEY] == value
          return msg
        else
          opts[:debug] ? p(received.shift) : received.shift
        end
      end        
    end
  end
end

#disconnected?Boolean

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
201
202
# File 'lib/vorhees/client.rb', line 193

def disconnected?
  wait_for :timeout => 0.1 do
    begin
      return socket && socket.eof?
    rescue Errno::ECONNRESET
      return true
    end
  end
  return false
end

#eofObject



69
70
71
# File 'lib/vorhees/client.rb', line 69

def eof
  options[:eof]
end

#request_policy_fileObject

for flash XMLSocket



188
189
190
191
# File 'lib/vorhees/client.rb', line 188

def request_policy_file
  @socket.print "<policy-file-request/>\0"
  self
end

#send_data(data) ⇒ Object



100
101
102
103
104
105
# File 'lib/vorhees/client.rb', line 100

def send_data(data)
  data = data.chomp(options[:eof]) + eof
  sent << data
  socket.print data
  socket.flush
end

#send_json(hash) ⇒ Object



96
97
98
# File 'lib/vorhees/client.rb', line 96

def send_json hash
  send_data hash.to_json # JSON.unparse(hash)
end

#send_message(*args) ⇒ Object



90
91
92
93
94
# File 'lib/vorhees/client.rb', line 90

def send_message *args
  values = args.last.is_a?(Hash) ? args.pop : {}
  values[options[:key]] = args.shift if args.first.is_a?(String)
  send_json values
end

#sends(*args) ⇒ Object

client.sends ‘ERROR’, :message => ‘INVALID_RECORD’

> ‘“message”:“INVALID_RECORD”’



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vorhees/client.rb', line 77

def sends *args
  if args.last.is_a?(Hash)
    send_message(*args)
  else
    begin
      JSON.unparse(args.flatten.first)
      send_data(*args)
    rescue
      send_message(*args)
    end
  end
end

#wait_for(opts = {}, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/vorhees/client.rb', line 142

def wait_for opts={}, &block
  opts = options.merge(opts)
  assertion_failed = nil
  test = lambda do
    begin
      yield
    rescue Spec::Expectations::ExpectationNotMetError => e
      assertion_failed = true
      # if there's a background server running, now is a good time
      # to let it do it's thing.        
      Thread.pass
      retry
    end         
  end
  SystemTimer.timeout(opts[:timeout]) do
    until test.call do
      receive_data options
    end
  end    
rescue Timeout::Error
ensure
  # let the error be thrown one more time, this time it won't be caught
  yield if assertion_failed
end

#wait_for_response(opts = {}) {|response.first && response.first.parse| ... } ⇒ Object Also known as: response

Yields:



118
119
120
121
122
123
# File 'lib/vorhees/client.rb', line 118

def wait_for_response opts={}
  opts = options.merge(opts)
  response = wait_for_responses opts
  yield(response.first && response.first.parse) if block_given?
  response.first
end

#wait_for_responses(opts = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/vorhees/client.rb', line 107

def wait_for_responses(opts={})
  wait_for opts do
    if options[:exactly]
      received.length == options[:exactly]
    else
      received.length >=(options[:at_least] || 1)        
    end
  end
  received
end