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.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/lib/baykit/bayserver/agent/accept_handler.rb +0 -26
  3. data/lib/baykit/bayserver/agent/channel_listener.rb +6 -6
  4. data/lib/baykit/bayserver/agent/command_receiver.rb +97 -38
  5. data/lib/baykit/bayserver/agent/grand_agent.rb +444 -134
  6. data/lib/baykit/bayserver/agent/letter.rb +30 -0
  7. data/lib/baykit/bayserver/agent/lifecycle_listener.rb +9 -7
  8. data/lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb +298 -0
  9. data/lib/baykit/bayserver/agent/multiplexer/job_multiplexer.rb +257 -0
  10. data/lib/baykit/bayserver/agent/multiplexer/job_multiplexer_base.rb +52 -0
  11. data/lib/baykit/bayserver/agent/multiplexer/multiplexer_base.rb +150 -0
  12. data/lib/baykit/bayserver/agent/multiplexer/plain_transporter.rb +129 -0
  13. data/lib/baykit/bayserver/agent/multiplexer/rudder_state.rb +81 -0
  14. data/lib/baykit/bayserver/agent/multiplexer/secure_transporter.rb +50 -0
  15. data/lib/baykit/bayserver/agent/multiplexer/spider_multiplexer.rb +561 -0
  16. data/lib/baykit/bayserver/agent/multiplexer/spin_multiplexer.rb +311 -0
  17. data/lib/baykit/bayserver/agent/multiplexer/taxi_multiplexer.rb +194 -0
  18. data/lib/baykit/bayserver/agent/multiplexer/transporter.rb +61 -0
  19. data/lib/baykit/bayserver/agent/multiplexer/write_unit.rb +30 -0
  20. data/lib/baykit/bayserver/agent/non_blocking_handler.rb +0 -319
  21. data/lib/baykit/bayserver/agent/signal/signal_agent.rb +2 -1
  22. data/lib/baykit/bayserver/agent/timer_handler.rb +1 -1
  23. data/lib/baykit/bayserver/agent/transporter/data_listener.rb +7 -7
  24. data/lib/baykit/bayserver/agent/transporter/package.rb +0 -3
  25. data/lib/baykit/bayserver/agent/transporter/transporter.rb +5 -139
  26. data/lib/baykit/bayserver/bay_log.rb +1 -0
  27. data/lib/baykit/bayserver/bayserver.rb +32 -28
  28. data/lib/baykit/bayserver/{util → common}/groups.rb +1 -1
  29. data/lib/baykit/bayserver/common/inbound_handler.rb +12 -0
  30. data/lib/baykit/bayserver/{docker/base → common}/inbound_ship.rb +94 -19
  31. data/lib/baykit/bayserver/{docker/base → common}/inbound_ship_store.rb +7 -9
  32. data/lib/baykit/bayserver/common/multiplexer.rb +98 -0
  33. data/lib/baykit/bayserver/common/postpone.rb +15 -0
  34. data/lib/baykit/bayserver/common/read_only_ship.rb +42 -0
  35. data/lib/baykit/bayserver/common/recipient.rb +24 -0
  36. data/lib/baykit/bayserver/common/vehicle.rb +24 -0
  37. data/lib/baykit/bayserver/common/vehicle_runner.rb +127 -0
  38. data/lib/baykit/bayserver/{docker/warp → common}/warp_data.rb +18 -10
  39. data/lib/baykit/bayserver/common/warp_handler.rb +27 -0
  40. data/lib/baykit/bayserver/{docker/warp → common}/warp_ship.rb +107 -27
  41. data/lib/baykit/bayserver/{docker/warp → common}/warp_ship_store.rb +3 -8
  42. data/lib/baykit/bayserver/docker/base/inbound_data_listener.rb +2 -2
  43. data/lib/baykit/bayserver/docker/base/port_base.rb +59 -27
  44. data/lib/baykit/bayserver/docker/{warp/warp_docker.rb → base/warp_base.rb} +43 -50
  45. data/lib/baykit/bayserver/docker/built_in/built_in_harbor_docker.rb +118 -31
  46. data/lib/baykit/bayserver/docker/built_in/built_in_log_docker.rb +60 -63
  47. data/lib/baykit/bayserver/docker/built_in/built_in_permission_docker.rb +11 -10
  48. data/lib/baykit/bayserver/docker/built_in/built_in_secure_docker.rb +13 -4
  49. data/lib/baykit/bayserver/docker/city.rb +27 -13
  50. data/lib/baykit/bayserver/docker/club.rb +8 -8
  51. data/lib/baykit/bayserver/docker/docker.rb +9 -1
  52. data/lib/baykit/bayserver/docker/harbor.rb +184 -22
  53. data/lib/baykit/bayserver/docker/permission.rb +7 -6
  54. data/lib/baykit/bayserver/docker/port.rb +39 -11
  55. data/lib/baykit/bayserver/docker/secure.rb +11 -9
  56. data/lib/baykit/bayserver/docker/send_file/file_content_handler.rb +99 -4
  57. data/lib/baykit/bayserver/docker/send_file/send_file_docker.rb +6 -2
  58. data/lib/baykit/bayserver/docker/send_file/send_file_ship.rb +92 -0
  59. data/lib/baykit/bayserver/docker/town.rb +31 -15
  60. data/lib/baykit/bayserver/docker/trouble.rb +17 -5
  61. data/lib/baykit/bayserver/docker/warp.rb +34 -0
  62. data/lib/baykit/bayserver/mem_usage.rb +9 -9
  63. data/lib/baykit/bayserver/protocol/command_packer.rb +1 -8
  64. data/lib/baykit/bayserver/protocol/packet_packer.rb +7 -11
  65. data/lib/baykit/bayserver/protocol/packet_store.rb +4 -4
  66. data/lib/baykit/bayserver/protocol/protocol_handler.rb +26 -8
  67. data/lib/baykit/bayserver/protocol/protocol_handler_store.rb +5 -5
  68. data/lib/baykit/bayserver/rudders/io_rudder.rb +62 -0
  69. data/lib/baykit/bayserver/rudders/rudder.rb +29 -0
  70. data/lib/baykit/bayserver/{watercraft → ships}/ship.rb +65 -41
  71. data/lib/baykit/bayserver/taxi/taxi.rb +15 -10
  72. data/lib/baykit/bayserver/taxi/taxi_runner.rb +9 -94
  73. data/lib/baykit/bayserver/tours/read_file_taxi.rb +2 -2
  74. data/lib/baykit/bayserver/tours/req_content_handler.rb +16 -13
  75. data/lib/baykit/bayserver/tours/tour.rb +2 -2
  76. data/lib/baykit/bayserver/tours/tour_handler.rb +36 -0
  77. data/lib/baykit/bayserver/tours/tour_req.rb +14 -24
  78. data/lib/baykit/bayserver/tours/tour_res.rb +5 -89
  79. data/lib/baykit/bayserver/tours/tour_store.rb +5 -5
  80. data/lib/baykit/bayserver/train/train_runner.rb +17 -13
  81. data/lib/baykit/bayserver/util/data_consume_listener.rb +1 -1
  82. data/lib/baykit/bayserver/util/executor_service.rb +11 -7
  83. data/lib/baykit/bayserver/util/ip_matcher.rb +9 -39
  84. data/lib/baykit/bayserver/util/locale.rb +1 -1
  85. data/lib/baykit/bayserver/util/selector.rb +24 -5
  86. data/lib/baykit/bayserver/util/sys_util.rb +6 -6
  87. data/lib/baykit/bayserver/version.rb +1 -1
  88. metadata +37 -18
  89. data/lib/baykit/bayserver/agent/grand_agent_monitor.rb +0 -231
  90. data/lib/baykit/bayserver/agent/transporter/plain_transporter.rb +0 -55
  91. data/lib/baykit/bayserver/agent/transporter/secure_transporter.rb +0 -81
  92. data/lib/baykit/bayserver/docker/base/inbound_handler.rb +0 -42
  93. data/lib/baykit/bayserver/docker/warp/package.rb +0 -4
  94. data/lib/baykit/bayserver/docker/warp/warp_data_listener.rb +0 -112
  95. data/lib/baykit/bayserver/docker/warp/warp_handler.rb +0 -38
  96. /data/lib/baykit/bayserver/{util → common}/cities.rb +0 -0
