Net/Server.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 120 CC 5 SCAR 24.8
CC
5
Cov
0%
setup
30.0
import
2.0
server
2.0
CRAP 12.0
1 use strict;
2 use warnings;
3  
4 package Bot::Net::Server;
5  
6 use Bot::Net::Mixin;
7  
8 require Exporter;
9 push our @ISA, 'Exporter';
10  
11 our @EXPORT = (
12 # Re-export POE::Session constants
13 qw/ OBJECT SESSION KERNEL HEAP STATE SENDER CALLER_FILE CALLER_LINE
14 CALLER_STATE ARG0 ARG1 ARG2 ARG3 ARG4 ARG5 ARG6 ARG7 ARG8 ARG9 /,
15  
16 # Re-export POE::Declarative
17 @POE::Declarative::EXPORT,
18
19 # Re-export Data::Remember
20 qw/ remember recall forget brain /,
21  
22 # Add in our own subs
23 qw/ server setup /,
24 );
25  
26 =head1 NAME
27  
28 Bot::Net::Server - mixin class for building Bot::Net servers
29  
30 =head1 SYNOPSIS
31  
32 bin/botnet server --name Main --mixin IRC
33  
34 =head1 DESCRIPTION
35  
36 This is the main mixin class implemented by all L<Bot::Net> servers. A server may facilitate the communication between bots. In some cases, a server may also simultaneously be a bot too (IRC server bots can be helpful for authentication and channel and nick management, for example).
37  
38 =head1 METHODS
39  
40 =head2 import
41  
42 Custom exporter for this mixin.
43  
44 =cut
45  
46 sub import {
47 my $class = shift;
48  
49 $class->export_to_level(1, undef);
50 $class->export_poe_declarative_to_level;
51  
52 my $package = caller;
53 no strict 'refs';
54 push @{ $package . '::ISA' }, qw/ Bot::Net::Object /;
55 }
56  
57 =head2 server
58  
59 This is a helper for L<POE::Declarative>. It prefixes "server_" to the name of your POE states. For example:
60  
61 on server startup => run { ... };
62  
63 is the same as:
64  
65 on server_startup => run { ... };
66  
67 It can also be used to yield messages:
68  
69 yield server 'startup'; # probably shouldn't actually do that
70  
71 You may choose to use it or not.
72  
73 =cut
74  
75 sub server($) { 'server_'.shift }
76  
77 =head2 setup
78  
79 Setup the server and call all the mixin C<setup> methods.
80  
81 =cut
82  
83 sub setup {
84 my $class = shift;
85 my $self = bless {}, $class;
86  
87 my $name = Bot::Net->short_name_for_server($class);
88 my $config_file = Bot::Net->config->server_file($name);
89  
90 my $config;
91 {
92 no strict 'refs';
93 $config = ${ $class . '::CONFIG' } || $config_file;
94 }
95  
96 my $brain = brain->new_heap( Hybrid => [] => 'Memory' );
97  
98 # Configuration is defined in the package itself (mostly for testing)
99 if (ref $config) {
100 $brain->remember( [ 'config' ] => $config );
101 }
102  
103 # Use the YAML config file
104 else {
105 -f $config_file
106 or die qq{Server startup failed, }
107 .qq{no configuration found for $name: $config_file};
108  
109 $brain->register_brain(
110 config => [ YAML => file => $config_file ]
111 );
112 }
113  
114 if (my $state_file = $brain->recall([ config => 'state_file' ])) {
115 $brain->register_brain(
116 state => [ DBM => file => $state_file ]
117 );
118 }
119  
120 $brain->remember( [ 'name' ] => $name );
121 $brain->remember( [ 'log' ] => $self->log);
122  
123 # Setup any mixins
124 my $mixins = Bot::Net::Mixin::_mixins_for_package($class);
125 for my $mixin (@$mixins) {
126
127 # Don't setup this one
128 next if $mixin->isa('Bot::Net::Server');
129  
130 if (my $method = $mixin->can('setup')) {
131 $method->($self, $brain);
132 }
133 }
134  
135 POE::Declarative->setup($self, $brain);
136 }
137  
138 =head2 default_configuration PACKAGE
139  
140 Returns a base configuration appropriate for all servers.
141  
142 =cut
143  
144 sub default_configuration {
145 my $class = shift;
146 my $package = shift;
147  
148 my $filename = join '/', split /::/,
149 Bot::Net->short_name_for_server($package);
150  
151 return {
152 state_file => 'var/server/'.$filename.'.db',
153 };
154 }
155  
156 =head1 SERVER STATES
157  
158 These are additional states your server (or server mixin) may choose to implement that are provided to your server.
159  
160 =head2 on server startup
161  
162 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.
163  
164 =head2 on server quit
165  
166 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> state and exiting shortly after emitting this state. (If they are not, the server might just be stuck alive and have to be killed externally.)
167  
168 This should be used by protocol mixins to implement the shutdown sequence for their listening ports, open files, etc.
169  
170 =head2 on server shutdown
171  
172 This is called synchronously at the end of the L</on _stop> handler for the L<POE> session.
173  
174 =head1 POE STATES
175  
176 =head2 on _start
177  
178 Handles session startup. At startup, it loads the information stored in the configuration file and then fires L</on server startup>.
179  
180 =cut
181  
182 on _start => run {
183 yield server 'startup';
184 undef;
185 };
186  
187 =head2 on _default ARG0 .. ARGN
188  
189 Performs logging for the general messages that are not handled by the system.
190  
191 =cut
192  
193 on _default => run {
194 my ( $event, $args ) = @_[ ARG0 .. $#_ ];
195  
196 my $msg = "$event: ";
197 foreach (@$args) {
198 SWITCH: {
199 if ( ref($_) eq 'ARRAY' ) {
200 $msg .= "[". join ( ", ", @$_ ). "] ";
201 last SWITCH;
202 }
203 if ( ref($_) eq 'HASH' ) {
204 $msg .= "{". join ( ", ", %$_ ). "} ";
205 last SWITCH;
206 }
207 unless (defined $_) {
208 $_ = '';
209 }
210 $msg .= "'$_' ";
211 }
212 }
213 recall('log')->debug($msg);
214 return 0; # Don't handle signals.
215 };
216  
217 =head2 on _stop
218  
219 This calls (synchronously) the L</on server shutdown> state, to handle any final clean up before quitting.
220  
221 =cut
222  
223 on _stop => run {
224 call get(SESSION) => server 'shutdown';
225 };
226  
227 =head1 SEE ALSO
228  
229 L<Bot::Net::Mixin::Server::IRC>
230  
231 =head1 AUTHORS
232  
233 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
234  
235 =head1 COPYRIGHT AND LICENSE
236  
237 Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
238  
239 This program is free software and may be modified and distributed under the same terms as Perl itself.
240  
241 =cut
242  
243 1;