Exampple.Client (exampple v0.10.6)
Client is a simple state machine inside of a process which helps us to create a client connection to a XMPP Server. The dynamic of the connection is contained inside of the client when we perform the connection and then we can send different kind of stanzas to the server.
The aim of this Client is to create a simple way to test the system but you can use it even to create bots based on XMPP.
It has the possibility to register templates and returns back the stanzas which are being received from the XMPP server to the calling process. This way we are responsible of the communication.
Link to this section Summary
Functions
Add hook letting us to run a specific code for a received stanza. The hooks should be anonymous functions in the way
Returns a specification to start this module under a supervisor.
Send a message to the client process to start the connection. Optionally,
we can specify the name
of the process where to connect, by default this
is the value provided by __MODULE__
.
Send a message to the client process identified by name
as PID
or registered process to stop the connection. The name
parameter
is optional, by default it's set to __MODULE__
.
Ask to the process about whether the connection is active. The checks are based
on the process relanted to the passed name
(it must be a registered name), the
PID should be valid and alive, and then we ask to the client if it's connected.
If no parameter is provided it is __MODULE__
.
Send information (stanzas) via the client process directly to the XMPP Server. These stanzas could be sent to whoever inside of the XMPP network locally or even to other domains if the network is federated.
Starts the client. We can send a name
to be registered or use
the module name (__MODULE__
) by default. As args
the
system requires a Map with the information for the connection.
The information we can provide is
Stops the process. You can specify the name
of the process to be
stopped, by default this value is set as __MODULE__
.
Add process as a tracer for the current XMPP client. It let us receive all of the events happening inside of the client. We can have as many tracers as needed.
Upgrade a connection as TLS.
Wait until the system is connected to start processing messages. This is in use for functional tests and could be used as a phase in the start of the applications.
Link to this section Types
hook_function()
Specs
hook_function() :: (Exampple.Router.Conn.t() -> {:ok, Exampple.Router.Conn.t()} | :drop)
hook_name()
Specs
hook_name() :: String.t()
Link to this section Functions
add_hook(name \\ __MODULE__, hook_name, hook_function)
Specs
add_hook(GenServer.server(), hook_name(), hook_function()) :: :ok
Add hook letting us to run a specific code for a received stanza. The hooks should be anonymous functions in the way:
fn conn ->
if conn.stanza_type == "iq" and conn.type == "result" do
:drop
else
{:ok, conn}
end
end
These functions let us stop processing of the stanzas or add specific listeners for an incoming stanza we are awaiting for. For example, if we want to get the stanza in our process:
parent = self()
f = fn conn ->
if conn.stanza_type == "iq" and conn.type == "error" do
send(parent, {:stanza_error, conn.stanza})
end
{:ok, conn}
end
child_spec(init_arg)
Returns a specification to start this module under a supervisor.
See Supervisor
.
connect(name \\ __MODULE__)
Specs
Send a message to the client process to start the connection. Optionally,
we can specify the name
of the process where to connect, by default this
is the value provided by __MODULE__
.
del_hook(name \\ __MODULE__, hook_name)
Specs
del_hook(GenServer.server(), hook_name()) :: :ok
disconnect(name \\ __MODULE__)
Specs
Send a message to the client process identified by name
as PID
or registered process to stop the connection. The name
parameter
is optional, by default it's set to __MODULE__
.
is_connected?(name \\ __MODULE__)
Specs
Ask to the process about whether the connection is active. The checks are based
on the process relanted to the passed name
(it must be a registered name), the
PID should be valid and alive, and then we ask to the client if it's connected.
If no parameter is provided it is __MODULE__
.
send(data_or_conn, name \\ __MODULE__)
Specs
send(binary() | Exampple.Router.Conn.t(), GenServer.server()) :: :ok
Send information (stanzas) via the client process directly to the XMPP Server. These stanzas could be sent to whoever inside of the XMPP network locally or even to other domains if the network is federated.
The accepted paramter data_or_conn
could be a string (or binary)
or a Conn
struct. Optionally, you can specify a second parameter
as the name
of the registered process. The default value for the
name
paramter is __MODULE__
.
Example:
iex> Exampple.Client.send("<presence/>")
:ok
start_link(name \\ __MODULE__, args)
Specs
start_link(atom() | pid(), map()) :: GenStateMachine.on_start()
Starts the client. We can send a name
to be registered or use
the module name (__MODULE__
) by default. As args
the
system requires a Map with the information for the connection.
The information we can provide is:
host
: the hostname or IP where we will be connected.port
: the port number where we will be connected.domain
: the XMPP domain we are going to use.trimmed
: if the stanzas will be trimmed (defaultfalse
).set_from
: if we are going to set thefrom
for each stanza written by the client (defaultfalse
).ping
: if we active the ping. We can set here the number of milliseconds to send the ping or false to disable it (defaultfalse
).tcp_handler
: the module which we will use to handle the connection (defaultExampple.Tcp
).tls_handler
: the module which we will use to handle the TLS connection, if any (defaultExampple.Tls
).trace
: indicate if you want to receive trace output from the client.
stop(name \\ __MODULE__)
Specs
Stops the process. You can specify the name
of the process to be
stopped, by default this value is set as __MODULE__
.
trace(name \\ __MODULE__, enable?)
Specs
trace(GenServer.server(), enable? :: boolean()) :: :ok
Add process as a tracer for the current XMPP client. It let us receive all of the events happening inside of the client. We can have as many tracers as needed.
upgrade_tls(name \\ __MODULE__)
Specs
Upgrade a connection as TLS.
wait_for_connected(name \\ __MODULE__, sleep \\ 250, retries \\ 2)
Wait until the system is connected to start processing messages. This is in use for functional tests and could be used as a phase in the start of the applications.