/[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

revision 199 by dpavlin, Wed Nov 14 18:16:14 2007 UTC revision 210 by dpavlin, Sun Nov 18 17:03:09 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  create_dump
11    session
12    
13  sock  sock
14  state  state
# Line 33  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 => 'state.db',          store => { ... },
38          debug => 1,          debug => 1,
39            create_dump => 1,
40    });    });
41    
42  =cut  =cut
# Line 44  sub new { Line 46  sub new {
46          my $self = $class->SUPER::new( @_ );          my $self = $class->SUPER::new( @_ );
47    
48          confess "need sock" unless $self->sock;          confess "need sock" unless $self->sock;
49            confess "need store" unless $self->store;
50            my $peerhost = $self->sock->peerhost || confess "can't get sock->peerhost";
51    
52          $self->debug( 0 ) unless $self->debug;          $self->debug( 0 ) unless $self->debug;
53    
54          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug;
55    
56          my $store_obj = CWMP::Store->new({          my $store_obj = CWMP::Store->new({
57                  debug => $self->debug,                  debug => $self->debug,
# Line 59  sub new { Line 63  sub new {
63          # FIXME looks ugly. Should we have separate accessor for this?          # FIXME looks ugly. Should we have separate accessor for this?
64          $self->store( $store_obj );          $self->store( $store_obj );
65    
66            $self->create_dump( 1 ) if $self->debug > 2;
67    
68          return $self;          return $self;
69  }  }
70    
# Line 105  sub process_request { Line 111  sub process_request {
111          $dump_nr++;          $dump_nr++;
112          my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost);          my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost);
113    
114          if ( $self->debug > 2 ) {          if ( $self->create_dump ) {
115                  write_file( $file, $r->as_string );                  write_file( $file, $r->as_string );
116                  warn "### request dumped to file: $file\n";                  warn "### request dumped to file: $file\n" if $self->debug;
117          }          }
118    
119          my $state;          my $state;
# Line 120  sub process_request { Line 126  sub process_request {
126    
127                  $state = CWMP::Request->parse( $xml );                  $state = CWMP::Request->parse( $xml );
128    
129                  if ( defined( $state->{_dispatch} ) && $self->debug > 2 ) {                  if ( defined( $state->{_dispatch} ) && $self->create_dump ) {
130                          my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch});                          my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch});
131                          symlink $file, $type || warn "can't symlink $file -> $type: $!";                          symlink $file, $type || warn "can't symlink $file -> $type: $!";
132                  }                  }
133    
134                  warn "## acquired state = ", dump( $state ), "\n";                  warn "## acquired state = ", dump( $state ), "\n" if $self->debug;
135    
136                    if ( ! defined( $state->{DeviceID} ) ) {
137                            warn "## state with DeviceID, using old one...\n";
138                            $state->{DeviceID} = $self->state->{DeviceID};
139                    }
140    
141                  $self->state( $state );                  $self->state( $state );
142                  $self->store->update_state( ID => $state->{ID}, $state );                  $self->store->update_state( $state );
143    
144          } else {          } else {
145    
# Line 139  sub process_request { Line 150  sub process_request {
150                  #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;                  #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
151          }          }
152    
   
153          $sock->send(join("\r\n",          $sock->send(join("\r\n",
154                  'HTTP/1.1 200 OK',                  'HTTP/1.1 200 OK',
155                  'Content-Type: text/xml; charset="utf-8"',                  'Content-Type: text/xml; charset="utf-8"',
156                  'Server: AcmeCWMP/42',                  'Server: PerlCWMP/42',
157                  'SOAPServer: AcmeCWMP/42'                  'SOAPServer: PerlCWMP/42'
158          )."\r\n");          )."\r\n");
159    
160          $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} );
161    
162          my $uid = $self->store->ID_to_uid( $state->{ID}, $state );          my $uid = $self->store->state_to_uid( $state );
163    
164            my $to_uid = join(" ", "to $uid",
165                            # board
166                            $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'},
167                            # version
168                            $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'},
169                            # summary
170    #                       $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'},
171            ) . "\n";
172    
173          my $queue = CWMP::Queue->new({          my $queue = CWMP::Queue->new({
174                  id => $uid,                  id => $uid,
# Line 163  sub process_request { Line 182  sub process_request {
182          } elsif ( $job = $queue->dequeue ) {          } elsif ( $job = $queue->dequeue ) {
183                  $xml = $self->dispatch( $job->dispatch );                  $xml = $self->dispatch( $job->dispatch );
184          } elsif ( $size == 0 ) {          } elsif ( $size == 0 ) {
185                  warn ">>> no more queued commands, closing connection to $uid\n";                  warn ">>> no more queued commands, closing connection $to_uid";
186                  return 0;                  return 0;
187          } else {          } else {
188                  warn ">>> empty response to $uid\n";                  warn ">>> empty response $to_uid";
189                  $state->{NoMoreRequests} = 1;                  $state->{NoMoreRequests} = 1;
190                  $xml = $self->dispatch( 'xml', sub {} );                  $xml = $self->dispatch( 'xml', sub {} );
191          }          }
# Line 174  sub process_request { Line 193  sub process_request {
193          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
194          $sock->send( $xml ) or die "can't send response";          $sock->send( $xml ) or die "can't send response";
195    
196          warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes to $uid\n";          warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid";
197    
198          $job->finish if $job;          $job->finish if $job;
199          warn "### request over for $uid\n" if $self->debug;          warn "### request over for $uid\n" if $self->debug;
# Line 193  If debugging level of 3 or more, it will Line 212  If debugging level of 3 or more, it will
212  sub dispatch {  sub dispatch {
213          my $self = shift;          my $self = shift;
214    
 warn "##!!! dispatch(",dump( @_ ),")\n";  
   
215          my $dispatch = shift || die "no dispatch?";          my $dispatch = shift || die "no dispatch?";
216          my $args = shift;          my $args = shift;
217    
# Line 204  warn "##!!! dispatch(",dump( @_ ),")\n"; Line 221  warn "##!!! dispatch(",dump( @_ ),")\n";
221                  warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";                  warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
222                  my $xml = $response->$dispatch( $self->state, $args );                  my $xml = $response->$dispatch( $self->state, $args );
223                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
224                  if ( $self->debug > 2 ) {                  if ( $self->create_dump ) {
225                          my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);                          my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
226                          write_file( $file, $xml );                          write_file( $file, $xml );
227                          warn "### response dump: $file\n";                          warn "### response dump: $file\n" if $self->debug;
228                  }                  }
229                  return $xml;                  return $xml;
230          } else {          } else {

Legend:
Removed from v.199  
changed lines
  Added in v.210

  ViewVC Help
Powered by ViewVC 1.1.26