X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=lib%2FCWMP%2FSession.pm;h=e2f6b8cbc8454232530aa3227b9517f5aa83f54e;hp=263794bcc39a541bc2a56ab165539d21b19a75b8;hb=d4b4202a2f21c686eb6bb6c9efaf49cf3ed7c954;hpb=edfd1a00b150b9255d8190d1f0f3272b81e5b1e1 diff --git a/lib/CWMP/Session.pm b/lib/CWMP/Session.pm index 263794b..e2f6b8c 100644 --- a/lib/CWMP/Session.pm +++ b/lib/CWMP/Session.pm @@ -7,23 +7,25 @@ use warnings; use base qw/Class::Accessor/; __PACKAGE__->mk_accessors( qw/ debug -store_path +create_dump +session sock state -queue store / ); use HTTP::Daemon; use Data::Dump qw/dump/; -use Carp qw/confess cluck croak/; +use Carp qw/carp confess cluck croak/; use File::Slurp; use CWMP::Request; -use CWMP::Response; +use CWMP::Methods; use CWMP::Store; +#use Devel::LeakTrace::Fast; + =head1 NAME CWMP::Session - implement logic of CWMP protocol @@ -34,9 +36,9 @@ CWMP::Session - implement logic of CWMP protocol my $server = CWMP::Session->new({ sock => $io_socket_object, - store_path => 'state.db', - queue => [ qw/GetRPCMethods GetParameterNames/ ], + store => { ... }, debug => 1, + create_dump => 1, }); =cut @@ -46,17 +48,24 @@ sub new { my $self = $class->SUPER::new( @_ ); confess "need sock" unless $self->sock; + confess "need store" unless $self->store; + my $peerhost = $self->sock->peerhost || confess "can't get sock->peerhost"; $self->debug( 0 ) unless $self->debug; - warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; + warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug; - $self->store( CWMP::Store->new({ + my $store_obj = CWMP::Store->new({ debug => $self->debug, - path => $self->store_path, - }) ); + %{ $self->store }, + }); + + croak "can't open ", dump( $self->store ), ": $!" unless $store_obj; + + # FIXME looks ugly. Should we have separate accessor for this? + $self->store( $store_obj ); - croak "can't open ", $self->store_path, ": $!" unless $self->store; + $self->create_dump( 1 ) if $self->debug > 2; return $self; } @@ -93,67 +102,98 @@ sub process_request { # solution from http://use.perl.org/~Matts/journal/12896 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new; - my $r = $sock->get_request || confess "can't get_request"; + my $r = $sock->get_request; + + if ( ! $r ) { + carp "can't get_request"; + return 0; + } - my $chunk = $r->content; + my $xml = $r->content; - my $size = length( $chunk ); + my $size = length( $xml ); warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; - if ( $self->debug > 2 ) { - my $file = sprintf("dump/%04d.request", $dump_nr); + $dump_nr++; + my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost); + + if ( $self->create_dump ) { write_file( $file, $r->as_string ); - warn "### request dump: $file\n"; + warn "### request dumped to file: $file\n" if $self->debug; } my $state; if ( $size > 0 ) { - die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); - + die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') ); - if ( $chunk ) { - warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; + warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; - $state = CWMP::Request->parse( $chunk ); + $state = CWMP::Request->parse( $xml ); - warn "## acquired state = ", dump( $state ), "\n"; + if ( defined( $state->{_dispatch} ) && $self->create_dump ) { + my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch}); + symlink $file, $type || warn "can't symlink $file -> $type: $!"; + } - $self->state( $state ); - $self->store->update_state( ID => $state->{ID}, $state ); + warn "## acquired state = ", dump( $state ), "\n" if $self->debug; - } else { - warn "## empty request\n"; + if ( ! defined( $state->{DeviceID} ) ) { + warn "## state with DeviceID, using old one...\n"; + $state->{DeviceID} = $self->state->{DeviceID}; } + $self->state( $state ); + $self->store->update_state( $state ); + } else { + + warn "## empty request, using last request state\n"; + $state = $self->state; - warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; + delete( $state->{_dispatch} ); + #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; } - $sock->send(join("\r\n", 'HTTP/1.1 200 OK', 'Content-Type: text/xml; charset="utf-8"', - 'Server: AcmeCWMP/42', - 'SOAPServer: AcmeCWMP/42' + 'Server: PerlCWMP/42', + 'SOAPServer: PerlCWMP/42' )."\r\n"); $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); - - my $xml = ''; + + my $uid = $self->store->state_to_uid( $state ); + + my $to_uid = join(" ", grep { defined($_) } "to $uid", + # board + $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'}, + # version + $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'}, + # summary +# $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'}, + ) . "\n"; + + my $queue = CWMP::Queue->new({ + id => $uid, + debug => $self->debug, + }); + my $job; + $xml = ''; if ( my $dispatch = $state->{_dispatch} ) { $xml = $self->dispatch( $dispatch ); - } elsif ( $dispatch = shift @{ $self->queue } ) { - $xml = $self->dispatch( $dispatch ); + } elsif ( $job = $queue->dequeue ) { + $xml = $self->dispatch( $job->dispatch ); } elsif ( $size == 0 ) { - warn ">>> closing connection\n"; - return 0; + warn ">>> no more queued commands, no client pending, closing connection $to_uid"; + $sock->close; + return; } else { - warn ">>> empty response\n"; + warn ">>> empty response $to_uid"; $state->{NoMoreRequests} = 1; $xml = $self->dispatch( 'xml', sub {} ); } @@ -161,9 +201,10 @@ sub process_request { $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); $sock->send( $xml ) or die "can't send response"; - warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; + warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid"; - warn "### request over\n" if $self->debug; + $job->finish if $job; + warn "### request over for $uid\n" if $self->debug; return 1; # next request }; @@ -180,17 +221,18 @@ sub dispatch { my $self = shift; my $dispatch = shift || die "no dispatch?"; + my $args = shift; - my $response = CWMP::Response->new({ debug => $self->debug }); + my $response = CWMP::Methods->new({ debug => $self->debug }); if ( $response->can( $dispatch ) ) { - warn ">>> dispatching to $dispatch\n"; - my $xml = $response->$dispatch( $self->state, @_ ); + warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n"; + my $xml = $response->$dispatch( $self->state, $args ); warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; - if ( $self->debug > 2 ) { - my $file = sprintf("dump/%04d.response", $dump_nr++); + if ( $self->create_dump ) { + my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); write_file( $file, $xml ); - warn "### response dump: $file\n"; + warn "### response dump: $file\n" if $self->debug; } return $xml; } else {