| 1 | package Amethyst::Connection; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Socket qw(AF_INET SOCK_STREAM); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use POE qw(Wheel::SocketFactory Wheel::ReadWrite | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | Filter::Line Driver::SysRW); | |||||||||
| 8 | |||||||||||
| 9 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 10 | 0 | my $class = shift; | |||||||||
| 11 | 0 | ▶ | my $args = ($#_ == 0) ? { %{ (shift) } } : { @_ }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 12 | |||||||||||
| 13 | 0 | ▶ | die "No name for connection" unless $args->{Name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | |||||||||||
| 15 | 0 | my %states = map { $_ => "handler_$_" } qw( | |||||||||
| 16 | _start _stop | ||||||||||
| 17 | connect connect_ok connect_fail | ||||||||||
| 18 | disconnect keepalive | ||||||||||
| 19 | read write error | ||||||||||
| 20 | init login logout send process | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | 0 | POE::Session->create( | |||||||||
| 24 | package_states => [ $class => \%states, ], | ||||||||||
| 25 | args => [ $args ], | ||||||||||
| 26 | ); | ||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub handler__start { | ||||||||
|
Subroutine
handler__start: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 30 | 0 | my ($kernel, $session, $sender, $heap, $args) = | |||||||||
| 31 | @_[KERNEL, SESSION, SENDER, HEAP, ARG0]; | ||||||||||
| 32 | |||||||||||
| 33 | 0 | $heap->{Args} = $args; | |||||||||
| 34 | 0 | $heap->{Amethyst} = $sender->ID; | |||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | $kernel->alias_set($args->{Alias}) if $args->{Alias}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | $heap->{Debug} = exists $args->{Debug} ? $args->{Debug} : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | ▶ | $heap->{Brains} = $args->{Brains} if exists $args->{Brains}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 0 | $heap->{Keepalive} = 0; | |||||||||
| 41 | |||||||||||
| 42 | 0 | ▶ | print STDERR "START(Connection $session)\n" if $heap->{Debug} > 7; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | |||||||||||
| 44 | # This has to happen _now_ before the 'connect' trigger | ||||||||||
| 45 | # happens in Amethyst herself, that being already on the | ||||||||||
| 46 | # queue. | ||||||||||
| 47 | 0 | $kernel->call($sender, 'register_connection', $args->{Name}); | |||||||||
| 48 | 0 | $kernel->yield('init'); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub handler__stop { | ||||||||
|
Subroutine
handler__stop: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 52 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 53 | 0 | ▶ | print STDERR "STOP(Connection $session)\n" if $heap->{Debug} > 7; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub handler_read { | ||||||||
|
Subroutine
handler_read: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 57 | 0 | my ($kernel, $heap, $session, $data) = @_[KERNEL, HEAP, SESSION, ARG0]; | |||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | warn "<<< $data\n" if $heap->{Debug} > 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | |||||||||||
| 61 | $kernel->delay('keepalive', $heap->{Keepalive}) | ||||||||||
| 62 | 0 | ▶ | if $heap->{Keepalive}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 0 | $kernel->yield('process', $data); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub handler_write { | ||||||||
|
Subroutine
handler_write: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 68 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 69 | 0 | my @data = @_[ARG0 .. $#_]; | |||||||||
| 70 | |||||||||||
| 71 | 0 | my $wheel = $heap->{ReadWrite}; | |||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | unless ($wheel) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | print STDERR "No write wheel\n"; | |||||||||
| 75 | 0 | return; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 0 | foreach my $data (@data) { | |||||||||
| 79 | 0 | ▶ | warn ">>> $data\n" if $heap->{Debug} > 5; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | $wheel->put($data); | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | $kernel->delay('keepalive', $heap->{Keepalive}) | ||||||||||
| 84 | 0 | ▶ | if $heap->{Keepalive}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub handler_init { | ||||||||
|
Subroutine
handler_init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 88 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 89 | # Not overriding this is not fatal. | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 0 | ▶ | sub handler_login { | ||||||||
|
Subroutine
handler_login: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 93 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 94 | 0 | die "Login not implemented by connection"; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub handler_logout { | ||||||||
|
Subroutine
handler_logout: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 98 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub handler_disconnect { | ||||||||
|
Subroutine
handler_disconnect: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 102 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 103 | 0 | $kernel->yield('logout'); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | sub handler_keepalive { | ||||||||
|
Subroutine
handler_keepalive: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 107 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 108 | 0 | die "Keepalive requested but not implemented by connection"; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 0 | ▶ | sub handler_send { | ||||||||
|
Subroutine
handler_send: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 112 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 113 | 0 | die "Send not implemented by connection"; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub handler_process { | ||||||||
|
Subroutine
handler_process: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 117 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 118 | 0 | die "Process not implemented by connection"; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | ▶ | sub handler_error { | ||||||||
|
Subroutine
handler_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 122 | 0 | my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP]; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | sub handler_connect_ok { | ||||||||
|
Subroutine
handler_connect_ok: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 126 | 0 | my ($kernel, $heap, $session, $socket) = @_[KERNEL, HEAP, SESSION, ARG0]; | |||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | print STDERR "Connect OK\n" if $heap->{Debug} > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | |||||||||||
| 130 | 0 | delete $heap->{SocketFactory}; | |||||||||
| 131 | |||||||||||
| 132 | 0 | my $wheel = POE::Wheel::ReadWrite->new( | |||||||||
| 133 | Handle => $socket, | ||||||||||
| 134 | Driver => POE::Driver::SysRW->new(), | ||||||||||
| 135 | Filter => POE::Filter::Line->new( | ||||||||||
| 136 | # XXX We should use a telnet_ga as a terminator too. | ||||||||||
| 137 | InputRegexp => qr'\015?\012', | ||||||||||
| 138 | OutputLiteral => "\015\012", | ||||||||||
| 139 | ), | ||||||||||
| 140 | InputEvent => 'read', | ||||||||||
| 141 | ErrorEvent => 'error', | ||||||||||
| 142 | ); | ||||||||||
| 143 | |||||||||||
| 144 | 0 | $heap->{ReadWrite} = $wheel; | |||||||||
| 145 | |||||||||||
| 146 | 0 | $kernel->yield('login'); | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 0 | ▶ | sub handler_connect_fail { | ||||||||
|
Subroutine
handler_connect_fail: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 150 | 0 | my ($kernel, $heap, $session, $socket) = @_[KERNEL, HEAP, SESSION, ARG0]; | |||||||||
| 151 | |||||||||||
| 152 | 0 | print STDERR "Connect failure\n"; | |||||||||
| 153 | |||||||||||
| 154 | 0 | $heap->{ConnectFailures}++; | |||||||||
| 155 | |||||||||||
| 156 | 0 | ▶ | if ($heap->{ConnectFailures} > 3) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | print STDERR "Too many connect failures: Giving up\n"; | |||||||||
| 158 | } | ||||||||||
| 159 | else { | ||||||||||
| 160 | 0 | $kernel->alarm('connect', time() + 5); | |||||||||
| 161 | } | ||||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 0 | ▶ | sub handler_connect { | ||||||||
|
Subroutine
handler_connect: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 165 | 0 | my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; | |||||||||
| 166 | |||||||||||
| 167 | 0 | my $host = $heap->{Args}->{Host}; | |||||||||
| 168 | 0 | my $port = $heap->{Args}->{Port}; | |||||||||
| 169 | 0 | ▶ | print STDERR "Connecting to $host:$port\n" if $heap->{Debug} > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | |||||||||||
| 171 | 0 | my $wheel = new POE::Wheel::SocketFactory( | |||||||||
| 172 | SocketDomain => AF_INET, | ||||||||||
| 173 | SocketType => SOCK_STREAM, | ||||||||||
| 174 | SocketProtocol => 'tcp', | ||||||||||
| 175 | |||||||||||
| 176 | RemoteAddress => $host, | ||||||||||
| 177 | RemotePort => $port, | ||||||||||
| 178 | |||||||||||
| 179 | SuccessEvent => 'connect_ok', | ||||||||||
| 180 | FailureEvent => 'connect_fail', | ||||||||||
| 181 | ); | ||||||||||
| 182 | |||||||||||
| 183 | 0 | $heap->{SocketFactory} = $wheel; | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | 1 | 1; | |||||||||