Class-Publisher
view release on metacpan or search on metacpan
t/Telephone.pm view on Meta::CPAN
sub switch_on {
my $self = shift;
TRACE('Switching on: ' . $self->{number});
$self->notify_subscribers('connect');
}
sub switch_off {
my $self = shift;
TRACE('Switching off: ' . $self->{number});
$self->hangup if $self->busy;
$self->online(0);
$self->notify_subscribers('disconnect');
}
sub busy {
my $self = shift;
return defined $_[0] ? $self->{busy} = shift : $self->{busy}
}
sub online {
my $self = shift;
return defined $_[0] ? $self->{online} = shift : $self->{online}
}
sub call {
my $self = shift;
my $number = shift;
return unless $self->online();
if ($self->busy()) {
TRACE('Cannot make a call. Call already in progress');
}
$self->busy(1);
delete $self->{hangup};
$self->{calls_made}++;
TRACE($self->{number} . "calling $number");
$self->notify_subscribers('call', number => $number);
return 1;
}
sub connect {
my $self = shift;
my ($other_phone) = @_;
TRACE($self->{number} . ' connected to ' . $other_phone->{number});
$self->{connections}++;
$self->{other_party} = $other_phone;
# Ordinarily the exchange would route communications
$other_phone->add_subscriber('communicate', $self);
$self->busy(1);
}
sub hangup {
my $self = shift;
my ($reason) = @_;
TRACE($self->{number} . " hanging up [$reason]") if $reason;
if ($self->{other_party}) {
$self->delete_subscriber('communicate', $self->{other_party});
$self->notify_subscribers('end_call');
delete $self->{other_party};
}
$self->busy(0);
$self->{listened_to} = undef;
$self->{hangup} = $reason;
}
sub speak {
my $self = shift;
my ($words) = @_;
TRACE($self->{number} . " saying '$words'");
$self->notify_subscribers('communicate', words => $words);
}
t/Telephone.pm view on Meta::CPAN
my $self = shift;
return 1 if $self->{shift->{number}};
}
sub connect_phones {
my $self = shift;
my ($caller, $action, %params) = @_;
my $recipient = $self->{$params{number}};
if ($recipient) {
if ($recipient->busy()) {
$caller->hangup('BUSY');
} else {
$caller->connect($recipient);
$recipient->connect($caller);
}
} else {
$caller->hangup('WRONG NUMBER');
}
}
t/exchange.t view on Meta::CPAN
# Switch the test phone on now the exchange is initialised
$test_phone->switch_on();
ok($test_phone->online(), 'phone now online - exchange is initialised');
# Create a new phone
my $friends_phone = new Telephone('0123 555 1111');
ok($friends_phone->online, 'new phone is online');
# Call a non existant phone number
$test_phone->call('0123 555 9999');
ok(!$test_phone->busy, 'call to non-existant number failed');
is($test_phone->{hangup}, 'WRONG NUMBER', 'exchange caught "call" event');
# Call the friend's phone
$test_phone->call($friends_phone->{number});
ok($test_phone->busy && $friends_phone->busy,
'call connected between two phones');
# Try and call from another phone while still connected
my $home_phone = new Telephone('0123 555 5555');
$home_phone->call($test_phone->{number});
ok(!$home_phone->busy && $home_phone->{hangup} eq 'BUSY',
'cannot call a phone that is engaged');
# say something
$test_phone->speak('Wazzzup!');
is($friends_phone->{listened_to}[-1], 'Wazzzup!',
'friends phone caught communicate event');
$friends_phone->speak('Who is this?');
is($test_phone->{listened_to}[-1], 'Who is this?',
'test phone caught communicate response');
$test_phone->speak('Err. I think I have the wrong number');
$friends_phone->speak('Bye then!');
# Hang up
$friends_phone->hangup();
ok(!$test_phone->busy && !$friends_phone->busy, 'both phones hung up');
# Check subscription cancelled
ok(!$test_phone->get_subscribers('communicate'),
'test phone has no "communicate" subscribers');
ok(!$friends_phone->get_subscribers('communicate'),
'other phone has no "communicate" subscribers');
ok($exchange->valid_phone($friends_phone));
$friends_phone->switch_off;
ok(!$exchange->valid_phone($friends_phone), 'exchange caught disconnect event');
( run in 0.234 second using v1.01-cache-2.11-cpan-87723dcf8b7 )