@@ -2,12 +2,18 @@ require 'socket'
2
2
  require 'objspace'
3
3
 
4
4
  require 'baykit/bayserver/sink'
5
- require 'baykit/bayserver/agent/accept_handler'
6
5
  require 'baykit/bayserver/agent/command_receiver'
7
- require 'baykit/bayserver/agent/grand_agent_monitor'
8
- require 'baykit/bayserver/agent/spin_handler'
6
+ require 'baykit/bayserver/agent/letter'
7
+ require 'baykit/bayserver/agent/multiplexer/spider_multiplexer'
8
+ require 'baykit/bayserver/agent/multiplexer/spin_multiplexer'
9
+ require 'baykit/bayserver/agent/multiplexer/job_multiplexer'
10
+ require 'baykit/bayserver/agent/multiplexer/taxi_multiplexer'
11
+ require 'baykit/bayserver/agent/multiplexer/rudder_state'
12
+ require 'baykit/bayserver/agent/monitor/grand_agent_monitor'
9
13
  require 'baykit/bayserver/agent/signal/signal_agent'
10
14
 
15
+ require 'baykit/bayserver/docker/harbor'
16
+
11
17
  require 'baykit/bayserver/train/train_runner'
12
18
  require 'baykit/bayserver/taxi/taxi_runner'
