dango 0.0.13 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -3,6 +3,8 @@ README.txt
3
3
  lib/dango/client_framework.rb
4
4
  lib/dango/framework_base.rb
5
5
  lib/dango/server_framework.rb
6
+ lib/dango/mutex_socket_list.rb
7
+ lib/dango/socket_list.rb
6
8
  lib/dango/monitor/dango_monitor_client.rb
7
9
  lib/dango/script/dango_server.rb
8
10
  lib/dango/shared/memory_store.rb
@@ -98,41 +98,40 @@ class DangoClientFramework
98
98
  loop do
99
99
  begin
100
100
  if @sock
101
- cs_logger.debug "#{@sid}:start receive_data"
101
+ cs_logger.debug "start receive_data #{@sid}"
102
102
  ret_obj = nil
103
103
  begin
104
104
  ret_obj = cs_receive_data(@sock) # データ受信処理
105
105
  rescue DangoFrameworkConnectionError
106
- cs_logger.debug "#{@sid}:sock error:#{error_message($!, 'u')}"
106
+ cs_logger.debug "sock error #{@sid}:#{error_message($!, 'u')}"
107
107
  throw(:session_closed)
108
108
  rescue DangoFrameworkError
109
- cs_logger.debug "#{@sid}:sock error:#{error_message($!, 'u')}"
109
+ cs_logger.debug "sock error #{@sid}:#{error_message($!, 'u')}"
110
110
  throw(:session_closed)
111
111
  end
112
- cs_logger.debug "#{@sid}:finished cs_receive_data:#{ret_obj.inspect}"
112
+ cs_logger.debug "finished cs_receive_data:#{ret_obj.inspect} #{@sid}"
113
113
 
114
114
  if !(ret_obj && ret_obj["notice"])
115
- cs_logger.info "#{@sid}:no notice:#{ret_obj["notice"].inspect}"
115
+ cs_logger.info "no notice:#{ret_obj["notice"].inspect} #{@sid}"
116
116
  end
117
117
 
118
118
  # メソッド呼び出し
119
- cs_logger.info "#{@sid}:calling method=#{ret_obj["notice"].inspect}"
119
+ cs_logger.info "calling method=#{ret_obj["notice"].inspect} #{@sid}"
120
120
  begin
121
121
  __send__("cs_receive_#{ret_obj["notice"]}", ret_obj)
122
122
  rescue
123
- cs_logger.error "error in cs_receive_#{ret_obj["notice"]}\n#{error_message($!, 'u')}"
123
+ cs_logger.error "error in cs_receive_#{ret_obj["notice"]} #{@sid}\n#{error_message($!, 'u')}"
124
124
  ensure
125
125
  # データの受信の送信返し
126
126
  response_notice_name = "_response"
127
127
  send_obj = {"_id" => ret_obj["_id"]}
128
128
  cs_client_send_data(response_notice_name, send_obj)
129
- cs_logger.debug "#{@sid}:response_notice_name=#{response_notice_name.inspect}"
129
+ cs_logger.debug "response_notice_name=#{response_notice_name.inspect} #{@sid}"
130
130
  end
131
- cs_logger.debug "#{@sid}:called method=#{ret_obj["notice"].inspect}"
132
-
131
+ cs_logger.debug "called method=#{ret_obj["notice"].inspect} #{@sid}"
133
132
 
134
133
  else
135
- sleep 0.1
134
+ sleep 0.01
136
135
  end
137
136
 
138
137
  rescue Exception
@@ -150,6 +149,10 @@ class DangoClientFramework
150
149
  @sid = rec_obj["_sid"]
151
150
  end
152
151
 
152
+ # システムからのメッセージ通知なら
153
+ def cs_receive__notice_system_message(rec_obj)
154
+ end
155
+
153
156
  # ユーザーが終了させたい時に呼び出すメソッド(セッションを切る)
154
157
  def cs_client_close
155
158
  client_quit() # クライアントの終了処理
@@ -188,7 +191,7 @@ class DangoClientFramework
188
191
 
189
192
  # クライアントからサーバーへのデータ送信
190
193
  def cs_client_send_data(action_name, send_obj)
191
- cs_logger.debug "cs_client_send_data(#{action_name.inspect}, #{send_obj['_id']}) #{Time.now}"
194
+ cs_logger.debug "cs_client_send_data(#{action_name.inspect}, #{send_obj['_id']}) #{Time.now_to_s}"
192
195
  send_obj_dup = send_obj.dup
193
196
  send_obj_dup["action"] = action_name.to_s
194
197
  cs_send_data(@sock, send_obj_dup)
@@ -41,8 +41,10 @@ end
41
41
 
42
42
  # Timeクラスの表示形式修正
43
43
  class Time
44
- def to_s
45
- self.strftime("%Y-%m-%d %H:%M:%S")
44
+ def self.now_to_s
45
+ # self.strftime("%Y-%m-%d %H:%M:%S")
46
+ now = self.now
47
+ now.strftime("%Y-%m-%dT%H:%M:%S.") << "%06d " % now.usec
46
48
  end
