dango 0.0.18 → 0.0.19

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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2007 Keisuke Minami
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/Manifest.txt CHANGED
@@ -1,5 +1,6 @@
1
1
  Manifest.txt
2
2
  README.txt
3
+ MIT-LICENSE
3
4
  lib/dango/client_framework.rb
4
5
  lib/dango/framework_base.rb
5
6
  lib/dango/server_framework.rb
data/README.txt CHANGED
@@ -30,16 +30,9 @@ Download and install dango with the following.
30
30
 
31
31
  == Simple Example
32
32
 
33
- ruby "rubygems" # you use rubygems
34
- ruby "image_size"
35
- ruby "open-uri"
36
- open("http://www.rubycgi.org/image/ruby_gtk_book_title.jpg", "rb") do |fh|
37
- p ImageSize.new(fh.read).get_size
38
- end
39
-
40
33
  == Licence
41
34
 
42
- This code is free to use under the terms of the Ruby's licence.
35
+ This code is free to use under the terms of the MIT licence.
43
36
 
44
37
  == Contact
45
38
 
@@ -237,7 +237,7 @@ class DangoClientFramework
237
237
 
238
238
  # 送信スレッドが開始していなければ開始
239
239
  if !receive_thread
240
- receive_thread = Thread.start do
240
+ receive_thread = Thread.start(action_name, send_obj) do
241
241
  cs_client_send_data(action_name, send_obj)
242
242
  end
243
243
  end
@@ -187,12 +187,12 @@ module DangoFrameworkModule
187
187
  # cs_logger.debug "ret_data_orig:#{ret_data_orig.inspect}"
188
188
 
189
189
  ret_data = cs_receive_decrypt(ret_data_orig[0..-2])
190
+ # cs_logger.debug "ret_data:#{ret_data.inspect}"
190
191
 
191
192
  @recv_count += 1 if @recv_count # 受信回数カウント
192
- # cs_logger.debug "ret_data:#{ret_data.inspect}"
193
193
 
194
194
  begin
195
- if ret_data == "\n" # データが空ならparseしない
195
+ if ret_data == "" || ret_data == "{}" # データが空ならparseしない
196
196
  data = {}
197
197
  elsif dtype == 0
198
198
  data = JSON.parse(ret_data)
@@ -16,9 +16,10 @@ require "dango/mutex_socket_list"
16
16
  class DangoServerFramework
17
17
  include DangoFrameworkModule
18
18
 
19
- SendReceiveSleepIntervalSec = 0.1 # データ送信後の順の際のタイムアウトチェック間隔秒
20
- SendReceiveTimeoutDefaultSec = 2.0 # データ送受信時のデフォルトタイムアウト秒数
21
- SendTimeoutSec = 10.0 # データ送受信時の送信のタイムアウト秒数
19
+ SendReceiveSleepIntervalSec = 0.1 # データ送信後の順の際のタイムアウトチェック間隔秒
20
+ SendReceiveTimeoutDefaultSec = 2.0 # データ送受信時のデフォルトタイムアウト秒数
21
+ SendTimeoutSec = 10.0 # データ送受信時の送信のタイムアウト秒数
22
+ HeratBeatIntervalSec = 60.0 # S=>Cのハートビートの送信タイミング
22
23
 
23
24
  class DangoGServer < GServer
24
25
  def initialize(parent, *args)
@@ -87,6 +88,7 @@ class DangoServerFramework
87
88
  mutex_socket_list_init() # ソケット毎用のmutexを初期化
88
89
  @mutex_proc_thread = Mutex.new # スレッドが同時起動しないようにするためのMutex
89
90
  cs_server_init() # 初期設定読み込み
91
+ heart_beat_thread_start() # ハートビートスレッドの開始
90
92
 
91
93
  @mutex_send_response = Mutex.new # 送信レスポンス用のMutex
92
94
  @arr_send_response = [] # 送信レスポンス用のキャッシュ
@@ -94,8 +96,8 @@ class DangoServerFramework
94
96
  @gserver = DangoGServer.new(self, @connection_server_port,
95
97
  @connection_server_host,
96
98
  @connection_server_max_connections)
