1 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST |
2 |
package CWMP::Server; |
package CWMP::Session; |
3 |
|
|
4 |
use strict; |
use strict; |
5 |
use warnings; |
use warnings; |
7 |
use base qw/Class::Accessor/; |
use base qw/Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors( qw/ |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
debug |
10 |
port |
store_path |
11 |
|
|
12 |
sock |
sock |
13 |
state |
state |
14 |
queue |
queue |
15 |
|
store |
16 |
/ ); |
/ ); |
17 |
|
|
18 |
use IO::Socket::INET; |
use IO::Socket::INET; |
19 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
20 |
|
use Carp qw/confess cluck croak/; |
21 |
|
|
22 |
use CWMP::Request; |
use CWMP::Request; |
23 |
use CWMP::Response; |
use CWMP::Response; |
24 |
use Carp qw/confess cluck/; |
use CWMP::Store; |
25 |
|
|
26 |
=head1 NAME |
=head1 NAME |
27 |
|
|
28 |
CWMP::Server - implement logic of CWMP protocol |
CWMP::Session - implement logic of CWMP protocol |
29 |
|
|
30 |
=head1 METHODS |
=head1 METHODS |
31 |
|
|
32 |
=head2 new |
=head2 new |
33 |
|
|
34 |
my $server = CWMP::Server->new({ port => 3333 }); |
my $server = CWMP::Session->new({ |
35 |
|
sock => $io_socket_object, |
36 |
=head2 run |
store_path => 'state.db', |
37 |
|
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
38 |
|
debug => 1, |
39 |
|
}); |
40 |
|
|
|
$server->run(); |
|
41 |
|
|
42 |
=cut |
=cut |
43 |
|
|
44 |
sub run { |
sub new { |
45 |
my $self = shift; |
my $class = shift; |
46 |
|
my $self = $class->SUPER::new( @_ ); |
47 |
|
|
48 |
my $listen = IO::Socket::INET->new( |
confess "need sock" unless $self->sock; |
|
Listen => 5, |
|
|
# LocalAddr => 'localhost', |
|
|
LocalPort => $self->port, |
|
|
Proto => 'tcp', |
|
|
Blocking => 1, |
|
|
ReuseAddr => 1, |
|
|
); |
|
|
|
|
|
warn "ACS waiting for request on port ", $self->port, |
|
|
$self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "", |
|
|
"\n"; |
|
|
|
|
|
while ( my $sock = $listen->accept ) { |
|
|
$sock->autoflush(1); |
|
|
|
|
|
warn "connection from ", $sock->peerhost, "\n" if $self->debug; |
|
|
|
|
|
$self->sock( $sock ); # FIXME this will not work for multiple clients |
|
|
while ( $self->process_request ) { |
|
|
warn "...another one bites a dust...\n"; |
|
|
} |
|
49 |
|
|
50 |
warn "...returning to accepting new connections\n"; |
$self->debug( 0 ) unless $self->debug; |
51 |
} |
|
52 |
|
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
53 |
|
|
54 |
|
$self->store( CWMP::Store->new({ |
55 |
|
debug => $self->debug, |
56 |
|
path => $self->store_path, |
57 |
|
}) ); |
58 |
|
|
59 |
|
croak "can't open ", $self->store_path, ": $!" unless $self->store; |
60 |
|
|
61 |
|
return $self; |
62 |
} |
} |
63 |
|
|
64 |
=head2 process_request |
=head2 process_request |
74 |
|
|
75 |
my $sock = $self->sock || die "no sock?"; |
my $sock = $self->sock || die "no sock?"; |
76 |
|
|
77 |
die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' ); |
die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); |
78 |
|
|
79 |
if ( ! $sock->connected ) { |
if ( ! $sock->connected ) { |
80 |
warn "SOCKET NOT CONNECTED\n"; |
warn "SOCKET NOT CONNECTED\n"; |
114 |
|
|
115 |
do { |
do { |
116 |
|
|
117 |
warn "get chunk len\n" if $self->debug; |
warn "get chunk len\n" if $self->debug > 1; |
118 |
|
|
119 |
my $hex; |
my $hex; |
120 |
do { |
do { |
125 |
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i); |
126 |
$len = hex( $hex ); |
$len = hex( $hex ); |
127 |
|
|
128 |
warn "getting chunk of $len bytes\n" if $self->debug; |
warn "getting chunk of $len bytes\n" if $self->debug > 1; |
129 |
|
|
130 |
$sock->read( my $buff, $len ); |
$sock->read( my $buff, $len ); |
131 |
$chunk .= $buff; |
$chunk .= $buff; |
132 |
|
|
133 |
warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug; |
warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1; |
134 |
|
|
135 |
} while ( $len > 0 ); |
} while ( $len > 0 ); |
136 |
my $sep = $sock->getline; |
my $sep = $sock->getline; |
142 |
|
|
143 |
my $size = length( $chunk ); |
my $size = length( $chunk ); |
144 |
|
|
145 |
warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; |
warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n"; |
146 |
|
|
147 |
my $state; |
my $state; |
148 |
|
|
156 |
|
|
157 |
$state = CWMP::Request->parse( $chunk ); |
$state = CWMP::Request->parse( $chunk ); |
158 |
|
|
159 |
warn "acquired state = ", dump( $state ), "\n"; |
warn "## acquired state = ", dump( $state ), "\n"; |
160 |
|
|
161 |
$self->state( $state ); |
$self->state( $state ); |
162 |
|
$self->store->update_state( $state->{ID} => $state ); |
163 |
|
|
164 |
} else { |
} else { |
165 |
warn "empty request\n"; |
warn "## empty request\n"; |
166 |
} |
} |
167 |
|
|
168 |
} else { |
} else { |
169 |
$state = $self->state; |
$state = $self->state; |
170 |
warn "last request state = ", dump( $state ), "\n"; |
warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
171 |
} |
} |
172 |
|
|
173 |
|
|
176 |
'Content-Type: text/xml; charset="utf-8"', |
'Content-Type: text/xml; charset="utf-8"', |
177 |
'Server: AcmeCWMP/42', |
'Server: AcmeCWMP/42', |
178 |
'SOAPServer: AcmeCWMP/42' |
'SOAPServer: AcmeCWMP/42' |
179 |
)); |
)."\r\n"); |
180 |
|
|
181 |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
182 |
|
|
198 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
199 |
$sock->send( $xml ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
200 |
|
|
201 |
warn "### request over"; |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
202 |
|
|
203 |
|
warn "### request over\n" if $self->debug; |
204 |
|
|
205 |
|
return 1; # next request |
206 |
}; |
}; |
207 |
|
|
208 |
=head2 dispatch |
=head2 dispatch |
220 |
|
|
221 |
if ( $response->can( $dispatch ) ) { |
if ( $response->can( $dispatch ) ) { |
222 |
warn ">>> dispatching to $dispatch\n"; |
warn ">>> dispatching to $dispatch\n"; |
223 |
my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n"; |
my $xml = $response->$dispatch( $self->state, @_ ); |
224 |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
225 |
return $xml; |
return $xml; |
226 |
} else { |
} else { |