8 |
__PACKAGE__->mk_accessors( qw/ |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
debug |
10 |
port |
port |
11 |
|
store_path |
12 |
|
|
13 |
sock |
sock |
14 |
state |
state |
15 |
queue |
queue |
16 |
|
store |
17 |
/ ); |
/ ); |
18 |
|
|
19 |
use IO::Socket::INET; |
use IO::Socket::INET; |
20 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
21 |
|
use Carp qw/confess cluck croak/; |
22 |
|
|
23 |
use CWMP::Request; |
use CWMP::Request; |
24 |
use CWMP::Response; |
use CWMP::Response; |
25 |
use Carp qw/confess cluck/; |
use CWMP::Store; |
26 |
|
|
27 |
=head1 NAME |
=head1 NAME |
28 |
|
|
32 |
|
|
33 |
=head2 new |
=head2 new |
34 |
|
|
35 |
my $server = CWMP::Server->new({ port => 3333 }); |
my $server = CWMP::Server->new({ |
36 |
|
port => 3333, |
37 |
|
store_path => 'state.db', |
38 |
|
debug => 1, |
39 |
|
}); |
40 |
|
|
41 |
=head2 run |
=head2 run |
42 |
|
|
60 |
$self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "", |
$self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "", |
61 |
"\n"; |
"\n"; |
62 |
|
|
63 |
|
$self->debug( 0 ) unless $self->debug; |
64 |
warn "## debug level: ", $self->debug, "\n" if $self->debug; |
warn "## debug level: ", $self->debug, "\n" if $self->debug; |
65 |
|
|
66 |
|
$self->store( CWMP::Store->new({ |
67 |
|
debug => $self->debug, |
68 |
|
path => $self->store_path, |
69 |
|
}) ); |
70 |
|
croak "can't open ", $self->store_path, ": $!" unless $self->store; |
71 |
|
|
72 |
while ( my $sock = $listen->accept ) { |
while ( my $sock = $listen->accept ) { |
73 |
$sock->autoflush(1); |
$sock->autoflush(1); |
74 |
|
|
164 |
|
|
165 |
my $size = length( $chunk ); |
my $size = length( $chunk ); |
166 |
|
|
167 |
warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; |
warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; |
168 |
|
|
169 |
my $state; |
my $state; |
170 |
|
|
219 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
220 |
$sock->send( $xml ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
221 |
|
|
222 |
|
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
223 |
|
|
224 |
warn "### request over\n" if $self->debug; |
warn "### request over\n" if $self->debug; |
225 |
|
|
226 |
|
return 1; # next request |
227 |
}; |
}; |
228 |
|
|
229 |
=head2 dispatch |
=head2 dispatch |
241 |
|
|
242 |
if ( $response->can( $dispatch ) ) { |
if ( $response->can( $dispatch ) ) { |
243 |
warn ">>> dispatching to $dispatch\n"; |
warn ">>> dispatching to $dispatch\n"; |
244 |
my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n"; |
my $xml = $response->$dispatch( $self->state, @_ ); |
245 |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
246 |
return $xml; |
return $xml; |
247 |
} else { |
} else { |