97
- @gserver.audit = true # Turn logging on.
98
- @gserver.start
99
+ @gserver.audit = true # Turn logging on.
100
+ @gserver.start
99
101
  end
100
102
 
101
103
  # サーバーのループ時のクラス読み直し処理
@@ -192,11 +194,9 @@ class DangoServerFramework
192
194
  cs_connect() # 接続時メソッド呼び出し
193
195
 
194
196
  # 接続直後のsid通知
195
- # Thread.start do
196
- send_obj = {"_sid"=>sid}
197
- cs_server_send_data(sid, "_notice_sid", send_obj)
198
- cs_logger.debug "_notice_sid #{sid} #{Time.now_to_s}"
199
- # end
197
+ send_obj = {"_sid"=>sid}
198
+ cs_server_send_data(sid, "_notice_sid", send_obj)
199
+ cs_logger.debug "_notice_sid #{sid} #{Time.now_to_s}"
200
200
 
201
201
  # 誰かが接続したときに60秒以上経った@arr_send_responseを削除
202
202
  @mutex_send_response.synchronize do
@@ -223,9 +223,12 @@ class DangoServerFramework
223
223
  policy_data = '<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>'
224
224
  sock.send(policy_data, 0)
225
225
  sock.flush
226
- raise(DangoFrameworkError, "policy file sent. disconnectiong...")
226
+ raise(DangoFrameworkFlashPolicyError, "policy file sent. disconnectiong...")
227
227
 
228
- elsif ret_obj != {} # 受信データがあれば
228
+ elsif ret_obj == {} # 受信データがなければ
229
+ # cs_logger.debug "ret_obj is empty. #{Time.now_to_s}"
230
+
231
+ else # 受信データがあれば
229
232
  if !(ret_obj && ret_obj["action"]) # actionが無い場合はエラー
230
233
  cs_logger.error "no action error:#{ret_obj.inspect}"
231
234
  raise(DangoFrameworkError, "no action error")
@@ -267,6 +270,9 @@ class DangoServerFramework
267
270
 
268
271
  end # loop
269
272
 
273
+ rescue DangoFrameworkFlashPolicyError
274
+ cs_logger.debug "Flash policy file send. #{Thread.current.object_id} #{Time.now_to_s}"
275
+
270
276
  rescue DangoFrameworkReadNoDataError
271
277
  cs_logger.debug "read no data. maybe client closed. #{Thread.current.object_id} #{Time.now_to_s}"
272
278
 
@@ -485,6 +491,31 @@ class DangoServerFramework
485
491
  cs_server_receive_decrypt(str)
486
492
  end
487
493
 
494
+ # ハートビートスレッドの開始
495
+ def heart_beat_thread_start
496
+ Thread.start do
497
+ loop do
498
+ begin
499
+ sleep HeratBeatIntervalSec
500
+
501
+ if ! @gserver.stopped? # gserverが起動していれば
502
+ cs_logger.debug "heart_beat_thread_start #{Time.now_to_s}"
503
+
504
+ # 全員へ空パケットを送信
505
+ socket_list.keys.each do |sid|
506
+ cs_logger.debug "heart_beat_thread_start:sid=#{sid}"
507
+ sock = socket_list[sid]
508
+ cs_send_data(sock, {})
509
+ end
510
+ end
511
+
512
+ rescue
513
+ cs_logger.error "Exception #{Thread.current.object_id} #{Time.now_to_s} #{error_message($!, 'u')}"
514
+ end
515
+ end
516
+ end
517
+ end
518
+
488
519
  # サーバーからクライアントへのデータ送信
489
520
  def cs_server_send_data(sid, notice_name, send_obj, options = {})
490
521
  cs_logger.debug "cs_server_send_data(#{sid.inspect}, #{notice_name.inspect}) #{Time.now_to_s}"
@@ -495,25 +526,27 @@ class DangoServerFramework
495
526
  timeout = options[:timeout] || SendReceiveTimeoutDefaultSec
496
527
  dtype = options[:type] || 0
497
528
 
