lib/yolodice_client.rb
##
# Calls an arbitrary method on the server.
#
# Parameters:
# * <tt>method</tt> -- method name,
# * <tt>*arguments</tt> -- any arguments for the method, will be passed as the <tt>params</tt> object (optional),
# * <tt>&blk</tt> -- a callback (optional) to be called upon receiving a response for async calls. The callback will receive the response object.
Calls an arbitrary method on the server.
Calls an arbitrary method on the server .
def call method, *arguments, &blk
    raise ConnectionClosedError, "Not connected" unless @connection && !@connection.closed?
    params = if arguments.count == 0
               nil
             elsif arguments.is_a?(Array) && arguments[0].is_a?(Hash)
               arguments[0]
             else
               arguments
             end
    id = @thread_semaphore.synchronize{ @req_id_seq += 1 }
    request = {
      id: id,
      method: method
    }
    request[:params] = params if params != nil
    if blk
      @thread_semaphore.synchronize{ @current_requests[id] = blk }
      log.debug{ "Calling remote method #{method}(#{params.inspect if params != nil}) with an async callback" }
      log.debug{ ">>> #{request.to_json}" }
      @connection.puts request.to_json
      nil
    else
      @thread_semaphore.synchronize{ @current_requests[id] = Thread.current.object_id }
      queue = (@receive_queues[Thread.current.object_id] ||= Queue.new)
      queue.clear
      log.debug{ "Calling remote method #{method}(#{params.inspect if params != nil})" }
      log.debug{ ">>> #{request.to_json}" }
      @connection.puts request.to_json
      response = queue.pop
      if response.has_key? 'result'
        response['result']
      elsif response['error']
        raise RemoteError.new response['error']
      elsif response['client_error'] && response['client_error'] == 'Connection closed'
        raise ConnectionClosedError
      end
    end
  end
def call method , * arguments , & blk raise ConnectionClosedError , "Not connected" unless @connection && ! @connection . closed? params = if arguments . count == 0 nil elsif arguments . is_a? ( Array ) && arguments [ 0 ] . is_a? ( Hash ) arguments [ 0 ] else arguments end id = @thread_semaphore . synchronize { @req_id_seq += 1 } request = { id : id , method : method } request [ :params ] = params if params != nil if blk @thread_semaphore . synchronize { @current_requests [ id ] = blk } log . debug { "Calling remote method #{method}(#{params.inspect if params != nil}) with an async callback" } log . debug { ">>> #{request.to_json}" } @connection . puts request . to_json nil else @thread_semaphore . synchronize { @current_requests [ id ] = Thread . current . object_id } queue = ( @receive_queues [ Thread . current . object_id ] ||= Queue . new ) queue . clear log . debug { "Calling remote method #{method}(#{params.inspect if params != nil})" } log . debug { ">>> #{request.to_json}" } @connection . puts request . to_json response = queue . pop if response . has_key? 'result' response [ 'result' ] elsif response [ 'error' ] raise RemoteError . new response [ 'error' ] elsif response [ 'client_error' ] && response [ 'client_error' ] == 'Connection closed' raise ConnectionClosedError end end end
Calls an arbitrary method on the server.
Calls an arbitrary method on the server .
# a regular blocking request
method
<not_specific>
arguments
<not_specific>
blk
<not_specific>