-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoled
143 lines (127 loc) · 4.09 KB
/
moled
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/local/bin/perl
use warnings;
use strict;
sub POE::Kernel::ASSERT_DEFAULT () { 1 }
#sub POE::Kernel::ASSERT_DATA () { 1 }
sub POE::Kernel::ASSERT_EVENTS () { 1 }
#sub POE::Kernel::ASSERT_FILES () { 1 }
#sub POE::Kernel::ASSERT_RETVALS () { 1 }
#sub POE::Kernel::ASSERT_USAGE () { 1 }
#sub POE::Kernel::TRACE_DEFAULT() { 1 }
#sub POE::Kernel::TRACE_EVENTS() { 1 }
#sub POE::Kernel::TRACE_PROFILE() { 1 }
#sub POE::Kernel::TRACE_SESSIONS() { 1 }
#sub POE::Kernel::TRACE_REFCNT() { 1 }
sub Mole::MSG_DEBUG() { 0 }
$Data::Dumper::Maxdepth = Mole::MSG_DEBUG;
use YAML;
use POE;
use POE::Filter::Reference;
use POE::Component::Server::TCP;
#use POE::Component::Server::PreforkTCP;
use Mole::Snmp;
use Mole::Message;
use Data::Dumper;
#use Mole::Message::Systable;
my $debug = &Mole::MSG_DEBUG();
my $MCNT = 0;
#POE::Component::Server::PreforkTCP->new
POE::Component::Server::TCP->new
( Alias => $Mole::Server::ALIAS,
Address => "localhost",
Port => 4269,
#ClientFilter => ["POE::Filter::Reference", "YAML"],
ClientFilter => ["POE::Filter::Reference"],
InlineStates => { 'return' => \&return,
},
#MaxServer => 10, # Prefork argument
#MinServer => 1, # Prefork argument
ClientInput => \&ClientInput, # Handle client requests here.
ClientConnected => \&ClientConnected,
ClientDisconnected => \&ClientDisconnected,
);
Mole::Snmp->spawn;
POE::Session->create
(inline_states => {
_start => sub {
my $kernel = $_[KERNEL];
$kernel->yield('heartbeat');
},
heartbeat=> sub{
my $kernel = $_[KERNEL];
printf("--- THE MOLE LIVES -- %s ---\n", time);
$kernel->delay_set('heartbeat', 300);
},
_stop => sub {},
}
);
$poe_kernel->run();
################################################################################
sub ClientInput {
my ( $kernel, $heap, $msg ) = @_[ KERNEL, HEAP, ARG0 ];
$MCNT++;
my $response;
#my $reference = $kernel->alias_resolve('SNMP');
#open(FOO, ">>/tmp/moled.log");
#unless (defined($reference)) {
# printf(FOO "$$ Spawning Mole::SNMP\n");
#}
#close(FOO);
# Process the request into buckets for odd, even, and
# Build the response hash, then send it to the client.
if (&validate($msg) eq '0') {
$response = [$msg->mid, "OK"];
&process($msg);
} else {
$response = [$msg->mid, 'REJECTED'];
}
##my $response = sprintf("Client MID=%s, SvrMcnt=%s", $message->mid, $MCNT);
#$heap->{client}->put( $response );
}
################################################################################
sub ClientConnected {
my $kernel = $_[KERNEL];
#$kernel->post( moled => "shutdown" );
}
sub ClientDisconnected {
my $kernel = $_[KERNEL];
#$kernel->post( moled => "shutdown" );
}
################################################################################
sub process {
my $msg = shift;
$msg->debug('moled::process',3 ) if $debug;
$poe_kernel->post($msg->dstsess, $msg->dstevent, $msg, 'return');
}
################################################################################
sub validate {
my $message = shift;
return &validate_post($message);
return -1;
}
################################################################################
sub return {
my ($heap, $msg) = @_[ HEAP, ARG0 ];
$msg->debug('moled::return') if $debug;
##
## TODO Need eval to trap error here
##
$heap->{client}->put($msg);
}
################################################################################
sub validate_post {
my $message = shift;
#return -1 unless defined($message->orb);
#return -1 unless defined($message->mid);
#return -1 unless defined($message->session);
#return -1 unless defined($message->event);
#return -1 unless defined($message->args);
##
## TODO Determine if message supported by daemon
## TODO Determine if named-session exists
## TODO Determine if named-event exists
##
return 0;
}
package Mole::Server;
our $ALIAS="moled";