47
49
  end
48
50
 
@@ -59,7 +59,21 @@ class DangoMonitorClient < DangoClientFramework
59
59
  raise("monitor_server_reload Timeout error")
60
60
  end
61
61
  raise("code is not 0 (faild). :code=#{ret_obj['code']}") if ret_obj['code'] != 0
62
- ret_obj['all_info']
62
+ true
63
+ end
64
+
65
+ # システムメッセージの送信
66
+ def send_system_message(message = "")
67
+ cs_logger.debug "DangoMonitorClient:send_system_message"
68
+ begin
69
+ ret_obj = cs_client_send_receive_data('monitor_send_system_message', {:message=>message})
70
+ rescue DangoFrameworkConnectionError
71
+ raise("monitor_send_system_message Connection error")
72
+ rescue DangoFrameworkTimeoutError
73
+ raise("monitor_send_system_message Timeout error")
74
+ end
75
+ raise("code is not 0 (faild). :code=#{ret_obj['code']}") if ret_obj['code'] != 0
76
+ true
63
77
  end
64
78
  end
65
79
 
@@ -0,0 +1,35 @@
1
+ #!ruby -Ku
2
+
3
+ # ソケット毎用のmutex
4
+ class MutexSocketList
5
+ def initialize()
6
+ @msl_hash = Hash.new
7
+ end
8
+
9
+ def keys()
10
+ @msl_hash.keys
11
+ end
12
+
13
+ def delete(key)
14
+ @msl_hash.delete(key)
15
+ end
16
+
17
+ def add(key)
18
+ @msl_hash[key] = Mutex.new
19
+ end
20
+
21
+ # def [](key)
22
+ # raise("not exist key(#{key})") if ! @msl_hash.has_key?(key)
23
+ # add(key) if @msl_hash.has_key?(key)
24
+ # @msl_hash[key]
25
+ # end
26
+
27
+ def synchronize(key)
28
+ raise("not exist key(#{key})") if ! @msl_hash.has_key?(key)
29
+ ret = nil
30
+ @msl_hash[key].synchronize do
31
+ ret = yield
32
+ end
33
+ ret
34
+ end
35
+ end
@@ -9,6 +9,8 @@ require 'ipaddr'
9
9
  require 'digest/md5'
10
10
 
11
11
  require "dango/framework_base"
12
+ require "dango/socket_list"
13
+ require "dango/mutex_socket_list"
12
14
 
13
15
  # フレームワーククラス
14
16
  class DangoServerFramework
@@ -54,7 +56,7 @@ class DangoServerFramework
54
56
 
55
57
  loop do # 待ちで無限ループに入る
56
58
  sleep 3
57
- # p( Time.now.to_s + ":#{@server_reload.inspect}")
59
+ # p( Time.now_to_s + ":#{@server_reload.inspect}")
58
60
  check_reload_class()
59
61
  end
60
62
  end
@@ -71,14 +73,17 @@ class DangoServerFramework
71
73
  set_server_variables() # 各種サーバー仕様の変数設定
72
74
 
73
75
  cs_logger.debug("===== server initialize =====") # loggerの準備
76
+ cs_logger.debug("Process.pid=#{Process.pid}")
74
77
  cs_logger.debug("port=#{@connection_server_port}")
75
78
  cs_logger.debug("host=#{@connection_server_host}")
76
79
  cs_logger.debug("max_connections=#{@connection_server_max_connections}")
77
80
 
81
+ open("tmp/pids/dango.pid", "wb"){|fh| fh.write Process.pid.to_s }
82
+
78
83
  shared_init() # 共有メモリを初期化
79
84
  notice_shared_init() # 通知共有メモリを初期化
80
85
  socket_list_init() # ソケット一覧を初期化
81
- mutex_socket_list_init(cs_logger()) # ソケット毎用のmutexを初期化
86
+ mutex_socket_list_init() # ソケット毎用のmutexを初期化
82
87
  @mutex_proc_thread = Mutex.new # スレッドが同時起動しないようにするためのMutex
83
88
  cs_server_init() # 初期設定読み込み
84
89
 
@@ -175,20 +180,21 @@ class DangoServerFramework
175
180
  cs_logger.debug "#{sock} is accepted. key=#{Thread.current.object_id}"
176
181
 
177
182
  begin
178
- session[:session_id] = Thread.current.object_id
179
- session_id = session[:session_id]
180
- socket_list.add(session_id, sock)
181
- mutex_socket_list.add(session_id)
182
-
183
183
  sock.binmode
184
184
  sock.sync = true
185
185
 
186
+ session[:sid] = Thread.current.object_id
187
+ sid = session[:sid]
188
+ socket_list.add(sid, sock)
189
+ mutex_socket_list.add(sid)
190
+
186
191
  cs_connect() # 接続時メソッド呼び出し
187
192
 
188
193
  # 接続直後のsid通知
189
194
  # Thread.start do
