1 package Bit::MorseSignals::Receiver;
7 use Encode qw<decode_utf8>;
10 use Bit::MorseSignals qw<:consts>;
14 Bit::MorseSignals::Receiver - Base class for Bit::MorseSignals receivers.
22 our $VERSION = '0.08';
26 use Bit::MorseSignals::Receiver;
28 my $pants = Bit::MorseSignals::Receiver->new(done => sub { print "received $_[1]!\n" });
30 my $bit = comes_from_somewhere_lets_say_signals();
36 Base class for L<Bit::MorseSignals> receivers. Please refer to this module for more general information about the protocol.
38 Given a sequence of bits coming from the L<Bit::MorseSignals> protocol, the receiver object detects when a packet has been completed and then reconstructs the original message depending of the datatype specified in the header.
43 croak 'First argument isn\'t a valid ' . __PACKAGE__ . ' object'
44 unless ref $_[0] and $_[0]->isa(__PACKAGE__);
49 =head2 C<< new < done => $cb > >>
51 L<Bit::MorseSignals::Receiver> object constructor. With the C<'done'> option, you can specify a callback that will be triggered every time a message is completed, and in which C<$_[0]> will be the receiver object and C<$_[1]> the message received.
57 return unless $class = ref $class || $class;
58 croak 'Optional arguments must be passed as key => value pairs' if @_ % 2;
71 Tells the receiver that you have received the bit C<$bit>. Returns true while the message isn't completed, and C<undef> as soon as it is.
76 my ($self, $bit) = @_;
80 return unless defined $bit;
84 if ($self->{state} == 3) { # data
86 vec($self->{buf}, $self->{len}, 1) = $bit;
88 if ($self->{len} >= $self->{sig_len}) {
90 for (1 .. $self->{sig_len}) {
91 if (vec($self->{buf}, $self->{len} - $_, 1) != vec($self->{sig}, $_-1, 1)) {
97 my $base = int $self->{sig_len} / 8 + $self->{sig_len} % 8 != 0;
98 substr $self->{buf}, -$base, $base, '';
99 my @demanglers = (sub { $_[0] }, \&decode_utf8, \&thaw );
100 # BM_DATA_{PLAIN, UTF8, STORABLE}
101 $self->{msg} = defined $demanglers[$self->{type}]
103 local $SIG{__DIE__} = sub { warn @_ };
104 $demanglers[$self->{type}]->($self->{buf})
108 $self->{done}->($self, $self->{msg}) if $self->{done};
113 } elsif ($self->{state} == 2) { # header
115 vec($self->{buf}, $self->{len}++, 1) = $bit;
116 if ($self->{len} >= 3) {
117 my $type = 2 * vec($self->{buf}, 1, 1)
118 + vec($self->{buf}, 0, 1);
119 $type = BM_DATA_PLAIN if vec($self->{buf}, 2, 1);
120 @{$self}{qw<state type buf len>} = (3, $type, '', 0);
123 } elsif ($self->{state} == 1) { # end of signature
125 if ($self->{sig_bit} != $bit) {
128 vec($self->{sig}, $self->{sig_len}++, 1) = $bit;
132 @{$self}{qw<state sig sig_bit sig_len buf len>}
133 = (1, '', $bit, 1, '', 0 );
134 vec($self->{sig}, 0, 1) = $bit;
143 Resets the current receiver state, obliterating any current message being received.
151 @{$self}{qw<sig sig_bit sig_len type buf len>} = ();
157 True when the receiver is in the middle of assembling a message.
164 return $self->{state} > 0;
169 The last message completed, or C<undef> when no message has been assembled yet.
181 An object module shouldn't export any function, and so does this one.
185 L<Carp> (standard since perl 5), L<Encode> (since perl 5.007003), L<Storable> (idem).
189 L<Bit::MorseSignals>, L<Bit::MorseSignals::Emitter>.
193 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
195 You can contact me by mail or on C<irc.perl.org> (vincent).
199 Please report any bugs or feature requests to C<bug-bit-morsesignals-receiver at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bit-MorseSignals>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
203 You can find documentation for this module with the perldoc command.
205 perldoc Bit::MorseSignals::Receiver
207 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/Bit-MorseSignals>.
209 =head1 COPYRIGHT & LICENSE
211 Copyright 2008 Vincent Pit, all rights reserved.
213 This program is free software; you can redistribute it and/or modify it
214 under the same terms as Perl itself.
218 1; # End of Bit::MorseSignals::Receiver