/[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 234 - (show annotations)
Sun Nov 25 23:26:42 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 6546 byte(s)
 r278@brr:  dpavlin | 2007-11-26 00:17:17 +0100
 Massive re-write of server testing, which now behaves more
 like real CPE client (reconnecting on inform, etc)

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 create_dump
11 session
12
13 sock
14 state
15 store
16 / );
17
18 use HTTP::Daemon;
19 use Data::Dump qw/dump/;
20 use Carp qw/carp confess cluck croak/;
21 use File::Slurp;
22 use File::Path qw/mkpath/;
23
24 use CWMP::Request;
25 use CWMP::Methods;
26 use CWMP::Store;
27
28 #use Devel::LeakTrace::Fast;
29
30 =head1 NAME
31
32 CWMP::Session - implement logic of CWMP protocol
33
34 =head1 METHODS
35
36 =head2 new
37
38 my $server = CWMP::Session->new({
39 sock => $io_socket_object,
40 store => { ... },
41 debug => 1,
42 create_dump => 1,
43 });
44
45 =cut
46
47 sub new {
48 my $class = shift;
49 my $self = $class->SUPER::new( @_ );
50
51 confess "need sock" unless $self->sock;
52 confess "need store" unless $self->store;
53 my $peerhost = $self->sock->peerhost || confess "can't get sock->peerhost";
54
55 $self->debug( 0 ) unless $self->debug;
56
57 warn "created ", __PACKAGE__, "(", dump( @_ ), ") for $peerhost\n" if $self->debug;
58
59 my $store_obj = CWMP::Store->new({
60 debug => $self->debug,
61 %{ $self->store },
62 });
63
64 croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
65
66 # FIXME looks ugly. Should we have separate accessor for this?
67 $self->store( $store_obj );
68
69 $self->create_dump( 1 ) if $self->debug > 2;
70
71 return $self;
72 }
73
74 =head2 process_request
75
76 One request from client/response from server cycle. Call multiple times to
77 facilitate brain-dead concept of adding state to stateless protocol like
78 HTTP.
79
80 If used with debugging level of 3 or more, it will also create dumps of
81 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
82 of requests in single session.
83
84 =cut
85
86 my $dump_by_ip;
87
88 sub process_request {
89 my $self = shift;
90
91 my $sock = $self->sock || die "no sock?";
92
93 # die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
94
95 if ( ! $sock->connected ) {
96 warn "SOCKET NOT CONNECTED\n";
97 return 0;
98 }
99
100 bless $sock, 'HTTP::Daemon::ClientConn';
101
102 # why do I have to do this?
103 # solution from http://use.perl.org/~Matts/journal/12896
104 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
105
106 my $r = $sock->get_request;
107
108 if ( ! $r ) {
109 warn "WARNING: can't get_request\n";
110 return 0;
111 }
112
113 my $ip = $sock->peerhost || confess "can't get peerhost from sock: $!";
114
115 my $xml = $r->content;
116
117 my $size = length( $xml );
118
119 warn "<<<< $ip [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
120
121 my $dump_nr = $dump_by_ip->{$ip}++;
122 my $file = sprintf("./dump/%s/%04d.request", $ip, $dump_nr);
123
124 if ( $self->create_dump ) {
125 mkpath "dump/$ip" unless -e "dump/$ip";
126 write_file( $file, $r->as_string );
127 warn "### request dumped to file: $file\n" if $self->debug;
128 }
129
130 my $state;
131
132 if ( $size > 0 ) {
133
134 die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') );
135
136 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
137
138 $state = CWMP::Request->parse( $xml );
139
140 if ( defined( $state->{_trigger} ) && $self->create_dump ) {
141 my $type = sprintf("dump/%s/%04d-%s", $ip, $dump_nr, $state->{_trigger});
142 $file =~ s!^.*?([^/]+)$!$1!; #!vim
143 symlink $file, $type || warn "can't symlink $file -> $type: $!";
144 }
145
146 warn "## acquired state = ", dump( $state ), "\n" if $self->debug;
147
148 if ( ! defined( $state->{DeviceID} ) ) {
149 if ( $self->state ) {
150 warn "## state without DeviceID, using old one...\n";
151 $state->{DeviceID} = $self->state->{DeviceID};
152 } else {
153 warn "WARNING: state without DeviceID, and I don't have old one!\n";
154 warn "## state = ",dump( $state );
155 }
156 }
157
158 $self->state( $state );
159 $self->store->update_state( $state );
160
161 } else {
162
163 warn "## empty request, using last request state\n";
164
165 $state = $self->state;
166 delete( $state->{_dispatch} );
167 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
168 }
169
170 $sock->send(join("\r\n",
171 'HTTP/1.1 200 OK',
172 'Content-Type: text/xml; charset="utf-8"',
173 'Server: PerlCWMP/42',
174 'SOAPServer: PerlCWMP/42'
175 )."\r\n");
176
177 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
178
179 my $uid = $self->store->state_to_uid( $state );
180
181 my $to_uid = join(" ", grep { defined($_) } "to $uid",
182 # board
183 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'},
184 # version
185 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'},
186 # summary
187 # $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'},
188 ) . "\n";
189
190 my $queue = CWMP::Queue->new({
191 id => $uid,
192 debug => $self->debug,
193 });
194 my $job;
195 $xml = '';
196
197 if ( my $dispatch = $state->{_dispatch} ) {
198 $xml = $self->dispatch( $dispatch );
199 } elsif ( $job = $queue->dequeue ) {
200 $xml = $self->dispatch( $job->dispatch );
201 } elsif ( $size == 0 ) {
202 warn ">>> over, closing connection $to_uid";
203 $sock->close;
204 return;
205 } else {
206 warn ">>> empty response $to_uid";
207 $state->{NoMoreRequests} = 1;
208 $xml = $self->dispatch( 'xml', sub {} );
209 }
210
211 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
212 $sock->send( $xml ) or die "can't send response";
213
214 warn ">>>> " . $ip . " [" . localtime() . "] sent ", length( $xml )," bytes $to_uid";
215
216 $job->finish if $job;
217 warn "### request over for $uid\n" if $self->debug;
218
219 return 1; # next request
220 };
221
222 =head2 dispatch
223
224 $xml = $self->dispatch('Inform', $response_arguments );
225
226 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
227
228 =cut
229
230 sub dispatch {
231 my $self = shift;
232
233 my $dispatch = shift || die "no dispatch?";
234 my $args = shift;
235
236 my $response = CWMP::Methods->new({ debug => $self->debug });
237
238 if ( $response->can( $dispatch ) ) {
239 warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
240 my $xml = $response->$dispatch( $self->state, $args );
241 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
242 if ( $self->create_dump ) {
243 my $ip = $self->sock->peerhost || confess "can't get sock->peerhost: $!";
244 my $dump_nr = $dump_by_ip->{$ip}++;
245 my $file = sprintf("dump/%s/%04d.response", $ip, $dump_nr );
246 write_file( $file, $xml );
247 warn "### response dump: $file\n" if $self->debug;
248 }
249 return $xml;
250 } else {
251 confess "can't dispatch to $dispatch";
252 }
253 };
254
255
256 =head2 error
257
258 return $self->error( 501, 'System error' );
259
260 =cut
261
262 sub error {
263 my ($self, $number, $msg) = @_;
264 $msg ||= 'ERROR';
265 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
266 warn "Error - $number - $msg\n";
267 return 0; # close connection
268 }
269
270 1;

  ViewVC Help
Powered by ViewVC 1.1.26