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 |
|
queue |
|
14 |
store |
store |
15 |
/ ); |
/ ); |
16 |
|
|
20 |
use File::Slurp; |
use File::Slurp; |
21 |
|
|
22 |
use CWMP::Request; |
use CWMP::Request; |
23 |
use CWMP::Response; |
use CWMP::Methods; |
24 |
use CWMP::Store; |
use CWMP::Store; |
25 |
|
|
26 |
=head1 NAME |
=head1 NAME |
33 |
|
|
34 |
my $server = CWMP::Session->new({ |
my $server = CWMP::Session->new({ |
35 |
sock => $io_socket_object, |
sock => $io_socket_object, |
36 |
store_path => 'state.db', |
store => 'state.db', |
|
queue => [ qw/GetRPCMethods GetParameterNames/ ], |
|
37 |
debug => 1, |
debug => 1, |
38 |
}); |
}); |
39 |
|
|
49 |
|
|
50 |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
warn "created ", __PACKAGE__, "(", dump( @_ ), ") for ", $self->sock->peerhost, "\n" if $self->debug; |
51 |
|
|
52 |
$self->store( CWMP::Store->new({ |
my $store_obj = CWMP::Store->new({ |
53 |
debug => $self->debug, |
debug => $self->debug, |
54 |
path => $self->store_path, |
%{ $self->store }, |
55 |
}) ); |
}); |
56 |
|
|
57 |
|
croak "can't open ", dump( $self->store ), ": $!" unless $store_obj; |
58 |
|
|
59 |
croak "can't open ", $self->store_path, ": $!" unless $self->store; |
# FIXME looks ugly. Should we have separate accessor for this? |
60 |
|
$self->store( $store_obj ); |
61 |
|
|
62 |
return $self; |
return $self; |
63 |
} |
} |
96 |
|
|
97 |
my $r = $sock->get_request || confess "can't get_request"; |
my $r = $sock->get_request || confess "can't get_request"; |
98 |
|
|
99 |
my $chunk = $r->content; |
my $xml = $r->content; |
100 |
|
|
101 |
my $size = length( $chunk ); |
my $size = length( $xml ); |
102 |
|
|
103 |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
warn "<<<< ", $sock->peerhost, " [" . localtime() . "] ", $r->method, " ", $r->uri, " $size bytes\n"; |
104 |
|
|
105 |
|
$dump_nr++; |
106 |
|
my $file = sprintf("dump/%04d-%s.request", $dump_nr, $sock->peerhost); |
107 |
|
|
108 |
if ( $self->debug > 2 ) { |
if ( $self->debug > 2 ) { |
|
my $file = sprintf("dump/%04d.request", $dump_nr); |
|
109 |
write_file( $file, $r->as_string ); |
write_file( $file, $r->as_string ); |
110 |
warn "### request dump: $file\n"; |
warn "### request dumped to file: $file\n"; |
111 |
} |
} |
112 |
|
|
113 |
my $state; |
my $state; |
114 |
|
|
115 |
if ( $size > 0 ) { |
if ( $size > 0 ) { |
116 |
|
|
117 |
die "no SOAPAction header in ",dump($chunk) unless defined ( $r->header('SOAPAction') ); |
die "no SOAPAction header in ",dump($xml) unless defined ( $r->header('SOAPAction') ); |
|
|
|
118 |
|
|
119 |
if ( $chunk ) { |
warn "## request payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
|
warn "## request chunk: ",length($chunk)," bytes\n$chunk\n" if $self->debug; |
|
120 |
|
|
121 |
$state = CWMP::Request->parse( $chunk ); |
$state = CWMP::Request->parse( $xml ); |
122 |
|
|
123 |
warn "## acquired state = ", dump( $state ), "\n"; |
if ( defined( $state->{_dispatch} ) && $self->debug > 2 ) { |
124 |
|
my $type = sprintf("dump/%04d-%s-%s", $dump_nr, $sock->peerhost, $state->{_dispatch}); |
125 |
|
symlink $file, $type || warn "can't symlink $file -> $type: $!"; |
126 |
|
} |
127 |
|
|
128 |
$self->state( $state ); |
warn "## acquired state = ", dump( $state ), "\n"; |
|
$self->store->update_state( ID => $state->{ID}, $state ); |
|
129 |
|
|
130 |
} else { |
$self->state( $state ); |
131 |
warn "## empty request\n"; |
$self->store->update_state( ID => $state->{ID}, $state ); |
|
} |
|
132 |
|
|
133 |
} else { |
} else { |
134 |
|
|
135 |
|
warn "## empty request, using last request state\n"; |
136 |
|
|
137 |
$state = $self->state; |
$state = $self->state; |
138 |
warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
delete( $state->{_dispatch} ); |
139 |
|
#warn "last request state = ", dump( $state ), "\n" if $self->debug > 1; |
140 |
} |
} |
141 |
|
|
142 |
|
|
148 |
)."\r\n"); |
)."\r\n"); |
149 |
|
|
150 |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
$sock->send( "Set-Cookie: ID=" . $state->{ID} . "; path=/\r\n" ) if ( $state->{ID} ); |
151 |
|
|
152 |
my $xml = ''; |
my $uid = $self->store->ID_to_uid( $state->{ID}, $state ); |
153 |
|
|
154 |
|
my $queue = CWMP::Queue->new({ |
155 |
|
id => $uid, |
156 |
|
debug => $self->debug, |
157 |
|
}); |
158 |
|
my $job; |
159 |
|
$xml = ''; |
160 |
|
|
161 |
if ( my $dispatch = $state->{_dispatch} ) { |
if ( my $dispatch = $state->{_dispatch} ) { |
162 |
$xml = $self->dispatch( $dispatch ); |
$xml = $self->dispatch( $dispatch ); |
163 |
} elsif ( $dispatch = shift @{ $self->queue } ) { |
} elsif ( $job = $queue->dequeue ) { |
164 |
$xml = $self->dispatch( $dispatch ); |
$xml = $self->dispatch( $job->dispatch ); |
165 |
} elsif ( $size == 0 ) { |
} elsif ( $size == 0 ) { |
166 |
warn ">>> closing connection\n"; |
warn ">>> no more queued commands, closing connection to $uid\n"; |
167 |
return 0; |
return 0; |
168 |
} else { |
} else { |
169 |
warn ">>> empty response\n"; |
warn ">>> empty response to $uid\n"; |
170 |
$state->{NoMoreRequests} = 1; |
$state->{NoMoreRequests} = 1; |
171 |
$xml = $self->dispatch( 'xml', sub {} ); |
$xml = $self->dispatch( 'xml', sub {} ); |
172 |
} |
} |
174 |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
$sock->send( "Content-Length: " . length( $xml ) . "\r\n\r\n" ); |
175 |
$sock->send( $xml ) or die "can't send response"; |
$sock->send( $xml ) or die "can't send response"; |
176 |
|
|
177 |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes\n"; |
warn ">>>> " . $sock->peerhost . " [" . localtime() . "] sent ", length( $xml )," bytes to $uid\n"; |
178 |
|
|
179 |
warn "### request over\n" if $self->debug; |
$job->finish if $job; |
180 |
|
warn "### request over for $uid\n" if $self->debug; |
181 |
|
|
182 |
return 1; # next request |
return 1; # next request |
183 |
}; |
}; |
194 |
my $self = shift; |
my $self = shift; |
195 |
|
|
196 |
my $dispatch = shift || die "no dispatch?"; |
my $dispatch = shift || die "no dispatch?"; |
197 |
|
my @args = @_; |
198 |
|
|
199 |
|
if ( ref($dispatch) eq 'ARRAY' ) { |
200 |
|
my @a = @$dispatch; |
201 |
|
$dispatch = shift @a; |
202 |
|
push @args, @a; |
203 |
|
} |
204 |
|
|
205 |
my $response = CWMP::Response->new({ debug => $self->debug }); |
my $response = CWMP::Methods->new({ debug => $self->debug }); |
206 |
|
|
207 |
if ( $response->can( $dispatch ) ) { |
if ( $response->can( $dispatch ) ) { |
208 |
warn ">>> dispatching to $dispatch\n"; |
warn ">>> dispatching to $dispatch\n"; |
209 |
my $xml = $response->$dispatch( $self->state, @_ ); |
my $xml = $response->$dispatch( $self->state, @args ); |
210 |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
warn "## response payload: ",length($xml)," bytes\n$xml\n" if $self->debug; |
211 |
if ( $self->debug > 2 ) { |
if ( $self->debug > 2 ) { |
212 |
my $file = sprintf("dump/%04d.response", $dump_nr++); |
my $file = sprintf("dump/%04d-%s.response", $dump_nr++, $self->sock->peerhost); |
213 |
write_file( $file, $xml ); |
write_file( $file, $xml ); |
214 |
warn "### response dump: $file\n"; |
warn "### response dump: $file\n"; |
215 |
} |
} |