13
19
 
@@ -22,6 +28,8 @@ module Baykit
22
28
  include Baykit::BayServer::Train
23
29
  include Baykit::BayServer::Taxi
24
30
  include Baykit::BayServer::Agent::Signal
31
+ include Baykit::BayServer::Agent::Multiplexer
32
+ include Baykit::BayServer::Docker
25
33
  include Baykit::BayServer::Util
26
34
 
27
35
  SELECT_TIMEOUT_SEC = 10
@@ -32,66 +40,88 @@ module Baykit
32
40
  CMD_MEM_USAGE = 3
33
41
  CMD_SHUTDOWN = 4
34
42
  CMD_ABORT = 5
43
+ CMD_CATCHUP = 6
35
44
 
36
45
  attr :agent_id
37
46
  attr :anchorable
38
- attr :non_blocking_handler
39
- attr :spin_handler
40
- attr :accept_handler
47
+ attr :net_multiplexer
48
+ attr :job_multiplexer
49
+ attr :taxi_multiplexer
50
+ attr :spin_multiplexer
51
+ attr :spider_multiplexer
52
+ attr :job_multiplexer
53
+ attr :taxi_multiplexer
54
+ attr :recipient
55
+
41
56
  attr :send_wakeup_pipe
42
- attr :select_wakeup_pipe
43
- attr :select_timeout_sec
44
57
  attr :max_inbound_ships
45
- attr :selector
46
58
  attr :unanchorable_transporters
47
59
  attr :aborted
48
60
  attr :command_receiver
49
61
  attr :timer_handlers
62
+ attr :last_timeout_check
63
+ attr :letter_queue
64
+ attr :letter_queue_lock
65
+ attr :postpone_queue
66
+ attr :postpone_queue_lock
50
67
 
51
68
  class << self
52
69
  attr :agents
53
70
  attr :listeners
54
71
  attr :agent_count
55
- attr :anchorable_port_map
56
- attr :unanchorable_port_map
57
72
  attr :max_ships
58
73
  attr :max_agent_id
59
- attr :multi_core
60
74
  end
61
75
 
62
76
  # Class variables
63
77
  @agent_count = 0
64
78
  @max_agent_id = 0
65
79
  @max_ships = 0
66
- @multi_core = false
67
80
 
68
81
  @agents = []
69
82
  @listeners = []
70
83
 
71
- @anchorable_port_map = {}
72
- @unanchorable_port_map = {}
73
84
  @finale = false
74
85
 
75
86
  def initialize (agent_id, max_ships, anchorable)
76
87
  @agent_id = agent_id
88
+ @max_inbound_ships = max_ships
77
89
  @anchorable = anchorable
78
-
79
90
  @timer_handlers = []
80
- if @anchorable
81
- @accept_handler = AcceptHandler.new(self, GrandAgent.anchorable_port_map)
82
- else
83
- @accept_handler = nil
91
+ @select_timeout_sec = SELECT_TIMEOUT_SEC
92
+ @aborted = false
93
+ @letter_queue = []
94
+ @letter_queue_lock = Mutex.new
95
+ @postpone_queue = []
96
+ @postpone_queue_lock = Mutex.new
97
+
98
+ @spider_multiplexer = SpiderMultiplexer.new(self, anchorable)
99
+ @spin_multiplexer = SpinMultiplexer.new(self)
100
+ @job_multiplexer = JobMultiplexer.new(self, anchorable)
101
+ @taxi_multiplexer = TaxiMultiplexer.new(self)
102
+
103
+ case BayServer.harbor.recipient
104
+ when Harbor::RECIPIENT_TYPE_SPIDER
105
+ @recipient = @spider_multiplexer
106
+
107
+ when Harbor::RECIPIENT_TYPE_PIPE
108
+ raise NotImplementedError.new
84
109
  end