190
- send_obj = {"_sid"=>session_id}
191
- cs_server_send_data(session_id, "_notice_sid", send_obj)
195
+ send_obj = {"_sid"=>sid}
196
+ cs_server_send_data(sid, "_notice_sid", send_obj)
197
+ cs_logger.debug "_notice_sid #{sid} #{Time.now_to_s}"
192
198
  # end
193
199
 
194
200
  # メインループ入り
@@ -225,24 +231,28 @@ class DangoServerFramework
225
231
  @mutex_proc_thread.synchronize do
226
232
 
227
233
  if action_name == '_response' # 送信パケットの返事なら
228
- # cs_logger.debug "action_name=#{action_name} #{Time.now}"
234
+ # cs_logger.debug "action_name=#{action_name} #{Time.now_to_s}"
229
235
  action_name_is_response(ret_obj)
230
236
 
231
237
  elsif action_name == 'monitor_all_info' # メンテナンスアクション名なら
232
238
  cs_check_monitor_error(sock, "return_monitor_all_info")
233
- cs_server_monitor_all_info()
239
+ cs_server_monitor_all_info(ret_obj)
234
240
 
235
241
  elsif action_name == 'monitor_server_reload' # メンテナンスアクション名なら
236
242
  cs_check_monitor_error(sock, "return_monitor_server_reload")
237
- cs_server_monitor_server_reload()
243
+ cs_server_monitor_server_reload(ret_obj)
244
+
245
+ elsif action_name == 'monitor_send_system_message' # メンテナンスアクション名なら
246
+ cs_check_monitor_error(sock, "return_monitor_send_system_message")
247
+ cs_server_monitor_send_system_message(ret_obj)
238
248
 
239
249
  else # メンテナンスコマンド以外のユーザーアクション名なら
240
- cs_logger.debug "action_name=#{action_name} #{Time.now}"
250
+ cs_logger.debug "action_name=#{action_name} #{sid} #{Time.now_to_s}"
241
251
  begin
242
252
  __send__("cs_receive_#{action_name}", ret_obj)
243
253
  rescue NoMethodError
244
254
  @recv_fail_count += 1 if @recv_fail_count # 受信失敗回数カウント
245
- cs_logger.error "not find action #{action_name}:#{error_message($!, 'u')}"
255
+ cs_logger.error "not find action #{action_name}:#{sid}:#{error_message($!, 'u')}"
246
256
  end
247
257
  end
248
258
 
@@ -251,17 +261,17 @@ class DangoServerFramework
251
261
  end
252
262
 
253
263
  rescue DangoFrameworkReadNoDataError
254
- cs_logger.debug "read no data. maybe client closed. #{Thread.current.object_id} #{Time.now}"
264
+ cs_logger.debug "read no data. maybe client closed. #{Thread.current.object_id} #{Time.now_to_s}"
255
265
 
256
266
  rescue DangoFrameworkConnectionError
257
267
  @recv_fail_count += 1 if @recv_fail_count # 受信失敗回数カウント
258
- cs_logger.debug "connection error. #{Thread.current.object_id} #{Time.now}\n#{error_message($!, 'u')}"
268
+ cs_logger.debug "connection error. #{Thread.current.object_id} #{Time.now_to_s}\n#{error_message($!, 'u')}"
259
269
 
260
270
  rescue DangoFrameworkError
261
- cs_logger.debug "DangoFrameworkError. #{Thread.current.object_id} #{Time.now}\n#{error_message($!, 'u')}"
271
+ cs_logger.debug "DangoFrameworkError. #{Thread.current.object_id} #{Time.now_to_s}\n#{error_message($!, 'u')}"
262
272
 
263
273
  rescue Exception
264
- cs_logger.error "Exception #{Thread.current.object_id} #{Time.now} #{error_message($!, 'u')}"
274
+ cs_logger.error "Exception #{Thread.current.object_id} #{Time.now_to_s} #{error_message($!, 'u')}"
265
275
 
266
276
  ensure
267
277
  cs_logger.debug "#{sock.inspect} #{Thread.current.object_id} is gone"
@@ -337,12 +347,9 @@ class DangoServerFramework
337
347
  def shared_init
338
348
  @shared = select_shared_database_manager()
339
349
  end
340
- def shared
341
- @shared
342
- end
350
+ attr_accessor(:shared)
343
351
 
344
352
  # 通知共有メモリ
345
-
346
353
  def notice_shared_init
347
354
  # @notice_shared = NoticeShared.new(self)
348
355
  @notice_shared = select_shared_database_manager()
@@ -364,24 +371,24 @@ class DangoServerFramework
364
371
  end
365
372
  end
366
373
 
367
- def add_connectable(key, session_id) # 接続許可にする
374
+ def add_connectable(key, sid) # 接続許可にする
368
375
  if !self[:notice_list].has_key?(key) # キーがなければ
369
376
  raise(DangoFrameworkError, "notice_list not has key. key=#{key.inspect}")
370
377
  end
371
378
 
372
379
  self.transaction(:notice_list) do |notice_list|
