/[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 197 - (show annotations)
Mon Nov 12 22:03:01 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 5418 byte(s)
 r206@brr:  dpavlin | 2007-11-12 23:02:21 +0100
 - move protocol dump to new cpe-queue.pl command
 - queue now stores data in YAML to preserve perl structures intact
 - queue jobs are now finished correctly
 - remove all traces of default_queue

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

  ViewVC Help
Powered by ViewVC 1.1.26