85
110
 
86
- @spin_handler = SpinHandler.new(self)
87
- @non_blocking_handler = NonBlockingHandler.new(self)
111
+ case BayServer.harbor.net_multiplexer
112
+ when Harbor::MULTIPLEXER_TYPE_SPIDER
113
+ @net_multiplexer = @spider_multiplexer
88
114
 
89
- @select_timeout_sec = SELECT_TIMEOUT_SEC
90
- @max_inbound_ships = max_ships
91
- @selector = Selector.new()
92
- @aborted = false
93
- @unanchorable_transporters = {}
115
+ when Harbor::MULTIPLEXER_TYPE_JOB
116
+ @net_multiplexer = @job_multiplexer
117
+
118
+ when Harbor::MULTIPLEXER_TYPE_PIGEON, Harbor::MULTIPLEXER_TYPE_SPIN,
119
+ Harbor::MULTIPLEXER_TYPE_TAXI, Harbor::MULTIPLEXER_TYPE_TRAIN
94
120
 
121
+ raise Sink.new("Multiplexer not supported: %s", Harbor.get_multiplexer_type_name(BayServer.harbor.net_multiplexer))
122
+ end
123
+
124
+ @last_timeout_check = 0
95
125
  end
96
126
 
97
127
 
@@ -100,17 +130,41 @@ module Baykit
100
130
  end
101
131
 
102
132
 
103
- def inspect()
133
+ def inspect
104
134
  return to_s
105
135
  end
106
136
 
137
+ #########################################
138
+ # Custom methods
139
+ #########################################
140
+ def start
141
+ Thread.new do
142
+ run
143
+ end
144
+ end
145
+
107
146
  def run
108
147
  BayLog.info(BayMessage.get(:MSG_RUNNING_GRAND_AGENT, self))
109
- @select_wakeup_pipe = IO.pipe
110
- @selector.register(@select_wakeup_pipe[0], Selector::OP_READ)
111
- @selector.register(@command_receiver.communication_channel, Selector::OP_READ)
148
+
149
+ if @net_multiplexer.is_non_blocking
150
+ BayLog.info("rec=%s", @command_receiver)
151
+ @command_receiver.rudder.set_non_blocking
152
+ end
153
+
154
+ @net_multiplexer.req_read(@command_receiver.rudder)
155
+
156
+ if @anchorable
157
+ # Adds server socket channel of anchorable ports
158
+ BayServer.anchorable_port_map.keys.each do |rd|
159
+ if @net_multiplexer.is_non_blocking
160
+ rd.set_non_blocking
161
+ end
162
+ @net_multiplexer.add_rudder_state(rd, RudderState.new(rd))
163
+ end
164
+ end
112
165
 
113
166
  # Set up unanchorable channel
167
+ =begin
114
168
  for ch in GrandAgent.unanchorable_port_map.keys() do
115
169
  port_dkr = GrandAgent.unanchorable_port_map[ch]
116
170
  tp = port_dkr.new_transporter(self, ch)
@@ -121,124 +175,166 @@ module Baykit
121
175
  @non_blocking_handler.ask_to_read(ch)
122
176
  end
123
177
  end
178
+ =end
124
179
 
125
180
  busy = true
126
181
  begin
127
- while not @aborted
128
-
129
- count = -1
130
-
131
- if @accept_handler
132
- test_busy = @accept_handler.ch_count >= @max_inbound_ships
133
- if test_busy != busy
134
- busy = test_busy
135
- if busy
136
- @accept_handler.on_busy()
137
- else
138
- @accept_handler.on_free()
139
- end
140
- end
141
- end
182
+ while true
142
183
 
143
- if !busy && @selector.count() == 2
144
- # agent finished
145
- BayLog.debug("%s Selector has no key", self)
146
- break
184
+ test_busy = @net_multiplexer.is_busy
185
+ if test_busy != busy
186
+ busy = test_busy
187
+ if busy
188
+ @net_multiplexer.on_busy
189
+ else
190
+ @net_multiplexer.on_free
191
+ end
147
192
  end
148
193
 
