Class: TestClient

Inherits:
DangoClientFramework show all
Defined in:
lib/dango/tester/dango_tester_client.rb

Overview

テスト接続用のクラス

Constant Summary collapse

SendReceiveSleepIntervalSec =

データ送信後の順の際のタイムアウトチェック間隔秒

0.1
ReceiveWaitTimeoutSec =

データ受信確認の待ち秒数

10
ReceiveTrapTimeoutSec =

データ受信確認のトラップの秒数

0

Constants inherited from DangoClientFramework

DangoClientFramework::HeartBeatSendIntervalSec

Constants included from DangoFrameworkModule

DangoFrameworkModule::CommMaxDigit, DangoFrameworkModule::DefaultEncodeType, DangoFrameworkModule::EncodeTypeJSON, DangoFrameworkModule::EncodeTypeMarshal, DangoFrameworkModule::EncodeTypeYAML, DangoFrameworkModule::MaxLenRecv, DangoFrameworkModule::MaxLenSend, DangoFrameworkModule::ReadTimeoutSec

Instance Attribute Summary collapse

Attributes inherited from DangoClientFramework

#sid

Attributes included from DangoFrameworkModule

#dango_logger

Instance Method Summary collapse

Methods inherited from DangoClientFramework

#dango_client_close, #dango_client_notice_shared, #dango_client_notice_shared_init, #dango_client_receive_decrypt, #dango_client_send_encrypt, #dango_heart_beat_thread_init, #dango_receive__heart_beat, #dango_receive__notice_sid, #dango_receive__notice_system_message, #dango_receive_decrypt, #dango_send_encrypt, #dango_session_closed, #send_action, #send_action_return_notice, #send_return_shared, #send_return_shared_init

Methods included from DangoFrameworkModule

#dango_receive_data, #dango_send_data, #debug_print, #error_print, #logger

Methods included from ErrorMessage

#error_message

Constructor Details

#initialize(env, config, c_name) ⇒ TestClient

Returns a new instance of TestClient.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/dango/tester/dango_tester_client.rb', line 158

def initialize(env, config, c_name)
  @config = config
  @client_name = c_name # クライアント名
  @client_data = {} # データ保管用
  
  @receive_cache_auto_delete_sec = config['tester']['receive_cache_auto_delete_sec']
  
  super(env, config)
  
  send_receive_shared_init()
  
#    @receive_mutex = Mutex.new # 受信用の排他処理
#    @receive_arr = []
#    @trap_thread_hash = {}
#    @receive_methods = []
  
  send_receive_shared[:receive_cache] = [] # 受信データ用のキャッシュ初期化
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

データ受信時の処理をするためにmethod_missingを定義



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dango/tester/dango_tester_client.rb', line 214

def method_missing(name, *args)
  if name.to_s =~ /^dango_receive_/ && args.length == 1
    ret_obj = args[0]
    logger.debug "ret_obj:#{name}:" + ret_obj.inspect + " " + Time.now_to_s
    send_receive_shared.transaction(:receive_cache) do |receive_cache|
      receive_cache.push([name, ret_obj, Time.now])
      receive_cache
      send_receive_shared.commit(receive_cache)
    end
    
  else
    logger.info "method not found. #{name.inspect} #{args.inspect}"
    raise(NoMethodError, "method not found. #{name.inspect} #{args.inspect}")
  end
end

Instance Attribute Details

#client_nameObject

Returns the value of attribute client_name.



178
179
180
# File 'lib/dango/tester/dango_tester_client.rb', line 178

def client_name
  @client_name
end

#send_receive_sharedObject (readonly)

Returns the value of attribute send_receive_shared.



192
193
194
# File 'lib/dango/tester/dango_tester_client.rb', line 192

def send_receive_shared
  @send_receive_shared
end

Instance Method Details

#[](key) ⇒ Object



184
185
186
# File 'lib/dango/tester/dango_tester_client.rb', line 184

def [](key)
  @client_data[key]
end

#[]=(key, value) ⇒ Object

データ保管用の場所を作っておく



181
182
183
# File 'lib/dango/tester/dango_tester_client.rb', line 181

