]> git.vpit.fr Git - perl/modules/Bit-MorseSignals.git/blob - lib/Bit/MorseSignals/Receiver.pm
Make sure the POD headings are linkable
[perl/modules/Bit-MorseSignals.git] / lib / Bit / MorseSignals / Receiver.pm
1 package Bit::MorseSignals::Receiver;
2
3 use strict;
4 use warnings;
5
6 use Carp     qw<croak>;
7 use Encode   qw<decode_utf8>;
8 use Storable qw<thaw>;
9
10 use Bit::MorseSignals qw<:consts>;
11
12 =head1 NAME
13
14 Bit::MorseSignals::Receiver - Base class for Bit::MorseSignals receivers.
15
16 =head1 VERSION
17
18 Version 0.08
19
20 =cut
21
22 our $VERSION = '0.08';
23
24 =head1 SYNOPSIS
25
26     use Bit::MorseSignals::Receiver;
27
28     my $pants = Bit::MorseSignals::Receiver->new(done => sub { print "received $_[1]!\n" });
29     while (...) {
30      my $bit = comes_from_somewhere_lets_say_signals();
31      $pants->push($bit);
32     }
33
34 =head1 DESCRIPTION
35
36 Base class for L<Bit::MorseSignals> receivers. Please refer to this module for more general information about the protocol.
37
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.
39
40 =cut
41
42 sub _check_self {
43  croak 'First argument isn\'t a valid ' . __PACKAGE__ . ' object'
44   unless ref $_[0] and $_[0]->isa(__PACKAGE__);
45 }
46
47 =head1 METHODS
48
49 =head2 C<new>
50
51     my $bmr = Bit::MorseSignals::Receiver->new(done => $cb);
52
53 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.
54
55 =cut
56
57 sub new {
58  my $class = shift;
59  return unless $class = ref $class || $class;
60  croak 'Optional arguments must be passed as key => value pairs' if @_ % 2;
61  my %opts = @_;
62  my $self = {
63   msg    => undef,
64   done   => $opts{done},
65  };
66  bless $self, $class;
67  $self->reset;
68  return $self;
69 }
70
71 =head2 C<push>
72
73     $bmr->push($bit);
74
75 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
77 =cut
78
79 sub push {
80  my ($self, $bit) = @_;
81  _check_self($self);
82  if (!defined $bit) {
83   $bit = $_;
84   return unless defined $bit;
85  }
86  $bit = $bit ? 1 : 0;
87
88  if ($self->{state} == 3) { # data
89
90   vec($self->{buf}, $self->{len}, 1) = $bit;
91   ++$self->{len};
92   if ($self->{len} >= $self->{sig_len}) {
93    my $res = 1;
94    for (1 .. $self->{sig_len}) {
95     if (vec($self->{buf}, $self->{len} - $_, 1) != vec($self->{sig}, $_-1, 1)) {
96      $res = 0;
97      last;
98     }
99    }
100    if ($res) {
101     my $base = int $self->{sig_len} / 8 + $self->{sig_len} % 8 != 0;
102     substr $self->{buf}, -$base, $base, '';
103     my @demanglers = (sub { $_[0] }, \&decode_utf8, \&thaw  );
104     #        BM_DATA_{PLAIN,         UTF8,          STORABLE}
105     $self->{msg} = defined $demanglers[$self->{type}]
106                     ? do {
107                        local $SIG{__DIE__} = sub { warn @_ };
108                        $demanglers[$self->{type}]->($self->{buf})
109                       }
110                     : $self->{buf};
111     $self->reset;
112     $self->{done}->($self, $self->{msg}) if $self->{done};
113     return;
114    }
115   }
116
117  } elsif ($self->{state} == 2) { # header
118
119   vec($self->{buf}, $self->{len}++, 1) = $bit;
120   if ($self->{len} >= 3) {
121    my $type = 2 * vec($self->{buf}, 1, 1)
122                 + vec($self->{buf}, 0, 1);
123    $type = BM_DATA_PLAIN if vec($self->{buf}, 2, 1);
124    @{$self}{qw<state type buf len>} = (3, $type, '', 0);
125   }
126
127  } elsif ($self->{state} == 1) { # end of signature
128
129   if ($self->{sig_bit} != $bit) {
130    $self->{state} = 2;
131   }
132   vec($self->{sig}, $self->{sig_len}++, 1) = $bit;
133
134  } else { # first bit
135
136   @{$self}{qw<state sig sig_bit sig_len buf len>}
137            = (1,    '', $bit,   1,      '', 0  );
138   vec($self->{sig}, 0, 1) = $bit;
139
140  }
141
142  return $self;
143 }
144
145 =head2 C<reset>
146
147 Resets the current receiver state, obliterating any current message being received.
148
149 =cut
150
151 sub reset {
152  my ($self) = @_;
153  _check_self($self);
154  $self->{state} = 0;
155  @{$self}{qw<sig sig_bit sig_len type buf len>} = ();
156  return $self;
157 }
158
159 =head2 C<busy>
160
161 True when the receiver is in the middle of assembling a message.
162
163 =cut
164
165 sub busy {
166  my ($self) = @_;
167  _check_self($self);
168  return $self->{state} > 0;
169 }
170
171 =head2 C<msg>
172
173 The last message completed, or C<undef> when no message has been assembled yet.
174
175 =cut
176
177 sub msg {
178  my ($self) = @_;
179  _check_self($self);
180  return $self->{msg};
181 }
182
183 =head1 EXPORT
184
185 An object module shouldn't export any function, and so does this one.
186
187 =head1 DEPENDENCIES
188
189 L<Carp> (standard since perl 5), L<Encode> (since perl 5.007003), L<Storable> (idem).
190
191 =head1 SEE ALSO
192
193 L<Bit::MorseSignals>, L<Bit::MorseSignals::Emitter>.
194
195 =head1 AUTHOR
196
197 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
198
199 You can contact me by mail or on C<irc.perl.org> (vincent).
200
201 =head1 BUGS
202
203 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.
204
205 =head1 SUPPORT
206
207 You can find documentation for this module with the perldoc command.
208
209     perldoc Bit::MorseSignals::Receiver
210
211 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/Bit-MorseSignals>.
212
213 =head1 COPYRIGHT & LICENSE
214
215 Copyright 2008 Vincent Pit, all rights reserved.
216
217 This program is free software; you can redistribute it and/or modify it
218 under the same terms as Perl itself.
219
220 =cut
221
222 1; # End of Bit::MorseSignals::Receiver