/[cwmp]/google/trunk/lib/CWMP/Session.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /google/trunk/lib/CWMP/Session.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

google/lib/CWMP/Session.pm revision 111 by dpavlin, Sun Oct 21 22:58:22 2007 UTC google/trunk/lib/CWMP/Session.pm revision 170 by dpavlin, Sun Oct 28 13:05:01 2007 UTC
# Line 7  use warnings; Line 7  use warnings;
7  use base qw/Class::Accessor/;  use base qw/Class::Accessor/;
8  __PACKAGE__->mk_accessors( qw/  __PACKAGE__->mk_accessors( qw/
9  debug  debug
10  store_path  store
11    
12  sock  sock
13  state  state
# Line 34  CWMP::Session - implement logic of CWMP Line 34  CWMP::Session - implement logic of CWMP
34    
35    my $server = CWMP::Session->new({    my $server = CWMP::Session->new({
36          sock => $io_socket_object,          sock => $io_socket_object,
37          store_path => 'state.db',          store => 'state.db',
38          queue => [ qw/GetRPCMethods GetParameterNames/ ],          queue => [
39                    'GetRPCMethods',
40                    [ 'GetParameterValyes', 'InternetGatewayDevice.DeviceInfo.SerialNumber', 0 ],
41            ],
42          debug => 1,          debug => 1,
43    });    });
44    
# Line 51  sub new { Line 54  sub new {
54    
55          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
56    
57          $self->store( CWMP::Store->new({          my $store_obj = CWMP::Store->new({
58                  debug => $self->debug,                  debug => $self->debug,
59                  path => $self->store_path,                  %{ $self->store },
60          }) );          });
61    
62            croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
63    
64          croak "can't open ", $self->store_path, ": $!" unless $self->store;          # FIXME looks ugly. Should we have separate accessor for this?
65            $self->store( $store_obj );
66    
67          return $self;          return $self;
68  }  }
# Line 68  facilitate brain-dead concept of adding Line 74  facilitate brain-dead concept of adding
74  HTTP.  HTTP.
75    
76  If used with debugging level of 3 or more, it will also create dumps of  If used with debugging level of 3 or more, it will also create dumps of
77  requests named C<< nr.dump >> where C<nr> is number from 0 to total number  requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
78  of requests in single session.  of requests in single session.
79    
80  =cut  =cut
# Line 95  sub process_request { Line 101  sub process_request {
101    
102          my $r = $sock->get_request || confess "can't get_request";          my $r = $sock->get_request || confess "can't get_request";
103    
104          my $chunk = $r->content;          my $xml = $r->content;
105    
106          my $size = length( $chunk );          my $size = length( $xml );
107    
108          warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";          warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
109    
110          if ( $self->debug > 2 ) {          if ( $self->debug > 2 ) {
111                  my $file = $dump_nr++ . '.dump';                  my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost);
112                  write_file( $file, $r->as_string );                  write_file( $file, $r->as_string );
113                  warn "### request dump: $file\n";                  warn "### request dumped to file: $file\n";
114          }          }
115    
116          my $state;          my $state;
117    
118          if ( $size > 0 ) {          if ( $size > 0 ) {
119    
120                  die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );                  die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') );
121    
122                    warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
123    
124                  if ( $chunk ) {                  $state = CWMP::Request->parse( $xml );
                         warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;  
125    
126                          $state = CWMP::Request->parse( $chunk );                  warn "## acquired state = ", dump( $state ), "\n";
127    
128                          warn "## acquired state = ", dump( $state ), "\n";                  $self->state( $state );
129                    $self->store->update_state( ID => $state->{ID}, $state );
130    
131                          $self->state( $state );          } else {
                         $self->store->update_state( ID => $state->{ID}, $state );  
132    
133                  } else {                  warn "## empty request, using last request state\n";
                         warn "## empty request\n";  
                 }  
134    
         } else {  
135                  $state = $self->state;                  $state = $self->state;
136                  warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;                  delete( $state->{_dispatch} );
137                    #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
138          }          }
139    
140    
# Line 143  sub process_request { Line 147  sub process_request {
147    
148          $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );          $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
149                    
150          my $xml = '';          $xml = '';
151    
152          if ( my $dispatch = $state->{_dispatch} ) {          if ( my $dispatch = $state->{_dispatch} ) {
153                  $xml = $self->dispatch( $dispatch );                  $xml = $self->dispatch( $dispatch );
154          } elsif ( $dispatch = shift @{ $self->queue } ) {          } elsif ( $dispatch = shift @{ $self->queue } ) {
155                  $xml = $self->dispatch( $dispatch );                  $xml = $self->dispatch( $dispatch );
156          } elsif ( $size == 0 ) {          } elsif ( $size == 0 ) {
157                  warn ">>> closing connection\n";                  warn ">>> no more queued commands, closing connection\n";
158                  return 0;                  return 0;
159          } else {          } else {
160                  warn ">>> empty response\n";                  warn ">>> empty response\n";
# Line 172  sub process_request { Line 176  sub process_request {
176    
177    $xml = $self->dispatch('Inform', $response_arguments );    $xml = $self->dispatch('Inform', $response_arguments );
178    
179    If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
180    
181  =cut  =cut
182    
183  sub dispatch {  sub dispatch {
184          my $self = shift;          my $self = shift;
185    
186          my $dispatch = shift || die "no dispatch?";          my $dispatch = shift || die "no dispatch?";
187            my @args = @_;
188    
189            if ( ref($dispatch) eq 'ARRAY' ) {
190                    my @a = @$dispatch;
191                    $dispatch = shift @a;
192                    push @args, @a;
193            }
194    
195          my $response = CWMP::Response->new({ debug => $self->debug });          my $response = CWMP::Response->new({ debug => $self->debug });
196    
197          if ( $response->can( $dispatch ) ) {          if ( $response->can( $dispatch ) ) {
198                  warn ">>> dispatching to $dispatch\n";                  warn ">>> dispatching to $dispatch\n";
199                  my $xml = $response->$dispatch( $self->state, @_ );                  my $xml = $response->$dispatch( $self->state, @args );
200                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
201                    if ( $self->debug > 2 ) {
202                            my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
203                            write_file( $file, $xml );
204                            warn "### response dump: $file\n";
205                    }
206                  return $xml;                  return $xml;
207          } else {          } else {
208                  confess "can't dispatch to $dispatch";                  confess "can't dispatch to $dispatch";

Legend:
Removed from v.111  
changed lines
  Added in v.170

  ViewVC Help
Powered by ViewVC 1.1.26