-
Notifications
You must be signed in to change notification settings - Fork 10
role_worker
role_worker behavior
The role_worker behavior takes care of communication via interfaces supported in the framework. For a behavior implementation it is required to define callbacks, responsible for parsing a raw data into user defined data structures (Module:to_term/3) and for generation of raw data from a user defined data structure (Module:from_term/2), initialization callback Module:start/3, configuration callback Module:ctrl/2 and Module:stop/1 called when the process is terminated. One fsm_mod_supervisor can supervise several interface processors according to the agent configuration. Useful examples of the interfaces processors are role_at (EvoLogics modem interface processor) and role_nmea (NMEA interface procerssor).
Agents can interact with other agents and with external applications using interface processors of different types, as shown on the figure below:
- TCP socket: tcp. This is the most common interface type, used by the interface processors, that can be used both for agent-to-agent and agent-to-external application interaction
- Erlang port: port. Ports provide the basic mechanism for communication with the external world, providing a byte-oriented interface to an external program. This interface type can be used for the interaction of the agents with external applications only
- Erlang message queue: erlang. The interface can be used for direct message exchange between agents without data parsing by interface processors
- Web interface: cowboy. This interface type converts user actions in a web browser to messages delivered via interface processors to agents and generates necessary responses to the user requests. Server side is implemented using cowboy HTTP server. Cowboy is a small, fast and modular HTTP server written in Erlang
Interface processor configuration is defined by one of the following tuples
{role, Name, iface, Interface}
or
{role, Name, param, Param, iface, Interface}
- Name is an atom, unequally identifying the interface processor
- Param is an optional parameter passed as params of mm record to Module:start/3
- Interface defines specific for the supported types interface parameters
Interface parameters of a TCP socket interface processor is defined by described below tuple
{socket, Address, Port, Type}
- Address is an IPv4 address string
- Port a socket port number
- Side is an atom client or server
{role, nmea, iface, {socket, "10.1.0.12", 12000, server}}
Interface parameters of an Erlang port interface processor is defined by described below tuple
{port, Port, PortSettings}
- Port a double tuple describing an external program to run
- PortSettings port settings, directly passed as the second argument to erlang:open_port/2 call
Configuration example
{role,ext_example,iface,{port,{ext_example,ext_example},[]}}
tbd
tbd
mm() =
#mm{role = atom,
role_id = atom,
status = true | false,
params = [any()],
iface = iface()}
cfg() = any()
ip4_address() = {0..255, 0..255, 0..255, 0..255}
iface() = socket_iface() | port_iface() | cowboy_iface() | erlang_iface()
socket_iface() =
{socket,
IP :: ip4_address(),
Port :: integer() > 0,
Type :: client | server}
port_iface() =
{port,
PortName,
PortSettings} %% see erlang:open_port/2 type specification
cowboy_iface() =
{cowboy,
IP :: string(),
Port :: integer() > 0}
erlang_iface() =
{erlang, ModuleID :: atom} |
{erlang, {ModuleID :: atom, Remote :: node()}
parse_state() =
[TermList :: [any()],
ErrorList:: [{error, any()}],
Raw :: binary(),
More :: binary(),
NewCfg :: cfg()]
Types
Module = atom()
Tail = binary()
Chunk = binary()
Cfg = cfg()
The following functions must be exported from a role_worker callback module.
Types:
RoleID = atom
ModID = atom
MiddleMen = mm()
Result = {ok,pid()} | ignore | {error,any()}
Types:
Cfg = cfg()
Types:
Tail = binary()
Bin = binary() | list()
Cfg = cfg()
Result = parse_state()
Types:
Term = any()
Cfg = cfg()
Result = {error, Reason} | [Bin :: binary(), NewCfg :: cfg()]
Types:
Ctrl = any()
Cfg = cfg()
Result = cfg()
Types:
Raw = binary()
Cfg = cfg()
Result = [{more,binary()} | {error,any()} | {ctrl, any()} | any()]