1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
7 use base qw/Class::Accessor/;
8 __PACKAGE__->mk_accessors( qw/
19 use Data::Dump qw/dump/;
20 use Carp qw/confess cluck croak/;
29 CWMP::Session - implement logic of CWMP protocol
35 my $server = CWMP::Session->new({
36 sock => $io_socket_object,
46 my $self = $class->SUPER::new( @_ );
48 confess "need sock" unless $self->sock;
49 confess "need store" unless $self->store;
50 my $peerhost = $self->sock->peerhost || confess "can't get sock->peerhost";
52 $self->debug( 0 ) unless $self->debug;
54 warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug;
56 my $store_obj = CWMP::Store->new({
57 debug => $self->debug,
61 croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
63 # FIXME looks ugly. Should we have separate accessor for this?
64 $self->store( $store_obj );
66 $self->create_dump( 1 ) if $self->debug > 2;
71 =head2 process_request
73 One request from client/response from server cycle. Call multiple times to
74 facilitate brain-dead concept of adding state to stateless protocol like
77 If used with debugging level of 3 or more, it will also create dumps of
78 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
79 of requests in single session.
88 my $sock = $self->sock || die "no sock?";
90 # die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
92 if ( ! $sock->connected ) {
93 warn "SOCKET NOT CONNECTED\n";
97 bless $sock, 'HTTP::Daemon::ClientConn';
99 # why do I have to do this?
100 # solution from http://use.perl.org/~Matts/journal/12896
101 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
103 my $r = $sock->get_request || confess "can't get_request";
105 my $xml = $r->content;
107 my $size = length( $xml );
109 warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
112 my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost);
114 if ( $self->create_dump ) {
115 write_file( $file, $r->as_string );
116 warn "### request dumped to file: $file\n" if $self->debug;
123 die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') );
125 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
127 $state = CWMP::Request->parse( $xml );
129 if ( defined( $state->{_dispatch} ) && $self->create_dump ) {
130 my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch});
131 symlink $file, $type || warn "can't symlink $file -> $type: $!";
134 warn "## acquired state = ", dump( $state ), "\n" if $self->debug;
136 if ( ! defined( $state->{DeviceID} ) ) {
137 warn "## state with DeviceID, using old one...\n";
138 $state->{DeviceID} = $self->state->{DeviceID};
141 $self->state( $state );
142 $self->store->update_state( $state );
146 warn "## empty request, using last request state\n";
148 $state = $self->state;
149 delete( $state->{_dispatch} );
150 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
153 $sock->send(join("\r\n",
155 'Content-Type: text/xml; charset="utf-8"',
156 'Server: PerlCWMP/42',
157 'SOAPServer: PerlCWMP/42'
160 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
162 my $uid = $self->store->state_to_uid( $state );
164 my $to_uid = join(" ", grep { defined($_) } "to $uid",
166 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'},
168 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'},
170 # $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'},
173 my $queue = CWMP::Queue->new({
175 debug => $self->debug,
180 if ( my $dispatch = $state->{_dispatch} ) {
181 $xml = $self->dispatch( $dispatch );
182 } elsif ( $job = $queue->dequeue ) {
183 $xml = $self->dispatch( $job->dispatch );
184 } elsif ( $size == 0 ) {
185 warn ">>> no more queued commands, closing connection $to_uid";
188 warn ">>> empty response $to_uid";
189 $state->{NoMoreRequests} = 1;
190 $xml = $self->dispatch( 'xml', sub {} );
193 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
194 $sock->send( $xml ) or die "can't send response";
196 warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid";
198 $job->finish if $job;
199 warn "### request over for $uid\n" if $self->debug;
201 return 1; # next request
206 $xml = $self->dispatch('Inform', $response_arguments );
208 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
215 my $dispatch = shift || die "no dispatch?";
218 my $response = CWMP::Methods->new({ debug => $self->debug });
220 if ( $response->can( $dispatch ) ) {
221 warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
222 my $xml = $response->$dispatch( $self->state, $args );
223 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
224 if ( $self->create_dump ) {
225 my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
226 write_file( $file, $xml );
227 warn "### response dump: $file\n" if $self->debug;
231 confess "can't dispatch to $dispatch";
238 return $self->error( 501, 'System error' );
243 my ($self, $number, $msg) = @_;
245 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
246 warn "Error - $number - $msg\n";
247 return 0; # close connection