373
- notice_list[key] << session_id
380
+ notice_list[key] << sid
374
381
  notice_list
375
382
  end
376
383
  end
377
384
 
378
- def remove_connectable(key, session_id) # 接続を不許可にする
385
+ def remove_connectable(key, sid) # 接続を不許可にする
379
386
  if !self[:notice_list].has_key?(key) # キーがなければ
380
387
  raise(DangoFrameworkError, "notice_list not has key. key=#{key.inspect}")
381
388
  end
382
389
 
383
390
  self.transaction(:notice_list) do |notice_list|
384
- notice_list[key].delete(session_id)
391
+ notice_list[key].delete(sid)
385
392
  notice_list
386
393
  end
387
394
  end
@@ -403,8 +410,8 @@ class DangoServerFramework
403
410
 
404
411
  @parent.cs_logger.debug "self.get_connectables(key):#{self.get_connectables(key).inspect}"
405
412
 
406
- self.get_connectables(key).each do |session_id|
407
- @parent.cs_server_send_data(session_id, "notice_shared_#{key}".to_sym, value)
413
+ self.get_connectables(key).each do |sid|
414
+ @parent.cs_server_send_data(sid, "notice_shared_#{key}".to_sym, value)
408
415
  end
409
416
  end
410
417
 
@@ -425,10 +432,7 @@ class DangoServerFramework
425
432
  end
426
433
  end
427
434
  end
428
-
429
- def notice_shared
430
- @notice_shared
431
- end
435
+ attr_accessor(:notice_shared)
432
436
 
433
437
  # セッション
434
438
  class Session < Hash
@@ -439,87 +443,16 @@ class DangoServerFramework
439
443
  end
440
444
 
441
445
  # ソケットとIDの対応
442
- class SocketList
443
- def initialize
444
- @sl_hash = Hash.new
445
- @sl_mutex = Mutex.new
446
- end
447
- def keys()
448
- @sl_mutex.synchronize do
449
- @sl_hash.keys
450
- end
451
- end
452
- def delete(key)
453
- @sl_mutex.synchronize do
454
- @sl_hash.delete(key)
455
- end
456
- end
457
- def add(key, sock)
458
- @sl_mutex.synchronize do
459
- raise("already exist key(#{key})") if @sl_hash.has_key?(key)
460
- raise("sock(#{sock.ins}) is not Socket") if sock.kind_of?(Socket)
461
- @sl_hash[key] = sock
462
- end
463
- end
464
- def [](key)
465
- @sl_mutex.synchronize do
466
- raise("not exist key(#{key})") if ! @sl_hash.has_key?(key)
467
- @sl_hash[key]
468
- end
469
- end
470
- def []=(key, sock)
471
- @sl_mutex.synchronize do
472
- raise("not exist key(#{key})") if ! @sl_hash.has_key?(key)
473
- raise("sock(#{sock.class}) is not Socket") if sock.kind_of?(Socket)
474
- @sl_hash[key] = sock
475
- end
476
- end
477
- end
478
446
  def socket_list_init
479
447
  @socket_list = SocketList.new
480
448
  end
481
- def socket_list
482
- @socket_list
483
- end
449
+ attr_accessor(:socket_list)
484
450
 
485
451
  # ソケット毎用のmutex
486
- class MutexSocketList
487
- def initialize(cs_logger)
488
- @cs_logger = cs_logger
489
- @msl_hash = Hash.new
490
- end
491
- def keys()
492
- @msl_hash.keys
493
- end
494
- def delete(key)
495
- @msl_hash.delete(key)
496
- end
497
- def add(key)
498
- @msl_hash[key] = Mutex.new
499
- end
500
- # def [](key)
501
- # raise("not exist key(#{key})") if ! @msl_hash.has_key?(key)
502
- ## add(key) if @msl_hash.has_key?(key)
503
- # @msl_hash[key]
504
- # end
505
- def synchronize(key)
506
- raise("not exist key(#{key})") if ! @msl_hash.has_key?(key)
507
- # @cs_logger.debug("mutex_socket_list starting synchronize. key=#{key}")
508
- ret = nil
509
- @msl_hash[key].synchronize do
510
- # @cs_logger.debug("mutex_socket_list started synchronize. key=#{key}")
511
- ret = yield
512
- # @cs_logger.debug("mutex_socket_list finished synchronize. key=#{key}")
513
- end
514
- ret
515
- end
516
- end
517
- def mutex_socket_list_init(cs_logger)
518
- @mutex_sock_list = MutexSocketList.new(cs_logger) # ソケット毎用のmutex
519
- end
520
- def mutex_socket_list
521
- @mutex_sock_list
452
+ def mutex_socket_list_init()
453
+ @mutex_socket_list = MutexSocketList.new() # ソケット毎用のmutex
522
454
  end
455
+ attr_accessor(:mutex_socket_list)
523
456
 
524
457
  # 接続時に呼び出されるメソッド
525
458
  def cs_connect; end
@@ -545,22 +478,23 @@ class DangoServerFramework
545
478
  end
546
479
 
