Permalink
Browse files

Massive redoing of Reflex roles and some of the objects that use them.

For example, Reflex::Handle is deprecated in favor of Reflex::Stream,
which is implemented now in terms of Reflex::Role::Streaming.
  • Loading branch information...
1 parent c1c573e commit 08bafbd2b824ae0ceed83ea9e70757929899527d @rcaputo committed Jun 27, 2010
View
@@ -2,18 +2,18 @@ package EchoStream;
use Moose;
extends 'Reflex::Stream';
-sub on_stream_data {
+sub on_handle_data {
my ($self, $args) = @_;
$self->put($args->{data});
}
-sub on_stream_failure {
+sub on_handle_error {
my ($self, $args) = @_;
warn "$args->{errfun} error $args->{errnum}: $args->{errstr}\n";
$self->emit( event => "stopped", args => {} );
}
-sub on_stream_closed {
+sub on_handle_closed {
my ($self, $args) = @_;
$self->emit( event => "stopped", args => {} );
}
View
@@ -9,16 +9,16 @@
# the composition architectures in past examples.
{
- package Reflex::UdpPeer::Echo;
+ package Reflex::Udp::Echo;
use Moose;
extends 'Reflex::UdpPeer';
- sub on_udppeer_datagram {
+ sub on_socket_datagram {
my ($self, $args) = @_;
my $data = $args->{datagram};
if ($data =~ /^\s*shutdown\s*$/) {
- $self->destruct();
+ $self->stop_socket_readable();
return;
}
@@ -28,7 +28,7 @@
);
}
- sub on_udppeer_error {
+ sub on_socket_error {
my ($self, $args) = @_;
warn "$args->{op} error $args->{errnum}: $args->{errstr}";
$self->destruct();
@@ -38,7 +38,12 @@
# Main.
my $port = 12345;
-my $peer = Reflex::UdpPeer::Echo->new( port => $port );
+my $peer = Reflex::Udp::Echo->new(
+ socket => IO::Socket::INET->new(
+ LocalPort => $port,
+ Proto => 'udp',
+ )
+);
print "UDP echo service is listening on port $port.\n";
Reflex::Object->run_all();
exit;
View
@@ -9,7 +9,7 @@
# Reflex::UdpPeer object rather than inheriting from that class.
{
- package Reflex::UdpPeer::Echo;
+ package Reflex::Udp::Echo;
use Moose;
extends 'Reflex::Object';
use Reflex::UdpPeer;
@@ -20,12 +20,17 @@
);
has peer => (
- isa => 'Reflex::UdpPeer|Undef',
+ isa => 'Maybe[Reflex::UdpPeer]',
is => 'rw',
traits => ['Reflex::Trait::Observed'],
setup => sub {
my $self = shift;
- Reflex::UdpPeer->new(port => $self->port());
+ Reflex::UdpPeer->new(
+ socket => IO::Socket::INET->new(
+ LocalPort => $self->port(),
+ Proto => 'udp',
+ )
+ )
},
);
@@ -46,6 +51,7 @@
sub on_peer_error {
my ($self, $args) = @_;
+
warn "$args->{op} error $args->{errnum}: $args->{errstr}";
$self->peer(undef);
}
@@ -54,7 +60,7 @@
# Main.
my $port = 12345;
-my $peer = Reflex::UdpPeer::Echo->new( port => $port );
+my $peer = Reflex::Udp::Echo->new( port => $port );
print "UDP echo service is listening on port $port.\n";
Reflex::Object->run_all();
exit;
View
@@ -4,42 +4,51 @@
use strict;
use lib qw(../lib);
-# An object's emitted events can also trigger methods in the subclass.
# This example creates a UDP echo server using a role rather than
# inheritance.
{
- package Reflex::UdpPeer::Echo;
+ package Reflex::Udp::Echo;
use Moose;
extends 'Reflex::Object';
- with 'Reflex::Role::UdpPeer';
+ use IO::Socket::INET;
- sub on_udppeer_datagram {
- my ($self, $args) = @_;
- my $data = $args->{datagram};
+ has socket => (
+ is => 'ro',
+ isa => 'FileHandle',
+ required => 1,
+ );
- if ($data =~ /^\s*shutdown\s*$/) {
- $self->destruct();
+ with 'Reflex::Role::Recving' => {
+ handle => 'socket',
+
+ # Expose send_socket() as send().
+ -alias => { send_socket => 'send' },
+ -excludes => 'send_socket'
+
+ };
+
+ sub on_socket_datagram {
+ my ($self, $arg) = @_;
+
+ if ($arg->{datagram} =~ /^\s*shutdown\s*$/) {
+ $self->stop_socket_readable();
return;
}
- $self->send(
- datagram => $data,
- remote_addr => $args->{remote_addr},
- );
- }
-
- sub on_udppeer_error {
- my ($self, $args) = @_;
- warn "$args->{op} error $args->{errnum}: $args->{errstr}";
- $self->destruct();
+ $self->send(%$arg);
}
}
# Main.
my $port = 12345;
-my $peer = Reflex::UdpPeer::Echo->new( port => $port );
+my $peer = Reflex::Udp::Echo->new(
+ socket => IO::Socket::INET->new(
+ LocalPort => $port,
+ Proto => 'udp',
+ )
+);
print "UDP echo service is listening on port $port.\n";
Reflex::Object->run_all();
exit;
View
@@ -1,15 +0,0 @@
-package Stream;
-use Moose;
-extends 'Reflex::Object';
-
-has handle => ( is => 'rw', isa => 'FileHandle', required => 1 );
-
-with 'Reflex::Role::Streaming' => {
- handle => 'handle',
-
- # Expose put_handle() as put().
- -alias => { put_handle => 'put' },
- -excludes => 'put_handle',
-};
-
-1;
View
@@ -1,7 +1,9 @@
use Moose;
use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC);
use Proxy;
-use Stream;
+use Reflex::Stream;
+1;
+
# Socket pair 1. Writes to either end are readable at the other.
my ($socket_1a, $socket_1b);
@@ -17,8 +19,8 @@
handle_b => $socket_2b,
);
-my $s1 = Stream->new( handle => $socket_1a );
-my $s2 = Stream->new( handle => $socket_2a );
+my $s1 = Reflex::Stream->new( handle => $socket_1a );
+my $s2 = Reflex::Stream->new( handle => $socket_2a );
# Write data to Socket 1a.
# It will appear on Socket 1b, via the socketpair.
@@ -20,8 +20,10 @@ sub next {
my $queue = $self->queue();
- # TODO - Probably should bail out if the event loop ends.
- $POE::Kernel::poe_kernel->run_one_timeslice() while @$queue < 1;
+ # Run while the queue is empty and POE has things to do.
+ 1 while (
+ @$queue < 1 and $POE::Kernel::poe_kernel->run_one_timeslice()
+ );
return shift @$queue;
}
View
@@ -39,7 +39,7 @@ sub BUILD {
my $packed_address;
if ($handle->isa("IO::Socket::INET")) {
- # TODO - Non-bollocking resolver.
+ # TODO - Need a non-bollocking resolver.
my $inet_address = inet_aton($self->remote_addr());
$packed_address = pack_sockaddr_in($self->remote_port(), $inet_address);
}
@@ -4,7 +4,7 @@ extends 'Reflex::POE::Wheel';
use POE::Wheel::Run;
# These are class methods, returning static class data.
-# TODO - How does Moose do this?
+# TODO - What's the proper way to do this with Moose?
sub event_to_index {
return(
View
@@ -13,12 +13,15 @@ END {
our @CARP_NOT = (__PACKAGE__);
# Singleton POE::Session.
-# TODO - Extract the POE bits into another role.
+# TODO - Extract the POE bits into another role if we want to support
+# other event loops at the top level rather than beneath POE.
-# TODO - How to prevent this from being redefined?
+# TODO - How to prevent these from being redefined?
+# TODO - Such as if POE is loaded elsewhere first?
+#
#sub POE::Kernel::ASSERT_DEFAULT () { 1 }
+#sub POE::Kernel::CATCH_EXCEPTIONS () { 0 }
-sub POE::Kernel::CATCH_EXCEPTIONS () { 0 }
use POE;
use Reflex::POE::Session;
@@ -92,7 +95,7 @@ my $singleton_session_id = POE::Session->create(
return if Reflex::POE::Session->deliver($_[SENDER]->ID, $event, $args);
# Unhandled event.
- # TODO - Anything special?
+ # TODO - Should anything special be done in this case?
},
### Support POE::Wheel classes.
@@ -243,8 +246,8 @@ sub BUILD {
}
# TODO - Who is the watcher?
- # TODO - watch() takes multiple event/callback pairs. We can
- # combine them into a hash and call watch() once.
+ # TODO - Optimization! watch() takes multiple event/callback
+ # pairs. We can combine them into a hash and call watch() once.
$self->watch($self, $1 => $value);
next CALLBACK;
}
@@ -332,8 +335,9 @@ sub _is_watched {
sub emit {
my ($self, @args) = @_;
- # TODO - Checking arguments is tedious, but check_args() method
- # calls add up.
+ # TODO - Is there a better way to check parameters? Checking them
+ # in custom code is tedious. Calling check_args() is relatively
+ # slow. Can we have our peanut butter and our chocolate together?
my $args = $self->check_args(
\@args,
Oops, something went wrong.

0 comments on commit 08bafbd

Please sign in to comment.