7 |
use base qw/Class::Accessor/; |
use base qw/Class::Accessor/; |
8 |
__PACKAGE__->mk_accessors( qw/ |
__PACKAGE__->mk_accessors( qw/ |
9 |
debug |
debug |
10 |
store_path |
store |
11 |
|
|
12 |
sock |
sock |
13 |
state |
state |
34 |
|
|
35 |
my $server = CWMP::Session->new({ |
my $server = CWMP::Session->new({ |
36 |
sock => $io_socket_object, |
sock => $io_socket_object, |
37 |
store_path => 'state.db', |
store => 'state.db', |
38 |
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
queue => [ |
39 |
|
'GetRPCMethods', |
40 |
|
[ 'GetParameterValyes', 'InternetGatewayDevice.DeviceInfo.SerialNumber', 0 ], |
41 |
|
], |
42 |
debug => 1, |
debug => 1, |
43 |
}); |
}); |
44 |
|
|
54 |
|
|
55 |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
56 |
|
|
57 |
$self->store( CWMP::Store->new({ |
my $store_obj = CWMP::Store->new({ |
58 |
debug => $self->debug, |
debug => $self->debug, |
59 |
path => $self->store_path, |
%{ $self->store }, |
60 |
}) ); |
}); |
61 |
|
|
62 |
|
croak "can't open ", dump( $self->store ), ": $!" unless $store_obj; |
63 |
|
|
64 |
croak "can't open ", $self->store_path, ": $!" unless $self->store; |
# FIXME looks ugly. Should we have separate accessor for this? |
65 |
|
$self->store( $store_obj ); |
66 |
|
|
67 |
return $self; |
return $self; |
68 |
} |
} |
101 |
|
|
102 |
my $r = $sock->get_request || confess "can't get_request"; |
my $r = $sock->get_request || confess "can't get_request"; |
103 |
|
|
104 |
my $chunk = $r->content; |
my $xml = $r->content; |
105 |
|
|
106 |
my $size = length( $chunk ); |
my $size = length( $xml ); |
107 |
|
|
108 |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
109 |
|
|
110 |
if ( $self->debug > 2 ) { |
if ( $self->debug > 2 ) { |
111 |
my $file = sprintf("dump/%04d.request", $dump_nr); |
my $file = sprintf("dump/%04d-%s.request", $dump_nr++, $sock->peerhost); |
112 |
write_file( $file, $r->as_string ); |
write_file( $file, $r->as_string ); |
113 |
warn "### request dump: $file\n"; |
warn "### request dumped to file: $file\n"; |
114 |
} |
} |
115 |
|
|
116 |
my $state; |
my $state; |
117 |
|
|
118 |
if ( $size > 0 ) { |
if ( $size > 0 ) { |
119 |
|
|
120 |
die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); |
die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') ); |
121 |
|
|
122 |
|
warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
123 |
|
|
124 |
if ( $chunk ) { |
$state = CWMP::Request->parse( $xml ); |
|
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
|
125 |
|
|
126 |
$state = CWMP::Request->parse( $chunk ); |
warn "## acquired state = ", dump( $state ), "\n"; |
127 |
|
|
128 |
warn "## acquired state = ", dump( $state ), "\n"; |
$self->state( $state ); |
129 |
|
$self->store->update_state( ID => $state->{ID}, $state ); |
130 |
|
|
131 |
$self->state( $state ); |
} else { |
|
$self->store->update_state( ID => $state->{ID}, $state ); |
|
132 |
|
|
133 |
} else { |
warn "## empty request, using last request state\n"; |
|
warn "## empty request\n"; |
|
|
} |
|
134 |
|
|
|
} else { |
|
135 |
$state = $self->state; |
$state = $self->state; |
136 |
warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
delete( $state->{_dispatch} ); |
137 |
|
#warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
138 |
} |
} |
139 |
|
|
140 |
|
|
154 |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
155 |
$xml = $self->dispatch( $dispatch ); |
$xml = $self->dispatch( $dispatch ); |
156 |
} elsif ( $size == 0 ) { |
} elsif ( $size == 0 ) { |
157 |
warn ">>> closing connection\n"; |
warn ">>> no more queued commands, closing connection\n"; |
158 |
return 0; |
return 0; |
159 |
} else { |
} else { |
160 |
warn ">>> empty response\n"; |
warn ">>> empty response\n"; |
184 |
my $self = shift; |
my $self = shift; |
185 |
|
|
186 |
my $dispatch = shift || die "no dispatch?"; |
my $dispatch = shift || die "no dispatch?"; |
187 |
|
my @args = @_; |
188 |
|
|
189 |
|
if ( ref($dispatch) eq 'ARRAY' ) { |
190 |
|
my @a = @$dispatch; |
191 |
|
$dispatch = shift @a; |
192 |
|
push @args, @a; |
193 |
|
} |
194 |
|
|
195 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
my $response = CWMP::Response->new({ debug => $self->debug }); |
196 |
|
|
197 |
if ( $response->can( $dispatch ) ) { |
if ( $response->can( $dispatch ) ) { |
198 |
warn ">>> dispatching to $dispatch\n"; |
warn ">>> dispatching to $dispatch\n"; |
199 |
my $xml = $response->$dispatch( $self->state, @_ ); |
my $xml = $response->$dispatch( $self->state, @args ); |
200 |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
201 |
if ( $self->debug > 2 ) { |
if ( $self->debug > 2 ) { |
202 |
my $file = sprintf("dump/%04d.response", $dump_nr++); |
my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); |
203 |
write_file( $file, $xml ); |
write_file( $file, $xml ); |
204 |
warn "### response dump: $file\n"; |
warn "### response dump: $file\n"; |
205 |
} |
} |