149
- if !@spin_handler.empty?
150
- timeout = 0
194
+ if not @spin_multiplexer.is_empty
195
+ # If "SpinHandler" is running, the select function does not block.
196
+ received = @recipient.receive(false)
197
+ @spin_multiplexer.process_data
151
198
  else
152
- timeout = @select_timeout_sec
199
+ received = @recipient.receive(true)
153
200
  end
154
201
 
155
202
  if @aborted
156
203
  BayLog.info("%s aborted by another thread", self)
157
- break
204
+ break;
158
205
  end
159
- #@BayServer.debug("Selecting... read=" + read_list.to_s)
160
- selected_map = @selector.select(timeout)
161
- #BayLog.debug("%s selected: %s", self, selected_map)
162
206
 
163
- processed = @non_blocking_handler.register_channel_ops() > 0
164
-
165
- if @aborted
166
- BayLog.info("%s aborted by another thread", self)
167
- break
168
- end
169
-
170
- if selected_map.length == 0
171
- # No channel is selected
172
- processed |= @spin_handler.process_data()
207
+ if @spin_multiplexer.is_empty && @letter_queue.empty?
208
+ # timed out
209
+ # check per 10 seconds
210
+ if Time.now.tv_sec - @last_timeout_check >= 10
211
+ ring
212
+ end
173
213
  end
174
214
 
175
- selected_map.keys().each do |ch|
176
- if ch == @select_wakeup_pipe[0]
177
- # Waked up by ask_to_*
178
- on_waked_up(ch)
179
- elsif ch == @command_receiver.communication_channel
180
- @command_receiver.on_pipe_readable()
181
- elsif @accept_handler && @accept_handler.server_socket?(ch)
182
- @accept_handler.on_acceptable(ch)
183
- else
184
- @non_blocking_handler.handle_channel(ch, selected_map[ch])
215
+ while !@letter_queue.empty?
216
+ let = nil
217
+ @letter_queue_lock.synchronize do
218
+ let = @letter_queue.shift
185
219
  end
186
- processed = true
187
- end
188
220
 
189
- if not processed
190
- # timeout check
191
- @timer_handlers.each do |h|
192
- h.on_timer()
221
+ case let.type
222
+ when Letter::ACCEPTED
223
+ on_accept(let)
224
+ when Letter::CONNECTED
225
+ on_connect(let)
226
+ when Letter::READ
227
+ on_read(let)
228
+ when Letter::WROTE
229
+ on_wrote(let)
230
+ when Letter::CLOSEREQ
231
+ on_close_req(let)
193
232
  end
194
233
  end
195
-
196
234
  end # while
197
235
 
198
- rescue => e
236
+ rescue Exception => e
199
237
  BayLog.fatal_e(e)
200
238
  ensure
201
239
  BayLog.info("%s end", self)
202
- shutdown()
240
+ shutdown
203
241
  end
204
242
  end
205
243
 
206
- def shutdown()
207
- BayLog.info("%s shutdown", self)
208
244
 
209
- if @accept_handler != nil
210
- @accept_handler.shutdown()
211
- end
212
245
 
213
- @command_receiver.end()
214
- @non_blocking_handler.close_all()
246
+ def abort_agent
247
+ BayLog.info("%s abort", self)
215
248
 
216
- GrandAgent.listeners.each do |lis|
217
- lis.remove(self)
249
+ if BayServer.harbor.multi_core
250
+ exit(1)
218
251
  end
252
+ end
219
253
 
220
- GrandAgent.agents.delete(@agent_id)
254
+ def req_shutdown
255
+ @aborted = true
256
+ @recipient.wakeup
257
+ end
221
258
 
222
- @agent_id = -1
223
- if BayServer.harbor.multi_core
224
- exit(1)
259
+
260
+ def print_usage
261
+ # print memory usage
262
+ BayLog.info("%s MemUsage", self)
263
+ BayLog.info(" Ruby version: %s", RUBY_VERSION)
264
+ memsize = ObjectSpace.memsize_of_all
265
+ # formatted by comma
266
+ msize_comma = memsize.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse.then do |str|
267
+ str[0] == ',' ? str[1..-1] : str
225
268
  end
269
+ BayLog.info(" Total object size: %s bytes", msize_comma)
270
+ MemUsage.get(@agent_id).print_usage(1)
226
271
  end
227
272
 