547
480
  # サーバーからクライアントへのデータ送信
548
- def cs_server_send_data(session_id, notice_name, send_obj, options = {})
549
- # cs_logger.debug "cs_server_send_data(#{session_id.inspect}, #{notice_name.inspect}, #{send_obj.inspect}, #{options.inspect})"
481
+ def cs_server_send_data(sid, notice_name, send_obj, options = {})
482
+ cs_logger.debug "cs_server_send_data(#{sid.inspect}, #{notice_name.inspect}) #{Time.now_to_s}"
483
+ # cs_logger.debug "cs_server_send_data(#{sid.inspect}, #{notice_name.inspect}, #{send_obj.inspect}, #{options.inspect})"
550
484
 
551
485
  raise(DangoFrameworkError, "send_obj is not Hash.") if ! send_obj.is_a?(Hash)
552
486
 
553
487
  timeout = options[:timeout] || SendReceiveTimeoutDefaultSec
554
488
  dtype = options[:type] || 0
555
489
 
556
- sock = socket_list[session_id]
557
- raise(DangoFrameworkError, "not found session_id=#{session_id}") if !sock
558
- raise(DangoFrameworkConnectionError, "socket closed. s_id=#{session_id}") if sock.closed?
490
+ sock = socket_list[sid]
491
+ raise(DangoFrameworkError, "not found sid=#{sid}") if !sock
492
+ raise(DangoFrameworkConnectionError, "socket closed. s_id=#{sid}") if sock.closed?
559
493
 
560
494
  # cs_logger.debug "cs_server_send_data:sending data notice_name=#{notice_name.inspect}"
561
495
  send_obj_dup = send_obj.deep_dup
562
496
  send_obj_dup["notice"] = notice_name.to_s
563
- send_obj_dup["_id"] = Digest::MD5.hexdigest(session_id.to_s + Time.now.to_i.to_s)
497
+ send_obj_dup["_id"] = Digest::MD5.hexdigest(sid.to_s + Time.now.to_i.to_s)
564
498
 
565
499
  # データ送信時にクライアントからのレスポンスを確認する(Flashのバグらしきもの対策)
566
500
  # if notice_name.to_s =~ /^return_/
@@ -573,22 +507,23 @@ class DangoServerFramework
573
507
  else
574
508
  Thread.start do
575
509
  begin
576
- mutex_socket_list.synchronize(session_id) do
510
+ mutex_socket_list.synchronize(sid) do
511
+ cs_logger.warn "mutex_socket_list:synchronize:#{notice_name} #{sid} #{Time.now_to_s}"
577
512
  have_came_response = nil
578
513
 
579
514
  receive_thread = Thread.start do
580
515
  begin
581
516
  cs_send_data(sock, send_obj_dup, :type=>dtype) # データ送信
582
- # cs_logger.debug "cs_server_send_data:send data notice_name=#{notice_name.inspect} #{send_obj_dup['_id']}"
517
+ cs_logger.debug "cs_server_send_data:sent:notice_name=#{notice_name.inspect} #{sid} #{Time.now_to_s}"
583
518
  rescue DangoFrameworkError
584
- cs_logger.debug "cs_server_send_data:DangoFrameworkError:notice_name=#{notice_name.inspect}\n"
519
+ cs_logger.debug "ERROR:cs_server_send_data:DangoFrameworkError:notice_name=#{notice_name.inspect} #{sid} #{Time.now_to_s}"
585
520
  end
586
521
  end
587
522
 
588
523
  # データ送信スレッドの終了待ち
589
524
  if !receive_thread.join(SendTimeoutSec)
590
525
  # タイムアウトの場合は
591
- cs_logger.warn "cs_server_send_data:send timeout:#{notice_name}"
526
+ cs_logger.warn "cs_server_send_data:send timeout:#{notice_name} #{sid} #{Time.now_to_s}"
592
527
  else
593
528
  end_reserved_time = Time.now + timeout
594
529
 
@@ -603,7 +538,7 @@ class DangoServerFramework
603
538
  @mutex_send_response.synchronize do
604
539
  if @arr_send_response.delete(send_obj_dup["_id"])
605
540
  have_came_response = true
606
- # cs_logger.debug "response check ok:#{notice_name} #{send_obj_dup['_id'].inspect}"
541
+ # cs_logger.debug "response check ok:#{notice_name} #{sid} #{Time.now_to_s}"
607
542
  throw(:send_timeout)
608
543
  end
609
544
  end
@@ -613,7 +548,7 @@ class DangoServerFramework
613
548
  end # catch 終わり
614
549
 
615
550
  if !have_came_response # 戻ってきたデータがあるかどうかチェック
616
- cs_logger.warn "!!! received data is none. #{notice_name} #{send_obj_dup['_id']}"
551
+ cs_logger.warn "!!! received data is none:#{notice_name} #{sid} #{Time.now_to_s}"
617
552
  end
618
553
  end
619
554
  end
@@ -643,13 +578,13 @@ class DangoServerFramework
643
578
 
644
579
  if !match
