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.



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

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.



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

def address
  @address
end

#portObject

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#closeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gqtp/client.rb', line 72

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
66
67
68
69
70
# File 'lib/gqtp/client.rb', line 48

def read(&block)
  sync = !block_given?
  parser = Parser.new
  response_body = nil

  sequential_request = SequentialRequest.new
  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
    sequential_request << read_body_request
  end
  sequential_request << read_header_request

  if sync
    sequential_request.wait
    [parser.header, response_body]
  else
    sequential_request
  end
end

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



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

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

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