bayserver-core 3.0.3 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/baykit/bayserver/agent/command_receiver.rb +2 -0
- data/lib/baykit/bayserver/agent/grand_agent.rb +101 -93
- data/lib/baykit/bayserver/agent/letters/accepted_letter.rb +2 -2
- data/lib/baykit/bayserver/agent/letters/closed_letter.rb +2 -2
- data/lib/baykit/bayserver/agent/letters/connected_letter.rb +2 -2
- data/lib/baykit/bayserver/agent/letters/error_letter.rb +2 -2
- data/lib/baykit/bayserver/agent/letters/letter.rb +7 -3
- data/lib/baykit/bayserver/agent/letters/read_letter.rb +2 -2
- data/lib/baykit/bayserver/agent/letters/wrote_letter.rb +2 -2
- data/lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb +20 -22
- data/lib/baykit/bayserver/agent/multiplexer/job_multiplexer.rb +44 -31
- data/lib/baykit/bayserver/agent/multiplexer/job_multiplexer_base.rb +5 -0
- data/lib/baykit/bayserver/agent/multiplexer/multiplexer_base.rb +4 -4
- data/lib/baykit/bayserver/agent/multiplexer/plain_transporter.rb +2 -3
- data/lib/baykit/bayserver/agent/multiplexer/spider_multiplexer.rb +113 -59
- data/lib/baykit/bayserver/agent/multiplexer/spin_multiplexer.rb +22 -17
- data/lib/baykit/bayserver/agent/multiplexer/taxi_multiplexer.rb +17 -13
- data/lib/baykit/bayserver/bay_log.rb +20 -16
- data/lib/baykit/bayserver/bayserver.rb +8 -15
- data/lib/baykit/bayserver/common/cities.rb +1 -1
- data/lib/baykit/bayserver/common/inbound_ship.rb +1 -1
- data/lib/baykit/bayserver/common/inbound_ship_store.rb +0 -1
- data/lib/baykit/bayserver/{agent/multiplexer → common}/rudder_state.rb +43 -7
- data/lib/baykit/bayserver/common/rudder_state_store.rb +63 -0
- data/lib/baykit/bayserver/{agent/multiplexer → common}/transporter.rb +1 -3
- data/lib/baykit/bayserver/common/warp_ship.rb +4 -3
- data/lib/baykit/bayserver/{agent/multiplexer → common}/write_unit.rb +1 -3
- data/lib/baykit/bayserver/docker/base/inbound_data_listener.rb +0 -1
- data/lib/baykit/bayserver/docker/base/port_base.rb +5 -5
- data/lib/baykit/bayserver/docker/base/warp_base.rb +7 -4
- data/lib/baykit/bayserver/docker/built_in/built_in_harbor_docker.rb +25 -2
- data/lib/baykit/bayserver/docker/built_in/built_in_log_docker.rb +42 -26
- data/lib/baykit/bayserver/docker/harbor.rb +14 -0
- data/lib/baykit/bayserver/docker/send_file/file_content.rb +66 -0
- data/lib/baykit/bayserver/docker/send_file/file_content_handler.rb +113 -29
- data/lib/baykit/bayserver/docker/send_file/file_store.rb +117 -0
- data/lib/baykit/bayserver/docker/send_file/send_file_docker.rb +13 -5
- data/lib/baykit/bayserver/docker/send_file/send_file_ship.rb +11 -2
- data/lib/baykit/bayserver/docker/send_file/wait_file_ship.rb +92 -0
- data/lib/baykit/bayserver/mem_usage.rb +2 -0
- data/lib/baykit/bayserver/protocol/packet.rb +2 -1
- data/lib/baykit/bayserver/protocol/packet_part_accessor.rb +5 -5
- data/lib/baykit/bayserver/protocol/protocol_exception.rb +7 -1
- data/lib/baykit/bayserver/rudders/io_rudder.rb +2 -2
- data/lib/baykit/bayserver/rudders/rudder.rb +4 -0
- data/lib/baykit/bayserver/rudders/rudder_base.rb +21 -0
- data/lib/baykit/bayserver/tours/req_content_handler.rb +13 -13
- data/lib/baykit/bayserver/tours/tour.rb +1 -6
- data/lib/baykit/bayserver/tours/tour_req.rb +4 -8
- data/lib/baykit/bayserver/tours/tour_res.rb +1 -8
- data/lib/baykit/bayserver/util/cgi_util.rb +7 -2
- data/lib/baykit/bayserver/util/headers.rb +2 -2
- data/lib/baykit/bayserver/util/http_status.rb +7 -0
- data/lib/baykit/bayserver/util/http_util.rb +16 -0
- data/lib/baykit/bayserver/util/nio_selector.rb +103 -0
- data/lib/baykit/bayserver/util/rb_selector.rb +55 -0
- data/lib/baykit/bayserver/util/selector.rb +43 -80
- data/lib/baykit/bayserver/util/simple_buffer.rb +1 -1
- data/lib/baykit/bayserver/version.rb +1 -1
- metadata +16 -20
- data/lib/baykit/bayserver/agent/accept_handler.rb +0 -71
- data/lib/baykit/bayserver/agent/channel_listener.rb +0 -35
- data/lib/baykit/bayserver/agent/transporter/data_listener.rb +0 -38
- data/lib/baykit/bayserver/agent/transporter/package.rb +0 -0
- data/lib/baykit/bayserver/agent/transporter/spin_read_transporter.rb +0 -111
- data/lib/baykit/bayserver/agent/transporter/spin_write_transporter.rb +0 -125
- data/lib/baykit/bayserver/agent/transporter/transporter.rb +0 -332
- data/lib/baykit/bayserver/docker/built_in/log_boat.rb +0 -71
- data/lib/baykit/bayserver/tours/send_file_yacht.rb +0 -97
- data/lib/baykit/bayserver/watercraft/boat.rb +0 -43
- data/lib/baykit/bayserver/watercraft/yacht.rb +0 -42
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
require 'baykit/bayserver/tours/tour'
|
|
2
|
+
require 'baykit/bayserver/common/read_only_ship'
|
|
3
|
+
|
|
4
|
+
module Baykit
|
|
5
|
+
module BayServer
|
|
6
|
+
module Docker
|
|
7
|
+
module SendFile
|
|
8
|
+
class WaitFileShip < Baykit::BayServer::Common::ReadOnlyShip
|
|
9
|
+
|
|
10
|
+
include Baykit::BayServer::Agent
|
|
11
|
+
include Baykit::BayServer::Tours
|
|
12
|
+
|
|
13
|
+
attr :file_content
|
|
14
|
+
attr :handler
|
|
15
|
+
|
|
16
|
+
attr :tour
|
|
17
|
+
attr :tour_id
|
|
18
|
+
def initialize()
|
|
19
|
+
@file_content = nil
|
|
20
|
+
@handler = nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def init(rd, tp, tur, file_content, handler)
|
|
24
|
+
super(tur.ship.agent_id, rd, tp)
|
|
25
|
+
@tour = tur
|
|
26
|
+
@tour_id = tur.tour_id
|
|
27
|
+
@file_content = file_content
|
|
28
|
+
@handler = handler
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_s
|
|
32
|
+
return "agt#" + @agent_id.to_s + " wait_file#" + @ship_id.to_s + "/" + @object_id.to_s
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
######################################################
|
|
36
|
+
# Implements Reusable
|
|
37
|
+
######################################################
|
|
38
|
+
|
|
39
|
+
def reset
|
|
40
|
+
super
|
|
41
|
+
@file_content = nil
|
|
42
|
+
@tour_id = 0
|
|
43
|
+
@tour = nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
######################################################
|
|
47
|
+
# Implements ReadOnlyShip
|
|
48
|
+
######################################################
|
|
49
|
+
|
|
50
|
+
def notify_read(buf)
|
|
51
|
+
|
|
52
|
+
BayLog.debug("%s file read completed", self)
|
|
53
|
+
|
|
54
|
+
begin
|
|
55
|
+
@handler.send_file_from_cache
|
|
56
|
+
rescue HttpException => e
|
|
57
|
+
begin
|
|
58
|
+
@tour.res.send_error(Tour::TOUR_ID_NOCHECK, e.status, e.message)
|
|
59
|
+
rescue IOError => ex
|
|
60
|
+
notify_error(ex)
|
|
61
|
+
return NextSocketAction::CLOSE
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
return NextSocketAction::CONTINUE
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def notify_error(e)
|
|
69
|
+
BayLog.debug_e(e, "%s Error notified", self)
|
|
70
|
+
begin
|
|
71
|
+
@tour.res.send_error(@tour_id, HttpStatus.INTERNAL_SERVER_ERROR, null, e)
|
|
72
|
+
rescue IOError => ex
|
|
73
|
+
BayLog.debug_e(ex)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def notify_eof()
|
|
78
|
+
raise Sink.new
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def notify_close
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def check_timeout(duration_sec)
|
|
85
|
+
return false
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -5,6 +5,7 @@ require 'baykit/bayserver/protocol/protocol_handler_store'
|
|
|
5
5
|
require 'baykit/bayserver/protocol/packet_store'
|
|
6
6
|
require 'baykit/bayserver/tours/tour_store'
|
|
7
7
|
require 'baykit/bayserver/common/inbound_ship_store'
|
|
8
|
+
require 'baykit/bayserver/common/rudder_state_store'
|
|
8
9
|
require 'baykit/bayserver/docker/base/warp_base'
|
|
9
10
|
|
|
10
11
|
require 'baykit/bayserver/util/string_util'
|
|
@@ -52,6 +53,7 @@ module Baykit
|
|
|
52
53
|
PacketStore.get_stores(@agent_id).each do |store|
|
|
53
54
|
store.print_usage(indent+1)
|
|
54
55
|
end
|
|
56
|
+
RudderStateStore.get_store(@agent_id).print_usage(indent+1)
|
|
55
57
|
TourStore.get_store(@agent_id).print_usage(indent+1);
|
|
56
58
|
BayServer.cities.cities.each do |city|
|
|
57
59
|
print_city_usage(nil, city, indent)
|
|
@@ -29,9 +29,9 @@ module Baykit
|
|
|
29
29
|
def put_bytes(buf, ofs=0, len=buf.length)
|
|
30
30
|
if len > 0
|
|
31
31
|
check_write(len)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
while @start + @pos + len > @packet.buf.length
|
|
33
|
+
packet.expand
|
|
34
|
+
end
|
|
35
35
|
begin
|
|
36
36
|
@packet.buf[@start + @pos, len] = buf[ofs, len]
|
|
37
37
|
rescue IndexError => e
|
|
@@ -100,13 +100,13 @@ module Baykit
|
|
|
100
100
|
def check_read(len)
|
|
101
101
|
max_len = (@max_len >= 0) ? @max_len : (@packet.buf_len - @start)
|
|
102
102
|
if @pos + len > max_len
|
|
103
|
-
raise
|
|
103
|
+
raise IOError.new("Invalid array index: @pos=#{@pos} @max=#{@max_len} len=#{len}")
|
|
104
104
|
end
|
|
105
105
|
end
|
|
106
106
|
|
|
107
107
|
def check_write(len)
|
|
108
108
|
if @max_len > 0 && @pos + len > @max_len
|
|
109
|
-
raise
|
|
109
|
+
raise IOError.new("Buffer overflow: @pos=#{@pos} @max=#{@max_len} len=#{len}")
|
|
110
110
|
end
|
|
111
111
|
end
|
|
112
112
|
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
#
|
|
2
|
+
# ProtocolException is thrown when protocol-level violations are detected,
|
|
3
|
+
# such as invalid packet framing or incorrect packet ordering.
|
|
4
|
+
# (Invalid HTTP headers or content length values result in an HttpException,
|
|
5
|
+
# which causes a 400 Bad Request response to be returned to the client.)
|
|
6
|
+
#
|
|
1
7
|
module Baykit
|
|
2
8
|
module BayServer
|
|
3
9
|
module Protocol
|
|
@@ -5,7 +11,7 @@ module Baykit
|
|
|
5
11
|
def initialize(fmt = nil, *args)
|
|
6
12
|
super(if fmt == nil
|
|
7
13
|
nil
|
|
8
|
-
elsif args
|
|
14
|
+
elsif args.empty?
|
|
9
15
|
sprintf("%s", fmt)
|
|
10
16
|
else
|
|
11
17
|
sprintf(fmt, *args)
|
|
@@ -15,26 +15,26 @@ module Baykit
|
|
|
15
15
|
raise NotImplementedError.new
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
#DEV_NULL = nil
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
class DevNullReqContentHandler
|
|
21
|
+
include ReqContentHandler # implements
|
|
22
|
+
def on_read_req_content(tur, buf, start, len)
|
|
23
|
+
end
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
def on_end_req_content(tur)
|
|
26
|
+
end
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
28
|
+
def on_abort_req(tur)
|
|
29
|
+
return false
|
|
30
|
+
end
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
DEV_NULL = DevNullReqContentHandler.new()
|
|
32
|
+
end
|
|
35
33
|
end
|
|
36
34
|
end
|
|
37
35
|
end
|
|
38
36
|
end
|
|
39
37
|
|
|
38
|
+
Baykit::BayServer::Tours::ReqContentHandler::DEV_NULL = Baykit::BayServer::Tours::ReqContentHandler::DevNullReqContentHandler.new
|
|
39
|
+
|
|
40
40
|
|
|
@@ -126,12 +126,7 @@ module Baykit
|
|
|
126
126
|
if city == nil
|
|
127
127
|
raise HttpException.new HttpStatus::NOT_FOUND, @req.uri
|
|
128
128
|
else
|
|
129
|
-
|
|
130
|
-
city.enter(self)
|
|
131
|
-
rescue HttpException => e
|
|
132
|
-
change_state(TOUR_ID_NOCHECK, TourState::ABORTED)
|
|
133
|
-
raise e
|
|
134
|
-
end
|
|
129
|
+
city.enter(self)
|
|
135
130
|
end
|
|
136
131
|
end
|
|
137
132
|
|
|
@@ -205,16 +205,12 @@ module Baykit
|
|
|
205
205
|
#@tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED)
|
|
206
206
|
return true
|
|
207
207
|
|
|
208
|
-
elsif @tour.
|
|
208
|
+
elsif @tour.reading?
|
|
209
209
|
aborted = true
|
|
210
210
|
if @content_handler != nil
|
|
211
211
|
aborted = @content_handler.on_abort_req(@tour)
|
|
212
212
|
end
|
|
213
213
|
|
|
214
|
-
#if aborted
|
|
215
|
-
# @tour.change_state(Tour::TOUR_ID_NOCHECK, Tour::TourState::ABORTED)
|
|
216
|
-
#end
|
|
217
|
-
|
|
218
214
|
return aborted
|
|
219
215
|
else
|
|
220
216
|
BayLog.debug("%s tour is not preparing or not running", @tour)
|
|
@@ -227,9 +223,9 @@ module Baykit
|
|
|
227
223
|
if hnd == nil
|
|
228
224
|
raise Sink.new("nil")
|
|
229
225
|
end
|
|
230
|
-
if @content_handler != nil
|
|
231
|
-
|
|
232
|
-
end
|
|
226
|
+
#if @content_handler != nil
|
|
227
|
+
# raise Sink.new("content handler already set")
|
|
228
|
+
#end
|
|
233
229
|
|
|
234
230
|
@content_handler = hnd
|
|
235
231
|
end
|
|
@@ -3,7 +3,6 @@ require 'baykit/bayserver/protocol/protocol_exception'
|
|
|
3
3
|
require 'baykit/bayserver/agent/multiplexer/plain_transporter'
|
|
4
4
|
require 'baykit/bayserver/taxi/taxi_runner'
|
|
5
5
|
require 'baykit/bayserver/docker/harbor'
|
|
6
|
-
require 'baykit/bayserver/tours/send_file_yacht'
|
|
7
6
|
require 'baykit/bayserver/tours/read_file_taxi'
|
|
8
7
|
require 'baykit/bayserver/tours/content_consume_listener'
|
|
9
8
|
|
|
@@ -22,7 +21,6 @@ module Baykit
|
|
|
22
21
|
include Baykit::BayServer::Docker
|
|
23
22
|
include Baykit::BayServer::Tours
|
|
24
23
|
include Baykit::BayServer::Taxi
|
|
25
|
-
include Baykit::BayServer::Agent::Transporter
|
|
26
24
|
|
|
27
25
|
attr :tour
|
|
28
26
|
|
|
@@ -35,7 +33,6 @@ module Baykit
|
|
|
35
33
|
attr :res_consume_listener
|
|
36
34
|
|
|
37
35
|
attr_accessor :header_sent
|
|
38
|
-
attr :yacht
|
|
39
36
|
|
|
40
37
|
###########################
|
|
41
38
|
# Response Content info
|
|
@@ -55,7 +52,6 @@ module Baykit
|
|
|
55
52
|
end
|
|
56
53
|
|
|
57
54
|
def init()
|
|
58
|
-
@yacht = SendFileYacht.new()
|
|
59
55
|
end
|
|
60
56
|
|
|
61
57
|
def to_s()
|
|
@@ -69,9 +65,6 @@ module Baykit
|
|
|
69
65
|
def reset()
|
|
70
66
|
@charset = nil
|
|
71
67
|
@header_sent = false
|
|
72
|
-
if @yacht != nil
|
|
73
|
-
@yacht.reset()
|
|
74
|
-
end
|
|
75
68
|
|
|
76
69
|
@available = false
|
|
77
70
|
@res_consume_listener = nil
|
|
@@ -184,7 +177,7 @@ module Baykit
|
|
|
184
177
|
raise e
|
|
185
178
|
ensure
|
|
186
179
|
@header_sent = true
|
|
187
|
-
|
|
180
|
+
end_res_content(chk_tour_id)
|
|
188
181
|
end
|
|
189
182
|
end
|
|
190
183
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require 'date'
|
|
1
2
|
require 'baykit/bayserver/util/string_util'
|
|
2
3
|
|
|
3
4
|
module Baykit
|
|
@@ -105,8 +106,12 @@ module Baykit
|
|
|
105
106
|
end
|
|
106
107
|
end
|
|
107
108
|
|
|
108
|
-
|
|
109
|
-
|
|
109
|
+
if tur.req.remote_address
|
|
110
|
+
add_env(REMOTE_ADDR, tur.req.remote_address, &block)
|
|
111
|
+
end
|
|
112
|
+
if tur.req.remote_port
|
|
113
|
+
add_env(REMOTE_PORT, tur.req.remote_port, &block)
|
|
114
|
+
end
|
|
110
115
|
#add_env(REMOTE_USER, "unknown")
|
|
111
116
|
|
|
112
117
|
add_env(REQUEST_SCHEME, tur.is_secure ? "https": "http", &block)
|
|
@@ -36,7 +36,7 @@ module Baykit
|
|
|
36
36
|
CONNECTION_CLOSE = 1
|
|
37
37
|
CONNECTION_KEEP_ALIVE = 2
|
|
38
38
|
CONNECTION_UPGRADE = 3
|
|
39
|
-
|
|
39
|
+
CONNECTION_UNKNOWN = 4
|
|
40
40
|
|
|
41
41
|
attr :status
|
|
42
42
|
attr :headers
|
|
@@ -185,7 +185,7 @@ module Baykit
|
|
|
185
185
|
when "upgrade" then
|
|
186
186
|
return CONNECTION_UPGRADE
|
|
187
187
|
else
|
|
188
|
-
return
|
|
188
|
+
return CONNECTION_UNKNOWN
|
|
189
189
|
end
|
|
190
190
|
end
|
|
191
191
|
end
|
|
@@ -29,6 +29,7 @@ module Baykit
|
|
|
29
29
|
end
|
|
30
30
|
@status = {}
|
|
31
31
|
@initialized = false
|
|
32
|
+
@desc_ok = nil
|
|
32
33
|
|
|
33
34
|
def self.init(bcf_file)
|
|
34
35
|
if(@initialized)
|
|
@@ -42,10 +43,16 @@ module Baykit
|
|
|
42
43
|
@status[kv.key.to_i] = kv.value
|
|
43
44
|
end
|
|
44
45
|
end
|
|
46
|
+
|
|
47
|
+
@desc_ok = @status[OK]
|
|
45
48
|
@initialized = true
|
|
46
49
|
end
|
|
47
50
|
|
|
48
51
|
def self.description(status_code)
|
|
52
|
+
if status_code == OK
|
|
53
|
+
return @desc_ok
|
|
54
|
+
end
|
|
55
|
+
|
|
49
56
|
desc = @status[status_code]
|
|
50
57
|
if(desc == nil)
|
|
51
58
|
BayLog.error("Status #{status_code} is invalid.")
|
|
@@ -5,12 +5,14 @@ require 'baykit/bayserver/util/string_util'
|
|
|
5
5
|
require 'baykit/bayserver/util/char_util'
|
|
6
6
|
require 'baykit/bayserver/util/headers'
|
|
7
7
|
require 'baykit/bayserver/util/http_util'
|
|
8
|
+
require 'baykit/bayserver/protocol/protocol_exception'
|
|
8
9
|
|
|
9
10
|
module Baykit
|
|
10
11
|
module BayServer
|
|
11
12
|
module Util
|
|
12
13
|
class HttpUtil
|
|
13
14
|
include Baykit::BayServer::Util
|
|
15
|
+
include Baykit::BayServer::Protocol
|
|
14
16
|
|
|
15
17
|
MAX_LINE_LEN = 5000
|
|
16
18
|
|
|
@@ -151,6 +153,20 @@ module Baykit
|
|
|
151
153
|
return nil
|
|
152
154
|
end
|
|
153
155
|
end
|
|
156
|
+
|
|
157
|
+
def HttpUtil.check_uri(uri)
|
|
158
|
+
if uri == nil
|
|
159
|
+
raise ProtocolException, "path is null"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
if uri.include?("\x00")
|
|
163
|
+
raise ProtocolException, "path contains null byte"
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
if uri.each_char.any? { |ch| (ch.ord < 0x20) || (ch.ord == 0x7f) }
|
|
167
|
+
raise ProtocolException, "path contains control character"
|
|
168
|
+
end
|
|
169
|
+
end
|
|
154
170
|
end
|
|
155
171
|
end
|
|
156
172
|
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
require "nio"
|
|
2
|
+
require "thread"
|
|
3
|
+
require 'baykit/bayserver/util/selector'
|
|
4
|
+
|
|
5
|
+
module Baykit
|
|
6
|
+
module BayServer
|
|
7
|
+
module Util
|
|
8
|
+
class NioSelector < Selector
|
|
9
|
+
|
|
10
|
+
attr :selector
|
|
11
|
+
attr :io_monitor_map
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
super
|
|
15
|
+
|
|
16
|
+
@selector = NIO::Selector.new
|
|
17
|
+
@io_monitor_map = {} # io -> monitor(:r)
|
|
18
|
+
end
|
|
19
|
+
def select(timeout_sec=nil)
|
|
20
|
+
timeout_sec = 0 if timeout_sec.nil?
|
|
21
|
+
ready_mon_list = @selector.select(timeout_sec)
|
|
22
|
+
|
|
23
|
+
result = {}
|
|
24
|
+
if ready_mon_list
|
|
25
|
+
ready_mon_list.each do |mon|
|
|
26
|
+
io = mon.io
|
|
27
|
+
if mon.readable?
|
|
28
|
+
register_read(io, result)
|
|
29
|
+
end
|
|
30
|
+
if mon.writable?
|
|
31
|
+
register_write(io, result)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
result
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def count
|
|
39
|
+
@lock.synchronize { @ops.length }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
def register_read(io, io_op)
|
|
44
|
+
|
|
45
|
+
if !@io_op_map.key?(io)
|
|
46
|
+
@io_monitor_map[io] = @selector.register(io, :r)
|
|
47
|
+
else
|
|
48
|
+
op = @io_op_map[io]
|
|
49
|
+
if op & OP_WRITE != 0
|
|
50
|
+
@io_monitor_map[io].interests = :rw
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
super # Update @io_monitor_map
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def register_write(io, io_op)
|
|
58
|
+
|
|
59
|
+
if !@io_op_map.key?(io)
|
|
60
|
+
@io_monitor_map[io] = @selector.register(io, :w)
|
|
61
|
+
else
|
|
62
|
+
op = @io_op_map[io]
|
|
63
|
+
if op & OP_READ != 0
|
|
64
|
+
@io_monitor_map[io].interests = :rw
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
super # Update @io_monitor_map
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def unregister_read(io, io_op)
|
|
72
|
+
|
|
73
|
+
if @io_op_map.key?(io)
|
|
74
|
+
op = @io_op_map[io]
|
|
75
|
+
if op & OP_WRITE != 0
|
|
76
|
+
@io_monitor_map[io].interests = :w
|
|
77
|
+
else
|
|
78
|
+
@selector.deregister(io)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
super # Update @io_monitor_map
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def unregister_write(io, io_op)
|
|
87
|
+
|
|
88
|
+
if @io_op_map.key?(io)
|
|
89
|
+
op = @io_op_map[io]
|
|
90
|
+
if op & OP_READ != 0
|
|
91
|
+
@io_monitor_map[io].interests = :r
|
|
92
|
+
else
|
|
93
|
+
@selector.deregister(io)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
super # Update @io_monitor_map
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
require 'baykit/bayserver/util/selector'
|
|
2
|
+
|
|
3
|
+
module Baykit
|
|
4
|
+
module BayServer
|
|
5
|
+
module Util
|
|
6
|
+
|
|
7
|
+
class RbSelector < Selector
|
|
8
|
+
|
|
9
|
+
def initialize
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def select(timeout_sec = nil)
|
|
14
|
+
if timeout_sec == nil
|
|
15
|
+
timeout_sec = 0
|
|
16
|
+
end
|
|
17
|
+
except_list = []
|
|
18
|
+
|
|
19
|
+
read_list = []
|
|
20
|
+
write_list = []
|
|
21
|
+
@lock.synchronize do
|
|
22
|
+
@io_op_map.keys().each do |io|
|
|
23
|
+
if (@io_op_map[io] & OP_READ) != 0
|
|
24
|
+
read_list << io
|
|
25
|
+
end
|
|
26
|
+
if (@io_op_map[io] & OP_WRITE) != 0
|
|
27
|
+
write_list << io
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
#BayLog.debug("Select read_list=%s", read_list)
|
|
32
|
+
#BayLog.debug("Select write_list=%s", write_list)
|
|
33
|
+
selected_read_list, selected_write_list = Kernel.select(read_list, write_list, except_list, timeout_sec)
|
|
34
|
+
|
|
35
|
+
result = {}
|
|
36
|
+
if selected_read_list != nil
|
|
37
|
+
selected_read_list.each do |io|
|
|
38
|
+
register_read(io, result)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
if selected_write_list != nil
|
|
43
|
+
selected_write_list.each do |io|
|
|
44
|
+
register_write(io, result)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
return result
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|