X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=lib%2FCWMP%2FSession.pm;h=fd50a53821709b92e71460b9f5ee099de8f998bb;hp=40e5822e718044c60b3d38e3650b45550932961d;hb=f164de0a280f1ebcf8fd031f58ea917fb79bcedf;hpb=8ddfa88afea617dc8546f10313026d51fb79a926 diff --git a/lib/CWMP/Session.pm b/lib/CWMP/Session.pm index 40e5822..fd50a53 100644 --- a/lib/CWMP/Session.pm +++ b/lib/CWMP/Session.pm @@ -7,7 +7,7 @@ use warnings; use base qw/Class::Accessor/; __PACKAGE__->mk_accessors( qw/ debug -store_path +store sock state @@ -21,7 +21,7 @@ use Carp qw/confess cluck croak/; use File::Slurp; use CWMP::Request; -use CWMP::Response; +use CWMP::Methods; use CWMP::Store; =head1 NAME @@ -34,8 +34,11 @@ 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 => 'state.db', + queue => [ + 'GetRPCMethods', + [ 'GetParameterValyes', 'InternetGatewayDevice.DeviceInfo.SerialNumber', 0 ], + ], debug => 1, }); @@ -51,12 +54,15 @@ sub new { 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; - croak "can't open ", $self->store_path, ": $!" unless $self->store; + # FIXME looks ugly. Should we have separate accessor for this? + $self->store( $store_obj ); return $self; } @@ -68,7 +74,7 @@ facilitate brain-dead concept of adding state to stateless protocol like HTTP. If used with debugging level of 3 or more, it will also create dumps of -requests named C<< nr.dump >> where C is number from 0 to total number +requests named C<< dump/nr.request >> where C is number from 0 to total number of requests in single session. =cut @@ -95,42 +101,40 @@ sub process_request { my $r = $sock->get_request || confess "can't get_request"; - 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 = $dump_nr++ . '.dump'; + my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost); write_file( $file, $r->as_string ); - warn "### request dump: $file\n"; + warn "### request dumped to file: $file\n"; } 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') ); + warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; - if ( $chunk ) { - warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; + $state = CWMP::Request->parse( $xml ); - $state = CWMP::Request->parse( $chunk ); + warn "## acquired state = ", dump( $state ), "\n"; - warn "## acquired state = ", dump( $state ), "\n"; + $self->state( $state ); + $self->store->update_state( ID => $state->{ID}, $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; } @@ -143,14 +147,14 @@ sub process_request { $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); - my $xml = ''; + $xml = ''; 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"; + warn ">>> no more queued commands, closing connection\n"; return 0; } else { warn ">>> empty response\n"; @@ -172,19 +176,33 @@ sub process_request { $xml = $self->dispatch('Inform', $response_arguments ); +If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >> + =cut sub dispatch { my $self = shift; my $dispatch = shift || die "no dispatch?"; + my @args = @_; + + if ( ref($dispatch) eq 'ARRAY' ) { + my @a = @$dispatch; + $dispatch = shift @a; + push @args, @a; + } - 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, @_ ); + 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-%s.response", $dump_nr++, $self->sock->peerhost); + write_file( $file, $xml ); + warn "### response dump: $file\n"; + } return $xml; } else { confess "can't dispatch to $dispatch";