228
- def abort_agent()
229
- BayLog.info("%s abort", self)
273
+
274
+ def add_timer_handler(handler)
275
+ @timer_handlers << handler
276
+ end
277
+
278
+ def remove_timer_handler(handler)
279
+ @timer_handlers.delete(handler)
280
+ end
281
+
282
+ def add_command_receiver(rd)
283
+ @command_receiver = CommandReceiver.new()
284
+ com_transporter = PlainTransporter.new(@net_multiplexer, @command_receiver, true, 8, false)
285
+ @command_receiver.init(@agent_id, rd, com_transporter)
286
+ @net_multiplexer.add_rudder_state(@command_receiver.rudder, RudderState.new(@command_receiver.rudder, com_transporter))
287
+ BayLog.info("ComRec=%s", @command_receiver)
288
+ end
289
+
290
+ def send_accepted_letter(st, client_rd, e, wakeup)
291
+ send_letter(Letter.new(Letter::ACCEPTED, st, client_rd, -1, nil, e), wakeup)
292
+ end
293
+
294
+ def send_connected_letter(st, e, wakeup)
295
+ send_letter(Letter.new(Letter::CONNECTED, st, nil, -1, nil, e), wakeup)
296
+ end
297
+ def send_read_letter(st, n, adr, e, wakeup)
298
+ send_letter(Letter.new(Letter::READ, st, nil, n, adr, e), wakeup)
299
+ end
300
+
301
+ def send_wrote_letter(st, n, e, wakeup)
302
+ send_letter(Letter.new(Letter::WROTE, st, nil, n, nil, e), wakeup)
303
+ end
304
+
305
+ def send_close_req_letter(st, wakeup)
306
+ send_letter(Letter.new(Letter::CLOSEREQ, st, nil, -1, nil, nil), wakeup)
307
+ end
308
+
309
+ def shutdown
310
+ BayLog.info("%s shutdown", self)
311
+ if @aborted
312
+ return
313
+ end
314
+
315
+ @aborted = true
316
+ BayLog.debug("%s shutdown netMultiplexer", self)
317
+ @net_multiplexer.shutdown()
318
+
319
+ GrandAgent.listeners.each do |lis|
320
+ lis.remove(@agent_id)
321
+ end
322
+ @command_receiver.end()
323
+ GrandAgent.agents.delete(@agent_id)
230
324
 
231
325
  if BayServer.harbor.multi_core
326
+ BayLog.debug("%s exit", self)
232
327
  exit(1)
233
328
  end
329
+ @agent_id = -1
234
330
  end
235
331
 
236
- def req_shutdown()
237
- @aborted = true
238
- wakeup()
332
+ def abort
333
+ BayLog.fatal("%s abort", self)
239
334
  end
240
335
 
241
- def reload_cert()
336
+
337
+ def reload_cert
242
338
  GrandAgent.anchorable_port_map.values().each do |port|
243
339
  if port.secure()
244
340
  begin
@@ -250,53 +346,265 @@ module Baykit
250
346
  end
251
347
  end
252
348
 
253
- def print_usage()
254
- # print memory usage
255
- BayLog.info("%s MemUsage", self)
256
- BayLog.info(" Ruby version: %s", RUBY_VERSION)
257
- memsize = ObjectSpace.memsize_of_all
258
- # formatted by comma
259
- msize_comma = memsize.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse.then do |str|
260
- str[0] == ',' ? str[1..-1] : str
349
+ def add_postpone(p)
350
+ @postpone_queue_lock.synchronize do
351
+ @postpone_queue << p
261
352
  end
262
- BayLog.info(" Total object size: %s bytes", msize_comma)
263
- MemUsage.get(@agent_id).print_usage(1)
264
353
  end
265
354
 
266
- def wakeup
267
- #BayLog.debug("%s wakeup", self)
268
- IOUtil.write_int32(@select_wakeup_pipe[1], 0)
355
+ def count_postpone
356
+ return @postpone_queue.length
269
357
  end
270
358
 
271
- def run_command_receiver(com_channel)
272
- @command_receiver = CommandReceiver.new(self, com_channel)
359
+ def req_catch_up
360
+ BayLog.debug("%s Req catchUp", self)
361
+ if count_postpone > 0
362
+ catch_up
363
+ else
364
+ begin
365
+ @command_receiver.send_command_to_monitor(self, CMD_CATCHUP, false)
366
+ rescue IOError => e
367
+ BayLog.error_e(e)
368
+ abort
369
+ end
370
+ end
273
371
  end
