| 1 | package IO::Concurrent::Scenario; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Carp (); | ||
| 6 | |||
| 7 | use IO::Concurrent::Context; | ||
| 8 | use IO::Concurrent::Actor; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class) = @_; | ||
| 12 | return bless { | ||
| 13 | state => undef, | ||
| 14 | actors => [], | ||
| 15 | on_error => sub { Carp::croak(shift->error) }, | ||
| 16 | }, $class; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub wait_for_writable { | ||
| 20 | my ($self, $callback) = @_; | ||
| 21 | my $actor = IO::Concurrent::Actor->new(writable => $callback); | ||
| 22 | push @{ $self->{actors} } => $actor; | ||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub wait_for_readable { | ||
| 27 | my ($self, $callback) = @_; | ||
| 28 | my $actor = IO::Concurrent::Actor->new(readable => $callback); | ||
| 29 | push @{ $self->{actors} } => $actor; | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub on_error { | ||
| 34 | my ($self, $callback) = @_; | ||
| 35 | $self->{on_error} = $callback if @_ == 2; | ||
| 36 | return $self->{on_error}; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub create_context { | ||
| 40 | my ($self, $handler) = @_; | ||
| 41 | |||
| 42 | my @actors = @{ $self->{actors} }; | ||
| 43 | return IO::Concurrent::Context->new($handler, \@actors); | ||
| 44 | } | ||
| 45 | |||
| 46 | 1; | ||
| 47 | __END__ |