r222@brr: dpavlin | 2007-11-15 00:01:24 +0100
[perl-cwmp.git] / lib / CWMP / Session.pm
index cf11c30..3139cbb 100644 (file)
@@ -7,7 +7,8 @@ use warnings;
 use base qw/Class::Accessor/;
 __PACKAGE__->mk_accessors( qw/
 debug
-store
+create_dump
+session
 
 sock
 state
@@ -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
@@ -44,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,
@@ -59,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;
 }
 
@@ -105,7 +111,7 @@ sub process_request {
        $dump_nr++;
        my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost);
 
-       if ( $self->debug > 2 ) {
+       if ( $self->create_dump ) {
                write_file( $file, $r->as_string );
                warn "### request dumped to file: $file\n";
        }
@@ -120,13 +126,18 @@ sub process_request {
 
                $state = CWMP::Request->parse( $xml );
 
-               if ( defined( $state->{_dispatch} ) && $self->debug > 2 ) {
+               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 ( ! 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 );
 
@@ -193,22 +204,18 @@ If debugging level of 3 or more, it will create dumps of responses named C<< dum
 sub dispatch {
        my $self = shift;
 
-       my $dispatch = shift || die "no dispatch?";
-       my @args = @_;
+warn "##!!! dispatch(",dump( @_ ),")\n";
 
-       if ( ref($dispatch) eq 'ARRAY' ) {
-               my @a = @$dispatch;
-               $dispatch = shift @a;
-               push @args, @a;
-       }
+       my $dispatch = shift || die "no dispatch?";
+       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";