/
server.rb
99 lines (88 loc) · 2.66 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2013 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
require "gqtp/header"
module GQTP
class Server
attr_accessor :host, :port
def initialize(options={})
@options = options.dup
@options[:host] ||= @options[:address] || "0.0.0.0"
@options[:port] ||= 10043
@on_request = nil
@on_connect = nil
end
def run
@connection = create_connection
@connection.run do |client|
process_request(client, on_connect(client))
end
end
def shutdown
@connection.shutdown
end
def on_connect(*arguments, &block)
if block_given?
@on_connect = block
else
client, = arguments
if @on_connect
@on_connect.call(client)
else
nil
end
end
end
def on_request(*arguments, &block)
if block_given?
@on_request = block
else
request, client, connect_info = arguments
@on_request.call(request, client, connect_info)
end
end
private
def create_connection
connection = @options[:connection] || :thread
begin
require "gqtp/connection/#{connection}"
rescue LoadError
raise "unknown connection: <#{connection.inspect}>"
end
module_name = connection.to_s.capitalize
connection_module = GQTP::Connection::const_get(module_name)
connection_module::Server.new(@options)
end
def process_request(client, connect_info)
client.read(Header.size) do |header|
request_header = Header.parse(header)
client.read(request_header.size) do |body|
request = Request.new(request_header, body)
on_request(request, client, connect_info)
process_request(client, connect_info)
end
end
end
class Request
attr_reader :header, :body
def initialize(header, body)
@header = header
@body = body
end
end
end
end