def []=(key, value)
  @client_data[key] = value
end

#clear_receive(notice_name = nil, options = {}) ⇒ Object

clear_receive notice_nameを省略するとchacheにあるものすべて削除



291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/dango/tester/dango_tester_client.rb', line 291

def clear_receive(notice_name = nil, options = {})
  logger.debug "wait_receive_data:#{notice_name}:"
  
  # 結果を削除しておく
  send_receive_shared.transaction(:receive_cache) do |receive_cache|
    receive_cache.delete_if do |one_rec_data|
      true if (! notice_name) || (notice_name && one_rec_data[0] == notice_name)
    end
    receive_cache
    send_receive_shared.commit(receive_cache)
  end
end

#dango_client_initObject

起動処理



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/dango/tester/dango_tester_client.rb', line 195

def dango_client_init
  # サーバー接続情報など
  @connection_client_host = @config["network"]["host"] # 自動でこのホスト名でサーバー開始
  @connection_client_port = @config["network"]["port"] # 自動でこのポートでサーバー開始
  
  # ログ出力情報
  @connection_client_log_file  = @config["log"]["log_file"]
  @connection_client_log_level = @config["log"]["log_level"]
  @connection_client_log_max_size = @config["log"]["log_max_size"]
  @connection_client_log_shift_age = @config["log"]["log_shift_age"]
end

#get_receive_cacheObject

send_receive_sharedの中身を返すメソッド



231
232
233
# File 'lib/dango/tester/dango_tester_client.rb', line 231

def get_receive_cache
  send_receive_shared[:receive_cache]
end

#send(name, send_obj = {}) ⇒ Object

テストサーバー



208
209
210
211
# File 'lib/dango/tester/dango_tester_client.rb', line 208

def send(name, send_obj = {})
  logger.debug("tester.send:name=#{name}:send_obj=#{send_obj.inspect} ")
  send_action(name, send_obj)
end

#send_receive_shared_initObject

dango_client_send_receive_data用の共有メモリ



189
190
191
# File 'lib/dango/tester/dango_tester_client.rb', line 189

def send_receive_shared_init
  @send_receive_shared = SharedMemoryStore.new({})
end

#wait_receive(notice_name, options = {}) ⇒ Object

wait_receive



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/dango/tester/dango_tester_client.rb', line 237

def wait_receive(notice_name, options = {})
  logger.debug "wait_receive_data:#{notice_name}:"
  timeout = options[:timeout] || ReceiveWaitTimeoutSec
  
  # データ受信待ち
  receive_data = nil
  receive_idx = nil
  
  end_reserved_time = Time.now + timeout # 0以上ならタイムアウト時間決定
  
  loop do
    logger.debug "wait_receive_data:loop:#{send_receive_shared[:receive_cache].inspect}"
    
    send_receive_shared[:receive_cache].each_with_index do |rec_data, i|
      if rec_data[0].to_s == "dango_receive_" + notice_name.to_s
        receive_data = rec_data.deep_dup
        receive_idx = i
        break
      end
    end
    
    break if receive_data != nil
    break if end_reserved_time < Time.now
    
    sleep SendReceiveSleepIntervalSec # スリープ
  end
  
  # タイムアウトなら
  if receive_data == nil
    raise(DangoFrameworkTimeoutException, "timeout:timeout_sec=#{timeout}: \n client_name=#{@client_name}(#{self.sid}) \n :notice_name=#{notice_name} \n") 
  end
  
  # 結果を削除しておく
  send_receive_shared.transaction(:receive_cache) do |receive_cache|
    receive_cache.delete_at(receive_idx)
    receive_cache
    send_receive_shared.commit(receive_cache)
  end
  
  # 古いreceive_cacheを削除しておく
  send_receive_shared.transaction(:receive_cache) do |receive_cache|
    receive_cache = receive_cache.select do |one_receive_cache|
#        true
      one_receive_cache[2] > Time.now - @receive_cache_auto_delete_sec
    end
    
    receive_cache
    send_receive_shared.commit(receive_cache)
  end
  
  receive_data[1]
end