645
580
  send_obj = {'code'=>1, 'message'=>"error your ip address"}
646
- cs_server_send_data(session[:session_id], return_command, send_obj, :type=>2)
581
+ cs_server_send_data(session[:sid], return_command, send_obj, :type=>2)
647
582
  raise(DangoFrameworkError)
648
583
  end
649
584
  end
650
585
 
651
586
  # メンテナンスコマンド:サーバーからクライアントへのデータ送信
652
- def cs_server_monitor_all_info()
587
+ def cs_server_monitor_all_info(ret_obj)
653
588
  all_info = {
654
589
  'shareds' => shared.to_hash,
655
590
  'socket_list' => socket_list.keys,
@@ -665,16 +600,31 @@ class DangoServerFramework
665
600
  }
666
601
 
667
602
  send_obj = {'code'=>0, 'all_info'=>all_info}
668
- cs_server_send_data(session[:session_id], 'return_monitor_all_info', send_obj, :type=>2)
603
+ cs_server_send_data(session[:sid], 'return_monitor_all_info', send_obj, :type=>2)
669
604
  end
670
605
 
671
- # メンテナンスコマンド:サーバーからクライアントへのデータ送信
672
- def cs_server_monitor_server_reload()
606
+ # メンテナンスコマンド:サーバーデータ再読み込み
607
+ def cs_server_monitor_server_reload(ret_obj)
673
608
  cs_logger.debug "cs_server_monitor_server_reload"
674
609
  @server_reload = true
675
610
  cs_logger.debug "@server_reload=#{@server_reload.inspect}"
676
611
  send_obj = {'code'=>0, 'message'=>'success'}
677
- cs_server_send_data(session[:session_id], 'return_monitor_server_reload', send_obj, :type=>2)
612
+ cs_server_send_data(session[:sid], 'return_monitor_server_reload', send_obj, :type=>2)
613
+ end
614
+
615
+ # メンテナンスコマンド:接続者全員へメッセージ送信
616
+ def cs_server_monitor_send_system_message(ret_obj)
617
+ cs_logger.debug "cs_server_monitor_send_system_message:#{ret_obj.inspect}"
618
+
619
+ # 全員へ_notice_system_messageを通知
620
+ socket_list.keys.each do |sid|
621
+ next if session[:sid] == sid
622
+ send_obj = { "message" => ret_obj["message"] }
623
+ cs_server_send_data(sid, '_notice_system_message', send_obj)
624
+ end
625
+
626
+ send_obj = {'code'=>0, }
627
+ cs_server_send_data(session[:sid], 'return_monitor_send_system_message', send_obj, :type=>2)
678
628
  end
679
629
 
680
630
  # 必要に応じて追加するメソッド
@@ -687,7 +637,8 @@ class DangoServerFramework
687
637
  end
688
638
  end
689
639
 
690
- def method_action_notice_shared(key, name, *args) # 共有メモリ変更通知のメソッド
640
+ # 共有メモリ変更通知のメソッド
641
+ def method_action_notice_shared(key, name, *args)
691
642
  cs_logger.debug "method_name:#{name}"
692
643
  send_obj = args[0]
693
644
  notice_shared.notice_change(key.to_sym, send_obj)
@@ -700,8 +651,8 @@ class DangoServerFramework
700
651
  options = options.deep_dup
701
652
  options[:rate] ||= 10
702
653
 
703
- wait_msec = 1.0 / options[:rate]
704
- cs_logger.debug "flameloop_thread wait_msec:#{wait_msec.inspect}"
654
+ wait_sec = 1.0 / options[:rate]
655
+ cs_logger.debug "flameloop_thread wait_sec:#{wait_sec.inspect}"
705
656
 
706
657
  Thread.new do
707
658
  loop do
@@ -712,7 +663,7 @@ class DangoServerFramework
712
663
  cs_logger.debug "flameloop_thread error:#{error_message($!, 'u')}"
713
664
  end
714
665
  end
715
- sleep wait_msec
666
+ sleep wait_sec
716
667
  end
717
668
  end
718
669
 
@@ -0,0 +1,44 @@
1
+ #!ruby -Ku
2
+
3
+ # ソケットとIDの対応
4
+ class SocketList
5
+ def initialize
6
+ @sl_hash = Hash.new
7
+ @sl_mutex = Mutex.new
8
+ end
9
+
10
+ def keys()
11
+ @sl_mutex.synchronize do
12
+ @sl_hash.keys
13
+ end
14
+ end
15
+
16
+ def delete(key)
17
+ @sl_mutex.synchronize do
18
+ @sl_hash.delete(key)
19
+ end
20
+ end
21
+
22
+ def add(key, sock)
23
+ @sl_mutex.synchronize do
24
+ raise("already exist key(#{key})") if @sl_hash.has_key?(key)
25
+ raise("sock(#{sock.ins}) is not Socket") if sock.kind_of?(Socket)
26
+ @sl_hash[key] = sock
27
+ end
28
+ end
29
+
30
+ def [](key)
31
+ @sl_mutex.synchronize do
32
+ raise("not exist key(#{key})") if ! @sl_hash.has_key?(key)
33
+ @sl_hash[key]
34
+ end
35
+ end
36
+
37
+ def []=(key, sock)
38
+ @sl_mutex.synchronize do
39
+ raise("not exist key(#{key})") if ! @sl_hash.has_key?(key)
40
+ raise("sock(#{sock.class}) is not Socket") if sock.kind_of?(Socket)
41
+ @sl_hash[key] = sock
42
+ end
43
+ end
44
+ end
@@ -32,6 +32,33 @@ namespace :dango do
32
32
  puts "complete server_reload."
