X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=lib%2FCWMP%2FSession.pm;h=d6621687fd02f179d0ef9e27cdaab98dbd1d6b4b;hp=aee14183aae47929312f14a8ac99239005338f47;hb=b3f35798ca497b3d02382bfb9f80f97247694e3f;hpb=83b1af90b387c3cedb13d6a87712042c23365d16 diff --git a/lib/CWMP/Session.pm b/lib/CWMP/Session.pm index aee1418..d662168 100644 --- a/lib/CWMP/Session.pm +++ b/lib/CWMP/Session.pm @@ -7,22 +7,23 @@ 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 File::Slurp; +use Carp qw/carp confess cluck croak/; -use CWMP::Request; -use CWMP::Response; +use CWMP::Parser; +use CWMP::Methods; use CWMP::Store; +use CWMP::Vendor; + +#use Devel::LeakTrace::Fast; =head1 NAME @@ -34,9 +35,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 @@ -45,18 +46,21 @@ sub new { my $class = shift; my $self = $class->SUPER::new( @_ ); - confess "need sock" unless $self->sock; + confess "need store" unless $self->store; $self->debug( 0 ) unless $self->debug; - warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->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; } @@ -73,99 +77,84 @@ of requests in single session. =cut -my $dump_nr = 0; sub process_request { - my $self = shift; - - my $sock = $self->sock || die "no sock?"; - -# die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' ); - - if ( ! $sock->connected ) { - warn "SOCKET NOT CONNECTED\n"; - return 0; - } - - bless $sock, 'HTTP::Daemon::ClientConn'; - - # why do I have to do this? - # 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 $chunk = $r->content; + my ( $self, $ip, $xml ) = @_; - my $size = length( $chunk ); - - warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; - - if ( $self->debug > 2 ) { - my $file = sprintf("dump/%04d.request", $dump_nr++); - write_file( $file, $r->as_string ); - warn "### request dump: $file\n"; - } + my $size = length( $xml ); my $state; if ( $size > 0 ) { - die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); + warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; + $state = CWMP::Parser->parse( $xml ); - if ( $chunk ) { - warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; + warn "## acquired state = ", dump( $state ), "\n" if $self->debug; - $state = CWMP::Request->parse( $chunk ); + if ( ! defined( $state->{DeviceId} ) ) { + if ( $self->state ) { + warn "## state without DeviceId, using old one...\n"; + $state->{DeviceId} = $self->state->{DeviceId}; + } else { + warn "WARNING: state without DeviceId, and I don't have old one!\n"; + warn "## state = ",dump( $state ); + } + } - warn "## acquired state = ", dump( $state ), "\n"; + $self->state( $state ); + $self->store->update_state( $state ); - $self->state( $state ); - $self->store->update_state( ID => $state->{ID}, $state ); + } else { - } else { - warn "## empty request\n"; - } + warn "## empty request, using last request state\n"; - } else { $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; } + my $uid = $self->store->state_to_uid( $state ); - $sock->send(join("\r\n", - 'HTTP/1.1 200 OK', - 'Content-Type: text/xml; charset="utf-8"', - 'Server: AcmeCWMP/42', - 'SOAPServer: AcmeCWMP/42' - )."\r\n"); + my $queue = CWMP::Queue->new({ + id => $uid, + debug => $self->debug, + }); - $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); - - my $xml = ''; + $xml = ''; + my $status = 200; if ( my $dispatch = $state->{_dispatch} ) { $xml = $self->dispatch( $dispatch ); - } elsif ( $dispatch = shift @{ $self->queue } ) { - $xml = $self->dispatch( $dispatch ); - } elsif ( $size == 0 ) { - warn ">>> closing connection\n"; - return 0; + } elsif ( my $job = $queue->dequeue ) { + $xml = $self->dispatch( $job->dispatch ); + $job->finish; } else { - warn ">>> empty response\n"; - $state->{NoMoreRequests} = 1; - $xml = $self->dispatch( 'xml', sub {} ); + my @dispatch = CWMP::Vendor->all_parameters( $self->store, $uid, $queue ); + @dispatch = CWMP::Vendor->vendor_config( $self->store, $uid, $queue ) unless @dispatch; + $xml = $self->dispatch( @dispatch ) if @dispatch; + if ( ! $xml ) { + warn ">>> no more work for $uid sending empty response\n"; + $state->{NoMoreRequests} = 1; + $xml = ''; + $status = 204; + } } - $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); - $sock->send( $xml ) or die "can't send response"; + my $out = join("\r\n", + "HTTP/1.1 $status OK", + 'Content-Type: text/xml; charset="utf-8"', + 'Server: Perl-CWMP/42', + 'SOAPServer: Perl-CWMP/42' + ) . "\r\n"; - warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; + $out .= "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" if $state->{ID}; - warn "### request over\n" if $self->debug; + $out .= "Content-Length: " . length( $xml ) . "\r\n\r\n"; + $out .= $xml if length($xml); - return 1; # next request + return $out; # next request }; =head2 dispatch @@ -180,18 +169,14 @@ 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++); - write_file( $file, $xml ); - warn "### response dump: $file\n"; - } return $xml; } else { confess "can't dispatch to $dispatch";