NAME
MooX::Async::Console::TCP - A TCP framing module for MooX::Async::Console
SYNOPSIS
DESCRIPTION
A IO::Async::Listener subclass which listens on a TCP port. Each connection is created as a MooX::Async::Console::TCPClient module which interpets the byte stream and invokes "on_command" when a command is ready to be executed.
BUGS
Certainly.
PUBLIC INTERFACE
ATTRIBUTES
- address (default:
0.0.0.0
) -
The IP address to listen for connections on.
- port (default:
$ENV{MXACPORT} // 0
) -
The TCP port to listen for connections on. The default is 0 which lets the kernel select a port. The value in this attribute is updated when the socket is bound.
EVENTS
- on_command
-
Must be included in the constuctor. Invoked by this module to execute a command. This interface is described in "on_command" in MooX::Async::Console.
Arguments:
- command
-
Name of the command to execute.
- inform
-
Coderef with which the command can send messages over the connection.
- args
-
Arrayref of arguments to execute the command with.
- then
-
Future to complete or fail when the command is finished.
- on_terminate
-
Invoked by this module when the connection has terminated.
Arguments: none.
- on_success
-
Invoked by this module when a command has completed successfully.
Arguments: The result the command's Future was completed with.
If the implementation of this event returns a Future then that is used to provide the result sent to the client.
- on_failure
-
Invoked by this module when a command has failed.
Arguments: The Future's failure.
PRIVATE INTERFACE
CONSTRUCTION
Begins listening on the port when it's added to the loop.
Detaches all clients when removed from the loop.
_init
, which is used during the parent class IO::Async::Listener's own construction, replaces its $args
with a single entry of handle_constructor
.
handle_constructor
contains a coderef to attach the client implemented by MooX::Async::Console::TCPClient and handle its on_line and on_close events.
Client's on_line event handler
For the present this is extremely simple. The client types in a line of text and ends it with newline. That line is broken up into a list on whitespace and the first word in the list is the command name, the rest its args.
Only one command may be running at a time. This is enforced by the $state
variable.
The "on_command" event handler is invoked with a new Future.
Disconnecting the client is treated specially so that everything is shutdown in an orderly manner.
If the Future which is given to the command handler is failed with the word quit
then this is flagged using $quit
and the Future is replaced with a done Future with an appropriate message substituted.
After the Future completes succesfully a message is returned to the client containing its result.
If the $quit
flag is true the client is detached.
If the command handler's Future was failed then a message is logged and sent to the client.
METHODS
- _attach_client($client)
- _detach_client($client)
-
Add & remove the new client as a child of this notifier.
EVENTS
- on_accept
-
Implemented by this module, attaches the new client which has connected.
SEE ALSO
MooX::Async::Console::TCPClient
AUTHOR
Matthew King <chohag@jtan.com>