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