r271@brr: dpavlin | 2007-11-25 21:32:49 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 25 Nov 2007 20:33:31 +0000 (20:33 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 25 Nov 2007 20:33:31 +0000 (20:33 +0000)
 symlink dump files based on _trigger name

git-svn-id: https://perl-cwmp.googlecode.com/svn/trunk@229 836a5e1a-633d-0410-964b-294494ad4392

lib/CWMP/Request.pm
lib/CWMP/Session.pm

index 51f70f3..7d2084c 100644 (file)
@@ -157,8 +157,8 @@ sub parse {
                warn "### call_trigger( $trigger )\n";
                $self->call_trigger( $trigger, $state );
        }
-       # XXX don't propagate _trigger (useful?)
-       delete( $state->{_trigger} );
+       # XXX propagate _trigger (useful for symlinks)
+
        return $state;
 }
 
index e8fa1dd..e96f08d 100644 (file)
@@ -19,6 +19,7 @@ use HTTP::Daemon;
 use Data::Dump qw/dump/;
 use Carp qw/carp confess cluck croak/;
 use File::Slurp;
+use File::Path qw/mkpath/;
 
 use CWMP::Request;
 use CWMP::Methods;
@@ -82,7 +83,7 @@ of requests in single session.
 
 =cut
 
-my $dump_nr = 0;
+my $dump_by_ip;
 
 sub process_request {
        my $self = shift;
@@ -109,16 +110,19 @@ sub process_request {
                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" if $self->debug;
        }
@@ -133,8 +137,9 @@ 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: $!";
                }
 
@@ -201,7 +206,7 @@ sub process_request {
        $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 $to_uid";
+       warn ">>>> " . $ip . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid";
 
        $job->finish if $job;
        warn "### request over for $uid\n" if $self->debug;
@@ -230,7 +235,9 @@ 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" if $self->debug;
                }