re-enable all_parameteres collection of first connect
[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 use CWMP::Vendor;
25
26 #use Devel::LeakTrace::Fast;
27
28 =head1 NAME
29
30 CWMP::Session - implement logic of CWMP protocol
31
32 =head1 METHODS
33
34 =head2 new
35
36   my $server = CWMP::Session->new({
37         sock => $io_socket_object,
38         store => { ... },
39         debug => 1,
40         create_dump => 1,
41   });
42
43 =cut
44
45 sub new {
46         my $class = shift;
47         my $self = $class->SUPER::new( @_ );
48
49         confess "need store" unless $self->store;
50
51         $self->debug( 0 ) unless $self->debug;
52
53         my $store_obj = CWMP::Store->new({
54                 debug => $self->debug,
55                 %{ $self->store },
56         });
57
58         croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
59
60         # FIXME looks ugly. Should we have separate accessor for this?
61         $self->store( $store_obj );
62
63         $self->create_dump( 1 ) if $self->debug > 2;
64
65         return $self;
66 }
67
68 =head2 process_request
69
70 One request from client/response from server cycle. Call multiple times to
71 facilitate brain-dead concept of adding state to stateless protocol like
72 HTTP.
73
74 If used with debugging level of 3 or more, it will also create dumps of
75 requests named C<< dump/nr.request >> where C<nr> is number from 0 to total number
76 of requests in single session.
77
78 =cut
79
80
81 sub process_request {
82         my ( $self, $ip, $xml ) = @_;
83
84         my $size = length( $xml );
85
86         my $state;
87
88         if ( $size > 0 ) {
89
90                 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
91
92                 $state = CWMP::Parser->parse( $xml );
93
94                 warn "## acquired state = ", dump( $state ), "\n" if $self->debug;
95
96                 if ( ! defined( $state->{DeviceId} ) ) {
97                         if ( $self->state ) {
98                                 warn "## state without DeviceId, using old one...\n";
99                                 $state->{DeviceId} = $self->state->{DeviceId};
100                         } else {
101                                 warn "WARNING: state without DeviceId, and I don't have old one!\n";
102                                 warn "## state = ",dump( $state );
103                         }
104                 }
105
106                 $self->state( $state );
107                 $self->store->update_state( $state );
108
109         } else {
110
111                 warn "## empty request, using last request state\n";
112
113                 $state = $self->state;
114                 delete( $state->{_dispatch} );
115                 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
116         }
117
118         my $uid = $self->store->state_to_uid( $state );
119
120         my $queue = CWMP::Queue->new({
121                 id => $uid,
122                 debug => $self->debug,
123         });
124
125         $xml = '';
126         my $status = 200;
127
128         if ( my $dispatch = $state->{_dispatch} ) {
129                 $xml = $self->dispatch( $dispatch );
130         } elsif ( my $job = $queue->dequeue ) {
131                 $xml = $self->dispatch( $job->dispatch );
132                 $job->finish;
133         } else {
134                 my @dispatch;
135                 @dispatch = CWMP::Vendor->all_parameters( $self->store, $uid, $queue );
136 warn "XXX", dump @dispatch;
137                 @dispatch = CWMP::Vendor->vendor_config( $self->store, $uid, $queue ) unless @dispatch;
138 warn "XXX", dump @dispatch;
139                 $xml = $self->dispatch( @dispatch ) if @dispatch;
140                 if ( ! $xml ) {
141                         warn ">>> no more work for $uid sending empty response\n";
142                         $state->{NoMoreRequests} = 1;
143                         $xml = '';
144                         $status = 204;
145                 }
146         }
147
148         my $out = join("\r\n",
149                 "HTTP/1.1 $status OK",
150                 'Content-Type: text/xml; charset="utf-8"',
151                 'Server: Perl-CWMP/42',
152                 'SOAPServer: Perl-CWMP/42'
153         ) . "\r\n";
154
155         $out .= "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" if $state->{ID};
156
157         $out .= "Content-Length: " . length( $xml ) . "\r\n\r\n";
158         $out .= $xml if length($xml);
159
160         return $out;    # next request
161 };
162
163 =head2 dispatch
164
165   $xml = $self->dispatch('Inform', $response_arguments );
166
167 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
168
169 =cut
170
171 sub dispatch {
172         my $self = shift;
173
174         my $dispatch = shift || die "no dispatch?";
175         my $args = shift;
176
177         my $response = CWMP::Methods->new({ debug => $self->debug });
178
179         if ( $response->can( $dispatch ) ) {
180                 warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
181                 my $xml = $response->$dispatch( $self->state, $args );
182                 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
183                 return $xml;
184         } else {
185                 confess "can't dispatch to $dispatch";
186         }
187 };
188
189
190 =head2 error
191
192   return $self->error( 501, 'System error' );
193
194 =cut
195
196 sub error {
197   my ($self, $number, $msg) = @_;
198   $msg ||= 'ERROR';
199   $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
200   warn "Error - $number - $msg\n";
201   return 0;     # close connection
202 }
203
204 1;