fix CWMP::Vendor->vendor_config
[perl-cwmp.git] / lib / CWMP / Session.pm
index b971331..8119026 100644 (file)
@@ -15,14 +15,15 @@ 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::Parser;
 use CWMP::Methods;
 use CWMP::Store;
+use CWMP::Vendor;
+
+#use Devel::LeakTrace::Fast;
 
 =head1 NAME
 
@@ -45,14 +46,10 @@ sub new {
        my $class = shift;
        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 $peerhost\n" if $self->debug;
-
        my $store_obj = CWMP::Store->new({
                debug => $self->debug,
                %{ $self->store },
@@ -80,66 +77,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->create_dump ) {
-               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 );
+               $state = CWMP::Parser->parse( $xml );
 
-               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;
 
-               warn "## acquired state = ", dump( $state ), "\n";
-
-               if ( ! defined( $state->{DeviceID} ) ) {
-                       warn "## state with DeviceID, using old one...\n";
-                       $state->{DeviceID} = $self->state->{DeviceID};
+               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 {
 
@@ -150,47 +115,49 @@ 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'
-       )."\r\n");
-
-       $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
-
-       my $uid = $self->store->ID_to_uid( $state->{ID}, $state );
+       my $uid = $self->store->state_to_uid( $state );
 
        my $queue = CWMP::Queue->new({
                id => $uid,
                debug => $self->debug,
        });
-       my $job;
+
        $xml = '';
+       my $status = 200;
 
        if ( my $dispatch = $state->{_dispatch} ) {
                $xml = $self->dispatch( $dispatch );
-       } elsif ( $job = $queue->dequeue ) {
+       } elsif ( my $job = $queue->dequeue ) {
                $xml = $self->dispatch( $job->dispatch );
-       } elsif ( $size == 0 ) {
-               warn ">>> no more queued commands, closing connection to $uid\n";
-               return 0;
+               $job->finish;
        } else {
-               warn ">>> empty response to $uid\n";
-               $state->{NoMoreRequests} = 1;
-               $xml = $self->dispatch( 'xml', sub {} );
+               my @dispatch;
+#              @dispatch = CWMP::Vendor->all_parameters( $self->store, $uid, $queue );
+warn "XXX", dump @dispatch;
+               @dispatch = CWMP::Vendor->vendor_config( $self->store, $uid, $queue ) unless @dispatch;
+warn "XXX", dump @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 to $uid\n";
+       $out .= "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" if $state->{ID};
 
-       $job->finish if $job;
-       warn "### request over for $uid\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
@@ -213,11 +180,6 @@ sub dispatch {
                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->create_dump ) {
-                       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";