Class: Elrpc::RPCService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, socket, methods = nil) ⇒ RPCService

Returns a new instance of RPCService.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/elrpc.rb', line 288

def initialize(name, socket, methods = nil)
    @logger = Logger.new(STDOUT)
    @logger.level = Elrpc::default_log_level
  @logger.datetime_format = Elrpc.get_logger_format(name)

  @methods = Hash.new # name -> Method
  @session = Hash.new # uid -> proc
  @session_lock = Monitor.new

    @sending_queue = Queue.new # CallMessage

  @socket = socket
  @socket_state_lock = Monitor.new
    @socket_state = :socket_opened

  @wait_lock = nil
  @wait_cv = nil
  @close_hooks = []

  if methods then
    methods.each do |m|
      register_method(m)
    end
  end

  @sender_thread = Thread.start { sender_loop }
  @receiver_thread = Thread.start { receiver_loop }
  @worker_pool = WorkerPool.new(1, @logger)

  @logger.debug ":ready for I/O stream."
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



286
287
288
# File 'lib/elrpc.rb', line 286

def logger
  @logger
end

#socket_stateObject (readonly)

Returns the value of attribute socket_state.



285
286
287
# File 'lib/elrpc.rb', line 285

def socket_state
  @socket_state
end

Instance Method Details

#add_close_hook(&block) ⇒ Object



422
423
424
# File 'lib/elrpc.rb', line 422

def add_close_hook(&block)
  @close_hooks << block
end

#call_method(name, *args) ⇒ Object

相手のメソッドを呼ぶ(同期版)



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/elrpc.rb', line 342

def call_method(name, *args)
  mutex = Mutex.new
  cv = ConditionVariable.new
  ret = nil
  ex = nil
  call_method_async(name, *args) do |err, value|
    mutex.synchronize do
      ex = err
      ret = value
      cv.signal
    end
  end
  mutex.synchronize do
    cv.wait(mutex)
  end
  if !ex.nil?
    raise ex
  end
  return ret
end

#call_method_async(name, *args, &block) ⇒ Object

相手のメソッドを呼ぶblock(err, value)



332
333
334
335
336
337
338
339
# File 'lib/elrpc.rb', line 332

def call_method_async(name, *args, &block)
  uid = Elrpc.gen_uid
  msg = CallMessage.new(uid, name, args, block)
  # ここは競合しないのでロックしない
  @session[uid] = msg
  @sending_queue.push(msg)
  uid
end

#def_method(name, argdoc = nil, docstring = nil, &block) ⇒ Object

register_method の簡易版



326
327
328
# File 'lib/elrpc.rb', line 326

def def_method(name, argdoc=nil, docstring=nil, &block)
  register_method(Method.new(name, argdoc, docstring, &block))
end

#query_methodsObject

接続相手のメソッド一覧を返す(同期版)

[name, argdoc, docstring], …


375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/elrpc.rb', line 375

def query_methods
  mutex = Mutex.new
  cv = ConditionVariable.new
  ret = nil
  ex = nil
  query_methods_async do |err, value|
    mutex.synchronize do
      ex = err
      ret = value
      cv.signal
    end
  end
  mutex.synchronize do
    cv.wait(mutex)
  end
  if !ex.nil?
    raise ex
  end
  return ret
end

#query_methods_async(&block) ⇒ Object

接続相手のメソッド一覧を返す

[name, argdoc, docstring], …


365
366
367
368
369
370
371
# File 'lib/elrpc.rb', line 365

def query_methods_async(&block)
  uid = Elrpc.gen_uid
  msg = MethodsMessage.new(uid, block)
  @session[uid] = msg
  @sending_queue.push(msg)
  uid
end

#register_method(method) ⇒ Object

自分にメソッドを登録する



321
322
323
# File 'lib/elrpc.rb', line 321

def register_method(method)
  @methods[method.name] = method
end

#stopObject



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/elrpc.rb', line 396

def stop
  if @socket_state == :socket_opened then
    @logger.debug "RPCService.stop: received!"
    @worker_pool.kill
    @socket_state = :socket_closing
    @socket.close
    @sending_queue << nil # stop message
    @sender_thread.join(4) unless Thread.current == @sender_thread
    @receiver_thread.join(4) unless Thread.current == @receiver_thread
    _clear_waiting_sessions
    @socket_state = :scoket_not_connected
  end
  _wakeup
  @logger.debug "RPCService.stop: completed"
end

#waitObject

ソケットが相手から切断されるまでメインスレッドを止める



413
414
415
416
417
418
419
420
# File 'lib/elrpc.rb', line 413

def wait
  @wait_lock = Mutex.new
  @wait_cv = ConditionVariable.new
  @wait_lock.synchronize do
    @wait_cv.wait(@wait_lock)
  end
  stop
end