/[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 30 by dpavlin, Mon Jun 18 19:16:28 2007 UTC google/lib/CWMP/Session.pm revision 110 by dpavlin, Sun Oct 21 01:33:53 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;
6    
7  use base qw/HTTP::Server::Simple::CGI Class::Accessor/;  use base qw/Class::Accessor/;
   
8  __PACKAGE__->mk_accessors( qw/  __PACKAGE__->mk_accessors( qw/
9  debug  debug
10  / );  store_path
11    
12    sock
13    state
14    queue
15    store
16    / );
17    
18    use HTTP::Daemon;
19  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
20    use Carp qw/confess cluck croak/;
21    
22  sub handler {  use CWMP::Request;
23      my $self = shift;  use CWMP::Response;
24    use CWMP::Store;
25    
26      my $chunk;  =head1 NAME
27    
28          my $transfer_encoding = $self->header('Transfer-Encoding');  CWMP::Session - implement logic of CWMP protocol
29    
30          if ( $transfer_encoding && $transfer_encoding =~ qr/^chunked/i ) {  =head1 METHODS
31    
32                  my $last = 0;  =head2 new
                 do {  
                         my $len = <STDIN>;  
                         $len =~ s/[\n\r]*$//s;  
                         $len = hex( $len );  
33    
34                          $last = 1 if ( $len == 0 );    my $server = CWMP::Session->new({
35            sock => $io_socket_object,
36            store_path => 'state.db',
37            queue => [ qw/GetRPCMethods GetParameterNames/ ],
38            debug => 1,
39      });
40    
41                          warn "getting chunk of $len bytes\n";  =cut
42    
43                          while( $len > 0 ) {  sub new {
44                                  my $line = <STDIN>;          my $class = shift;
45                                  $chunk .= $line;          my $self = $class->SUPER::new( @_ );
                                 $len -= length( $line );  
                         }  
46    
47                  } while ( ! $last );          confess "need sock" unless $self->sock;
         }  
48    
49          warn "chunk (", length($chunk), " bytes)\n------>>\n$chunk\n<<-----\n";          $self->debug( 0 ) unless $self->debug;
50    
51      my $cgi  = new CGI( $chunk );          warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
52    
53      eval { $self->handle_request($cgi) };          $self->store( CWMP::Store->new({
54      if ($@) {                  debug => $self->debug,
55          my $error = $@;                  path => $self->store_path,
56          warn $error;          }) );
57      }  
58            croak "can't open ", $self->store_path, ": $!" unless $self->store;
59    
60            return $self;
61  }  }
62    
63  sub handle_request {  =head2 process_request
64          my ($self, $cgi) = @_;  
65                                                                        One request from client/response from server cycle. Call multiple times to
66          #... do something, print output to default  facilitate brain-dead concept of adding state to stateless protocol like
67          # selected filehandle...  HTTP.
68    
69    =cut
70    
71    sub process_request {
72            my $self = shift;
73    
74            my $sock = $self->sock || die "no sock?";
75    
76    #       die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
77    
78            if ( ! $sock->connected ) {
79                    warn "SOCKET NOT CONNECTED\n";
80                    return 0;
81            }
82    
83            bless $sock, 'HTTP::Daemon::ClientConn';
84    
85            # why do I have to do this?
86            # solution from http://use.perl.org/~Matts/journal/12896
87            ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
88    
89            my $r = $sock->get_request || confess "can't get_request";
90    
91            warn "<<<< ", $sock->peerhost, " - - [" . localtime() . "] ", $r->method, " ", $r->uri, "\n";
92    
93            my $chunk = $r->content;
94    
95            my $size = length( $chunk );
96    
97            warn "<<<< " . $sock->peerhost . " [" . localtime() . "] request $size bytes\n";
98    
99            my $state;
100    
101            if ( $size > 0 ) {
102    
103                    die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );
104    
105    
106                    if ( $chunk ) {
107                            warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug;
108    
109          warn ">> ", $ENV{REMOTE_ADDR}, $/;                          $state = CWMP::Request->parse( $chunk );
110    
111          warn "not SOAP request" unless defined ( $cgi->header('SOAPAction') );                          warn "## acquired state = ", dump( $state ), "\n";
112    
113  #       warn $cgi->param('POSTDATA'), dump( $cgi );                          $self->state( $state );
114                            $self->store->update_state( ID => $state->{ID}, $state );
115    
116          print "Content-Type: text/xml\r\n\r\n";                  } else {
117                            warn "## empty request\n";
118                    }
119    
120            } else {
121                    $state = $self->state;
122                    warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
123            }
124    
125    
126            $sock->send(join("\r\n",
127                    'HTTP/1.1 200 OK',
128                    'Content-Type: text/xml; charset="utf-8"',
129                    'Server: AcmeCWMP/42',
130                    'SOAPServer: AcmeCWMP/42'
131            )."\r\n");
132    
133            $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
134            
135            my $xml = '';
136    
137            if ( my $dispatch = $state->{_dispatch} ) {
138                    $xml = $self->dispatch( $dispatch );
139            } elsif ( $dispatch = shift @{ $self->queue } ) {
140                    $xml = $self->dispatch( $dispatch );
141            } elsif ( $size == 0 ) {
142                    warn ">>> closing connection\n";
143                    return 0;
144            } else {
145                    warn ">>> empty response\n";
146                    $state->{NoMoreRequests} = 1;
147                    $xml = $self->dispatch( 'xml', sub {} );
148            }
149    
150            $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
151            $sock->send( $xml ) or die "can't send response";
152    
153            warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
154    
155            warn "### request over\n" if $self->debug;
156    
157            return 1;       # next request
158  };  };
159    
160  1;  =head2 dispatch
161    
162      $xml = $self->dispatch('Inform', $response_arguments );
163    
164    =cut
165    
166    sub dispatch {
167            my $self = shift;
168    
169            my $dispatch = shift || die "no dispatch?";
170    
171            my $response = CWMP::Response->new({ debug => $self->debug });
172    
173            if ( $response->can( $dispatch ) ) {
174                    warn ">>> dispatching to $dispatch\n";
175                    my $xml = $response->$dispatch( $self->state, @_ );
176                    warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
177                    return $xml;
178            } else {
179                    confess "can't dispatch to $dispatch";
180            }
181    };
182    
183    
184    =head2 error
185    
186      return $self->error( 501, 'System error' );
187    
188    =cut
189    
190    sub error {
191      my ($self, $number, $msg) = @_;
192      $msg ||= 'ERROR';
193      $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
194      warn "Error - $number - $msg\n";
195      return 0;     # close connection
196    }
197    
198    1;

Legend:
Removed from v.30  
changed lines
  Added in v.110

  ViewVC Help
Powered by ViewVC 1.1.26