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

body[RW]

The full, raw body of the message.

custom_metadata[RW]
from[RW]

Who is this message coming from (Should be an identity)

requires_response[W]
to[RW]

Where or who is this message intended for (Can be an identity or a stream) Defaults to the catch-all stream ‘“”`

type[RW]

What type of message are we?

uuid[R]

Unique identifier for this Message. Automatically generated

Public Class Methods

new(message_type = nil) click to toggle source
# 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

<<(part) click to toggle source

Add a message part to this Message’s body

# File lib/pantry/message.rb, line 104
def <<(part)
  @body << part
end
[](key) click to toggle source

Access value from the custom metadata

# File lib/pantry/message.rb, line 87
def [](key)
  @custom_metadata[key]
end
[]=(key, val) click to toggle source

Set custom metadata on this message.

# File lib/pantry/message.rb, line 82
def []=(key, val)
  @custom_metadata[key] = val
end
build_response() click to toggle source

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
forwarded!() click to toggle source

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.

# File lib/pantry/message.rb, line 73
def forwarded!
  @forwarded = true
end
forwarded?() click to toggle source
# File lib/pantry/message.rb, line 77
def forwarded?
  @forwarded
end
from=(source) click to toggle source

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
from_server?() click to toggle source
# File lib/pantry/message.rb, line 56
def from_server?
  @from == Pantry::SERVER_IDENTITY
end
metadata() click to toggle source

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
metadata=(hash) click to toggle source

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
requires_response!() click to toggle source

Flag this message as requiring a response

# File lib/pantry/message.rb, line 61
def requires_response!
  @requires_response = true
end
requires_response?() click to toggle source

Does this message require a response message?

# File lib/pantry/message.rb, line 66
def requires_response?
  @requires_response
end