/[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/Server.pm revision 50 by dpavlin, Tue Jun 19 21:29:04 2007 UTC google/lib/CWMP/Session.pm revision 83 by dpavlin, Fri Jun 22 15:54:43 2007 UTC
# Line 1  Line 1 
1  # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST  # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
2  package CWMP::Server;  package CWMP::Session;
3    
4  use strict;  use strict;
5  use warnings;  use warnings;
# 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  port  store_path
11    
12  sock  sock
13  state  state
14  queue  queue
15    store
16  / );  / );
17    
18  use IO::Socket::INET;  use IO::Socket::INET;
19  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
20    use Carp qw/confess cluck croak/;
21    
22  use CWMP::Request;  use CWMP::Request;
23  use CWMP::Response;  use CWMP::Response;
24  use Carp qw/confess cluck/;  use CWMP::Store;
25    
26  =head1 NAME  =head1 NAME
27    
28  CWMP::Server - implement logic of CWMP protocol  CWMP::Session - implement logic of CWMP protocol
29    
30  =head1 METHODS  =head1 METHODS
31    
32  =head2 new  =head2 new
33    
34    my $server = CWMP::Server->new({ port => 3333 });    my $server = CWMP::Session->new({
35            sock => $io_socket_object,
36  =head2 run          store_path => 'state.db',
37            queue => [ qw/GetRPCMethods GetParameterNames/ ],
38            debug => 1,
39      });
40    
   $server->run();  
41    
42  =cut  =cut
43    
44  sub run {  sub new {
45          my $self = shift;          my $class = shift;
46            my $self = $class->SUPER::new( @_ );
47    
48          my $listen = IO::Socket::INET->new(          confess "need sock" unless $self->sock;
                 Listen    => 5,  
 #               LocalAddr => 'localhost',  
                 LocalPort => $self->port,  
                 Proto     => 'tcp',  
                 Blocking  => 1,  
                 ReuseAddr => 1,  
         );  
   
         warn "waiting for request on port ", $self->port, $/;  
   
         while ( my $sock = $listen->accept ) {  
                 $sock->autoflush(1);  
   
                 warn "connection from ", $sock->peerhost, "\n";  
   
                 $self->sock( $sock );   # FIXME this will not work for multiple clients  
                 while ( $self->process_request ) {  
                         warn "...another one bites a dust...\n";  
                 }  
49    
50                  warn "...returning to accepting new connections\n";          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
51          }  
52            $self->store( CWMP::Store->new({
53                    debug => $self->debug,
54                    path => $self->store_path,
55            }) );
56    
57            croak "can't open ", $self->store_path, ": $!" unless $self->store;
58    
59            return $self;
60  }  }
61    
62  =head2 process_request  =head2 process_request
# Line 79  sub process_request { Line 75  sub process_request {
75          die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' );          die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'IO::Socket::INET' );
76    
77          if ( ! $sock->connected ) {          if ( ! $sock->connected ) {
78                  warn "SOCKET NOT CONNECTED";                  warn "SOCKET NOT CONNECTED\n";
79                  return 0;                  return 0;
80          }          }
81    
# Line 116  sub process_request { Line 112  sub process_request {
112    
113                  do {                  do {
114    
115                          warn "get chunk len\n" if $self->debug;                          warn "get chunk len\n" if $self->debug > 1;
116                                                    
117                          my $hex;                          my $hex;
118                          do {                          do {
# Line 127  sub process_request { Line 123  sub process_request {
123                          die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);                          die "chunk size not valid hex: $hex" unless ( $hex =~ m/^[0-9a-f]+$/i);
124                          $len = hex( $hex );                          $len = hex( $hex );
125    
126                          warn "getting chunk of $len bytes\n" if $self->debug;                          warn "getting chunk of $len bytes\n" if $self->debug > 1;
127    
128                          $sock->read( my $buff, $len );                          $sock->read( my $buff, $len );
129                          $chunk .= $buff;                          $chunk .= $buff;
130    
131                          warn "--- $len bytes: --=>||$buff||<=--\n";                          warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1;
132    
133                  } while ( $len > 0 );                  } while ( $len > 0 );
134                  my $sep = $sock->getline;                  my $sep = $sock->getline;
# Line 144  sub process_request { Line 140  sub process_request {
140    
141          my $size = length( $chunk );          my $size = length( $chunk );
142    
143          warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";          warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
144    
145          my $state;          my $state;
146    
# Line 158  sub process_request { Line 154  sub process_request {
154    
155                          $state = CWMP::Request->parse( $chunk );                          $state = CWMP::Request->parse( $chunk );
156    
157                          warn "acquired state = ", dump( $state ), "\n";                          warn "## acquired state = ", dump( $state ), "\n";
158    
159                          $self->state( $state );                          $self->state( $state );
160                            $self->store->update_state( $state->{ID} => $state );
161    
162                  } else {                  } else {
163                          warn "empty request\n";                          warn "## empty request\n";
164                  }                  }
165    
166          } else {          } else {
167                  $state = $self->state;                  $state = $self->state;
168                  warn "last request state = ", dump( $state ), "\n";                  warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
169          }          }
170    
171    
# Line 177  sub process_request { Line 174  sub process_request {
174                  'Content-Type: text/xml; charset="utf-8"',                  'Content-Type: text/xml; charset="utf-8"',
175                  'Server: AcmeCWMP/42',                  'Server: AcmeCWMP/42',
176                  'SOAPServer: AcmeCWMP/42'                  'SOAPServer: AcmeCWMP/42'
177          ));          )."\r\n");
178    
179          $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} );
180                    
# Line 199  sub process_request { Line 196  sub process_request {
196          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
197          $sock->send( $xml ) or die "can't send response";          $sock->send( $xml ) or die "can't send response";
198    
199          warn "### request over";          warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
200    
201            warn "### request over\n" if $self->debug;
202    
203            return 1;       # next request
204  };  };
205    
206  =head2 dispatch  =head2 dispatch
# Line 218  sub dispatch { Line 218  sub dispatch {
218    
219          if ( $response->can( $dispatch ) ) {          if ( $response->can( $dispatch ) ) {
220                  warn ">>> dispatching to $dispatch\n";                  warn ">>> dispatching to $dispatch\n";
221                  my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n";                  my $xml = $response->$dispatch( $self->state, @_ );
222                  warn "## response payload: ",length($xml)," bytes\n$xml\n";                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
223                  return $xml;                  return $xml;
224          } else {          } else {
225                  confess "can't dispatch to $dispatch";                  confess "can't dispatch to $dispatch";
# Line 240  sub read_headers { Line 240  sub read_headers {
240    while (defined($_ = $self->sock->getline)) {    while (defined($_ = $self->sock->getline)) {
241      s/[\r\n]+$//;      s/[\r\n]+$//;
242      last unless length $_;      last unless length $_;
243          warn "-- $_\n";          warn "-- $_\n" if $self->debug;
244      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;
245      $self->{headers}->{$1} = $2;      $self->{headers}->{$1} = $2;
246    }    }

Legend:
Removed from v.50  
changed lines
  Added in v.83

  ViewVC Help
Powered by ViewVC 1.1.26