Class: AlWorker::IpcClient

Inherits:
UNIXSocket
  • Object
show all
Defined in:
lib/al_worker_ipc.rb

Overview

Note:

IPCクライアント

通信のデータ形式はJSONで行うが、ユーザプログラムとのデータ授受は、 HashでもJSONでも行える。 @valuesアクセッサは、専用メソッドとキャッシュを用意した。 ただし、Hashで授受する場合のみキャッシュを使う。

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ IpcClient

constructor



273
274
275
276
277
# File 'lib/al_worker_ipc.rb', line 273

def initialize( path )
  super
  @values = {}
  @status_code = ""
end

Instance Attribute Details

#status_codeString (readonly)

Returns 通信ステータスコード.

Returns:

  • (String)

    通信ステータスコード



267
268
269
# File 'lib/al_worker_ipc.rb', line 267

def status_code
  @status_code
end

#valuesHash (readonly)

Returns AlWorker::Ipc#valuesのキャッシュ.

Returns:

  • (Hash)

    AlWorker::Ipc#valuesのキャッシュ



264
265
266
# File 'lib/al_worker_ipc.rb', line 264

def values
  @values
end

Instance Method Details

#call(cmd, arg = {}) ⇒ Hash, NilClass

IPC 呼び出し

Parameters:

  • cmd (String)

    コール先メソッド名

  • arg (Hash, String) (defaults to: {})

    パラメータ

Returns:

  • (Hash)

    リプライデータ

  • (NilClass)

    IPCがクローズされた



288
289
290
291
292
# File 'lib/al_worker_ipc.rb', line 288

def call( cmd, arg = {} )
  json = call_json( cmd, arg )
  return nil if ! json
  JSON.parse( json ) rescue {}
end

#call_json(cmd, arg = {}) ⇒ String, NilClass

IPC 呼び出し JSON版

Parameters:

  • cmd (String)

    コール先メソッド名

  • arg (Hash, String) (defaults to: {})

    パラメータ

Returns:

  • (String)

    リプライデータ JSON文字列

  • (NilClass)

    IPCがクローズされた



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/al_worker_ipc.rb', line 303

def call_json( cmd, arg = {} )
  arg = arg.to_json  if arg.class == Hash
  self.puts "#{cmd} #{arg}"

  @status_code = self.gets
  if @status_code == nil
    @status_code = "503 Service Unavailable. IPC was closed."
    return nil
  end
  @status_code.chomp!
  if @status_code[3] == " "
    return "{}"
  end

  json = ""
  while txt = self.gets
    txt.chomp!
    break if txt == ""
    json << txt
  end
  return json
end

#get_value(key = nil) ⇒ Object, Hash Also known as: get_values

valueのゲッター タイムアウトなし(単一値/複数値)

Parameters:

  • key (String, Array) (defaults to: nil)

    キー

Returns:

  • (Object, Hash)



356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/al_worker_ipc.rb', line 356

def get_value( key = nil )
  ret = call( "get_values", key: key )
  return nil  if ! @status_code.start_with?( "200" )

  if key
    @values.merge!( ret )
    return @values[ key ]  if key.class == String
  else
    @values = ret
  end
  return ret
end

#get_value_wt(key = nil, timeout = nil) ⇒ Object, Hash Also known as: get_values_wt

Note:

valueのゲッター タイムアウト付き(単一値/複数値)

内部キャッシュ @valuesに保存する。

Parameters:

  • key (String, Array) (defaults to: nil)

    キー

  • timeout (Numeric) (defaults to: nil)

    タイムアウト時間

Returns:

  • (Object, Hash)



380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/al_worker_ipc.rb', line 380

def get_value_wt( key = nil, timeout = nil )
  ret = call( "get_values_wt", key: key, timeout: timeout )
  return nil  if ! @status_code.start_with?( "200" )
  locked = (@status_code == "200. OK")

  if key
    @values.merge!( ret )
    return @values[ key ],locked  if key.class == String
  else
    @values = ret
  end
  return ret,locked
end

#set_value(key, val) ⇒ Object

valueのセッター(単一値)

Parameters:

  • key (String)

    キー

  • val (Object)



333
334
335
336
# File 'lib/al_worker_ipc.rb', line 333

def set_value( key, val )
  @values[ key ] = val
  call_json( "set_values", { key=>val } )
end

#set_values(values) ⇒ Object

valueのセッター(複数値)

Parameters:

  • values (Hash)

    セットする値



344
345
346
347
# File 'lib/al_worker_ipc.rb', line 344

def set_values( values )
  @values.merge!( values )
  call_json( "set_values", values )
end