X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=lib%2FCWMP%2FSession.pm;h=25dd084af6243d99b7d96d8e575feb7870e94e1d;hb=774c5ba74d808d504f5e6dd2b838f68e37b50842;hp=488bd4f45e5fb36c06667f9e6e2529a46fbf03f9;hpb=fec1acf5d1dfce1a661535d2b40b0e9d97de6005;p=perl-cwmp.git diff --git a/lib/CWMP/Session.pm b/lib/CWMP/Session.pm index 488bd4f..25dd084 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 +create_dump +session sock state 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::Methods; use CWMP::Store; +#use Devel::LeakTrace::Fast; + =head1 NAME CWMP::Session - implement logic of CWMP protocol @@ -33,8 +34,9 @@ CWMP::Session - implement logic of CWMP protocol my $server = CWMP::Session->new({ sock => $io_socket_object, - store => 'state.db', + store => { ... }, debug => 1, + create_dump => 1, }); =cut @@ -43,12 +45,10 @@ 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; - my $store_obj = CWMP::Store->new({ debug => $self->debug, %{ $self->store }, @@ -59,6 +59,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; } @@ -74,61 +76,34 @@ 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 $xml = $r->content; + my ( $self, $ip, $xml ) = @_; my $size = length( $xml ); - warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; - - $dump_nr++; - my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost); - - if ( $self->debug > 2 ) { - write_file( $file, $r->as_string ); - warn "### request dumped to file: $file\n"; - } - my $state; if ( $size > 0 ) { - die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') ); - warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; $state = CWMP::Request->parse( $xml ); - if ( defined( $state->{_dispatch} ) && $self->debug > 2 ) { - 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; - warn "## acquired state = ", dump( $state ), "\n"; + 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 ); + } + } $self->state( $state ); - $self->store->update_state( ID => $state->{ID}, $state ); + $self->store->update_state( $state ); } else { @@ -139,18 +114,28 @@ sub process_request { #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; } - - $sock->send(join("\r\n", + my $out = join("\r\n", 'HTTP/1.1 200 OK', 'Content-Type: text/xml; charset="utf-8"', - 'Server: AcmeCWMP/42', - 'SOAPServer: AcmeCWMP/42' - )."\r\n"); + 'Server: Perl-CWMP/42', + 'SOAPServer: Perl-CWMP/42' + ) . "\r\n"; - $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); + $out .= "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 => $self->store->ID_to_uid( $state->{ID}, $state ), + id => $uid, debug => $self->debug, }); my $job; @@ -160,24 +145,19 @@ 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\n"; - return 0; } else { - warn ">>> empty response\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"; - - warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; + $out .= "Content-Length: " . length( $xml ) . "\r\n\r\n"; + $out .= $xml if length($xml); $job->finish if $job; - warn "### request over\n" if $self->debug; + warn "### request over for $uid\n" if $self->debug; - return 1; # next request + return $out; # next request }; =head2 dispatch @@ -192,25 +172,14 @@ 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 ) { - 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";