Skip to content

Commit

Permalink
Use "host" instead of "address"
Browse files Browse the repository at this point in the history
Because "host" is more general word rather than "address". GQTP can
accept "host name" or "IP address". "host" includes both but "address"
includes only "IP address".

This is a backward incompatible change. :address option key still
usable but it will be removed in the feature. 3rd party connection
module doesn't work.
  • Loading branch information
kou committed Jan 7, 2014
1 parent 730af93 commit 794afc9
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 58 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -22,15 +22,15 @@ for high concurrency use.

### Client

client = GQTP::Client.new(:address => "192.168.0.1", :port => 10043)
client = GQTP::Client.new(:host => "192.168.0.1", :port => 10043)
request = client.send("status") do |header, body|
p body # => "{\"alloc_count\":163,...}"
end
request.wait

### Server

server = GQTP::Server.new(:address => "192.168.0.1", :port => 10043)
server = GQTP::Server.new(:host => "192.168.0.1", :port => 10043)
server.on_request do |request, client|
body = "{\"alloc_count\":163,...}"
header = GQTP::Header.new
Expand All @@ -45,9 +45,9 @@ for high concurrency use.

### Proxy

proxy = GQTP::Proxy.new(:listen_address => "127.0.0.1",
proxy = GQTP::Proxy.new(:listen_host => "127.0.0.1",
:listen_port => 10043,
:upstream_address => "192.168.0.1",
:upstream_host => "192.168.0.1",
:upstream_port => 10043)
proxy.run.wait

Expand Down
28 changes: 14 additions & 14 deletions bin/gqtp-proxy
Expand Up @@ -22,27 +22,27 @@ require "ostruct"
require "gqtp"

options = OpenStruct.new
options.listen_address = "0.0.0.0"
options.listen_host = "0.0.0.0"
options.listen_port = 10043
options.upstream_address = nil
options.upstream_host = nil
options.upstream_port = 10043
options.backend = :thread

parser = OptionParser.new
parser.on("--listen-address=ADDRESS",
"IP address or host name to listen",
"(#{options.listen_address})") do |address|
options.listen_address = address
parser.on("--listen-host=HOST",
"IP host or host name to listen",
"(#{options.listen_host})") do |host|
options.listen_host = host
end
parser.on("--listen-port=PORT", Integer,
"Port number to listen",
"(#{options.listen_port})") do |port|
options.listen_port = port
end
parser.on("--upstream-address=ADDRESS",
"IP address or host name of upstream",
"(#{options.upstream_address})") do |address|
options.upstream_address = address
parser.on("--upstream-host=HOST",
"IP host or host name of upstream",
"(#{options.upstream_host})") do |host|
options.upstream_host = host
end
parser.on("--upstream-port=PORT", Integer,
"Port number of upstream",
Expand All @@ -64,14 +64,14 @@ rescue OptionParser::ParseError
exit(false)
end

if options.upstream_address.nil?
puts("--upstream-address is required.")
if options.upstream_host.nil?
puts("--upstream-host is required.")
exit(false)
end

proxy = GQTP::Proxy.new(:listen_address => options.listen_address,
proxy = GQTP::Proxy.new(:listen_host => options.listen_host,
:listen_port => options.listen_port,
:upstream_address => options.upstream_address,
:upstream_host => options.upstream_host,
:upstream_port => options.upstream_port,
:connection => options.backend)
proxy.run.wait
4 changes: 2 additions & 2 deletions lib/gqtp/client.rb
Expand Up @@ -21,10 +21,10 @@

module GQTP
class Client
attr_accessor :address, :port
attr_accessor :host, :port
def initialize(options={})
@options = options.dup
@options[:address] ||= "127.0.0.1"
@options[:host] ||= @options[:address] || "127.0.0.1"
@options[:port] ||= 10043
@connection = create_connection
end
Expand Down
12 changes: 6 additions & 6 deletions lib/gqtp/connection/coolio.rb
Expand Up @@ -83,13 +83,13 @@ def consume_data(size, callback)
end

class Client
attr_accessor :address, :port
attr_accessor :host, :port
def initialize(options={})
@options = options
@address = options[:address] || "127.0.0.1"
@host = options[:host] || "127.0.0.1"
@port = options[:port] || 10043
@loop = options[:loop] || ::Coolio::Loop.default
@socket = Socket.connect(@address, @port)
@socket = Socket.connect(@host, @port)
@socket.attach(@loop)
end

Expand All @@ -107,16 +107,16 @@ def close
end

class Server
attr_accessor :address, :port
attr_accessor :host, :port
def initialize(options={})
@options = options
@address = options[:address] || "0.0.0.0"
@host = options[:host] || "0.0.0.0"
@port = options[:port] || 10043
@loop = options[:loop] || ::Coolio::Loop.default
end

def run
@server = ::Coolio::TCPServer.new(@address, @port, Socket) do |client|
@server = ::Coolio::TCPServer.new(@host, @port, Socket) do |client|
yield(client)
end
@server.attach(@loop)
Expand Down
14 changes: 7 additions & 7 deletions lib/gqtp/connection/synchronous.rb
Expand Up @@ -58,15 +58,15 @@ def close
end

class Client
attr_accessor :address, :port
attr_accessor :host, :port
def initialize(options={})
@options = options
@address = options[:address] || "127.0.0.1"
@host = options[:host] || "127.0.0.1"
@port = options[:port] || 10043
begin
@socket = TCPSocket.open(@address, @port)
@socket = TCPSocket.open(@host, @port)
rescue SystemCallError
raise ConnectionError.new(@address, @port, $!)
raise ConnectionError.new(@host, @port, $!)
end
@io = IO.new(@socket)
end
Expand All @@ -85,16 +85,16 @@ def close
end

class Server
attr_accessor :address, :port
attr_accessor :host, :port
def initialize(options={})
@options = options
@address = options[:address] || "0.0.0.0"
@host = options[:host] || "0.0.0.0"
@port = options[:port] || 10043
@backlog = options[:backlog] || 128
end

def run
@server = TCPServer.new(@address, @port)
@server = TCPServer.new(@host, @port)
@server.listen(@backlog)
loop do
client = @server.accept
Expand Down
14 changes: 7 additions & 7 deletions lib/gqtp/connection/thread.rb
Expand Up @@ -69,15 +69,15 @@ def close
end

class Client
attr_accessor :address, :port
attr_accessor :host, :port
def initialize(options={})
@options = options
@address = options[:address] || "127.0.0.1"
@host = options[:host] || "127.0.0.1"
@port = options[:port] || 10043
begin
@socket = TCPSocket.open(@address, @port)
@socket = TCPSocket.open(@host, @port)
rescue SystemCallError
raise ConnectionError.new(@address, @port, $!)
raise ConnectionError.new(@host, @port, $!)
end
@io = IO.new(@socket)
end
Expand All @@ -96,16 +96,16 @@ def close
end

class Server
attr_accessor :address, :port
attr_accessor :host, :port
def initialize(options={})
@options = options
@address = options[:address] || "0.0.0.0"
@host = options[:host] || "0.0.0.0"
@port = options[:port] || 10043
@backlog = options[:backlog] || 128
end

def run
@server = TCPServer.new(@address, @port)
@server = TCPServer.new(@host, @port)
@server.listen(@backlog)
thread = ::Thread.new do
loop do
Expand Down
8 changes: 4 additions & 4 deletions lib/gqtp/error.rb
Expand Up @@ -21,14 +21,14 @@ class Error < StandardError
end

class ConnectionError < Error
attr_reader :address
attr_reader :host
attr_reader :port
attr_reader :detail
def initialize(address, port, detail)
@address = address
def initialize(host, port, detail)
@host = host
@port = port
@detail = detail
super("Failed to connect to <#{@address}:#{@port}>: " +
super("Failed to connect to <#{@host}:#{@port}>: " +
"#{@detail.message} (#{@detail.class})")
end
end
Expand Down
14 changes: 8 additions & 6 deletions lib/gqtp/proxy.rb
Expand Up @@ -20,16 +20,18 @@

module GQTP
class Proxy
attr_accessor :listen_address, :listen_port
attr_accessor :upstream_address, :upstream_port
attr_accessor :listen_host, :listen_port
attr_accessor :upstream_host, :upstream_port
def initialize(options={})
@options = options.dup
@listen_address = @options[:listen_address] || "0.0.0.0"
@listen_host = @options[:listen_host] || @options[:listen_address]
@listen_host ||= "0.0.0.0"
@listen_port = @options[:listen_port] || 10043
@upstream_address = @options[:upstream_address] || "127.0.0.1"
@upstream_host = @options[:upstream_host] || @options[:upstream_address]
@upstream_host ||= "127.0.0.1"
@upstream_port = @options[:upstream_port] || 10043
@connection = @options[:connection] || :thread
@server = Server.new(:address => @listen_address,
@server = Server.new(:host => @listen_host,
:port => @listen_port,
:connection => @connection)
end
Expand Down Expand Up @@ -67,7 +69,7 @@ def create_connection
require "gqtp/connection/#{@connection}"
module_name = @connection.to_s.capitalize
connection_module = GQTP::Connection::const_get(module_name)
connection_module::Client.new(:address => @upstream_address,
connection_module::Client.new(:host => @upstream_host,
:port => @upstream_port)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gqtp/server.rb
Expand Up @@ -20,10 +20,10 @@

module GQTP
class Server
attr_accessor :address, :port
attr_accessor :host, :port
def initialize(options={})
@options = options.dup
@options[:address] ||= "0.0.0.0"
@options[:host] ||= @options[:address] || "0.0.0.0"
@options[:port] ||= 10043
@on_request = nil
@on_connect = nil
Expand Down
12 changes: 6 additions & 6 deletions test/test-client.rb
Expand Up @@ -40,8 +40,8 @@ def test_no_server

class RequestTest < self
def setup
@address = "127.0.0.1"
@server = TCPServer.new(@address, 0)
@host = "127.0.0.1"
@server = TCPServer.new(@host, 0)
@port = @server.addr[1]

@request_body = nil
Expand Down Expand Up @@ -74,7 +74,7 @@ def process_client(client)
class SendTest < self
def test_sync
@response_body = "[false]"
client = GQTP::Client.new(:address => @address, :port => @port)
client = GQTP::Client.new(:host => @host, :port => @port)
client.send("status")
header, body = client.read
assert_equal(["status", @response_body.bytesize, @response_body],
Expand All @@ -83,7 +83,7 @@ def test_sync

def test_async
@response_body = "[false]"
client = GQTP::Client.new(:address => @address, :port => @port)
client = GQTP::Client.new(:host => @host, :port => @port)
request = client.send("status") do |header, body|
assert_equal(["status", @response_body.bytesize, @response_body],
[@request_body, header.size, body])
Expand All @@ -95,13 +95,13 @@ def test_async
class CloseTest < self
def test_sync
@response_body = "[]"
client = GQTP::Client.new(:address => @address, :port => @port)
client = GQTP::Client.new(:host => @host, :port => @port)
assert_true(client.close)
end

def test_async
@response_body = "[]"
client = GQTP::Client.new(:address => @address, :port => @port)
client = GQTP::Client.new(:host => @host, :port => @port)
closed = false
close_request = client.close do
closed = true
Expand Down

0 comments on commit 794afc9

Please sign in to comment.