Class: GQTP::Client
- Inherits:
-
Object
- Object
- GQTP::Client
- Defined in:
- lib/gqtp/client.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the opened connection.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #read(&block) ⇒ Object
- #send(body, options = {}, &block) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
25 26 27 28 29 30 31 |
# File 'lib/gqtp/client.rb', line 25 def initialize(={}) @options = .dup @options[:host] ||= @options[:address] || "127.0.0.1" @options[:port] ||= 10043 @backend = create_backend @close_requesting = false end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
24 25 26 |
# File 'lib/gqtp/client.rb', line 24 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
24 25 26 |
# File 'lib/gqtp/client.rb', line 24 def port @port end |
Instance Method Details
#close ⇒ true #close({}) { ... } ⇒ #wait
Closes the opened connection. You can’t send a new request after this method is called.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/gqtp/client.rb', line 99 def close sync = !block_given? sequential_request = SequentialRequest.new if @backend quit_request = send("quit", :header => header_for_close) do yield if block_given? end sequential_request << quit_request end if sync sequential_request.wait true else sequential_request end end |
#read(&block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gqtp/client.rb', line 56 def read(&block) sync = !block_given? parser = Parser.new response_body = nil sequential_request = SequentialRequest.new read_header_request = @backend.read(Header.size) do |header| parser << header read_body_request = @backend.read(parser.header.size) do |body| response_body = body if @close_requesting @backend.close @backend = nil end 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/gqtp/client.rb', line 33 def send(body, ={}, &block) header = [:header] || Header.new header.size = body.bytesize case body when /\A(?:shutdown|quit)(?:\z|\s)/ @close_requesting = true when /\A\/d\/(?:shutdown|quit)(?:\z|\?)/ @close_requesting = true end if block_given? sequential_request = SequentialRequest.new write_request = @backend.write(header.pack, body) do sequential_request << read(&block) end sequential_request << write_request sequential_request else @backend.write(header.pack, body) end end |