/[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 50 by dpavlin, Tue Jun 19 21:29:04 2007 UTC revision 79 by dpavlin, Fri Jun 22 14:32:13 2007 UTC
# Line 8  use base qw/Class::Accessor/; Line 8  use base qw/Class::Accessor/;
8  __PACKAGE__->mk_accessors( qw/  __PACKAGE__->mk_accessors( qw/
9  debug  debug
10  port  port
11    store_path
12    
13  sock  sock
14  state  state
15  queue  queue
16    store
17  / );  / );
18    
19  use IO::Socket::INET;  use IO::Socket::INET;
20  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
21    use Carp qw/confess cluck croak/;
22    
23  use CWMP::Request;  use CWMP::Request;
24  use CWMP::Response;  use CWMP::Response;
25  use Carp qw/confess cluck/;  use CWMP::Store;
26    
27  =head1 NAME  =head1 NAME
28    
# Line 27  CWMP::Server - implement logic of CWMP p Line 32  CWMP::Server - implement logic of CWMP p
32    
33  =head2 new  =head2 new
34    
35    my $server = CWMP::Server->new({ port => 3333 });    my $server = CWMP::Server->new({
36            port => 3333,
37            store_path => 'state.db',
38            debug => 1,
39      });
40    
41  =head2 run  =head2 run
42    
# Line 47  sub run { Line 56  sub run {
56                  ReuseAddr => 1,                  ReuseAddr => 1,
57          );          );
58    
59          warn "waiting for request on port ", $self->port, $/;          warn "ACS waiting for request on port ", $self->port,
60                    $self->queue ? " queue ( " . join(",",@{$self->queue}) . " )" : "",
61                    "\n";
62    
63            $self->debug( 0 ) unless $self->debug;
64            warn "## debug level: ", $self->debug, "\n" if $self->debug;
65    
66            $self->store( CWMP::Store->new({
67                    debug => $self->debug,
68                    path => $self->store_path,
69            }) );
70            croak "can't open ", $self->store_path, ": $!" unless $self->store;
71    
72          while ( my $sock = $listen->accept ) {          while ( my $sock = $listen->accept ) {
73                  $sock->autoflush(1);                  $sock->autoflush(1);
74    
75                  warn "connection from ", $sock->peerhost, "\n";                  warn "connection from ", $sock->peerhost, "\n" if $self->debug;
76    
77                  $self->sock( $sock );   # FIXME this will not work for multiple clients                  $self->sock( $sock );   # FIXME this will not work for multiple clients
78                  while ( $self->process_request ) {                  while ( $self->process_request ) {
79                          warn "...another one bites a dust...\n";                          warn "...another one bites the dust...\n";
80                  }                  }
81    
82                  warn "...returning to accepting new connections\n";                  warn "...returning to accepting new connections\n";
# Line 79  sub process_request { Line 99  sub process_request {
99          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' );
100    
101          if ( ! $sock->connected ) {          if ( ! $sock->connected ) {
102                  warn "SOCKET NOT CONNECTED";                  warn "SOCKET NOT CONNECTED\n";
103                  return 0;                  return 0;
104          }          }
105    
# Line 116  sub process_request { Line 136  sub process_request {
136    
137                  do {                  do {
138    
139                          warn "get chunk len\n" if $self->debug;                          warn "get chunk len\n" if $self->debug > 1;
140                                                    
141                          my $hex;                          my $hex;
142                          do {                          do {
# Line 127  sub process_request { Line 147  sub process_request {
147                          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);
148                          $len = hex( $hex );                          $len = hex( $hex );
149    
150                          warn "getting chunk of $len bytes\n" if $self->debug;                          warn "getting chunk of $len bytes\n" if $self->debug > 1;
151    
152                          $sock->read( my $buff, $len );                          $sock->read( my $buff, $len );
153                          $chunk .= $buff;                          $chunk .= $buff;
154    
155                          warn "--- $len bytes: --=>||$buff||<=--\n";                          warn "--- $len bytes: --=>||$buff||<=--\n" if $self->debug > 1;
156    
157                  } while ( $len > 0 );                  } while ( $len > 0 );
158                  my $sep = $sock->getline;                  my $sep = $sock->getline;
# Line 144  sub process_request { Line 164  sub process_request {
164    
165          my $size = length( $chunk );          my $size = length( $chunk );
166    
167          warn "<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";          warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
168    
169          my $state;          my $state;
170    
# Line 158  sub process_request { Line 178  sub process_request {
178    
179                          $state = CWMP::Request->parse( $chunk );                          $state = CWMP::Request->parse( $chunk );
180    
181                          warn "acquired state = ", dump( $state ), "\n";                          warn "## acquired state = ", dump( $state ), "\n";
182    
183                          $self->state( $state );                          $self->state( $state );
184                            $self->store->update_state( $state->{ID} => $state );
185    
186                  } else {                  } else {
187                          warn "empty request\n";                          warn "## empty request\n";
188                  }                  }
189    
190          } else {          } else {
191                  $state = $self->state;                  $state = $self->state;
192                  warn "last request state = ", dump( $state ), "\n";                  warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
193          }          }
194    
195    
# Line 177  sub process_request { Line 198  sub process_request {
198                  'Content-Type: text/xml; charset="utf-8"',                  'Content-Type: text/xml; charset="utf-8"',
199                  'Server: AcmeCWMP/42',                  'Server: AcmeCWMP/42',
200                  'SOAPServer: AcmeCWMP/42'                  'SOAPServer: AcmeCWMP/42'
201          ));          )."\r\n");
202    
203          $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} );
204                    
# Line 199  sub process_request { Line 220  sub process_request {
220          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );          $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
221          $sock->send( $xml ) or die "can't send response";          $sock->send( $xml ) or die "can't send response";
222    
223          warn "### request over";          warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
224    
225            warn "### request over\n" if $self->debug;
226    
227            return 1;       # next request
228  };  };
229    
230  =head2 dispatch  =head2 dispatch
# Line 218  sub dispatch { Line 242  sub dispatch {
242    
243          if ( $response->can( $dispatch ) ) {          if ( $response->can( $dispatch ) ) {
244                  warn ">>> dispatching to $dispatch\n";                  warn ">>> dispatching to $dispatch\n";
245                  my $xml = $response->$dispatch( $self->state, @_ ) . "\r\n";                  my $xml = $response->$dispatch( $self->state, @_ );
246                  warn "## response payload: ",length($xml)," bytes\n$xml\n";                  warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
247                  return $xml;                  return $xml;
248          } else {          } else {
249                  confess "can't dispatch to $dispatch";                  confess "can't dispatch to $dispatch";
# Line 240  sub read_headers { Line 264  sub read_headers {
264    while (defined($_ = $self->sock->getline)) {    while (defined($_ = $self->sock->getline)) {
265      s/[\r\n]+$//;      s/[\r\n]+$//;
266      last unless length $_;      last unless length $_;
267          warn "-- $_\n";          warn "-- $_\n" if $self->debug;
268      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;      return 0 if ! /^ ([\w\-]+) :[\ \t]* (.*) $/x;
269      $self->{headers}->{$1} = $2;      $self->{headers}->{$1} = $2;
270    }    }

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

  ViewVC Help
Powered by ViewVC 1.1.26