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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (show annotations)
Fri Oct 26 15:51:27 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 4723 byte(s)
take next pending command on empty request
1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
2 package CWMP::Session;
3
4 use strict;
5 use warnings;
6
7 use base qw/Class::Accessor/;
8 __PACKAGE__->mk_accessors( qw/
9 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/;
20 use Carp qw/confess cluck croak/;
21 use File::Slurp;
22
23 use CWMP::Request;
24 use CWMP::Response;
25 use CWMP::Store;
26
27 =head1 NAME
28
29 CWMP::Session - implement logic of CWMP protocol
30
31 =head1 METHODS
32
33 =head2 new
34
35 my $server = CWMP::Session->new({
36 sock => $io_socket_object,
37 store_path => 'state.db',
38 queue => [ qw/GetRPCMethods GetParameterNames/ ],
39 debug => 1,
40 });
41
42 =cut
43
44 sub new {
45 my $class = shift;
46 my $self = $class->SUPER::new( @_ );
47
48 confess "need sock" unless $self->sock;
49
50 $self->debug( 0 ) unless $self->debug;
51
52 warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
53
54 $self->store( CWMP::Store->new({
55 debug => $self->debug,
56 path => $self->store_path,
57 }) );
58
59 croak "can't open ", $self->store_path, ": $!" unless $self->store;
60
61 return $self;
62 }
63
64 =head2 process_request
65
66 One request from client/response from server cycle. Call multiple times to
67 facilitate brain-dead concept of adding state to stateless protocol like
68 HTTP.
69
70 If used with debugging level of 3 or more, it will also create dumps of
71 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
72 of requests in single session.
73
74 =cut
75
76 my $dump_nr = 0;
77
78 sub process_request {
79 my $self = shift;
80
81 my $sock = $self->sock || die "no sock?";
82
83 # die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
84
85 if ( ! $sock->connected ) {
86 warn "SOCKET NOT CONNECTED\n";
87 return 0;
88 }
89
90 bless $sock, 'HTTP::Daemon::ClientConn';
91
92 # why do I have to do this?
93 # solution from http://use.perl.org/~Matts/journal/12896
94 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
95
96 my $r = $sock->get_request || confess "can't get_request";
97
98 my $chunk = $r->content;
99
100 my $size = length( $chunk );
101
102 warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
103
104 if ( $self->debug > 2 ) {
105 my $file = sprintf("dump/%04d.request", $dump_nr++);
106 write_file( $file, $r->as_string );
107 warn "### request dump: $file\n";
108 }
109
110 my $state;
111
112 if ( $size > 0 ) {
113
114 die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );
115
116 $state = CWMP::Request->parse( $chunk );
117
118 warn "## acquired state = ", dump( $state ), "\n";
119
120 $self->state( $state );
121 $self->store->update_state( ID => $state->{ID}, $state );
122
123 } else {
124
125 warn "## empty request\n";
126
127 $state = $self->state;
128 delete( $state->{_dispatch} );
129 warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
130 }
131
132
133 $sock->send(join("\r\n",
134 'HTTP/1.1 200 OK',
135 'Content-Type: text/xml; charset="utf-8"',
136 'Server: AcmeCWMP/42',
137 'SOAPServer: AcmeCWMP/42'
138 )."\r\n");
139
140 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
141
142 my $xml = '';
143
144 if ( my $dispatch = $state->{_dispatch} ) {
145 $xml = $self->dispatch( $dispatch );
146 } elsif ( $dispatch = shift @{ $self->queue } ) {
147 $xml = $self->dispatch( $dispatch );
148 } elsif ( $size == 0 ) {
149 warn ">>> closing connection\n";
150 return 0;
151 } else {
152 warn ">>> empty response\n";
153 $state->{NoMoreRequests} = 1;
154 $xml = $self->dispatch( 'xml', sub {} );
155 }
156
157 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
158 $sock->send( $xml ) or die "can't send response";
159
160 warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
161
162 warn "### request over\n" if $self->debug;
163
164 return 1; # next request
165 };
166
167 =head2 dispatch
168
169 $xml = $self->dispatch('Inform', $response_arguments );
170
171 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
172
173 =cut
174
175 sub dispatch {
176 my $self = shift;
177
178 my $dispatch = shift || die "no dispatch?";
179
180 my $response = CWMP::Response->new({ debug => $self->debug });
181
182 if ( $response->can( $dispatch ) ) {
183 warn ">>> dispatching to $dispatch\n";
184 my $xml = $response->$dispatch( $self->state, @_ );
185 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
186 if ( $self->debug > 2 ) {
187 my $file = sprintf("dump/%04d.response", $dump_nr++);
188 write_file( $file, $xml );
189 warn "### response dump: $file\n";
190 }
191 return $xml;
192 } else {
193 confess "can't dispatch to $dispatch";
194 }
195 };
196
197
198 =head2 error
199
200 return $self->error( 501, 'System error' );
201
202 =cut
203
204 sub error {
205 my ($self, $number, $msg) = @_;
206 $msg ||= 'ERROR';
207 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
208 warn "Error - $number - $msg\n";
209 return 0; # close connection
210 }
211
212 1;

  ViewVC Help
Powered by ViewVC 1.1.26