Amethyst

 view release on metacpan or  search on metacpan

Amethyst/Connection.pm  view on Meta::CPAN

package Amethyst::Connection;

use strict;
use Socket qw(AF_INET SOCK_STREAM);
use Data::Dumper;
use POE qw(Wheel::SocketFactory Wheel::ReadWrite
				Filter::Line Driver::SysRW);

sub new {
	my $class = shift;
	my $args = ($#_ == 0) ? { %{ (shift) } } : { @_ };

	die "No name for connection" unless $args->{Name};

	my %states = map { $_ => "handler_$_" } qw(
					_start _stop
					connect connect_ok connect_fail
					disconnect keepalive
					read write error
					init login logout send process
					);

	POE::Session->create(
		package_states	=> [ $class => \%states, ],
		args			=> [ $args ],
			);
}

sub handler__start {
	my ($kernel, $session, $sender, $heap, $args) =
					@_[KERNEL, SESSION, SENDER, HEAP, ARG0];

	$heap->{Args} = $args;
	$heap->{Amethyst} = $sender->ID;

	$kernel->alias_set($args->{Alias}) if $args->{Alias};

	$heap->{Debug} = exists $args->{Debug} ? $args->{Debug} : 0;
	$heap->{Brains} = $args->{Brains} if exists $args->{Brains};
	$heap->{Keepalive} = 0;

	print STDERR "START(Connection $session)\n" if $heap->{Debug} > 7;

	# This has to happen _now_ before the 'connect' trigger
	# happens in Amethyst herself, that being already on the
	# queue.
	$kernel->call($sender, 'register_connection', $args->{Name});
	$kernel->yield('init');
}

sub handler__stop {
	my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
	print STDERR "STOP(Connection $session)\n" if $heap->{Debug} > 7;
}

sub handler_read {
	my ($kernel, $heap, $session, $data) = @_[KERNEL, HEAP, SESSION, ARG0];

	warn "<<< $data\n" if $heap->{Debug} > 5;

	$kernel->delay('keepalive', $heap->{Keepalive})
					if $heap->{Keepalive};

	$kernel->yield('process', $data);
}



( run in 1.450 second using v1.01-cache-2.11-cpan-5837b0d9d2c )