X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=lib%2FCWMP%2FSession.pm;h=6256f57bb1a8b94668c74420ae242a2864ca3549;hp=fd50a53821709b92e71460b9f5ee099de8f998bb;hb=7a346eeec59e7415d184a0e76cc863863cc308f6;hpb=f164de0a280f1ebcf8fd031f58ea917fb79bcedf diff --git a/lib/CWMP/Session.pm b/lib/CWMP/Session.pm index fd50a53..6256f57 100644 --- a/lib/CWMP/Session.pm +++ b/lib/CWMP/Session.pm @@ -7,11 +7,11 @@ use warnings; use base qw/Class::Accessor/; __PACKAGE__->mk_accessors( qw/ debug -store +create_dump +session sock state -queue store / ); @@ -34,12 +34,9 @@ CWMP::Session - implement logic of CWMP protocol my $server = CWMP::Session->new({ sock => $io_socket_object, - store => 'state.db', - queue => [ - 'GetRPCMethods', - [ 'GetParameterValyes', 'InternetGatewayDevice.DeviceInfo.SerialNumber', 0 ], - ], + store => { ... }, debug => 1, + create_dump => 1, }); =cut @@ -49,10 +46,12 @@ 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; my $store_obj = CWMP::Store->new({ debug => $self->debug, @@ -64,6 +63,8 @@ sub new { # FIXME looks ugly. Should we have separate accessor for this? $self->store( $store_obj ); + $self->create_dump( 1 ) if $self->debug > 2; + return $self; } @@ -107,10 +108,12 @@ sub process_request { warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; - if ( $self->debug > 2 ) { - my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost); + $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 dumped to file: $file\n"; + warn "### request dumped to file: $file\n" if $self->debug; } my $state; @@ -123,10 +126,20 @@ sub process_request { $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: $!"; + } + + warn "## acquired state = ", dump( $state ), "\n" if $self->debug; + + if ( ! defined( $state->{DeviceID} ) ) { + warn "## state with DeviceID, using old one...\n"; + $state->{DeviceID} = $self->state->{DeviceID}; + } $self->state( $state ); - $self->store->update_state( ID => $state->{ID}, $state ); + $self->store->update_state( $state ); } else { @@ -146,18 +159,25 @@ sub process_request { )."\r\n"); $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); - + + my $uid = $self->store->state_to_uid( $state ); + + 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 ">>> no more queued commands, closing connection\n"; + warn ">>> no more queued commands, closing connection to $uid\n"; return 0; } else { - warn ">>> empty response\n"; + warn ">>> empty response to $uid\n"; $state->{NoMoreRequests} = 1; $xml = $self->dispatch( 'xml', sub {} ); } @@ -165,9 +185,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\n"; - warn "### request over\n" if $self->debug; + $job->finish if $job; + warn "### request over for $uid\n" if $self->debug; return 1; # next request }; @@ -184,24 +205,18 @@ 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 $args = shift; my $response = CWMP::Methods->new({ debug => $self->debug }); if ( $response->can( $dispatch ) ) { - warn ">>> dispatching to $dispatch\n"; - my $xml = $response->$dispatch( $self->state, @args ); + 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 ) { + 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 {