274
372
 
275
- def add_timer_handler(handler)
276
- @timer_handlers << handler
373
+ def catch_up
374
+ BayLog.debug("%s catchUp", self)
375
+ @postpone_queue_lock.synchronize do
376
+ if not @postpone_queue.empty?
377
+ r = @postpone_queue.shift
378
+ r.run()
379
+ end
380
+ end
277
381
  end
278
382
 
279
- def remove_timer_handler(handler)
280
- @timer_handlers.delete(handler)
383
+ #########################################
384
+ # Private methods
385
+ #########################################
386
+ private
387
+ def ring
388
+ BayLog.trace("%s Ring", self)
389
+ # timeout check
390
+ @timer_handlers.each do |h|
391
+ h.on_timer
392
+ end
393
+ @last_timeout_check = Time.now.tv_sec
281
394
  end
282
395
 
283
- private
284
- def on_waked_up(pipe_fd)
285
- #BayLog.debug("%s waked up", self)
286
- val = IOUtil.read_int32(pipe_fd)
396
+ def send_letter(let, wakeup)
397
+ @letter_queue_lock.synchronize do
398
+ @letter_queue << let
399
+ end
400
+
401
+ if wakeup
402
+ @recipient.wakeup
403
+ end
404
+ end
405
+
406
+ def on_accept(let)
407
+ begin
408
+ if let.err != nil
409
+ raise let.err
410
+ end
411
+
412
+ p = BayServer::anchorable_port_map[let.state.rudder]
413
+ p.on_connected(@agent_id, let.client_rudder)
414
+ rescue IOError => e
415
+ BayLog.error_e(e)
416
+ next_action(let.state, NextSocketAction::CLOSE, false)
417
+ rescue HttpException => e
418
+ BayLog.error_e(e)
419
+ let.client_rudder.close
420
+ end
421
+
422
+ if !@net_multiplexer.is_busy
423
+ let.state.multiplexer.next_accept(let.state)
424
+ end
287
425
  end
288
426
 