498
- sock = socket_list[sid]
499
- raise(DangoFrameworkError, "not found sid=#{sid}") if !sock
500
- raise(DangoFrameworkConnectionError, "socket closed. sid=#{sid}") if sock.closed?
501
-
502
- # cs_logger.debug "cs_server_send_data:sending data notice_name=#{notice_name.inspect}"
503
- send_obj_dup = send_obj.deep_dup
504
- send_obj_dup["notice"] = notice_name.to_s
505
-
506
- # データ送信時にクライアントからのレスポンスを確認する(Flashのバグらしきもの対策)
507
- # if notice_name.to_s =~ /^return_/
508
- # cs_send_data(sock, send_obj_dup, :type=>dtype) # データ送信
509
- # cs_logger.debug "cs_server_send_data:finish send data notice_name=#{notice_name.inspect}\n"
510
- # else
529
+ # sidの通知の場合はレスポンスを気にしない
511
530
  if notice_name.to_s == "_notice_sid"
531
+ sock = socket_list[sid]
532
+ raise(DangoFrameworkError, "not found sid=#{sid}") if !sock
533
+ raise(DangoFrameworkConnectionError, "socket closed. sid=#{sid}") if sock.closed?
534
+
535
+ send_obj_dup = send_obj.deep_dup
536
+ send_obj_dup["notice"] = notice_name.to_s
537
+
512
538
  cs_send_data(sock, send_obj_dup, :type=>dtype) # データ送信
513
539
  # cs_logger.debug "cs_server_send_data:finish send data notice_name=#{notice_name.inspect}\n"
514
540
  else
515
- Thread.start do
541
+ Thread.start(sid, notice_name, send_obj, dtype, timeout) do
516
542
  begin
543
+ sock = socket_list[sid]
544
+ raise(DangoFrameworkError, "not found sid=#{sid}") if !sock
545
+ raise(DangoFrameworkConnectionError, "socket closed. sid=#{sid}") if sock.closed?
546
+
547
+ send_obj_dup = send_obj.deep_dup
548
+ send_obj_dup["notice"] = notice_name.to_s
549
+
517
550
  mutex_socket_list.synchronize(sid) do
518
551
  # cs_logger.debug "mutex_socket_list:synchronize:#{notice_name} #{sid} #{Time.now_to_s}"
519
552
 
@@ -521,7 +554,7 @@ class DangoServerFramework
521
554
  send_obj_dup["_id"] = Digest::MD5.hexdigest(digest_key)
522
555
  have_came_response = nil
523
556
 
524
- receive_thread = Thread.start do
557
+ receive_thread = Thread.start(sock, send_obj_dup, dtype) do
525
558
  begin
526
559
  cs_send_data(sock, send_obj_dup, :type=>dtype) # データ送信
527
560
  # cs_logger.debug "cs_server_send_data:sent:notice_name=#{send_obj_dup['_id']} #{notice_name.inspect} #{sid} #{Time.now_to_s}"
@@ -103,6 +103,7 @@ class TestClient < DangoClientFramework
103
103
  cs_logger.debug "trap_receive_data:#{receive_name}:#{Time.now_to_s}"
104
104
  timeout = options[:timeout] || ReceiveTrapTimeoutSec
105
105
  trap_proc = options[:proc] || nil
106
+ trap_loop = options[:loop] || nil # procがある場合のみ有効なフラグ
106
107
  raise(ArgumentError, ":proc is not Proc class.") if trap_proc && ! trap_proc.is_a?(Proc)
107
108
 
108
109
  # 戻ってきたデータのチェックメソッド
@@ -124,46 +125,51 @@ class TestClient < DangoClientFramework
124
125
  if ! @trap_thread_hash.has_key?(receive_name)
125
126
 
126
127
  # スレッド開始
127
- th = Thread.start do
128
- send_receive_shared[receive_name] = nil
129
- if timeout == 0
130
- end_reserved_time = Time.at(0) # 0ならepochを入れる
131
- else
132
- end_reserved_time = Time.now + timeout # 0以上ならタイムアウト時間決定
133
- end
134
-
135
- # タイムアウトチェック
128
+ th = Thread.start(receive_name, timeout, trap_proc, trap_loop) do
136
129
  loop do
