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.



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
319
320
321
322
# File 'lib/elrpc.rb', line 292

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.



290
291
292
# File 'lib/elrpc.rb', line 290

def logger
  @logger
end

#socket_stateObject (readonly)

Returns the value of attribute socket_state.



289
290
291
# File 'lib/elrpc.rb', line 289

def socket_state
  @socket_state
end

Instance Method Details

#_raise_connection_error(block) ⇒ Object



350
351
352
353
354
355
# File 'lib/elrpc.rb', line 350

def _raise_connection_error(block)
  job = lambda do
    block.call(EPCStackError.new("ConnectionClosed","Connection closed",""))
  end
  @worker_pool.invoke(job)
end

#add_close_hook(&block) ⇒ Object



439
440
441
# File 'lib/elrpc.rb', line 439

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

#alive?Boolean

Returns:

  • (Boolean)


324
325
326
# File 'lib/elrpc.rb', line 324

def alive?
  return @socket_state == :socket_opened
end

#call_method(name, *args) ⇒ Object

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



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/elrpc.rb', line 358

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)



340
341
342
343
344
345
346
347
348
# File 'lib/elrpc.rb', line 340

def call_method_async(name, *args, &block)
  return _raise_connection_error(block) if @socket_state != :socket_opened
  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 の簡易版



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

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

#query_methodsObject

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

[name, argdoc, docstring], …


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

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], …


381
382
383
384
385
386
387
388
# File 'lib/elrpc.rb', line 381

def query_methods_async(&block)
  return _raise_connection_error(block) if @socket_state != :socket_opened
  uid = Elrpc.gen_uid
  msg = MethodsMessage.new(uid, block)
  @session[uid] = msg
  @sending_queue.push(msg)
  uid
end

#register_method(method) ⇒ Object

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



329
330
331
# File 'lib/elrpc.rb', line 329

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

#stopObject



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/elrpc.rb', line 413

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 = :socket_not_connected
  end
  _wakeup
  @logger.debug "RPCService.stop: completed"
end

#waitObject

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



430
431
432
433
434
435
436
437
# File 'lib/elrpc.rb', line 430

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