/[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 202 - (show annotations)
Wed Nov 14 21:52:45 2007 UTC (16 years, 7 months ago) by dpavlin
File size: 5674 byte(s)
 r216@brr:  dpavlin | 2007-11-14 22:52:27 +0100
 - added create_dump accessor to create dump files on disk
 - if acquired state doesn't have DeviceID, use old one (so that store can map sessions)

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 store
12
13 sock
14 state
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::Methods;
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 => 'state.db',
38 debug => 1,
39 create_dump => 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 my $store_obj = CWMP::Store->new({
55 debug => $self->debug,
56 %{ $self->store },
57 });
58
59 croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
60
61 # FIXME looks ugly. Should we have separate accessor for this?
62 $self->store( $store_obj );
63
64 $self->create_dump( 1 ) if $self->debug > 2;
65
66 return $self;
67 }
68
69 =head2 process_request
70
71 One request from client/response from server cycle. Call multiple times to
72 facilitate brain-dead concept of adding state to stateless protocol like
73 HTTP.
74
75 If used with debugging level of 3 or more, it will also create dumps of
76 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
77 of requests in single session.
78
79 =cut
80
81 my $dump_nr = 0;
82
83 sub process_request {
84 my $self = shift;
85
86 my $sock = $self->sock || die "no sock?";
87
88 # die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
89
90 if ( ! $sock->connected ) {
91 warn "SOCKET NOT CONNECTED\n";
92 return 0;
93 }
94
95 bless $sock, 'HTTP::Daemon::ClientConn';
96
97 # why do I have to do this?
98 # solution from http://use.perl.org/~Matts/journal/12896
99 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
100
101 my $r = $sock->get_request || confess "can't get_request";
102
103 my $xml = $r->content;
104
105 my $size = length( $xml );
106
107 warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
108
109 $dump_nr++;
110 my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost);
111
112 if ( $self->create_dump ) {
113 write_file( $file, $r->as_string );
114 warn "### request dumped to file: $file\n";
115 }
116
117 my $state;
118
119 if ( $size > 0 ) {
120
121 die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') );
122
123 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
124
125 $state = CWMP::Request->parse( $xml );
126
127 if ( defined( $state->{_dispatch} ) && $self->create_dump ) {
128 my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch});
129 symlink $file, $type || warn "can't symlink $file -> $type: $!";
130 }
131
132 warn "## acquired state = ", dump( $state ), "\n";
133
134 if ( ! defined( $state->{DeviceID} ) ) {
135 warn "## state with DeviceID, using old one...\n";
136 $state->{DeviceID} = $self->state->{DeviceID};
137 }
138
139 $self->state( $state );
140 $self->store->update_state( ID => $state->{ID}, $state );
141
142 } else {
143
144 warn "## empty request, using last request state\n";
145
146 $state = $self->state;
147 delete( $state->{_dispatch} );
148 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
149 }
150
151
152 $sock->send(join("\r\n",
153 'HTTP/1.1 200 OK',
154 'Content-Type: text/xml; charset="utf-8"',
155 'Server: AcmeCWMP/42',
156 'SOAPServer: AcmeCWMP/42'
157 )."\r\n");
158
159 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
160
161 my $uid = $self->store->ID_to_uid( $state->{ID}, $state );
162
163 my $queue = CWMP::Queue->new({
164 id => $uid,
165 debug => $self->debug,
166 });
167 my $job;
168 $xml = '';
169
170 if ( my $dispatch = $state->{_dispatch} ) {
171 $xml = $self->dispatch( $dispatch );
172 } elsif ( $job = $queue->dequeue ) {
173 $xml = $self->dispatch( $job->dispatch );
174 } elsif ( $size == 0 ) {
175 warn ">>> no more queued commands, closing connection to $uid\n";
176 return 0;
177 } else {
178 warn ">>> empty response to $uid\n";
179 $state->{NoMoreRequests} = 1;
180 $xml = $self->dispatch( 'xml', sub {} );
181 }
182
183 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
184 $sock->send( $xml ) or die "can't send response";
185
186 warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes to $uid\n";
187
188 $job->finish if $job;
189 warn "### request over for $uid\n" if $self->debug;
190
191 return 1; # next request
192 };
193
194 =head2 dispatch
195
196 $xml = $self->dispatch('Inform', $response_arguments );
197
198 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
199
200 =cut
201
202 sub dispatch {
203 my $self = shift;
204
205 warn "##!!! dispatch(",dump( @_ ),")\n";
206
207 my $dispatch = shift || die "no dispatch?";
208 my $args = shift;
209
210 my $response = CWMP::Methods->new({ debug => $self->debug });
211
212 if ( $response->can( $dispatch ) ) {
213 warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
214 my $xml = $response->$dispatch( $self->state, $args );
215 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
216 if ( $self->create_dump ) {
217 my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
218 write_file( $file, $xml );
219 warn "### response dump: $file\n";
220 }
221 return $xml;
222 } else {
223 confess "can't dispatch to $dispatch";
224 }
225 };
226
227
228 =head2 error
229
230 return $self->error( 501, 'System error' );
231
232 =cut
233
234 sub error {
235 my ($self, $number, $msg) = @_;
236 $msg ||= 'ERROR';
237 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
238 warn "Error - $number - $msg\n";
239 return 0; # close connection
240 }
241
242 1;

  ViewVC Help
Powered by ViewVC 1.1.26