bayserver-core 2.3.2 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/baykit/bayserver/agent/accept_handler.rb +0 -26
- data/lib/baykit/bayserver/agent/channel_listener.rb +6 -6
- data/lib/baykit/bayserver/agent/command_receiver.rb +97 -38
- data/lib/baykit/bayserver/agent/grand_agent.rb +444 -134
- data/lib/baykit/bayserver/agent/letter.rb +30 -0
- data/lib/baykit/bayserver/agent/lifecycle_listener.rb +9 -7
- data/lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb +298 -0
- data/lib/baykit/bayserver/agent/multiplexer/job_multiplexer.rb +257 -0
- data/lib/baykit/bayserver/agent/multiplexer/job_multiplexer_base.rb +52 -0
- data/lib/baykit/bayserver/agent/multiplexer/multiplexer_base.rb +150 -0
- data/lib/baykit/bayserver/agent/multiplexer/plain_transporter.rb +129 -0
- data/lib/baykit/bayserver/agent/multiplexer/rudder_state.rb +81 -0
- data/lib/baykit/bayserver/agent/multiplexer/secure_transporter.rb +50 -0
- data/lib/baykit/bayserver/agent/multiplexer/spider_multiplexer.rb +561 -0
- data/lib/baykit/bayserver/agent/multiplexer/spin_multiplexer.rb +311 -0
- data/lib/baykit/bayserver/agent/multiplexer/taxi_multiplexer.rb +194 -0
- data/lib/baykit/bayserver/agent/multiplexer/transporter.rb +61 -0
- data/lib/baykit/bayserver/agent/multiplexer/write_unit.rb +30 -0
- data/lib/baykit/bayserver/agent/non_blocking_handler.rb +0 -319
- data/lib/baykit/bayserver/agent/signal/signal_agent.rb +2 -1
- data/lib/baykit/bayserver/agent/timer_handler.rb +1 -1
- data/lib/baykit/bayserver/agent/transporter/data_listener.rb +7 -7
- data/lib/baykit/bayserver/agent/transporter/package.rb +0 -3
- data/lib/baykit/bayserver/agent/transporter/transporter.rb +5 -139
- data/lib/baykit/bayserver/bay_log.rb +1 -0
- data/lib/baykit/bayserver/bayserver.rb +32 -28
- data/lib/baykit/bayserver/{util → common}/groups.rb +1 -1
- data/lib/baykit/bayserver/common/inbound_handler.rb +12 -0
- data/lib/baykit/bayserver/{docker/base → common}/inbound_ship.rb +94 -19
- data/lib/baykit/bayserver/{docker/base → common}/inbound_ship_store.rb +7 -9
- data/lib/baykit/bayserver/common/multiplexer.rb +98 -0
- data/lib/baykit/bayserver/common/postpone.rb +15 -0
- data/lib/baykit/bayserver/common/read_only_ship.rb +42 -0
- data/lib/baykit/bayserver/common/recipient.rb +24 -0
- data/lib/baykit/bayserver/common/vehicle.rb +24 -0
- data/lib/baykit/bayserver/common/vehicle_runner.rb +127 -0
- data/lib/baykit/bayserver/{docker/warp → common}/warp_data.rb +18 -10
- data/lib/baykit/bayserver/common/warp_handler.rb +27 -0
- data/lib/baykit/bayserver/{docker/warp → common}/warp_ship.rb +107 -27
- data/lib/baykit/bayserver/{docker/warp → common}/warp_ship_store.rb +3 -8
- data/lib/baykit/bayserver/docker/base/inbound_data_listener.rb +2 -2
- data/lib/baykit/bayserver/docker/base/port_base.rb +59 -27
- data/lib/baykit/bayserver/docker/{warp/warp_docker.rb → base/warp_base.rb} +43 -50
- data/lib/baykit/bayserver/docker/built_in/built_in_harbor_docker.rb +118 -31
- data/lib/baykit/bayserver/docker/built_in/built_in_log_docker.rb +60 -63
- data/lib/baykit/bayserver/docker/built_in/built_in_permission_docker.rb +11 -10
- data/lib/baykit/bayserver/docker/built_in/built_in_secure_docker.rb +13 -4
- data/lib/baykit/bayserver/docker/city.rb +27 -13
- data/lib/baykit/bayserver/docker/club.rb +8 -8
- data/lib/baykit/bayserver/docker/docker.rb +9 -1
- data/lib/baykit/bayserver/docker/harbor.rb +184 -22
- data/lib/baykit/bayserver/docker/permission.rb +7 -6
- data/lib/baykit/bayserver/docker/port.rb +39 -11
- data/lib/baykit/bayserver/docker/secure.rb +11 -9
- data/lib/baykit/bayserver/docker/send_file/file_content_handler.rb +99 -4
- data/lib/baykit/bayserver/docker/send_file/send_file_docker.rb +6 -2
- data/lib/baykit/bayserver/docker/send_file/send_file_ship.rb +92 -0
- data/lib/baykit/bayserver/docker/town.rb +31 -15
- data/lib/baykit/bayserver/docker/trouble.rb +17 -5
- data/lib/baykit/bayserver/docker/warp.rb +34 -0
- data/lib/baykit/bayserver/mem_usage.rb +9 -9
- data/lib/baykit/bayserver/protocol/command_packer.rb +1 -8
- data/lib/baykit/bayserver/protocol/packet_packer.rb +7 -11
- data/lib/baykit/bayserver/protocol/packet_store.rb +4 -4
- data/lib/baykit/bayserver/protocol/protocol_handler.rb +26 -8
- data/lib/baykit/bayserver/protocol/protocol_handler_store.rb +5 -5
- data/lib/baykit/bayserver/rudders/io_rudder.rb +62 -0
- data/lib/baykit/bayserver/rudders/rudder.rb +29 -0
- data/lib/baykit/bayserver/{watercraft → ships}/ship.rb +65 -41
- data/lib/baykit/bayserver/taxi/taxi.rb +15 -10
- data/lib/baykit/bayserver/taxi/taxi_runner.rb +9 -94
- data/lib/baykit/bayserver/tours/read_file_taxi.rb +2 -2
- data/lib/baykit/bayserver/tours/req_content_handler.rb +16 -13
- data/lib/baykit/bayserver/tours/tour.rb +2 -2
- data/lib/baykit/bayserver/tours/tour_handler.rb +36 -0
- data/lib/baykit/bayserver/tours/tour_req.rb +14 -24
- data/lib/baykit/bayserver/tours/tour_res.rb +5 -89
- data/lib/baykit/bayserver/tours/tour_store.rb +5 -5
- data/lib/baykit/bayserver/train/train_runner.rb +17 -13
- data/lib/baykit/bayserver/util/data_consume_listener.rb +1 -1
- data/lib/baykit/bayserver/util/executor_service.rb +11 -7
- data/lib/baykit/bayserver/util/ip_matcher.rb +9 -39
- data/lib/baykit/bayserver/util/locale.rb +1 -1
- data/lib/baykit/bayserver/util/selector.rb +24 -5
- data/lib/baykit/bayserver/util/sys_util.rb +6 -6
- data/lib/baykit/bayserver/version.rb +1 -1
- metadata +37 -18
- data/lib/baykit/bayserver/agent/grand_agent_monitor.rb +0 -231
- data/lib/baykit/bayserver/agent/transporter/plain_transporter.rb +0 -55
- data/lib/baykit/bayserver/agent/transporter/secure_transporter.rb +0 -81
- data/lib/baykit/bayserver/docker/base/inbound_handler.rb +0 -42
- data/lib/baykit/bayserver/docker/warp/package.rb +0 -4
- data/lib/baykit/bayserver/docker/warp/warp_data_listener.rb +0 -112
- data/lib/baykit/bayserver/docker/warp/warp_handler.rb +0 -38
- /data/lib/baykit/bayserver/{util → common}/cities.rb +0 -0
@@ -0,0 +1,30 @@
|
|
1
|
+
module Baykit
|
2
|
+
module BayServer
|
3
|
+
module Agent
|
4
|
+
class Letter
|
5
|
+
ACCEPTED = 1
|
6
|
+
CONNECTED = 2
|
7
|
+
READ = 3
|
8
|
+
WROTE = 4
|
9
|
+
CLOSEREQ = 5
|
10
|
+
|
11
|
+
attr :type
|
12
|
+
attr :state
|
13
|
+
attr :n_bytes
|
14
|
+
attr :address
|
15
|
+
attr :err
|
16
|
+
attr :client_rudder
|
17
|
+
|
18
|
+
def initialize(type, st, client_rd, n, adr, err)
|
19
|
+
@type = type
|
20
|
+
@state = st
|
21
|
+
@client_rudder = client_rd
|
22
|
+
@n_bytes = n
|
23
|
+
@err = err
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -2,13 +2,15 @@
|
|
2
2
|
module Baykit
|
3
3
|
module BayServer
|
4
4
|
module Agent
|
5
|
-
module LifecycleListener
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
module LifecycleListener # interface
|
6
|
+
|
7
|
+
def add(agent_id)
|
8
|
+
raise NotImplementedError
|
9
|
+
end
|
10
|
+
|
11
|
+
def remove(agent_id)
|
12
|
+
raise NotImplementedError
|
13
|
+
end
|
12
14
|
end
|
13
15
|
end
|
14
16
|
end
|
@@ -0,0 +1,298 @@
|
|
1
|
+
require 'fcntl'
|
2
|
+
require 'baykit/bayserver/agent/grand_agent'
|
3
|
+
require 'baykit/bayserver/rudders/io_rudder'
|
4
|
+
require 'baykit/bayserver/util/io_util'
|
5
|
+
|
6
|
+
module Baykit
|
7
|
+
module BayServer
|
8
|
+
module Agent
|
9
|
+
module Monitor
|
10
|
+
class GrandAgentMonitor
|
11
|
+
include Baykit::BayServer::Util
|
12
|
+
include Baykit::BayServer::Rudders
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr :num_agents
|
16
|
+
attr :cur_id
|
17
|
+
attr :anchored_port_map
|
18
|
+
attr :monitors
|
19
|
+
attr :finale
|
20
|
+
end
|
21
|
+
|
22
|
+
@num_agents = 0
|
23
|
+
@cur_id = 0
|
24
|
+
@anchored_port_map = []
|
25
|
+
@monitors = {}
|
26
|
+
@finale = false
|
27
|
+
|
28
|
+
attr :agent_id
|
29
|
+
attr :anchorable
|
30
|
+
attr :rudder
|
31
|
+
attr :child_thread
|
32
|
+
attr :child_pid
|
33
|
+
|
34
|
+
def initialize(agt_id, anchorable, rd, child_thread, child_pid)
|
35
|
+
@agent_id = agt_id
|
36
|
+
@anchorable = anchorable
|
37
|
+
@rudder = rd
|
38
|
+
@child_thread = child_thread
|
39
|
+
@child_pid = child_pid
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s()
|
43
|
+
return "Monitor##{@agent_id}"
|
44
|
+
end
|
45
|
+
|
46
|
+
def run()
|
47
|
+
begin
|
48
|
+
while true do
|
49
|
+
buf = " " * 4
|
50
|
+
|
51
|
+
n = @rudder.read(buf, 4)
|
52
|
+
if n == -1
|
53
|
+
raise EOFError.new
|
54
|
+
end
|
55
|
+
if n < 4
|
56
|
+
raise IOError.new("Cannot read int: nbytes=#{n}")
|
57
|
+
end
|
58
|
+
res = buffer_to_int(buf)
|
59
|
+
if res == GrandAgent::CMD_CLOSE
|
60
|
+
BayLog.debug("%s read Close", self)
|
61
|
+
break
|
62
|
+
elsif res == GrandAgent::CMD_CATCHUP
|
63
|
+
on_read_catch_up
|
64
|
+
else
|
65
|
+
BayLog.debug("%s read OK: %d", self, res);
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
rescue EOFError, IOError => e
|
70
|
+
BayLog.fatal("Agent terminated")
|
71
|
+
rescue Exception => e
|
72
|
+
BayLog.fatal_e(e)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def on_readable()
|
77
|
+
begin
|
78
|
+
res = IOUtil.read_int32(@communication_channel)
|
79
|
+
if res == nil || res == GrandAgent::CMD_CLOSE
|
80
|
+
close()
|
81
|
+
else
|
82
|
+
BayLog.debug("%s read OK: %d", self, res)
|
83
|
+
end
|
84
|
+
rescue IO::WaitReadable
|
85
|
+
#BayLog.debug("%s no data", self)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def shutdown()
|
90
|
+
BayLog.debug("%s send shutdown command", self)
|
91
|
+
send(GrandAgent::CMD_SHUTDOWN)
|
92
|
+
end
|
93
|
+
|
94
|
+
def abort()
|
95
|
+
BayLog.debug("%s Send abort command", self)
|
96
|
+
send(GrandAgent::CMD_ABORT)
|
97
|
+
end
|
98
|
+
|
99
|
+
def reload_cert()
|
100
|
+
BayLog.debug("%s Send reload command", self)
|
101
|
+
send(GrandAgent::CMD_RELOAD_CERT)
|
102
|
+
end
|
103
|
+
|
104
|
+
def print_usage()
|
105
|
+
BayLog.debug("%s Send mem_usage command", self)
|
106
|
+
send(GrandAgent::CMD_MEM_USAGE)
|
107
|
+
sleep(0.5) # Lazy implementation
|
108
|
+
end
|
109
|
+
|
110
|
+
def send(cmd)
|
111
|
+
BayLog.debug("%s send command %s rd=%s", self, cmd, @rudder)
|
112
|
+
buf = GrandAgentMonitor.int_to_buffer(cmd)
|
113
|
+
@rudder.write(buf)
|
114
|
+
end
|
115
|
+
|
116
|
+
def close()
|
117
|
+
@rudder.close()
|
118
|
+
end
|
119
|
+
|
120
|
+
def on_read_catch_up()
|
121
|
+
|
122
|
+
end
|
123
|
+
def req_catch_up()
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
def agent_aborted()
|
128
|
+
BayLog.info(BayMessage.get(:MSG_GRAND_AGENT_SHUTDOWN, @agent_id))
|
129
|
+
|
130
|
+
if @child_pid != nil
|
131
|
+
begin
|
132
|
+
Process.kill("TERM", @child_pid)
|
133
|
+
rescue => e
|
134
|
+
BayLog.debug_e(e, "Error on killing process")
|
135
|
+
end
|
136
|
+
Process.wait(@child_pid)
|
137
|
+
end
|
138
|
+
GrandAgentMonitor.monitors.delete(@agent_id)
|
139
|
+
|
140
|
+
if not GrandAgentMonitor.finale
|
141
|
+
if GrandAgentMonitor.monitors.length < GrandAgentMonitor.num_agents
|
142
|
+
begin
|
143
|
+
if !BayServer.harbor.multi_core
|
144
|
+
GrandAgent.add(-1, @anchorable)
|
145
|
+
end
|
146
|
+
GrandAgentMonitor.add(@anchorable)
|
147
|
+
rescue => e
|
148
|
+
BayLog.error_e(e)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def start
|
155
|
+
@child_thread = Thread.new do
|
156
|
+
run()
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
########################################
|
161
|
+
# Class methods
|
162
|
+
########################################
|
163
|
+
|
164
|
+
def self.init(num_agents, anchored_port_map)
|
165
|
+
@num_agents = num_agents
|
166
|
+
@anchored_port_map = anchored_port_map
|
167
|
+
@num_agents.times do
|
168
|
+
add(true)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def self.add(anchoroable)
|
173
|
+
@cur_id = @cur_id + 1
|
174
|
+
agt_id = @cur_id
|
175
|
+
if agt_id > 100
|
176
|
+
BayLog.error("Too many agents started")
|
177
|
+
exit(1)
|
178
|
+
end
|
179
|
+
|
180
|
+
if BayServer.harbor.multi_core
|
181
|
+
new_argv = BayServer.commandline_args.dup
|
182
|
+
new_argv.insert(0, "ruby")
|
183
|
+
new_argv << "-agentid=" + agt_id.to_s
|
184
|
+
|
185
|
+
ports = ""
|
186
|
+
|
187
|
+
no_close_io = {} # Port list not to close on spawned
|
188
|
+
@anchored_port_map.each_key do |ch|
|
189
|
+
no_close_io[ch] = ch
|
190
|
+
if ports != ""
|
191
|
+
ports +=","
|
192
|
+
end
|
193
|
+
ports += ch.fileno.to_s
|
194
|
+
end
|
195
|
+
new_argv << "-ports=" + ports
|
196
|
+
|
197
|
+
server = TCPServer.open("localhost", 0)
|
198
|
+
#BayLog.info("port=%d", server.local_address.ip_port)
|
199
|
+
new_argv << "-monitor_port=" + server.local_address.ip_port.to_s
|
200
|
+
|
201
|
+
if SysUtil.run_on_windows()
|
202
|
+
child_pid = spawn(ENV, new_argv.join(" "))
|
203
|
+
else
|
204
|
+
child_pid = spawn(ENV, new_argv.join(" "), no_close_io)
|
205
|
+
end
|
206
|
+
|
207
|
+
BayLog.debug("Process spawned cmd=%s pid=%d", new_argv, child_pid)
|
208
|
+
|
209
|
+
client_socket = server.accept()
|
210
|
+
server.close()
|
211
|
+
|
212
|
+
else
|
213
|
+
|
214
|
+
if SysUtil::run_on_windows()
|
215
|
+
pair = Socket.socketpair(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
216
|
+
else
|
217
|
+
pair = Socket.socketpair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
|
218
|
+
end
|
219
|
+
|
220
|
+
client_socket = pair[0]
|
221
|
+
GrandAgent.add(agt_id, anchoroable)
|
222
|
+
|
223
|
+
# Agents run on single core (thread mode)
|
224
|
+
child_thread = Thread.new() do
|
225
|
+
agt = GrandAgent.get(agt_id)
|
226
|
+
agt.add_command_receiver(IORudder.new(pair[1]))
|
227
|
+
agt.start()
|
228
|
+
end
|
229
|
+
|
230
|
+
end
|
231
|
+
|
232
|
+
mon =
|
233
|
+
GrandAgentMonitor.new(
|
234
|
+
agt_id,
|
235
|
+
anchoroable,
|
236
|
+
IORudder.new(client_socket),
|
237
|
+
child_thread,
|
238
|
+
child_pid)
|
239
|
+
@monitors[agt_id] = mon
|
240
|
+
mon.start
|
241
|
+
|
242
|
+
end
|
243
|
+
|
244
|
+
def self.join
|
245
|
+
while !@monitors.empty?
|
246
|
+
@monitors.values.each do |mon|
|
247
|
+
mon.child_thread.join
|
248
|
+
mon.agent_aborted
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
def self.reload_cert_all()
|
253
|
+
@monitors.values.each { |mon| mon.reload_cert() }
|
254
|
+
end
|
255
|
+
|
256
|
+
def self.restart_all()
|
257
|
+
old_monitors = @monitors.dup()
|
258
|
+
|
259
|
+
#@agent_count.times {add()}
|
260
|
+
|
261
|
+
old_monitors.values.each { |mon| mon.shutdown() }
|
262
|
+
end
|
263
|
+
|
264
|
+
def self.shutdown_all()
|
265
|
+
@finale = true
|
266
|
+
@monitors.dup().values.each do |mon|
|
267
|
+
mon.shutdown()
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
def self.abort_all()
|
272
|
+
@finale = true
|
273
|
+
@monitors.dup().values.each do |mon|
|
274
|
+
mon.abort()
|
275
|
+
end
|
276
|
+
exit(1)
|
277
|
+
end
|
278
|
+
|
279
|
+
def self.print_usage_all()
|
280
|
+
@monitors.values.each do |mon|
|
281
|
+
mon.print_usage()
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
def self.buffer_to_int(buf)
|
286
|
+
return buf.unpack("N").first
|
287
|
+
end
|
288
|
+
|
289
|
+
def self.int_to_buffer(val)
|
290
|
+
return [val].pack("N")
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
@@ -0,0 +1,257 @@
|
|
1
|
+
require 'baykit/bayserver/rudders/io_rudder'
|
2
|
+
|
3
|
+
require 'baykit/bayserver/agent/multiplexer/multiplexer_base'
|
4
|
+
require 'baykit/bayserver/agent/multiplexer/job_multiplexer_base'
|
5
|
+
|
6
|
+
|
7
|
+
module Baykit
|
8
|
+
module BayServer
|
9
|
+
module Agent
|
10
|
+
module Multiplexer
|
11
|
+
class JobMultiplexer < JobMultiplexerBase
|
12
|
+
include Baykit::BayServer::Agent::TimerHandler #implements
|
13
|
+
include Baykit::BayServer::Common::Recipient # implements
|
14
|
+
include Baykit::BayServer::Agent::Multiplexer
|
15
|
+
|
16
|
+
include Baykit::BayServer::Rudders
|
17
|
+
include Baykit::BayServer::Util
|
18
|
+
|
19
|
+
def initialize(agt, anchorable)
|
20
|
+
super
|
21
|
+
end
|
22
|
+
def to_s
|
23
|
+
return "JobMpx[#{@agent}]"
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
#########################################
|
28
|
+
# Implements Multiplexer
|
29
|
+
#########################################
|
30
|
+
|
31
|
+
def req_accept(rd)
|
32
|
+
BayLog.debug("%s reqAccept isShutdown=%s", @agent, @agent.aborted)
|
33
|
+
if @agent.aborted
|
34
|
+
return
|
35
|
+
end
|
36
|
+
|
37
|
+
st = get_rudder_state(rd)
|
38
|
+
|
39
|
+
Thread.new do
|
40
|
+
begin
|
41
|
+
if @agent.aborted
|
42
|
+
next
|
43
|
+
end
|
44
|
+
|
45
|
+
begin
|
46
|
+
client_skt, adr = rd.io.accept
|
47
|
+
rescue Exception => e
|
48
|
+
@agent.send_accepted_letter(st, nil, e, true)
|
49
|
+
next
|
50
|
+
end
|
51
|
+
|
52
|
+
BayLog.debug("%s Accepted skt=%s", @agent, client_skt)
|
53
|
+
if agent.aborted
|
54
|
+
BayLog.error("%s Agent is not alive (close)", @agent);
|
55
|
+
client_skt.close
|
56
|
+
else
|
57
|
+
@agent.send_accepted_letter(st, IORudder.new(client_skt), nil, true)
|
58
|
+
end
|
59
|
+
|
60
|
+
rescue Exception => e
|
61
|
+
BayLog.fatal_e(e)
|
62
|
+
@agent.shutdown
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def req_connect(rd, adr)
|
70
|
+
st = get_rudder_state(rd)
|
71
|
+
BayLog.debug("%s reqConnect adr=%s rd=%s chState=%s", @agent, adr.canonname, rd, st)
|
72
|
+
|
73
|
+
Thread.new do
|
74
|
+
begin
|
75
|
+
rd.io.connect(adr)
|
76
|
+
BayLog.debug("%s Connected rd=%s", @agent, rd)
|
77
|
+
@agent.send_connected_letter(st, nil, false)
|
78
|
+
rescue Exception => e
|
79
|
+
@agent.send_connected_letter(st, e, false)
|
80
|
+
return
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
st.connecting = true
|
85
|
+
end
|
86
|
+
|
87
|
+
def req_read(rd)
|
88
|
+
st = get_rudder_state(rd)
|
89
|
+
if st == nil
|
90
|
+
return
|
91
|
+
end
|
92
|
+
|
93
|
+
BayLog.debug("%s reqRead rd=%s state=%s", @agent, st.rudder, st);
|
94
|
+
need_read = false
|
95
|
+
st.reading_lock.synchronize do
|
96
|
+
if !st.reading
|
97
|
+
need_read = true
|
98
|
+
st.reading = true
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
if need_read
|
103
|
+
next_read(st)
|
104
|
+
end
|
105
|
+
|
106
|
+
st.access
|
107
|
+
end
|
108
|
+
|
109
|
+
def req_write(rd, buf, adr, tag, lis)
|
110
|
+
st = get_rudder_state(rd)
|
111
|
+
BayLog.debug("%s reqWrite st=%s", @agent, st)
|
112
|
+
|
113
|
+
if st == nil || st.closed
|
114
|
+
BayLog.warn("%s Channel is closed: %s", @agent, rd)
|
115
|
+
return
|
116
|
+
end
|
117
|
+
|
118
|
+
unt = WriteUnit.new(buf, adr, tag, lis)
|
119
|
+
st.writing_lock.synchronize do
|
120
|
+
st.write_queue << unt
|
121
|
+
end
|
122
|
+
|
123
|
+
need_write = false
|
124
|
+
st.writing_lock.synchronize do
|
125
|
+
if !st.writing
|
126
|
+
need_write = true
|
127
|
+
st.writing = true
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
if need_write
|
132
|
+
next_write(st)
|
133
|
+
end
|
134
|
+
|
135
|
+
st.access
|
136
|
+
end
|
137
|
+
|
138
|
+
def req_close(rd)
|
139
|
+
st = get_rudder_state(rd)
|
140
|
+
BayLog.debug("%s reqClose st=%s", @agent, st);
|
141
|
+
|
142
|
+
if st == nil
|
143
|
+
BayLog.warn("%s channel state not found: %s", @agent, rd)
|
144
|
+
return
|
145
|
+
end
|
146
|
+
|
147
|
+
Thread.new do
|
148
|
+
begin
|
149
|
+
st = get_rudder_state(rd)
|
150
|
+
if st == nil
|
151
|
+
BayLog.debug("%s Rudder is already closed: rd=%s", @agent, rd)
|
152
|
+
next
|
153
|
+
end
|
154
|
+
|
155
|
+
close_rudder(st)
|
156
|
+
rescue Exception => e
|
157
|
+
BayLog.fatal_e(e)
|
158
|
+
@agent.shutdown
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
st.access
|
163
|
+
end
|
164
|
+
|
165
|
+
def cancel_read(st)
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
def cancel_write(st)
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
def next_accept(st)
|
174
|
+
req_accept(st.rudder)
|
175
|
+
end
|
176
|
+
|
177
|
+
def next_read(st)
|
178
|
+
Thread.new do
|
179
|
+
if st.closed
|
180
|
+
#channel is already closed
|
181
|
+
BayLog.debug("%s Rudder is already closed: rd=%s", @agent, st.rudder);
|
182
|
+
next
|
183
|
+
end
|
184
|
+
|
185
|
+
begin
|
186
|
+
if st.handshaking
|
187
|
+
# Calls accept API for client socket
|
188
|
+
st.rudder.io.accept
|
189
|
+
st.handshaking = false
|
190
|
+
|
191
|
+
BayLog.debug("%s Handshake done (rd=%s)", self, st.rudder)
|
192
|
+
app_protocols = st.rudder.io.context.alpn_protocols
|
193
|
+
|
194
|
+
# HELP ME
|
195
|
+
# This code does not work!
|
196
|
+
# We cannot get application protocol name
|
197
|
+
proto = nil
|
198
|
+
if app_protocols != nil && app_protocols.length > 0
|
199
|
+
proto = app_protocols[0]
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
BayLog.debug("%s Try to Read (rd=%s)", @agent, st.rudder)
|
204
|
+
begin
|
205
|
+
n = st.rudder.read(st.read_buf, st.buf_size)
|
206
|
+
rescue EOFError => e
|
207
|
+
n = 0
|
208
|
+
st.read_buf.clear
|
209
|
+
end
|
210
|
+
|
211
|
+
@agent.send_read_letter(st, n, nil, nil, true)
|
212
|
+
|
213
|
+
rescue Exception => e
|
214
|
+
@agent.send_read_letter(st, -1, nil, e, true)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def next_write(st)
|
220
|
+
Thread.new do
|
221
|
+
BayLog.debug("%s next write st=%s", @agent, st)
|
222
|
+
|
223
|
+
if st == nil || st.closed
|
224
|
+
BayLog.warn("%s Channel is closed: %s", @agent, st.rudder)
|
225
|
+
next
|
226
|
+
end
|
227
|
+
|
228
|
+
u = st.write_queue[0]
|
229
|
+
BayLog.debug("%s Try to write: pkt=%s buflen=%d closed=%s", self, u.tag, u.buf.length, st.closed);
|
230
|
+
|
231
|
+
n = 0
|
232
|
+
begin
|
233
|
+
if !st.closed && u.buf.length > 0
|
234
|
+
n = st.rudder.write(u.buf)
|
235
|
+
u.buf.slice!(0, n)
|
236
|
+
end
|
237
|
+
rescue Exception => e
|
238
|
+
@agent.send_wrote_letter(st, -1, e, true)
|
239
|
+
next
|
240
|
+
end
|
241
|
+
|
242
|
+
@agent.send_wrote_letter(st, n, nil, true)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
def is_non_blocking()
|
247
|
+
return false
|
248
|
+
end
|
249
|
+
|
250
|
+
def use_async_api()
|
251
|
+
return false
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
|
2
|
+
module Baykit
|
3
|
+
module BayServer
|
4
|
+
module Agent
|
5
|
+
module Multiplexer
|
6
|
+
class JobMultiplexerBase < MultiplexerBase
|
7
|
+
include TimerHandler
|
8
|
+
|
9
|
+
attr :anchorable
|
10
|
+
attr :pipe
|
11
|
+
|
12
|
+
def initialize(agt, anchorable)
|
13
|
+
super(agt)
|
14
|
+
|
15
|
+
@anchorable = anchorable
|
16
|
+
@agent.add_timer_handler(self)
|
17
|
+
|
18
|
+
@pipe = IO::pipe
|
19
|
+
end
|
20
|
+
|
21
|
+
#########################################
|
22
|
+
# Implements Multiplexer
|
23
|
+
#########################################
|
24
|
+
|
25
|
+
def shutdown()
|
26
|
+
close_all()
|
27
|
+
end
|
28
|
+
|
29
|
+
def on_free()
|
30
|
+
if @agent.aborted
|
31
|
+
return
|
32
|
+
end
|
33
|
+
|
34
|
+
if @anchorable
|
35
|
+
BayServer::anchorable_port_map.keys.each do |rd|
|
36
|
+
req_accept(rd)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#########################################
|
42
|
+
# Implements TimerHandler
|
43
|
+
#########################################
|
44
|
+
|
45
|
+
def on_timer()
|
46
|
+
close_timeout_sockets()
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|