1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
7 use base qw/Class::Accessor/;
8 __PACKAGE__->mk_accessors( qw/
18 use Data::Dump qw/dump/;
19 use Carp qw/carp confess cluck croak/;
25 #use Devel::LeakTrace::Fast;
29 CWMP::Session - implement logic of CWMP protocol
35 my $server = CWMP::Session->new({
36 sock => $io_socket_object,
46 my $self = $class->SUPER::new( @_ );
48 confess "need store" unless $self->store;
50 $self->debug( 0 ) unless $self->debug;
52 my $store_obj = CWMP::Store->new({
53 debug => $self->debug,
57 croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
59 # FIXME looks ugly. Should we have separate accessor for this?
60 $self->store( $store_obj );
62 $self->create_dump( 1 ) if $self->debug > 2;
67 =head2 process_request
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
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.
81 my ( $self, $ip, $xml ) = @_;
83 my $size = length( $xml );
89 warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
91 $state = CWMP::Request->parse( $xml );
93 warn "## acquired state = ", dump( $state ), "\n" if $self->debug;
95 if ( ! defined( $state->{DeviceID} ) ) {
97 warn "## state without DeviceID, using old one...\n";
98 $state->{DeviceID} = $self->state->{DeviceID};
100 warn "WARNING: state without DeviceID, and I don't have old one!\n";
101 warn "## state = ",dump( $state );
105 $self->state( $state );
106 $self->store->update_state( $state );
110 warn "## empty request, using last request state\n";
112 $state = $self->state;
113 delete( $state->{_dispatch} );
114 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
117 my $uid = $self->store->state_to_uid( $state );
119 my $to_uid = join(" ", grep { defined($_) } "to $uid",
121 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.HardwareVersion'},
123 $state->{Parameter}->{'InternetGatewayDevice.DeviceInfo.SoftwareVersion'},
125 # $state->{Parameter}->{'InternetGatewayDevice.DeviceSummary'},
128 my $queue = CWMP::Queue->new({
130 debug => $self->debug,
134 if ( my $dispatch = $state->{_dispatch} ) {
135 $xml = $self->dispatch( $dispatch );
136 } elsif ( my $job = $queue->dequeue ) {
137 $xml = $self->dispatch( $job->dispatch );
140 my $stored = $self->store->get_state( $uid );
141 if ( ! defined $stored->{ParameterInfo} ) {
142 $xml = $self->dispatch( 'GetParameterNames', [ 'InternetGatewayDevice.', 1 ] );
144 my @params = grep { m/\.$/ } keys %{ $stored->{ParameterInfo} };
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}->{ $_ };
154 $self->store->set_state( $uid, $stored );
157 my @params = sort grep { ! exists $stored->{Parameter}->{$_} } grep { ! m/\.$/ } keys %{ $stored->{ParameterInfo} };
159 warn "# GetParameterValues ", dump( @params );
160 my $first = shift @params;
161 $xml = $self->dispatch( 'GetParameterValues', [ $first ] );
163 my @chunk = splice @params, 0, 16; # FIXME 16 seems to be max
164 $queue->enqueue( 'GetParameterValues', [ @chunk ] );
169 warn ">>> empty response $to_uid";
170 $state->{NoMoreRequests} = 1;
178 my $status = length($xml) ? 200 : 204;
180 my $out = join("\r\n",
181 "HTTP/1.1 $status OK",
182 'Content-Type: text/xml; charset="utf-8"',
183 'Server: Perl-CWMP/42',
184 'SOAPServer: Perl-CWMP/42'
187 $out .= "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" if $state->{ID};
189 $out .= "Content-Length: " . length( $xml ) . "\r\n\r\n";
190 $out .= $xml if length($xml);
192 warn "### request over for $uid\n" if $self->debug;
194 return $out; # next request
199 $xml = $self->dispatch('Inform', $response_arguments );
201 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
208 my $dispatch = shift || die "no dispatch?";
211 my $response = CWMP::Methods->new({ debug => $self->debug });
213 if ( $response->can( $dispatch ) ) {
214 warn ">>> dispatching to $dispatch with args ",dump( $args ),"\n";
215 my $xml = $response->$dispatch( $self->state, $args );
216 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
219 confess "can't dispatch to $dispatch";
226 return $self->error( 501, 'System error' );
231 my ($self, $number, $msg) = @_;
233 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
234 warn "Error - $number - $msg\n";
235 return 0; # close connection