Class: FSR::EventSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/fsr/event_socket.rb

Direct Known Subclasses

CommandSocket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ EventSocket

Returns a new instance of EventSocket.



5
6
7
# File 'lib/fsr/event_socket.rb', line 5

def initialize(socket)
  @socket = socket
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



3
4
5
# File 'lib/fsr/event_socket.rb', line 3

def socket
  @socket
end

Instance Method Details

#<<(cmd) ⇒ Object

Send a command, do not return response



16
17
18
# File 'lib/fsr/event_socket.rb', line 16

def <<(cmd)
  @socket.send("#{cmd}\n\n",0)
end

#get_header_and_bodyObject

Grab result from command and create a hash, simple but works.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fsr/event_socket.rb', line 21

def get_header_and_body
  headers, body = {}, ""
  until line = @socket.gets and line.chomp.empty?
    if (kv = line.chomp.split(/:\s+/,2)).size == 2
      headers.store *kv
    end
  end
  if (content_length = headers["Content-Length"].to_i) > 0
    Log.debug "content_length is #{content_length}, grabbing from socket"
    body << @socket.read(content_length)
  end
  headers.merge("body" => body.strip)
end

#responseObject

Scrub result into a hash



36
37
38
# File 'lib/fsr/event_socket.rb', line 36

def response
  get_header_and_body
end

#say(cmd) ⇒ Object

Send a command and return response



10
11
12
13
# File 'lib/fsr/event_socket.rb', line 10

def say(cmd)
  @socket.send("#{cmd}\n\n",0)
  response
end