Module: EMRPC::MarshalProtocol

Defined in:
lib/emrpc/protocols/marshal_protocol.rb

Constant Summary collapse

DEFAULT_CONST =
Marshal

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

By default, include Marshal-based serialization module.



28
29
30
31
# File 'lib/emrpc/protocols/marshal_protocol.rb', line 28

def self.included(base)
  base.send(:include, new(DEFAULT_CONST))
  #STDERR.puts "# Info: #{self} included into #{base} directly: using #{self}.new(#{DEFAULT_CONST})."
end

.new(marshal_const) ⇒ Object

Creates new protocol using specified dump/load interface. Note: interface must be a constant! See examples below. Examples:

1. include MarshalProtocol.new(Marshal)
2. include MarshalProtocol.new(YAML)
3. include MarshalProtocol.new(JSON)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/emrpc/protocols/marshal_protocol.rb', line 9

def self.new(marshal_const)
  const_name = marshal_const.name
  mod = Module.new
  mod.class_eval <<-EOF, __FILE__, __LINE__
    def send_marshalled_message(msg)
      send_message(#{const_name}.dump(msg))
    end
    def receive_message(msg)
      receive_marshalled_message(#{const_name}.load(msg))
    rescue Exception => e
      rescue_marshal_error(e)
    end
  EOF
  mod
end