Class: Elrpc::RPCService
- Inherits:
-
Object
- Object
- Elrpc::RPCService
- Defined in:
- lib/elrpc.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#socket_state ⇒ Object
readonly
Returns the value of attribute socket_state.
Instance Method Summary collapse
- #add_close_hook(&block) ⇒ Object
-
#call_method(name, *args) ⇒ Object
相手のメソッドを呼ぶ(同期版).
-
#call_method_async(name, *args, &block) ⇒ Object
相手のメソッドを呼ぶ block(err, value).
-
#def_method(name, argdoc = nil, docstring = nil, &block) ⇒ Object
register_method の簡易版.
-
#initialize(name, socket, methods = nil) ⇒ RPCService
constructor
A new instance of RPCService.
-
#query_methods ⇒ Object
接続相手のメソッド一覧を返す(同期版) [[name, argdoc, docstring], …].
-
#query_methods_async(&block) ⇒ Object
接続相手のメソッド一覧を返す [[name, argdoc, docstring], …].
-
#register_method(method) ⇒ Object
自分にメソッドを登録する.
- #stop ⇒ Object
-
#wait ⇒ Object
ソケットが相手から切断されるまでメインスレッドを止める.
Constructor Details
#initialize(name, socket, methods = nil) ⇒ RPCService
Returns a new instance of RPCService.
280 281 282 283 284 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 |
# File 'lib/elrpc.rb', line 280 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
#logger ⇒ Object
Returns the value of attribute logger.
278 279 280 |
# File 'lib/elrpc.rb', line 278 def logger @logger end |
#socket_state ⇒ Object (readonly)
Returns the value of attribute socket_state.
277 278 279 |
# File 'lib/elrpc.rb', line 277 def socket_state @socket_state end |
Instance Method Details
#add_close_hook(&block) ⇒ Object
414 415 416 |
# File 'lib/elrpc.rb', line 414 def add_close_hook(&block) @close_hooks << block end |
#call_method(name, *args) ⇒ Object
相手のメソッドを呼ぶ(同期版)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/elrpc.rb', line 334 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)
324 325 326 327 328 329 330 331 |
# File 'lib/elrpc.rb', line 324 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 の簡易版
318 319 320 |
# File 'lib/elrpc.rb', line 318 def def_method(name, argdoc=nil, docstring=nil, &block) register_method(Method.new(name, argdoc, docstring, &block)) end |
#query_methods ⇒ Object
接続相手のメソッド一覧を返す(同期版)
- [name, argdoc, docstring], …
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/elrpc.rb', line 367 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], …
357 358 359 360 361 362 363 |
# File 'lib/elrpc.rb', line 357 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
自分にメソッドを登録する
313 314 315 |
# File 'lib/elrpc.rb', line 313 def register_method(method) @methods[method.name] = method end |
#stop ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/elrpc.rb', line 388 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 |
#wait ⇒ Object
ソケットが相手から切断されるまでメインスレッドを止める
405 406 407 408 409 410 411 412 |
# File 'lib/elrpc.rb', line 405 def wait @wait_lock = Mutex.new @wait_cv = ConditionVariable.new @wait_lock.synchronize do @wait_cv.wait(@wait_lock) end stop end |