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.



285
286
287
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
# File 'lib/elrpc.rb', line 285

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.



283
284
285
# File 'lib/elrpc.rb', line 283

def logger
  @logger
end

#socket_stateObject (readonly)

Returns the value of attribute socket_state.



282
283
284
# File 'lib/elrpc.rb', line 282

def socket_state
  @socket_state
end

Instance Method Details

#add_close_hook(&block) ⇒ Object



419
420
421
# File 'lib/elrpc.rb', line 419

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

#call_method(name, *args) ⇒ Object

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



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

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)



329
330
331
332
333
334
335
336
# File 'lib/elrpc.rb', line 329

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 の簡易版



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

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

#query_methodsObject

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

[name, argdoc, docstring], …


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

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


362
363
364
365
366
367
368
# File 'lib/elrpc.rb', line 362

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

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



318
319
320
# File 'lib/elrpc.rb', line 318

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

#stopObject



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

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

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



410
411
412
413
414
415
416
417
# File 'lib/elrpc.rb', line 410

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