/
message.rb
71 lines (59 loc) · 1.85 KB
/
message.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Casos de uso
#* normal serialization is done with msgpack
#1) Create message with not serialized data: Header + Body.
# => Message.new(from, to, respond_to, action, additionals)
# > Message.header = Header
# > Message.body = Body (with attrs loaded, not crypted, not normally serialized)
#2) Serialize message: returns the serialized message, and sets the serialization flags on the Message instance.
#3) Create message with normally serialized data:
# => Message.parse(msg)
# > Message.header = Header
# > Message.body = Body (without attrs loaded, crypted/serialized string)
#
require_relative './message/header'
require_relative './message/body'
module Cucub
class Message
attr_reader :header
attr_reader :body
def initialize(opts={})
if opts.has_key?("header") && opts.has_key?("body")
@header = Cucub::Message::Header.parse(opts["header"])
@body = Cucub::Message::Body.load(opts["body"])
else
opts_for_header = opts.select{|key| ["from", "to", "respond_to", "layer", "uuid"].include?(key)}
@header = Cucub::Message::Header.new(opts_for_header)
@body = Cucub::Message::Body.new(opts["action"], opts["additionals"])
end
end
def uuid
@header.uuid
end
def serialize
{
:header => @header.serialize,
:body => @body.serialize!
}.to_msgpack
end
def locked?
@body.locked?
end
def unlock(mechanism)
@body.unlock(mechanism)
end
def action
@body.action
end
def additionals
@body.additionals
end
def self.parse(msg)
hash = MessagePack.unpack(msg)
message = Cucub::Message.new(hash)
end
def self.decode(msg)
decoded = MessagePack.unpack(msg)
return Cucub::Message.new(decoded["agent"], decoded["id"], decoded["body"])
end
end
end