1d870597260b2b2cc207397867b6328ecfe959b8
[perl-cwmp.git] / lib / CWMP / Session.pm
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 Data::Dump qw/dump/;
19 use Carp qw/carp confess cluck croak/;
20
21 use CWMP::Parser;
22 use CWMP::Methods;
23 use CWMP::Store;
24
25 #use Devel::LeakTrace::Fast;
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 => { ... },
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 store" unless $self->store;
49
50         $self->debug( 0 ) unless $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         $self->create_dump( 1 ) if $self->debug > 2;
63
64         return $self;
65 }
66
67 =head2 process_request
68
69 One request from client/response from server cycle. Call multiple times to
70 facilitate brain-dead concept of adding state to stateless protocol like
71 HTTP.
72
73 If used with debugging level of 3 or more, it will also create dumps of
74 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
75 of requests in single session.
76
77 =cut
78
79
80 sub process_request {
81         my ( $self, $ip, $xml ) = @_;
82
83         my $size = length( $xml );
84
85         my $state;
86
87         if ( $size > 0 ) {
88
89                 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
90
91                 $state = CWMP::Parser->parse( $xml );
92
93                 warn "## acquired state = ", dump( $state ), "\n" if $self->debug;
94
95                 if ( ! defined( $state->{DeviceId} ) ) {
96                         if ( $self->state ) {
97                                 warn "## state without DeviceId, using old one...\n";
98                                 $state->{DeviceId} = $self->state->{DeviceId};
99                         } else {
100                                 warn "WARNING: state without DeviceId, and I don't have old one!\n";
101                                 warn "## state = ",dump( $state );
102                         }
103                 }
104
105                 $self->state( $state );
106                 $self->store->update_state( $state );
107
108         } else {
109
110                 warn "## empty request, using last request state\n";
111
112                 $state = $self->state;
113                 delete( $state->{_dispatch} );
114                 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
115         }
116
117         my $uid = $self->store->state_to_uid( $state );
118
119         my $to_uid = join(" ", grep { defined($_) } "to $uid",
120                         # board
121                         $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'},
122                         # version
123                         $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'},
124                         # summary
125 #                       $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'},
126         ) . "\n";
127
128         my $queue = CWMP::Queue->new({
129                 id => $uid,
130                 debug => $self->debug,
131         });
132         $xml = '';
133
134         if ( my $dispatch = $state->{_dispatch} ) {
135                 $xml = $self->dispatch( $dispatch );
136         } elsif ( my $job = $queue->dequeue ) {
137                 $xml = $self->dispatch( $job->dispatch );
138                 $job->finish;
139         } else {
140                 my $stored = $self->store->get_state( $uid );
141                 if ( ! defined $stored->{ParameterInfo} ) {
142                         $xml = $self->dispatch( 'GetParameterNames', [ 'InternetGatewayDevice.', 1 ] );
143                 } else {
144                         my @params = grep { m/\.$/ } keys %{ $stored->{ParameterInfo} };
145                         if ( @params ) {
146                                 warn "# GetParameterNames ", dump( @params );
147                                 my $first = shift @params;
148                                 delete $stored->{ParameterInfo}->{$first};
149                                 $xml = $self->dispatch( 'GetParameterNames', [ $first, 1 ] );
150                                 foreach ( @params ) {
151                                         $queue->enqueue( 'GetParameterNames', [ $_, 1 ] );
152                                         delete $stored->{ParameterInfo}->{ $_ };
153                                 }
154                                 $self->store->set_state( $uid, $stored );
155                         } else {
156
157                                 my @params = sort
158                                         grep { ! exists $stored->{Parameter}->{$_} }
159                                         grep { ! m/\.$/ && ! m/NumberOfEntries/ }
160                                         keys %{ $stored->{ParameterInfo} }
161                                 ;
162                                 if ( @params ) {
163                                         warn "# GetParameterValues ", dump( @params );
164                                         my $first = shift @params;
165                                         $xml = $self->dispatch( 'GetParameterValues', [ $first ] );
166                                         while ( @params ) {
167                                                 my @chunk = splice @params, 0, 16; # FIXME 16 seems to be max
168                                                 $queue->enqueue( 'GetParameterValues', [ @chunk ] );
169                                         }
170
171                                 } else {
172
173                                         warn ">>> empty response $to_uid";
174                                         $state->{NoMoreRequests} = 1;
175                                         $xml = '';
176
177                                 }
178                         }
179                 }
180         }
181
182         my $status = length($xml) ? 200 : 204;
183
184         my $out = join("\r\n",
185                 "HTTP/1.1 $status OK",
186                 'Content-Type: text/xml; charset="utf-8"',
187                 'Server: Perl-CWMP/42',
188                 'SOAPServer: Perl-CWMP/42'
189         ) . "\r\n";
190
191         $out .= "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" if $state->{ID};
192
193         $out .= "Content-Length: " . length( $xml ) . "\r\n\r\n";
194         $out .= $xml if length($xml);
195
196         warn "### request over for $uid\n" if $self->debug;
197
198         return $out;    # next request
199 };
200
201 =head2 dispatch
202
203   $xml = $self->dispatch('Inform', $response_arguments );
204
205 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
206
207 =cut
208
209 sub dispatch {
210         my $self = shift;
211
212         my $dispatch = shift || die "no dispatch?";
213         my $args = shift;
214
215         my $response = CWMP::Methods->new({ debug => $self->debug });
216
217         if ( $response->can( $dispatch ) ) {
218                 warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
219                 my $xml = $response->$dispatch( $self->state, $args );
220                 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
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;