1 |
package MJAM::Connector::TcpHttp::Server; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use POE qw(Component::Server::HTTP); |
7 |
use HTTP::Status; |
8 |
use Data::Dumper; |
9 |
|
10 |
sub handler_root { |
11 |
my ($request, $response) = @_; |
12 |
print "#"; |
13 |
$response->code(RC_OK); |
14 |
$response->add_content(<<EOF); |
15 |
cmd server ready.<br> |
16 |
- <a href="/cmd/status">status</a><br> |
17 |
- <a href="/cmd/shutdown">shutdown</a><br> |
18 |
EOF |
19 |
return RC_OK; |
20 |
} |
21 |
|
22 |
sub handler_cmd { |
23 |
my ($request, $response) = @_; |
24 |
print "#"; |
25 |
$response->code(RC_OK); |
26 |
|
27 |
my $uri = $request->uri; |
28 |
$uri =~ m!/cmd/(.+?)$!; |
29 |
my $cmd = $1; |
30 |
|
31 |
if ($cmd) { |
32 |
$response->add_content(<<EOF); |
33 |
cmd: $cmd<br> |
34 |
EOF |
35 |
} |
36 |
|
37 |
my $conn = $request->connection; |
38 |
my $sess = $conn->{session}; |
39 |
#$response->add_content(Dumper($sess)); |
40 |
#$poe_kernel->signal('TERM'); |
41 |
#$poe_kernel->post('deamon', 'shutdown'); |
42 |
|
43 |
if ($cmd =~ m/shutdown/) { |
44 |
$poe_kernel->post($sess, 'shutdown'); |
45 |
} |
46 |
|
47 |
return RC_OK; |
48 |
} |
49 |
|
50 |
|
51 |
my $httpd; |
52 |
|
53 |
sub start { |
54 |
$httpd = POE::Component::Server::HTTP->new( |
55 |
Port => 8000, |
56 |
ContentHandler => { |
57 |
'/' => \&handler_root, |
58 |
'/cmd/' => \&handler_cmd, |
59 |
}, |
60 |
Headers => { Server => 'My Server' }, |
61 |
); |
62 |
} |
63 |
|
64 |
sub stop { |
65 |
$httpd && $httpd->shutdown(); |
66 |
#$poe_kernel->post(); |
67 |
} |
68 |
|
69 |
1; |