Class: Pantry::Message
- Inherits:
-
Object
- Object
- Pantry::Message
- Defined in:
- lib/pantry/message.rb
Overview
A Message is the container for all network communication between Clients and Servers. Messages know what stream they’ve been sent down, have a type to differentiate them from each other, and an arbitrarily large body.
Every message has three sections, the stream, metadata, and body. The stream defines where the message needs to go. The metadata defines information about the message, its type, if it needs a response, and anything else that doesn’t go in the body. The body is the request message itself and can be one or many parts.
Instance Attribute Summary collapse
-
#body ⇒ Object
The full, raw body of the message.
-
#custom_metadata ⇒ Object
Returns the value of attribute custom_metadata.
-
#from ⇒ Object
Who is this message coming from (Should be an identity).
-
#requires_response ⇒ Object
writeonly
Sets the attribute requires_response.
-
#to ⇒ Object
Where or who is this message intended for (Can be an identity or a stream) Defaults to the catch-all stream ‘“”`.
-
#type ⇒ Object
What type of message are we?.
-
#uuid ⇒ Object
readonly
Unique identifier for this Message.
Instance Method Summary collapse
-
#<<(part) ⇒ Object
Add a message part to this Message’s body.
-
#[](key) ⇒ Object
Access value from the custom metadata.
-
#[]=(key, val) ⇒ Object
Set custom metadata on this message.
-
#build_response ⇒ Object
Build a copy of this message to use when responding to the message.
-
#forwarded! ⇒ Object
Has this message been forwarded through the Server? This flag is checked when the message comes back through the Server, which lets it know if the message needs to continue back to another Client.
- #forwarded? ⇒ Boolean
- #from_server? ⇒ Boolean
-
#initialize(message_type = nil) ⇒ Message
constructor
A new instance of Message.
-
#metadata ⇒ Object
Return all of this message’s metadata as a hash.
-
#metadata=(hash) ⇒ Object
Given a hash, pull out the parts into local variables.
-
#requires_response! ⇒ Object
Flag this message as requiring a response.
-
#requires_response? ⇒ Boolean
Does this message require a response message?.
Constructor Details
#initialize(message_type = nil) ⇒ Message
Returns a new instance of Message.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pantry/message.rb', line 33 def initialize( = nil) @type = @body = [] @to = "" @requires_response = false @forwarded = false @custom_metadata = {} @uuid = SecureRandom.uuid end |
Instance Attribute Details
#body ⇒ Object
The full, raw body of the message.
27 28 29 |
# File 'lib/pantry/message.rb', line 27 def body @body end |
#custom_metadata ⇒ Object
Returns the value of attribute custom_metadata.
29 30 31 |
# File 'lib/pantry/message.rb', line 29 def @custom_metadata end |
#from ⇒ Object
Who is this message coming from (Should be an identity)
21 22 23 |
# File 'lib/pantry/message.rb', line 21 def from @from end |
#requires_response=(value) ⇒ Object (writeonly)
Sets the attribute requires_response
31 32 33 |
# File 'lib/pantry/message.rb', line 31 def requires_response=(value) @requires_response = value end |
#to ⇒ Object
Where or who is this message intended for (Can be an identity or a stream) Defaults to the catch-all stream ‘“”`
18 19 20 |
# File 'lib/pantry/message.rb', line 18 def to @to end |
#type ⇒ Object
What type of message are we?
24 25 26 |
# File 'lib/pantry/message.rb', line 24 def type @type end |
#uuid ⇒ Object (readonly)
Unique identifier for this Message. Automatically generated
14 15 16 |
# File 'lib/pantry/message.rb', line 14 def uuid @uuid end |
Instance Method Details
#<<(part) ⇒ Object
Add a message part to this Message’s body
104 105 106 |
# File 'lib/pantry/message.rb', line 104 def <<(part) @body << part end |
#[](key) ⇒ Object
Access value from the custom metadata
87 88 89 |
# File 'lib/pantry/message.rb', line 87 def [](key) @custom_metadata[key] end |
#[]=(key, val) ⇒ Object
Set custom metadata on this message.
82 83 84 |
# File 'lib/pantry/message.rb', line 82 def []=(key, val) @custom_metadata[key] = val end |
#build_response ⇒ Object
Build a copy of this message to use when responding to the message
93 94 95 96 97 98 99 100 101 |
# File 'lib/pantry/message.rb', line 93 def build_response response = self.clone response.body = [] response.to = self.from response.from = self.to response.requires_response = false response. = self..clone response end |
#forwarded! ⇒ Object
Has this message been forwarded through the Server? This flag is checked when the message comes back through the Server, which lets it know if the message needs to continue back to another Client.
73 74 75 |
# File 'lib/pantry/message.rb', line 73 def forwarded! @forwarded = true end |
#forwarded? ⇒ Boolean
77 78 79 |
# File 'lib/pantry/message.rb', line 77 def forwarded? @forwarded end |
#from_server? ⇒ Boolean
56 57 58 |
# File 'lib/pantry/message.rb', line 56 def from_server? @from == Pantry::SERVER_IDENTITY end |
#metadata ⇒ Object
Return all of this message’s metadata as a hash
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/pantry/message.rb', line 109 def { :uuid => self.uuid, :type => self.type, :from => self.from, :to => self.to || "", :requires_response => self.requires_response?, :forwarded => self.forwarded?, :custom => @custom_metadata } end |
#metadata=(hash) ⇒ Object
Given a hash, pull out the parts into local variables
122 123 124 125 126 127 128 129 130 |
# File 'lib/pantry/message.rb', line 122 def (hash) @uuid = hash[:uuid] @type = hash[:type] @from = hash[:from] @to = hash[:to] || "" @requires_response = hash[:requires_response] @forwarded = hash[:forwarded] @custom_metadata = hash[:custom] end |
#requires_response! ⇒ Object
Flag this message as requiring a response
61 62 63 |
# File 'lib/pantry/message.rb', line 61 def requires_response! @requires_response = true end |
#requires_response? ⇒ Boolean
Does this message require a response message?
66 67 68 |
# File 'lib/pantry/message.rb', line 66 def requires_response? @requires_response end |