Module: MessagePack::Rpc::Server Abstract

Defined in:
lib/msgpack/rpc/server.rb

Overview

This module is abstract.

Include from the class that implements the rpc server. You can expose When the client implementation class receives data from the communication line, it must call the receive_data() method and pass received data to the MessagePack::Rpc::Server module. Also, the client implementation class should define a method send_data() to actually send the data. Call this method from within the MessagePack::Rpc::Server module if necessary (Implement send_data() method to accept string objects in arguments). If you receive a protocol level error, override the on_error() method. the methods defined in that class as RPC procedures.

Module that implemented server protocol of MessagePack-RPC.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.msgpack_options(**opts) ⇒ Object

set MessagePack::Unpacker option.

Examples:

set :symbolize_keys option

class Server
  include MessagePack::Rpc::Server

  msgpack_options = {:symbolize_keys => true}

  def test(data)
    # data's key are symbolized.
    return data[:data]
  end
  remote_public :test
end


# File 'lib/msgpack/rpc/server.rb', line 33

.remote_async(name) ⇒ Object

expose asynchronous procedure. The method exposed by this method takes a deferred object as its first argument. Returns the processing result asynchronously through this object.

Examples:

expose asyncronous procedure

class Server
  include MessagePack::Rpc::Server

  def test(df, id)
    if id.nil?
      df.reject("id isnot defined")
    else
      df.resolve("hello #{id}")
    end
  end
  remote_async :test
end

Parameters:

  • name (Symbol)

    target method name.



# File 'lib/msgpack/rpc/server.rb', line 72

.remote_public(name) ⇒ Object

expose syncronous procedure. The return value of the method exposed by this method is the return value of the procedure. If an exception occurs, the exception is returned as an error value.

Examples:

expose syncronous procedure

class Server
  include MessagePack::Rpc::Server

  def test(id)
    raise("id isnot defined") if id.nil?
    return "hello #{id}"
  end
  remote_public :test
end

Parameters:

  • name (Symbol)

    target method name.



# File 'lib/msgpack/rpc/server.rb', line 50

Instance Method Details

#notify(meth, *args) ⇒ Object

send the notification to peer rpc client

Parameters:

  • meth (Symbol)

    notify name

  • args (Array)

    argument for notification



290
291
292
# File 'lib/msgpack/rpc/server.rb', line 290

def notify(meth, *args)
  send_data([2, meth, args].to_msgpack)
end

#receive_dgram(data) ⇒ Object

Note:

Use this method for datagram communication. Use it when it is guaranteed that data is exchanged in packets (it works a bit faster).

emqueu the received datagram to communication buffer

Parameters:

  • data (Blob)

    recevied data from peer rpc client.



305
306
307
308
309
310
311
312
313
# File 'lib/msgpack/rpc/server.rb', line 305

def receive_dgram(data)
  msg = MessagePack.unpack(data, self.class.msgpack_options)

  if not msg.kind_of?(Array)
    error_occured(RantimeError.new("not array message is received"))
  end

  eval_message(msg)
end

#receive_stream(data) ⇒ Object

emqueu the received data to communication buffer

Parameters:

  • data (Blob)

    recevied data from peer rpc client.



321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/msgpack/rpc/server.rb', line 321

def receive_stream(data)
  begin
    unpacker.feed_each(data) {|msg| eval_message(msg)}

  rescue MessagePack::UnpackError => e
    unpacker.reset
    error_occured(e)

  rescue => e
    error_occured(e)
  end
end

#reset_unpackerObject



179
180
181
# File 'lib/msgpack/rpc/server.rb', line 179

def reset_unpacker
  @unpacker = nil
end

#unpackerObject



175
176
177
# File 'lib/msgpack/rpc/server.rb', line 175

def unpacker
  return (@unpacker ||= self.class.new_unpacker)
end