Spell.Message

The Spell.Message module defines the struct and functions to back WAMP messages.

Note that these are distinct from Erlixir’s messages.

Source

Summary

get_code_for_type(type, default \\ nil)

Get the code for the message type

get_type_for_code(type, default \\ nil)

Get the message type for code

new!(options)

Returns a new message, or raises an exception

new(options)

Returns a new message

new_id()

Return a new WAMP id

receive_message(peer, type, body)

This macro expands familiar :ok/:error clauses to a receive clause for their respective WAMP message from peer given type

Types

wamp_integer :: integer

wamp_bool :: boolean

new_error :: :type_code_missing | :type_code_mismatch | {:args, :not_list} | {:code, :out_of_range | :bad_value}

t :: %Spell.Message{type: type, code: integer, args: args}

Functions

get_code_for_type(type, default \\ nil)

Specs:

  • get_code_for_type(type, default) :: integer | nil | default when default: any

Get the code for the message type.

Source
get_type_for_code(type, default \\ nil)

Specs:

  • get_type_for_code(integer, default) :: type | nil | default when default: any

Get the message type for code.

Source
new(options)

Specs:

Returns a new message.

Options

There is a one to one mapping between type and code. Either type or code must be provided. If both are provided, they must be consistent.

  • type :: atom the name of the message type. If type isn’t provided, it is be set by get_type_for_integer(code).
  • code :: integer the integer code for the message type. If code isn’t isn’t provided it is set by get_integer_for_type(type). type must have a valid code.
  • args :: [wamp_type] defaults to [], the list of message arguments.
Source
new!(options)

Specs:

Returns a new message, or raises an exception.

Options

See new/1.

Source
new_id()

Specs:

  • new_id :: integer

Return a new WAMP id.

To ensure the uniqueness of the new id we use :crypto.rand_bytes to generate a random seed

TODO: improve :random.uniform using a Mersenne Twister PRNG algorithm

Source

Macros

receive_message(peer, type, body)

This macro expands familiar :ok/:error clauses to a receive clause for their respective WAMP message from peer given type.

This macro is meant to be a convenience — feel free to drop down to the undelrying receive.

Example

def receive_subscribed(peer, subscribe_id) do
  receive_message peer, :subscribed do
    {:ok, [^subscribe_id, subscription]} -> {:ok, subscription}
    {:error, reason} -> {:error, reason}
  end
end

is expanded to

def receive_subscribed(peer, subscribe_id) do
  receive do
    {Peer, ^peer, %Message{type: :subscribed,
                           args: [^subscribe_id, subscription]}} ->
      {:ok, subscription}
    {Peer, ^peer, %Message{type: :error, args: [33, _, reason | _]}} ->
      case Message.normalize_error(error) do
        {:ok, reason} ->
           {:error, reason}
        {:error, _, reason} ->
          {:error, reason}
      end
  after
    @timeout -> {:error, :timeout}
  end
end
Source