use base qw/Class::Accessor/;
__PACKAGE__->mk_accessors( qw/
debug
-store_path
+store
sock
state
-queue
store
/ );
use File::Slurp;
use CWMP::Request;
-use CWMP::Response;
+use CWMP::Methods;
use CWMP::Store;
=head1 NAME
my $server = CWMP::Session->new({
sock => $io_socket_object,
- store_path => 'state.db',
- queue => [ qw/GetRPCMethods GetParameterNames/ ],
+ store => 'state.db',
debug => 1,
});
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug;
- $self->store( CWMP::Store->new({
+ my $store_obj = CWMP::Store->new({
debug => $self->debug,
- path => $self->store_path,
- }) );
+ %{ $self->store },
+ });
+
+ croak "can't open ", dump( $self->store ), ": $!" unless $store_obj;
- croak "can't open ", $self->store_path, ": $!" unless $self->store;
+ # FIXME looks ugly. Should we have separate accessor for this?
+ $self->store( $store_obj );
return $self;
}
my $r = $sock->get_request || confess "can't get_request";
- my $chunk = $r->content;
+ my $xml = $r->content;
- my $size = length( $chunk );
+ my $size = length( $xml );
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n";
+ $dump_nr++;
+ my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost);
+
if ( $self->debug > 2 ) {
- my $file = sprintf("dump/%04d.request", $dump_nr++);
write_file( $file, $r->as_string );
- warn "### request dump: $file\n";
+ warn "### request dumped to file: $file\n";
}
my $state;
if ( $size > 0 ) {
- die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') );
+ die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') );
- $state = CWMP::Request->parse( $chunk );
+ warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
+
+ $state = CWMP::Request->parse( $xml );
+
+ if ( defined( $state->{_dispatch} ) && $self->debug > 2 ) {
+ my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch});
+ symlink $file, $type || warn "can't symlink $file -> $type: $!";
+ }
warn "## acquired state = ", dump( $state ), "\n";
} else {
- warn "## empty request\n";
+ warn "## empty request, using last request state\n";
$state = $self->state;
delete( $state->{_dispatch} );
- warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
+ #warn "last request state = ", dump( $state ), "\n" if $self->debug > 1;
}
)."\r\n");
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} );
-
- my $xml = '';
+
+ my $queue = CWMP::Queue->new({
+ id => $self->store->ID_to_uid( $state->{ID}, $state ),
+ debug => $self->debug,
+ });
+ my $job;
+ $xml = '';
if ( my $dispatch = $state->{_dispatch} ) {
$xml = $self->dispatch( $dispatch );
- } elsif ( $dispatch = shift @{ $self->queue } ) {
- $xml = $self->dispatch( $dispatch );
+ } elsif ( $job = $queue->dequeue ) {
+ $xml = $self->dispatch( $job->dispatch );
} elsif ( $size == 0 ) {
- warn ">>> closing connection\n";
+ warn ">>> no more queued commands, closing connection\n";
return 0;
} else {
warn ">>> empty response\n";
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n";
+ $job->finish if $job;
warn "### request over\n" if $self->debug;
return 1; # next request
my $self = shift;
my $dispatch = shift || die "no dispatch?";
+ my @args = @_;
+
+ if ( ref($dispatch) eq 'ARRAY' ) {
+ my @a = @$dispatch;
+ $dispatch = shift @a;
+ push @args, @a;
+ }
- my $response = CWMP::Response->new({ debug => $self->debug });
+ my $response = CWMP::Methods->new({ debug => $self->debug });
if ( $response->can( $dispatch ) ) {
warn ">>> dispatching to $dispatch\n";
- my $xml = $response->$dispatch( $self->state, @_ );
+ my $xml = $response->$dispatch( $self->state, @args );
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug;
if ( $self->debug > 2 ) {
- my $file = sprintf("dump/%04d.response", $dump_nr++);
+ my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost);
write_file( $file, $xml );
warn "### response dump: $file\n";
}