Bot-Net

 view release on metacpan or  search on metacpan

lib/Bot/Net/Server.pm  view on Meta::CPAN

            or die qq{Server startup failed, }
                .qq{no configuration found for $name: $config_file};

        $brain->register_brain(
            config => [ YAML => file => $config_file ]
        );
    }

    if (my $state_file = $brain->recall([ config => 'state_file' ])) {
        $brain->register_brain(
            state => [ DBM => file => $state_file ]
        );
    }

    $brain->remember( [ 'name' ] => $name );
    $brain->remember( [ 'log'  ] => $self->log);

    # Setup any mixins
    my $mixins = Bot::Net::Mixin::_mixins_for_package($class);
    for my $mixin (@$mixins) {
        
        # Don't setup this one
        next if $mixin->isa('Bot::Net::Server');

        if (my $method = $mixin->can('setup')) {
            $method->($self, $brain);
        }
    }

    POE::Declarative->setup($self, $brain);
}

=head2 default_configuration PACKAGE

Returns a base configuration appropriate for all servers.

=cut

sub default_configuration {
    my $class   = shift;
    my $package = shift;

    my $filename = join '/', split /::/, 
        Bot::Net->short_name_for_server($package);

    return {
        state_file => 'var/server/'.$filename.'.db',
    };
}

=head1 SERVER STATES

These are additional states your server (or server mixin) may choose to implement that are provided to your server.

=head2 on server startup

This is yielded at the end of the L</on _start> handler for the L<POE> session. Your server should perform any initialization needed here.

=head2 on server quit

A server should emit this state when it wants the server to disconnect and shutdown. If all mixins are implemented correctly, they should listen for this state and close all resources, which should result in the server going into the L</on _stop> sta...

This should be used by protocol mixins to implement the shutdown sequence for their listening ports, open files, etc.

=head2 on server shutdown

This is called synchronously at the end of the L</on _stop> handler for the L<POE> session. 

=head1 POE STATES

=head2 on _start

Handles session startup. At startup, it loads the information stored in the configuration file and then fires L</on server startup>.

=cut

on _start => run {
    yield server 'startup';
    undef;
};

=head2 on _default ARG0 .. ARGN

Performs logging for the general messages that are not handled by the system.

=cut

on _default => run {
    my ( $event, $args ) = @_[ ARG0 .. $#_ ];

    my $msg = "$event: ";
    foreach (@$args) {
        SWITCH: {
            if ( ref($_) eq 'ARRAY' ) {
                $msg .= "[". join ( ", ", @$_ ). "] ";
                last SWITCH;
            }
            if ( ref($_) eq 'HASH' ) {
                $msg .= "{". join ( ", ", %$_ ). "} ";
                last SWITCH;
            }
            unless (defined $_) {
                $_ = '';
            }
            $msg .= "'$_' ";
        }
    }
    recall('log')->debug($msg);
    return 0;    # Don't handle signals.
};

=head2 on _stop

This calls (synchronously) the L</on server shutdown> state, to handle any final clean up before quitting.

=cut

on _stop => run {
    call get(SESSION) => server 'shutdown';
};



( run in 2.077 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )