1 package IPC::MorseSignals;
6 use Time::HiRes qw/usleep/;
7 use POSIX qw/SIGUSR1 SIGUSR2/;
11 IPC::MorseSignals - Communicate between processes with Morse signals.
19 our $VERSION = '0.02';
23 use IPC::MorseSignals qw/msend mrecv/;
27 die "fork() failed: $!";
29 local @SIG{qw/USR1 USR2/} = mrecv sub {
30 print STDERR "received $_[0]!\n";
35 msend "hello!\n" => $pid;
40 This module implements a rare form of IPC by sending Morse-like signals through C<SIGUSR1> and C<SIGUSR2>. Both of those signals are used, so you won't be able to keep them for something else when you use this module.
42 But, seriously, use something else for your IPC. :)
48 msend $msg, $pid [, $speed ]
50 Sends the string C<$msg> to the process C<$pid> (or to all the processes C<@$pid> if $pid is an array ref) at C<$speed> bits per second. Default speed is 512, don't set it too low or the target will miss bits and the whole message will be crippled.
55 my ($msg, $pid, $speed) = @_;
56 my @pid = (ref $pid eq 'ARRAY') ? @$pid : $pid;
57 return unless @pid && $msg;
59 my $delay = int(1_000_000 / $speed);
60 my @bits = split //, unpack 'B*', $msg;
61 my ($c, $n, @l) = (2, 0, 0, 0, 0);
66 if ($n > $l[$c]) { $l[$c] = $n; }
71 if ($n > $l[$c]) { $l[$c] = $n; }
72 ($c, $n) = ($l[0] > $l[1]) ? (1, $l[1]) : (0, $l[0]); # Take the smallest
74 @bits = (($c) x $n, 1 - $c, @bits, 1 - $c, ($c) x $n);
76 my $sig = ($_ == 0) ? SIGUSR1 : SIGUSR2;
86 Takes as its sole argument the callback triggered when a complete message is received, and returns two code references that should replace SIGUSR1 and SIGUSR2 signal handlers. Basically, you want to use it like this :
88 local @SIG{qw/USR1 USR2/} = mrecv sub { ... };
95 my ($bits, $state, $c, $n, $end) = ('', 0, undef, 0, '');
96 my $sighandler = sub {
99 if (defined $bits && (substr $bits, -$n) eq $end) { # done
100 substr $bits, -$n, $n, '';
101 $cb->(pack 'B*', $bits);
103 } elsif ($state == 1) {
106 $end = (1 - $c) . $c x $n;
127 This module exports on request its two only functions, L</msend> and L</mrecv>.
131 use base qw/Exporter/;
134 our %EXPORT_TAGS = ( 'funcs' => [ qw/msend mrecv/ ] );
135 our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
136 $EXPORT_TAGS{'all'} = \@EXPORT_OK;
140 Each byte of the data string is converted into its bits sequence, with bits of highest weight coming first. All those bits sequences are put into the same order as the characters occur in the stream. The emitter computes then the longuest sequence of successives 0 (say, C<m>) and 1 (C<n>). A signature is then chosen :
144 =item If C(m > n), we take C<n+1> times 1 follewed by C<1> 0 ;
146 =item Otherwise, we take C<m+1> times 0 follewed by C<1> 1.
150 The signal is then formed by concatenating the signature, the data bits and the reversed signature (i.e. the bits of the signature in the reverse order).
152 The receiver knows that the signature has been sent when it has catched at least one 0 and one 1. The signal is completely transferred when it has received for the first time the whole reversed signature.
156 This type of IPC is highly unreliable. Send little data at slow speed if you want it to reach its goal.
158 SIGUSR{1,2} seem to interrupt sleep, so it's not a good idea to transfer data to a sleeping process.
162 L<POSIX> (standard since perl 5) and L<Time::HiRes> (standard since perl 5.7.3) are required.
166 L<perlipc> for information about signals in perl.
168 For truely useful IPC, search for shared memory, pipes and semaphores.
172 Vincent Pit, C<< <perl at profvince.com> >>
174 You can contact me by mail or on #perl @ FreeNode (Prof_Vince).
178 Please report any bugs or feature requests to
179 C<bug-ipc-morsesignals at rt.cpan.org>, or through the web interface at
180 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IPC-MorseSignals>.
181 I will be notified, and then you'll automatically be notified of progress on
182 your bug as I make changes.
186 You can find documentation for this module with the perldoc command.
188 perldoc IPC::MorseSignals
190 =head1 ACKNOWLEDGEMENTS
192 Thanks for the inspiration, mofino ! I hope this module will fill all your IPC needs. :)
194 =head1 COPYRIGHT & LICENSE
196 Copyright 2007 Vincent Pit, all rights reserved.
198 This program is free software; you can redistribute it and/or modify it
199 under the same terms as Perl itself.
203 1; # End of IPC::MorseSignals