1 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/22/07 14:35:38 CEST |
2 |
package CWMP::Server; |
3 |
|
4 |
use strict; |
5 |
use warnings; |
6 |
|
7 |
|
8 |
use base qw/Class::Accessor/; |
9 |
__PACKAGE__->mk_accessors( qw/ |
10 |
port |
11 |
store_path |
12 |
default_queue |
13 |
debug |
14 |
/ ); |
15 |
|
16 |
use CWMP::Session; |
17 |
|
18 |
use Carp qw/confess/; |
19 |
use Data::Dump qw/dump/; |
20 |
|
21 |
=head1 NAME |
22 |
|
23 |
CWMP::Server - description |
24 |
|
25 |
=head1 METHODS |
26 |
|
27 |
=head2 new |
28 |
|
29 |
my $server = CWMP::Server->new({ |
30 |
port => 3333, |
31 |
store_path => 'state.db', |
32 |
default_queue => [ qw/GetRPCMethods GetParameterNames/ ], |
33 |
debug => 1 |
34 |
}); |
35 |
|
36 |
Options: |
37 |
|
38 |
=over 4 |
39 |
|
40 |
=item port |
41 |
|
42 |
port to listen on |
43 |
|
44 |
=item store_path |
45 |
|
46 |
path to L<DBM::Deep> database file to preserve state |
47 |
|
48 |
=item default_queue |
49 |
|
50 |
commands which will be issued to every CPE on connect |
51 |
|
52 |
=back |
53 |
|
54 |
=cut |
55 |
|
56 |
sub new { |
57 |
my $class = shift; |
58 |
my $self = $class->SUPER::new( @_ ); |
59 |
|
60 |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") object\n" if $self->debug; |
61 |
|
62 |
return $self; |
63 |
} |
64 |
|
65 |
=head2 run |
66 |
|
67 |
$server->run(); |
68 |
|
69 |
=cut |
70 |
|
71 |
sub run { |
72 |
my $self = shift; |
73 |
|
74 |
my $listen = IO::Socket::INET->new( |
75 |
Listen => 5, |
76 |
# LocalAddr => 'localhost', |
77 |
LocalPort => $self->port, |
78 |
Proto => 'tcp', |
79 |
Blocking => 1, |
80 |
ReuseAddr => 1, |
81 |
); |
82 |
|
83 |
warn "ACS waiting for request on port ", $self->port; |
84 |
|
85 |
$self->debug( 0 ) unless $self->debug; |
86 |
warn "## debug level: ", $self->debug, "\n" if $self->debug; |
87 |
|
88 |
warn $self->default_queue ? " queue ( " . join(",",@{$self->default_queue}) . " )" : "", |
89 |
"\n"; |
90 |
|
91 |
while ( my $sock = $listen->accept ) { |
92 |
$sock->autoflush(1); |
93 |
|
94 |
my $session = CWMP::Session->new({ |
95 |
sock => $sock, |
96 |
queue => $self->default_queue, |
97 |
store_path => $self->store_path, |
98 |
debug => $self->debug, |
99 |
}) || confess "can't create session"; |
100 |
|
101 |
while ( $session->process_request ) { |
102 |
warn "...another one bites the dust...\n"; |
103 |
} |
104 |
|
105 |
warn "...returning to accepting new connections\n"; |
106 |
} |
107 |
} |
108 |
|
109 |
1; |