line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Amethyst; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
13416
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
46
|
|
4
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
5
|
1
|
|
|
1
|
|
6
|
use vars qw(@ISA $VERSION); |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
65
|
|
6
|
1
|
|
|
1
|
|
5
|
use Exporter; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
7
|
1
|
|
|
1
|
|
2020
|
use POE; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Amethyst::Connection; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
$VERSION = '1.00'; |
11
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
|
|
|
|
|
|
my $class = shift; |
15
|
|
|
|
|
|
|
my $args = ($#_ == 0) ? { %{ (shift) } } : { @_ }; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my %states = map { $_ => "handler_$_" } qw( |
18
|
|
|
|
|
|
|
_start _stop _child signal |
19
|
|
|
|
|
|
|
add_connection register_connection connect |
20
|
|
|
|
|
|
|
add_brain think |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
POE::Session->create( |
23
|
|
|
|
|
|
|
package_states => [ $class => \%states ], |
24
|
|
|
|
|
|
|
args => [ $args ], |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub handler__start { |
29
|
|
|
|
|
|
|
my ($kernel, $session, $heap, $args) = @_[KERNEL, SESSION, HEAP, ARG0]; |
30
|
|
|
|
|
|
|
$heap->{Args} = $args; |
31
|
|
|
|
|
|
|
my $name = $args->{Name} || 'amethyst'; |
32
|
|
|
|
|
|
|
$kernel->alias_set($name); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
print STDERR "Starting Amethyst '$name'\n"; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$heap->{Connections} = { }; |
37
|
|
|
|
|
|
|
$heap->{Brains} = { }; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$kernel->sig('INT', 'signal'); |
40
|
|
|
|
|
|
|
$kernel->sig('TERM', 'signal'); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub handler_signal { |
44
|
|
|
|
|
|
|
my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; |
45
|
|
|
|
|
|
|
my $signame = $_[ARG0]; |
46
|
|
|
|
|
|
|
print STDERR "Exiting on signal $signame\n"; |
47
|
|
|
|
|
|
|
exit; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub handler__stop { |
51
|
|
|
|
|
|
|
my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub handler__child { |
55
|
|
|
|
|
|
|
my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub handler_register_connection { |
59
|
|
|
|
|
|
|
my ($kernel, $session, $sender, $heap, $name) = |
60
|
|
|
|
|
|
|
@_[KERNEL, SESSION, SENDER, HEAP, ARG0]; |
61
|
|
|
|
|
|
|
die "No name for connection $sender\n" unless $name; |
62
|
|
|
|
|
|
|
$heap->{Connections}->{$name} = $sender->ID; |
63
|
|
|
|
|
|
|
print STDERR "Registered connection $name as " . $sender->ID . "\n"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub handler_add_connection { |
67
|
|
|
|
|
|
|
my ($kernel, $session, $heap, $package, $args) |
68
|
|
|
|
|
|
|
= @_[KERNEL, SESSION, HEAP, ARG0, ARG1]; |
69
|
|
|
|
|
|
|
eval qq{ require $package; }; |
70
|
|
|
|
|
|
|
die $@ if $@; |
71
|
|
|
|
|
|
|
$package->new($args); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# This no longer creates a POE::Session |
75
|
|
|
|
|
|
|
sub handler_add_brain { |
76
|
|
|
|
|
|
|
my ($kernel, $session, $heap, $package, $args) |
77
|
|
|
|
|
|
|
= @_[KERNEL, SESSION, HEAP, ARG0, ARG1]; |
78
|
|
|
|
|
|
|
my $name = $args->{Name} || $package; |
79
|
|
|
|
|
|
|
my $priority = $args->{Priority} || 1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
eval qq{ require $package; }; |
82
|
|
|
|
|
|
|
if ($@) { |
83
|
|
|
|
|
|
|
print STDERR "Amethyst: Failed to add brain $name ($package)\n"; |
84
|
|
|
|
|
|
|
$heap->{BrainsFailed}->{$name} = 1; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
|
|
|
|
|
|
my $brain = $package->new($args); |
88
|
|
|
|
|
|
|
$heap->{Brains}->{$name} = [ $brain, $name, $priority, ]; |
89
|
|
|
|
|
|
|
$brain->init(); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub handler_connect { |
94
|
|
|
|
|
|
|
my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
print STDERR "Amethyst: connecting\n"; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
unless (%{ $heap->{Brains} }) { |
99
|
|
|
|
|
|
|
print STDERR "Amethyst: Warning: No brains!\n"; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
foreach (values %{$heap->{Connections}}) { |
103
|
|
|
|
|
|
|
print STDERR "Amethyst: Connecting $_\n"; |
104
|
|
|
|
|
|
|
$kernel->post($_, 'connect'); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub handler_think { |
109
|
|
|
|
|
|
|
my ($kernel, $session, $heap, $message, $brains, @args) = |
110
|
|
|
|
|
|
|
@_[KERNEL, SESSION, HEAP, ARG0 .. $#_]; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my @brains = $brains ? @$brains : keys %{$heap->{Brains}}; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
foreach my $name (@brains) { |
115
|
|
|
|
|
|
|
if ($heap->{BrainsFailed}->{$name}) { |
116
|
|
|
|
|
|
|
print STDERR "Amethyst: Cannot think in brain $name: " . |
117
|
|
|
|
|
|
|
"it failed to load at startup\n"; |
118
|
|
|
|
|
|
|
next; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
my $brain = $heap->{Brains}->{$name}; |
121
|
|
|
|
|
|
|
if ($heap->{BrainsFailed}->{$name}) { |
122
|
|
|
|
|
|
|
print STDERR "Amethyst: Cannot think in brain $name: " . |
123
|
|
|
|
|
|
|
"no such brain!\n"; |
124
|
|
|
|
|
|
|
next; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
last if $brain->[0]->think($message, @args); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
1; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
__END__ |