33
33
  end
34
34
 
35
+ desc "send_system_message" # システムメッセージ配信
36
+ task :send_system_message do
37
+ puts "send_system_message"
38
+ message_type = ENV["TYPE"]
39
+ if ! message_type
40
+ puts "please type. ex. rake dango:monitor:send_system_message TYPE=down_1min"
41
+ exit
42
+ end
43
+
44
+ require "yaml"
45
+ require 'dango/monitor/dango_monitor_client.rb'
46
+
47
+ system_message = YAML.load(open("dango/config/system_message.yml", "rb"){|fh| fh.read})
48
+ message = system_message[message_type]
49
+
50
+ begin
51
+ dm = DangoMonitorClient.new()
52
+ p dm.send_system_message(message)
53
+ dm.cs_client_close
54
+ rescue
55
+ p $!.class
56
+ p $!.message
57
+ p $!.backtrace
58
+ end
59
+ puts "complete server_reload."
60
+ end
61
+
35
62
  end
36
63
 
37
64
  end
@@ -43,8 +43,9 @@ end
43
43
 
44
44
  # テスト接続用のクラス
45
45
  class TestClient < DangoClientFramework
46
- SendReceiveSleepIntervalSec = 0.001 # データ送信後の順の際のタイムアウトチェック間隔秒
47
- SendReceiveTimeoutSec = 10 # データ送信後の順の際のタイムアウトチェック間隔秒
46
+ SendReceiveSleepIntervalSec = 0.001 # データ送信後の順の際のタイムアウトチェック間隔秒
47
+ ReceiveWaitTimeoutSec = 10 # データ受信確認の待ち秒数
48
+ ReceiveTrapTimeoutSec = 0 # データ受信確認のトラップの秒数
48
49
 
49
50
  def initialize(env, config, c_name)
50
51
  @config = config
@@ -55,6 +56,8 @@ class TestClient < DangoClientFramework
55
56
  @receive_mutex = Mutex.new # 送受信用の排他処理
56
57
  @receive_arr = []
57
58
 
59
+ @receive_methods = []
60
+
58
61
  send_receive_shared_init()
59
62
  end
60
63
 
@@ -79,34 +82,50 @@ class TestClient < DangoClientFramework
79
82
 
80
83
  # テストサーバー
81
84
  def send(name, send_obj = {})
82
- cs_logger.debug("tester.send:send_obj=#{send_obj.inspect} #{Time.now}")
85
+ cs_logger.debug("tester.send:send_obj=#{send_obj.inspect} #{Time.now_to_s}")
83
86
  cs_client_send_data(name, send_obj)
84
87
  end
85
88
 
86
89
  # trap_receive_data
87
90
  def trap_receive_data(receive_name, options = {})
88
- cs_logger.debug "trap_receive_data:#{receive_name}:#{Time.now}"
89
- timeout = options[:timeout] || SendReceiveTimeoutSec
91
+ cs_logger.debug "trap_receive_data:#{receive_name}:#{Time.now_to_s}"
92
+ timeout = options[:timeout] || ReceiveTrapTimeoutSec
93
+ trap_proc = options[:proc] || nil
94
+ raise(ArgumentError, ":proc is not Proc class.") if trap_proc && ! trap_proc.is_a?(Proc)
95
+
96
+ cs_logger.debug "trap_receive_data:trap_proc:#{trap_proc.inspect}"
90
97
 
91
98
  Thread.start do
92
99
  send_receive_shared[receive_name] = nil
93
- end_reserved_time = Time.now + timeout
100
+ if timeout == 0
101
+ end_reserved_time = Time.at(0) # 0ならepochを入れる
102
+ else
103
+ end_reserved_time = Time.now + timeout # 0以上ならタイムアウト時間決定
104
+ end
94
105
 
95
- # 戻ってきたデータのチェック
106
+ # 戻ってきたデータのチェックメソッド
96
107
  receive_name_sym = (receive_name.class == Symbol) ? (":"+receive_name.to_s) : ('"'+receive_name+'"')
