Spell.Message
The Spell.Message
module defines the struct and functions to
back WAMP messages.
Note that these are distinct from Erlixir’s messages.
Summary↑
get_code_for_type(type, default \\ nil) | Get the |
get_type_for_code(type, default \\ nil) | Get the message |
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 |
Types ↑
wamp_type :: wamp_integer | wamp_string | wamp_bool | wamp_dict | wamp_list | wamp_id | wamp_uri
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
Specs:
- get_code_for_type(type, default) :: integer | nil | default when default: any
Get the code
for the message type
.
Specs:
- get_type_for_code(integer, default) :: type | nil | default when default: any
Get the message type
for code
.
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. Iftype
isn’t provided, it is be set byget_type_for_integer(code)
.code :: integer
the integer code for the message type. Ifcode
isn’t isn’t provided it is set byget_integer_for_type(type)
.type
must have a valid code.args :: [wamp_type]
defaults to[]
, the list of message arguments.
Specs:
Returns a new message, or raises an exception.
Options
See new/1
.
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
Macros
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