NAME

Reflex::Role::Accepting - add connection accepting to a class

VERSION

This document describes version 0.100, released on April 02, 2017.

SYNOPSIS

package Reflex::Acceptor;

use Moose;
extends 'Reflex::Base';

has listener => (
	is        => 'rw',
	isa       => 'FileHandle',
	required  => 1
);

with 'Reflex::Role::Accepting' => {
	listener      => 'listener',
	cb_accept     => make_emitter(on_accept => "accept"),
	cb_error      => make_emitter(on_error => "error"),
	method_pause  => 'pause',
	method_resume => 'resume',
	method_stop   => 'stop',
};

1;

DESCRIPTION

Reflex::Role::Accepting is a parameterized Moose role that accepts client connections from a listening socket. The role's parameters allow the consumer to customize the role's behavior.

listener      key - name of attribute with the listening socket

cb_accept     method to call with accepted sockets (on_${listner}_accept)
cb_error      method to call with errors (on_${listener}_error)

method_pause  method to pause accepting
method_resume method to resume accepting
method_stop   method to stop accepting and release resources

accept() reactions to classes that contain listening sockets. Because it's a role, the class composition happens before runtime, as opposed to runtime composition that occurs in other reactive libraries.

See Reflex::Acceptor if you prefer runtime composition with objects, or if Moose syntax just gives you the heebie-jeebies.

Required Role Parameters

listener

The listener parameter must contain the name of an attribute that contains the listening socket handle. The name indirection allows the role to generate methods that are unique to the listening socket. This becomes important when a class wants to listen on more than one socket---each socket gets its own name, and distinct methods to tell them apart.

For example, a listener named "XYZ" would generate these methods by default:

cb_accept     => "on_XYZ_accept",
cb_error      => "on_XYZ_error",
method_pause  => "pause_XYZ",
# ... and so on.

Optional Role Parameters

cb_accept

cb_accept overrides the default name for the class's accept handler method. This handler will be called whenever a client connection is successfully accepted.

The default method name is "on_${listener}_accept", where $listener is the name of the listening socket attribute. This role defines a default callback that emits an "accept" event, which may be overridden with the ev_accept role parameter.

All callback methods receive two parameters: $self and an anonymous hash containing information specific to the callback. In cb_accept's case, the anonymous hash contains two values: accept()'s return value is named "peer", and the accepted client socket is named "socket".

See perldoc -f accept() for more information about "peer" and "socket".

cb_error

cb_error names the $self method that will be called whenever accept() encounters an error. By default, this method will be the catenation of "on_", the listener name, and "_error". As in on_XYZ_error(), if the listener is named "XYZ". The role defines a default callback that will emit an "error" event by default, with cb_error()'s parameters. The default "error" event may be overridden using the role's ev_error parameter.

cb_error callbacks receive two parameters, $self and an anonymous hashref of named values specific to the callback. Reflex error callbacks include three standard values. errfun contains a single word description of the function that failed. errnum contains the numeric value of $! at the time of failure. errstr holds the stringified version of $!.

Values of $! are passed as parameters since the global variable may change before the callback can be invoked.

When overriding this callback, please be sure to call stopped(), which is provided by Reflex::Role::Collectible. Calling stopped() is vital for collectible objects to be released from memory when managed by Reflex::Collection.

ev_accept

The ev_accept role parameter overrides the default event emitted by the cb_accept callback. It's moot if cb_accept is overridden.

ev_error

The ev_error role parameter overrides the default event emitted by the cb_error callback. It's moot if cb_error is overridden.

method_pause

method_pause defines the name of a method that will temporarily pause the class from accepting new clients. The role will define this method for you. The default method name is "pause_${listener}", where $listener is the name of the listening socket attribute.

method_resume

method_resume defines the name of a method that will allow the class to resume accepting new client connections. The role will define this method for you. The default method name is "resume_${listener}", where $listener is the name of the listening socket attribute.

method_stop

method_stop defines the name of a method that will permanently stop the class from accepting new clients. The role will define this method for you. The default method name is "stop_${listener}", where $listener is the name of the listening socket attribute.

EXAMPLES

TODO - I'm sure there are some.

SEE ALSO

Please see those modules/websites for more information related to this module.

BUGS AND LIMITATIONS

You can make new bug reports, and view existing ones, through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=Reflex.

AUTHOR

Rocco Caputo <rcaputo@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Rocco Caputo.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

AVAILABILITY

The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you, or see https://metacpan.org/module/Reflex/.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.