97
- instance_method_name = "cs_receive_#{receive_name}"
98
- expr = <<-EOF
99
- def self.#{instance_method_name}(ret_obj)
100
- cs_logger.debug "ret_obj:" + ret_obj.inspect
101
- send_receive_shared[#{receive_name_sym}] = ret_obj
102
- end
103
- EOF
104
- instance_eval expr
108
+
109
+ if ! @receive_methods.include?(receive_name_sym)
110
+ instance_method_name = "cs_receive_#{receive_name}"
111
+ expr = <<-EOF
112
+ def self.#{instance_method_name}(ret_obj)
113
+ cs_logger.debug "ret_obj:" + ret_obj.inspect
114
+ send_receive_shared[#{receive_name_sym}] = ret_obj
115
+ end
116
+ EOF
117
+ instance_eval expr
118
+ @receive_methods.push(receive_name_sym)
119
+ end
105
120
 
106
121
  # タイムアウトチェック
107
- (timeout.to_f / SendReceiveSleepIntervalSec).to_i.times do
108
- if Time.now > end_reserved_time
109
- raise(DangoFrameworkTimeoutError, "timeout:client_name=#{@client_name}:receive_name=#{receive_name}")
122
+ cs_logger.debug "trap_receive_data:666:#{end_reserved_time}"
123
+ loop do
124
+ # (timeout.to_f / SendReceiveSleepIntervalSec).to_i.times do
125
+ # if end_reserved_time < Time.now
126
+
127
+ if end_reserved_time != Time.at(0) && end_reserved_time < Time.now
128
+ raise(DangoFrameworkTimeoutError, "timeout:client_name=#{@client_name}:receive_name=#{receive_name} #{Time.now_to_s}")
110
129
  end
111
130
 
112
131
  # 戻ってきたデータがあれば
@@ -118,21 +137,33 @@ class TestClient < DangoClientFramework
118
137
  sleep SendReceiveSleepIntervalSec # スリープ
119
138
  end
120
139
 
121
- if !send_receive_shared[receive_name] # 戻ってきたデータがあるかどうかチェック
140
+ cs_logger.debug "trap_receive_data:555"
141
+ # 戻ってきたデータがあるかどうかチェック
142
+ if !send_receive_shared[receive_name]
122
143
  raise(DangoFrameworkError, "received data is none. sid=#{sid} receive_name=#{receive_name}")
123
144
  end
124
145
 
125
- @receive_mutex.synchronize do
126
- cs_logger.debug "receive_name:#{receive_name}:#{send_receive_shared[receive_name].inspect}"
127
- @receive_arr.push([receive_name, send_receive_shared[receive_name]])
128
- # p @receive_arr
146
+ # :procが設定されている場合は、それを呼び出す
147
+ cs_logger.debug "trap_receive_data:222:#{receive_name}:#{trap_proc.inspect}"
148
+ if trap_proc
149
+ cs_logger.debug "trap_receive_data:333:#{receive_name}"
150
+ cs_logger.debug "trap_receive_data:trap_proc is true:#{trap_proc.inspect}"
151
+ trap_proc.call(send_receive_shared[receive_name])
152
+
153
+ # :procが設定されていなければ、wait用に@receive_arrに入れておく
154
+ else
155
+ cs_logger.debug "trap_receive_data:444"
156
+ @receive_mutex.synchronize do
157
+ cs_logger.debug "receive_name:#{receive_name}:#{send_receive_shared[receive_name].inspect}"
158
+ @receive_arr.push([receive_name, send_receive_shared[receive_name]])
159
+ end
129
160
  end
130
161
  end
131
162
  end
132
163
 
133
164
  # wait_receive_data
134
165
  def wait_receive_data(receive_name, options = {})
135
- timeout = options[:timeout] || SendReceiveTimeoutSec
166
+ timeout = options[:timeout] || ReceiveWaitTimeoutSec
136
167
 
137
168
  # データ受信待ち
138
169
  receive_data = nil
@@ -147,7 +178,7 @@ class TestClient < DangoClientFramework
147
178
 
148
179
  # タイムアウトなら
149
180
  if receive_data == nil
150
- raise(DangoFrameworkTimeoutError, "timeout:client_name=#{@client_name}:receive_name=#{receive_name}")
181
+ raise(DangoFrameworkTimeoutError, "timeout:client_name=#{@client_name}:receive_name=#{receive_name} #{Time.now_to_s}")
151
182
  end
152
183
 
153
184
  # 結果を削除しておく
data/lib/dango/version.rb CHANGED
@@ -2,7 +2,7 @@ module Dango #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 13
5
+ TINY = 15
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: dango
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.13
7
- date: 2007-10-17 00:00:00 +09:00
6
+ version: 0.0.15
7
+ date: 2007-10-18 00:00:00 +09:00
8
8
  summary: Ruby - Flash conneciton network flamework
9
9
  require_paths:
10
10
  - lib
@@ -34,6 +34,8 @@ files:
34
34
  - lib/dango/client_framework.rb
35
35
  - lib/dango/framework_base.rb
36
36
  - lib/dango/server_framework.rb
37
+ - lib/dango/mutex_socket_list.rb
38
+ - lib/dango/socket_list.rb
37
39
  - lib/dango/monitor/dango_monitor_client.rb
38
40
  - lib/dango/script/dango_server.rb
39
41
  - lib/dango/shared/memory_store.rb