Class: GQTP::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
29
# File 'lib/gqtp/client.rb', line 24

def initialize(options={})
  @options = options.dup
  @options[:address] ||= "127.0.0.1"
  @options[:port] ||= 10041
  @connection = create_connection
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



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

def address
  @address
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#closeObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gqtp/client.rb', line 67

def close
  sync = !block_given?
  ack_request = nil
  quit_request = send("quit", :header => header_for_close) do
    ack_request = send("ACK", :header => header_for_close) do
      @connection.close
      yield if block_given?
    end
  end
  if sync
    quit_request.wait
    ack_request.wait
  end
end

#read(&block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gqtp/client.rb', line 48

def read(&block)
  sync = !block_given?
  parser = Parser.new
  response_body = nil
  read_body_request = nil
  read_header_request = @connection.read(Header.size) do |header|
    parser << header
    read_body_request = @connection.read(parser.header.size) do |body|
      response_body = body
      yield(parser.header, response_body) if block_given?
    end
  end
  if sync
    read_header_request.wait
    read_body_request.wait
    [parser.header, response_body]
  end
end

#send(body, options = {}, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gqtp/client.rb', line 31

def send(body, options={}, &block)
  header = options[:header] || Header.new
  header.size = body.bytesize

  write_request = @connection.write(header.pack, body) do
    if block_given?
      read(&block)
    end
  end

  if block_given?
    write_request
  else
    write_request.wait
  end
end