Cauldron2D.Beacon (Cauldron2D v0.3.3)

Copy Markdown View Source

How a server on the local network is found: the host says so every two seconds on a UDP port — port/0, the :beacon_port in the :cauldron_2d config, 2299 unless set — to the broadcast address and to this machine, and whoever listens (Cauldron2D.Beacon.Listener) hears it.

{:ok, beacon} = Cauldron2D.Beacon.start_link(game: :my_game, host: "arcade", http: "http://192.168.1.5:2280", node: :"my_game@arcade")

A call is one line, cauldron 1 <game> <host> <http url or -> <node or ->. A node is joined with its cookie, which the host shows and the caller types; it never goes over the air. A host started with :fields calls cauldron 2 <game> <host> <http or -> <node or -> <fields> instead, the fields a map of strings, URL-encoded as a query string. put/2 changes them while it calls.

Summary

Functions

Returns a specification to start this module under a supervisor.

The line a host calls with: fields nil for the first form, a map for the second.

A call heard, or :error for anything else.

The UDP port the calls go out on: config :cauldron_2d, beacon_port: n, else 2299.

Change the fields beacon calls with, from its next call on.

Start calling; :game the game's name, :host the name to call as, :http the web URL or nil, :node the node name or nil, :fields a map of strings or nil (default), :port the UDP port (default port/0).

Types

call()

@type call() :: %{
  game: String.t(),
  host: String.t(),
  http: String.t() | nil,
  node: atom() | nil,
  fields: %{required(String.t()) => String.t()}
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

line(game, host, http, node, fields \\ nil)

@spec line(
  atom() | String.t(),
  String.t(),
  String.t() | nil,
  atom() | nil,
  map() | nil
) :: String.t()

The line a host calls with: fields nil for the first form, a map for the second.

parse(text)

@spec parse(String.t()) :: {:ok, call()} | :error

A call heard, or :error for anything else.

port()

@spec port() :: pos_integer()

The UDP port the calls go out on: config :cauldron_2d, beacon_port: n, else 2299.

put(beacon, fields)

@spec put(GenServer.server(), %{required(String.t()) => String.t()}) :: :ok

Change the fields beacon calls with, from its next call on.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Start calling; :game the game's name, :host the name to call as, :http the web URL or nil, :node the node name or nil, :fields a map of strings or nil (default), :port the UDP port (default port/0).