X-Git-Url: http://git.rot13.org/?p=perl-cwmp.git;a=blobdiff_plain;f=lib%2FCWMP%2FSession.pm;h=27a255793b7302dba82eafd94338cb9a833c27e8;hp=c9c1f1ff74c5f667b8adb98ebb936af63432f104;hb=b5312b7c069d4c93c9be9f2c1aa713d8ec441c05;hpb=73114373a890391990bba21f0b4eadb18a4a859b diff --git a/lib/CWMP/Session.pm b/lib/CWMP/Session.pm index c9c1f1f..27a2557 100644 --- a/lib/CWMP/Session.pm +++ b/lib/CWMP/Session.pm @@ -17,13 +17,16 @@ 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 File::Path qw/mkpath/; use CWMP::Request; use CWMP::Methods; use CWMP::Store; +#use Devel::LeakTrace::Fast; + =head1 NAME CWMP::Session - implement logic of CWMP protocol @@ -80,7 +83,7 @@ of requests in single session. =cut -my $dump_nr = 0; +my $dump_by_ip; sub process_request { my $self = shift; @@ -100,20 +103,28 @@ 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 ) { + warn "WARNING: can't get_request\n"; + return 0; + } + + my $ip = $sock->peerhost || confess "can't get peerhost from sock: $!"; my $xml = $r->content; my $size = length( $xml ); - warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; + warn "<<<< $ip [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; - $dump_nr++; - my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost); + my $dump_nr = $dump_by_ip->{$ip}++; + my $file = sprintf("./dump/%s/%04d.request", $ip, $dump_nr); if ( $self->create_dump ) { + mkpath "dump/$ip" unless -e "dump/$ip"; 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; @@ -126,16 +137,22 @@ sub process_request { $state = CWMP::Request->parse( $xml ); - if ( defined( $state->{_dispatch} ) && $self->create_dump ) { - my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch}); + if ( defined( $state->{_trigger} ) && $self->create_dump ) { + my $type = sprintf("dump/%s/%04d-%s", $ip, $dump_nr, $state->{_trigger}); + $file =~ s!^.*?([^/]+)$!$1!; #!vim symlink $file, $type || warn "can't symlink $file -> $type: $!"; } - warn "## acquired state = ", dump( $state ), "\n"; + 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}; + 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 ); + } } $self->state( $state ); @@ -150,18 +167,26 @@ sub process_request { #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 $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, @@ -173,19 +198,18 @@ sub process_request { $xml = $self->dispatch( $dispatch ); } elsif ( $job = $queue->dequeue ) { $xml = $self->dispatch( $job->dispatch ); - } elsif ( $size == 0 ) { - warn ">>> no more queued commands, closing connection to $uid\n"; - return 0; } else { - warn ">>> empty response to $uid\n"; + warn ">>> empty response $to_uid"; $state->{NoMoreRequests} = 1; - $xml = $self->dispatch( 'xml', sub {} ); + $xml = ''; } $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); - $sock->send( $xml ) or die "can't send response"; + if (length($xml)) { + $sock->send( $xml ) or die "can't send response"; + } - warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes to $uid\n"; + warn ">>>> " . $ip . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid"; $job->finish if $job; warn "### request over for $uid\n" if $self->debug; @@ -214,9 +238,11 @@ sub dispatch { my $xml = $response->$dispatch( $self->state, $args ); warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; if ( $self->create_dump ) { - my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); + my $ip = $self->sock->peerhost || confess "can't get sock->peerhost: $!"; + my $dump_nr = $dump_by_ip->{$ip}++; + my $file = sprintf("dump/%s/%04d.response", $ip, $dump_nr ); write_file( $file, $xml ); - warn "### response dump: $file\n"; + warn "### response dump: $file\n" if $self->debug; } return $xml; } else {