/[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 198 - (show annotations)
Mon Nov 12 22:13:59 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 5469 byte(s)
 r208@brr:  dpavlin | 2007-11-12 23:13:39 +0100
 display CPE uid (serial number) for better info

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 $uid = $self->store->ID_to_uid( $state->{ID}, $state );
153
154 my $queue = CWMP::Queue->new({
155 id => $uid,
156 debug => $self->debug,
157 });
158 my $job;
159 $xml = '';
160
161 if ( my $dispatch = $state->{_dispatch} ) {
162 $xml = $self->dispatch( $dispatch );
163 } elsif ( $job = $queue->dequeue ) {
164 $xml = $self->dispatch( $job->dispatch );
165 } elsif ( $size == 0 ) {
166 warn ">>> no more queued commands, closing connection to $uid\n";
167 return 0;
168 } else {
169 warn ">>> empty response to $uid\n";
170 $state->{NoMoreRequests} = 1;
171 $xml = $self->dispatch( 'xml', sub {} );
172 }
173
174 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
175 $sock->send( $xml ) or die "can't send response";
176
177 warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes to $uid\n";
178
179 $job->finish if $job;
180 warn "### request over for $uid\n" if $self->debug;
181
182 return 1; # next request
183 };
184
185 =head2 dispatch
186
187 $xml = $self->dispatch('Inform', $response_arguments );
188
189 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
190
191 =cut
192
193 sub dispatch {
194 my $self = shift;
195
196 my $dispatch = shift || die "no dispatch?";
197 my @args = @_;
198
199 if ( ref($dispatch) eq 'ARRAY' ) {
200 my @a = @$dispatch;
201 $dispatch = shift @a;
202 push @args, @a;
203 }
204
205 my $response = CWMP::Methods->new({ debug => $self->debug });
206
207 if ( $response->can( $dispatch ) ) {
208 warn ">>> dispatching to $dispatch\n";
209 my $xml = $response->$dispatch( $self->state, @args );
210 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
211 if ( $self->debug > 2 ) {
212 my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
213 write_file( $file, $xml );
214 warn "### response dump: $file\n";
215 }
216 return $xml;
217 } else {
218 confess "can't dispatch to $dispatch";
219 }
220 };
221
222
223 =head2 error
224
225 return $self->error( 501, 'System error' );
226
227 =cut
228
229 sub error {
230 my ($self, $number, $msg) = @_;
231 $msg ||= 'ERROR';
232 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
233 warn "Error - $number - $msg\n";
234 return 0; # close connection
235 }
236
237 1;

  ViewVC Help
Powered by ViewVC 1.1.26