class Pantry::Message
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.
Attributes
The full, raw body of the message.
Who is this message coming from (Should be an identity)
Where or who is this message intended for (Can be an identity or a stream) Defaults to the catch-all stream ‘“”`
What type of message are we?
Unique identifier for this Message
. Automatically generated
Public Class Methods
# File lib/pantry/message.rb, line 33 def initialize(message_type = nil) @type = message_type @body = [] @to = "" @requires_response = false @forwarded = false @custom_metadata = {} @uuid = SecureRandom.uuid end
Public Instance Methods
Add a message part to this Message’s body
# File lib/pantry/message.rb, line 104 def <<(part) @body << part end
Access value from the custom metadata
# File lib/pantry/message.rb, line 87 def [](key) @custom_metadata[key] end
Set custom metadata on this message.
# File lib/pantry/message.rb, line 82 def []=(key, val) @custom_metadata[key] = val end
Build a copy of this message to use when responding to the message
# 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.custom_metadata = self.custom_metadata.clone response end
# File lib/pantry/message.rb, line 77 def forwarded? @forwarded end
Set the source of this message either by an object that responds to identity or a string.
# File lib/pantry/message.rb, line 48 def from=(source) if source.respond_to?(:identity) @from = source.identity else @from = source end end
# File lib/pantry/message.rb, line 56 def from_server? @from == Pantry::SERVER_IDENTITY end
Return all of this message’s metadata as a hash
# File lib/pantry/message.rb, line 109 def metadata { :uuid => self.uuid, :type => self.type, :from => self.from, :to => self.to || "", :requires_response => self.requires_response?, :forwarded => self.forwarded?, :custom => @custom_metadata } end
Given a hash, pull out the parts into local variables
# File lib/pantry/message.rb, line 122 def metadata=(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
Flag this message as requiring a response
# File lib/pantry/message.rb, line 61 def requires_response! @requires_response = true end
Does this message require a response message?
# File lib/pantry/message.rb, line 66 def requires_response? @requires_response end