1 # Dobrica Pavlinusic, <dpavlin@rot13.org> 06/18/07 10:19:50 CEST
7 use base qw/Class::Accessor/;
8 __PACKAGE__->mk_accessors( qw/
19 use Data::Dump qw/dump/;
20 use Carp qw/confess cluck croak/;
29 CWMP::Session - implement logic of CWMP protocol
35 my $server = CWMP::Session->new({
36 sock => $io_socket_object,
38 queue => [ qw/GetRPCMethods GetParameterNames/ ],
46 my $self = $class->SUPER::new( @_ );
48 confess "need sock" unless $self->sock;
50 $self->debug( 0 ) unless $self->debug;
52 warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
54 my $store_obj = CWMP::Store->new({
55 debug => $self->debug,
59 croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
61 # FIXME looks ugly. Should we have separate accessor for this?
62 $self->store( $store_obj );
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.
84 my $sock = $self->sock || die "no sock?";
86 # die "not IO::Socket::INET but ", ref( $sock ) unless ( ref($sock) eq 'Net::Server::Proto::TCP' );
88 if ( ! $sock->connected ) {
89 warn "SOCKET NOT CONNECTED\n";
93 bless $sock, 'HTTP::Daemon::ClientConn';
95 # why do I have to do this?
96 # solution from http://use.perl.org/~Matts/journal/12896
97 ${*$sock}{'httpd_daemon'} = HTTP::Daemon->new;
99 my $r = $sock->get_request || confess "can't get_request";
101 my $chunk = $r->content;
103 my $size = length( $chunk );
105 warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
107 if ( $self->debug > 2 ) {
108 my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost);
109 write_file( $file, $r->as_string );
110 warn "### request dump: $file\n";
117 die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );
119 $state = CWMP::Request->parse( $chunk );
121 warn "## acquired state = ", dump( $state ), "\n";
123 $self->state( $state );
124 $self->store->update_state( ID => $state->{ID}, $state );
128 warn "## empty request, using last request state\n";
130 $state = $self->state;
131 delete( $state->{_dispatch} );
132 #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
136 $sock->send(join("\r\n",
138 'Content-Type: text/xml; charset="utf-8"',
139 'Server: AcmeCWMP/42',
140 'SOAPServer: AcmeCWMP/42'
143 $sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
147 if ( my $dispatch = $state->{_dispatch} ) {
148 $xml = $self->dispatch( $dispatch );
149 } elsif ( $dispatch = shift @{ $self->queue } ) {
150 $xml = $self->dispatch( $dispatch );
151 } elsif ( $size == 0 ) {
152 warn ">>> no more queued commands, closing connection\n";
155 warn ">>> empty response\n";
156 $state->{NoMoreRequests} = 1;
157 $xml = $self->dispatch( 'xml', sub {} );
160 $sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" );
161 $sock->send( $xml ) or die "can't send response";
163 warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
165 warn "### request over\n" if $self->debug;
167 return 1; # next request
172 $xml = $self->dispatch('Inform', $response_arguments );
174 If debugging level of 3 or more, it will create dumps of responses named C<< dump/nr.response >>
181 my $dispatch = shift || die "no dispatch?";
183 my $response = CWMP::Response->new({ debug => $self->debug });
185 if ( $response->can( $dispatch ) ) {
186 warn ">>> dispatching to $dispatch\n";
187 my $xml = $response->$dispatch( $self->state, @_ );
188 warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
189 if ( $self->debug > 2 ) {
190 my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
191 write_file( $file, $xml );
192 warn "### response dump: $file\n";
196 confess "can't dispatch to $dispatch";
203 return $self->error( 501, 'System error' );
208 my ($self, $number, $msg) = @_;
210 $self->sock->send( "HTTP/1.1 $number $msg\r\n" );
211 warn "Error - $number - $msg\n";
212 return 0; # close connection