r137@llin (orig r136): dpavlin | 2007-10-26 23:20:50 +0200
[perl-cwmp.git] / lib / CWMP / Session.pm
index 263794b..bd24efd 100644 (file)
@@ -7,7 +7,7 @@ use warnings;
 use base qw/Class::Accessor/;
 __PACKAGE__->mk_accessors( qw/
 debug
-store_path
+store
 
 sock
 state
@@ -34,7 +34,7 @@ CWMP::Session - implement logic of CWMP protocol
 
   my $server = CWMP::Session->new({
        sock => $io_socket_object,
-       store_path => 'state.db',
+       store => 'state.db',
        queue => [ qw/GetRPCMethods GetParameterNames/ ],
        debug => 1,
   });
@@ -51,12 +51,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;
 }
@@ -102,7 +105,7 @@ sub process_request {
        warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
 
        if ( $self->debug > 2 ) {
-               my $file = sprintf("dump/%04d.request", $dump_nr);
+               my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost);
                write_file( $file, $r->as_string );
                warn "### request dump: $file\n";
        }
@@ -113,24 +116,20 @@ sub process_request {
 
                die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );
 
+               $state = CWMP::Request->parse( $chunk );
 
-               if ( $chunk ) {
-                       warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;
-
-                       $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;
        }
 
 
@@ -150,7 +149,7 @@ sub process_request {
        } 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";
@@ -188,7 +187,7 @@ sub dispatch {
                my $xml = $response->$dispatch( $self->state, @_ );
                warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
                if ( $self->debug > 2 ) {
-                       my $file = sprintf("dump/%04d.response", $dump_nr++);
+                       my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
                        write_file( $file, $xml );
                        warn "### response dump: $file\n";
                }