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 |
|
|
56 |
ReuseAddr => 1, |
ReuseAddr => 1, |
57 |
); |
); |
58 |
|
|
59 |
warn "waiting for request on port ", $self->port, $/; |
warn "ACS waiting for request on port ", $self->port, |
60 |
|
$self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "", |
61 |
|
"\n"; |
62 |
|
|
63 |
|
$self->debug( 0 ) unless $self->debug; |
64 |
|
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 |
|
|
75 |
warn "connection from ", $sock->peerhost, "\n"; |
warn "connection from ", $sock->peerhost, "\n" if $self->debug; |
76 |
|
|
77 |
$self->sock( $sock ); # FIXME this will not work for multiple clients |
$self->sock( $sock ); # FIXME this will not work for multiple clients |
78 |
while ( $self->process_request ) { |
while ( $self->process_request ) { |
79 |
warn "...another one bites a dust...\n"; |
warn "...another one bites the dust...\n"; |
80 |
} |
} |
81 |
|
|
82 |
warn "...returning to accepting new connections\n"; |
warn "...returning to accepting new connections\n"; |
99 |
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 'IO::Socket::INET' ); |
100 |
|
|
101 |
if ( ! $sock->connected ) { |
if ( ! $sock->connected ) { |
102 |
warn "SOCKET NOT CONNECTED"; |
warn "SOCKET NOT CONNECTED\n"; |
103 |
return 0; |
return 0; |
104 |
} |
} |
105 |
|
|
136 |
|
|
137 |
do { |
do { |
138 |
|
|
139 |
warn "get chunk len\n" if $self->debug; |
warn "get chunk len\n" if $self->debug > 1; |
140 |
|
|
141 |
my $hex; |
my $hex; |
142 |
do { |
do { |
147 |
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); |
148 |
$len = hex( $hex ); |
$len = hex( $hex ); |
149 |
|
|
150 |
warn "getting chunk of $len bytes\n" if $self->debug; |
warn "getting chunk of $len bytes\n" if $self->debug > 1; |
151 |
|
|
152 |
$sock->read( my $buff, $len ); |
$sock->read( my $buff, $len ); |
153 |
$chunk .= $buff; |
$chunk .= $buff; |
154 |
|
|
155 |
warn "--- $len bytes: --=>||$buff||<=--\n"; |
warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1; |
156 |
|
|
157 |
} while ( $len > 0 ); |
} while ( $len > 0 ); |
158 |
my $sep = $sock->getline; |
my $sep = $sock->getline; |
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 |
|
|
178 |
|
|
179 |
$state = CWMP::Request->parse( $chunk ); |
$state = CWMP::Request->parse( $chunk ); |
180 |
|
|
181 |
warn "acquired state = ", dump( $state ), "\n"; |
warn "## acquired state = ", dump( $state ), "\n"; |
182 |
|
|
183 |
$self->state( $state ); |
$self->state( $state ); |
184 |
|
|
185 |
} else { |
} else { |
186 |
warn "empty request\n"; |
warn "## empty request\n"; |
187 |
} |
} |
188 |
|
|
189 |
} else { |
} else { |
190 |
$state = $self->state; |
$state = $self->state; |
191 |
warn "last request state = ", dump( $state ), "\n"; |
warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
192 |
} |
} |
193 |
|
|
194 |
|
|
197 |
'Content-Type: text/xml; charset="utf-8"', |
'Content-Type: text/xml; charset="utf-8"', |
198 |
'Server: AcmeCWMP/42', |
'Server: AcmeCWMP/42', |
199 |
'SOAPServer: AcmeCWMP/42' |
'SOAPServer: AcmeCWMP/42' |
200 |
)); |
)."\r\n"); |
201 |
|
|
202 |
$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} ); |
203 |
|
|
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 "### request over"; |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
223 |
|
|
224 |
|
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"; |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
246 |
return $xml; |
return $xml; |
247 |
} else { |
} else { |
248 |
confess "can't dispatch to $dispatch"; |
confess "can't dispatch to $dispatch"; |
263 |
while (defined($_ = $self->sock->getline)) { |
while (defined($_ = $self->sock->getline)) { |
264 |
s/[\r\n]+$//; |
s/[\r\n]+$//; |
265 |
last unless length $_; |
last unless length $_; |
266 |
warn "-- $_\n"; |
warn "-- $_\n" if $self->debug; |
267 |
return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x; |
return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x; |
268 |
$self->{headers}->{$1} = $2; |
$self->{headers}->{$1} = $2; |
269 |
} |
} |