137
- if end_reserved_time != Time.at(0) && end_reserved_time < Time.now
138
- raise(DangoFrameworkTimeoutError, "timeout:timeout_sec=#{timeout}:\nclient_name=#{@client_name}(#{self.sid}):\nreceive_name=#{receive_name} \n#{Time.now_to_s}")
130
+ send_receive_shared[receive_name] = nil
131
+ if timeout == 0
132
+ end_reserved_time = Time.at(0) # 0ならepochを入れる
133
+ else
134
+ end_reserved_time = Time.now + timeout # 0以上ならタイムアウト時間決定
139
135
  end
140
136
 
141
- # 戻ってきたデータがあれば
142
- if send_receive_shared[receive_name]
143
- cs_logger.debug "receive_name:#{send_receive_shared[receive_name].inspect} #{Time.now_to_s}"
144
- break
137
+ # タイムアウトチェック
138
+ loop do
139
+ if end_reserved_time != Time.at(0) && end_reserved_time < Time.now
140
+ raise(DangoFrameworkTimeoutError, "timeout:timeout_sec=#{timeout}:\nclient_name=#{@client_name}(#{self.sid}):\nreceive_name=#{receive_name} \n#{Time.now_to_s}")
141
+ end
142
+
143
+ # 戻ってきたデータがあれば
144
+ if send_receive_shared[receive_name]
145
+ cs_logger.debug "receive_name:#{send_receive_shared[receive_name].inspect} #{Time.now_to_s}"
146
+ break
147
+ end
148
+
149
+ sleep SendReceiveSleepIntervalSec # スリープ
145
150
  end
146
151
 
147
- sleep SendReceiveSleepIntervalSec # スリープ
148
- end
149
-
150
- # 戻ってきたデータがあるかどうかチェック
151
- if !send_receive_shared[receive_name]
152
- raise(DangoFrameworkError, "received data is none. sid=#{sid} receive_name=#{receive_name}")
153
- end
154
-
155
- # :procが設定されている場合は、それを呼び出す
156
- if trap_proc
157
- cs_logger.debug "trap_receive_data:trap_proc:#{trap_proc.inspect} #{Time.now_to_s}"
158
- trap_proc.call(send_receive_shared[receive_name])
152
+ # 戻ってきたデータがあるかどうかチェック
153
+ if !send_receive_shared[receive_name]
154
+ raise(DangoFrameworkError, "received data is none. sid=#{self.sid} receive_name=#{receive_name}")
155
+ end
159
156
 
160
- # :procが設定されていなければ、wait用に@receive_arrに入れておく
161
- else
162
- @receive_mutex.synchronize do
163
- cs_logger.debug "receive_name:#{receive_name}:#{send_receive_shared[receive_name].inspect} #{Time.now_to_s}"
164
- @receive_arr.push([receive_name, send_receive_shared[receive_name]])
157
+ # :procが設定されている場合は、それを呼び出す
158
+ if trap_proc
159
+ cs_logger.debug "trap_receive_data:trap_proc:#{trap_proc.inspect} #{Time.now_to_s}"
160
+ trap_proc.call(send_receive_shared[receive_name])
161
+ break if ! trap_loop # loopする必要がなければスレッド終了
162
+
163
+ # :procが設定されていなければ、wait用に@receive_arrに入れておく
164
+ else
165
+ @receive_mutex.synchronize do
166
+ cs_logger.debug "receive_name:#{receive_name}:#{send_receive_shared[receive_name].inspect} #{Time.now_to_s}"
167
+ @receive_arr.push([receive_name, send_receive_shared[receive_name]])
168
+ end
169
+ break # スレッド終了
170
+
165
171
  end
166
- end
172
+ end # loop
167
173
  end # Thread
168
174
 
169
175
  @trap_thread_hash[receive_name] = th
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 = 18
5
+ TINY = 19
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.18
7
- date: 2007-10-22 00:00:00 +09:00
6
+ version: 0.0.19
7
+ date: 2007-10-24 00:00:00 +09:00
8
8
  summary: Ruby - Flash conneciton network flamework
9
9
  require_paths:
10
10
  - lib
@@ -31,6 +31,7 @@ authors:
31
31
  files:
32
32
  - Manifest.txt
33
33
  - README.txt
34
+ - MIT-LICENSE
34
35
  - lib/dango/client_framework.rb
35
36
  - lib/dango/framework_base.rb
36
37
  - lib/dango/server_framework.rb