427
+ def on_connect(let)
428
+ st = let.state
429
+ if st.closed
430
+ BayLog.debug("%s Rudder is already closed: rd=%s", self, st.rudder);
431
+ return;
432
+ end
433
+
434
+ BayLog.debug("%s connected rd=%s", self, st.rudder)
435
+ next_act = nil
436
+ begin
437
+ if let.err != nil
438
+ raise let.err
439
+ end
440
+
441
+ next_act = st.transporter.on_connect(st.rudder)
442
+ BayLog.debug("%s nextAct=%s", self, next_act)
443
+ rescue IOError => e
444
+ st.transporter.on_error(st.rudder, e)
445
+ next_act = NextSocketAction::CLOSE
446
+ end
447
+
448
+ if next_act == NextSocketAction::READ
449
+ # Read more
450
+ st.multiplexer.cancel_write(st)
451
+ end
452
+
453
+ next_action(st, next_act, false)
454
+ end
455
+
456
+ def on_read(let)
457
+ st = let.state
458
+ if st.closed
459
+ BayLog.debug("%s Rudder is already closed: rd=%s", self, st.rudder)
460
+ return
461
+ end
462
+
463
+ begin
464
+ if let.err != nil
465
+ BayLog.debug("%s error on OS read %s", self, let.err)
466
+ raise let.err
467
+ end
468
+
469
+ BayLog.debug("%s read %d bytes (rd=%s)", self, let.n_bytes, st.rudder)
470
+ st.bytes_read += let.n_bytes
471
+
472
+ if let.n_bytes <= 0
473
+ st.read_buf.clear
474
+ next_act = st.transporter.on_read(st.rudder, "", let.address)
475
+ else
476
+ next_act = st.transporter.on_read(st.rudder, st.read_buf, let.address)
477
+ end
478
+
479
+ rescue => e
480
+ st.transporter.on_error(st.rudder, e)
481
+ next_act = NextSocketAction::CLOSE
482
+ end
483
+
484
+ next_action(st, next_act, true)
485
+ end
486
+
487
+ def on_wrote(let)
488
+ st = let.state
489
+ if st.closed
490
+ BayLog.debug("%s Rudder is already closed: rd=%s", self, st.rudder)
491
+ return
492
+ end
493
+
494
+ begin
495
+ if let.err != nil
496
+ BayLog.debug("%s error on OS write %s", self, let.err)
497
+ raise let.err
498
+ end
499
+
500
+ BayLog.debug("%s wrote %d bytes rd=%s qlen=%d", self, let.n_bytes, st.rudder, st.write_queue.length)
501
+ st.bytes_wrote += let.n_bytes
502
+
503
+ if st.write_queue.empty?
504
+ raise Sink("%s Write queue is empty: rd=%s", self, st.rudder)
505
+ end
506
+
507
+ unit = st.write_queue[0]
508
+ if unit.buf.length > 0
509
+ BayLog.debug("Could not write enough data buf_len=%d", unit.buf.length)
510
+ else
511
+ st.multiplexer.consume_oldest_unit(st)
512
+ end
513
+
514
+ write_more = true
515
+
516
+ st.writing_lock.synchronize do
517
+ if st.write_queue.empty?
518
+ write_more = false
519
+ st.writing = false
520
+ end
521
+ end
522
+
523
+ if write_more
524
+ st.multiplexer.next_write(st)
525
+ else
526
+ if st.finale
527
+ # close
528
+ BayLog.debug("%s finale return Close", self)
529
+ next_action(st, NextSocketAction::CLOSE, false)
530
+ else
531
+ # Write off
532
+ st.multiplexer.cancel_write(st)
533
+ end
534
+ end
535
+ rescue SystemCallError, IOError => e
536
+ BayLog.debug("%s IO error on wrote", self)
537
+ st.transporter.on_error(st.rudder, e)
538
+ next_action(st, NextSocketAction::CLOSE, false)
539
+ end
540
+ end
541
+
542
+ def on_close_req(let)
543
+ st = let.state
544
+ BayLog.debug("%s reqClose rd=%s", self, st.rudder)
545
+ if st.closed
546
+ BayLog.debug("%s Rudder is already closed: rd=%s", self, st.rudder)
547
+ return
548
+ end
549
+
550
+ st.multiplexer.close_rudder(st)
551
+ st.access
552
+ end
553
+
554
+ def next_action(st, act, reading)
555
+ BayLog.debug("%s next action: %s (reading=%s)", self, act, reading)
556
+ cancel = false
557
+
558
+ case(act)
559
+ when NextSocketAction::CONTINUE
560
+ if reading
561
+ st.multiplexer.next_read(st)
562
+ end
563
+
564
+ when NextSocketAction::READ
565
+ st.multiplexer.next_read(st)
566
+
567
+ when NextSocketAction::WRITE
568
+ if reading
569
+ cancel = true
570
+ end
571
+
572
+ when NextSocketAction::CLOSE
573
+ if reading
574
+ cancel = true
575
+ end
576
+ st.multiplexer.close_rudder(st)
577
+
578
+ when NextSocketAction::SUSPEND
579
+ if reading
580
+ cancel = true
581
+ end
582
+
583
+ else
584
+ raise ArgumentError.new("Invalid action: #{act}")
585
+
586
+ end
587
+
588
+ if cancel
589
+ st.multiplexer.cancel_read(st)
590
+ st.reading_lock.synchronize do
591
+ BayLog.debug("%s Reading off %s", self, st.rudder)
592
+ st.reading = false
593
+ end
594
+ end
595
+
596
+ st.access
597
+ end
598
+
599
+
289
600
  ######################################################
290
601
  # class methods
291
602
  ######################################################
292
- def GrandAgent.init(agt_ids, anchorable_port_map, unanchorable_port_map, max_ships, multi_core)
603
+ def GrandAgent.init(agt_ids, max_ships)
293
604
  @agent_count = agt_ids.length
294
- @anchorable_port_map = anchorable_port_map
295
- @unanchorable_port_map = unanchorable_port_map != nil ? unanchorable_port_map : {}
296
605
  @max_ships = max_ships
297
- @multi_core = multi_core
298
606
 
299
- if(BayServer.harbor.multi_core?)
607
+ if BayServer.harbor.multi_core
300
608
  agt_ids.each do | agt_id |
301
609
  add(agt_id, true)
302
610
  end
@@ -320,8 +628,10 @@ module Baykit
320
628
  @agents[agt_id] = agt
321
629
 
322
630
  @listeners.each do |lis|
323
- lis.add(agt)
631
+ lis.add(agt.agent_id)
324
632
  end
633
+
634
+ return agt
325
635
  end
326
636
 
327
637
  def GrandAgent.add_lifecycle_listener(lis)