2 IPC::MorseSignals - Communicate between processes with Morse signals.
8 use IPC::MorseSignals qw/msend mrecv/;
12 die "fork() failed: $!";
14 my $s = mrecv local %SIG, cb => sub {
15 print STDERR "received $_[1] from $_[0]!\n";
20 msend "hello!\n" => $pid;
24 This module implements a rare form of IPC by sending Morse-like signals
25 through "SIGUSR1" and "SIGUSR2". Both of those signals are used, so you
26 won't be able to keep them for something else when you use this module.
28 But, seriously, use something else for your IPC. :)
32 msend $msg, $pid [, speed => $speed, utf8 => $utf8, sign => $sign ]
34 Sends the string $msg to the process $pid (or to all the processes @$pid
35 if $pid is an array ref) at $speed bits per second. Default speed is
36 512, don't set it too low or the target will miss bits and the whole
37 message will be crippled. If the "utf8" flag is set (default is unset),
38 the string will first be encoded in UTF-8. The "utf8" bit of the packet
39 message is turned on, so that the receiver is aware of it. If the "sign"
40 flag is unset (default is set), the PID of the sender won't be shipped
44 mrecv %SIG [, cb => $callback ]
46 Takes as its first argument the %SIG hash and returns a hash reference
47 that represent the current state of the receiver. %SIG's fields 'USR1'
48 and 'USR2' will be replaced by the receiver's callbacks. "cb" specifies
49 the callback to trigger each time a complete message has arrived.
50 Basically, you want to use it like this :
52 my $rcv = mrecv local %SIG, cb => sub { ... };
54 In the callback, $_[0] is the sender's PID (or 0 if the sender wanted to
55 stay anonymous) and $_[1] is the message received.
60 Resets the state of the receiver $rcv. Useful to abort transfers.
65 Returns true if the receiver $rcv is currently busy with incoming data,
71 Holds the PID of the last process that sent data to the receiver $rcv, 0
72 if that process was anonymous, or "undef" if no message has arrived yet.
73 It isn't cleared by "mreset".
78 Holds the last message received by $rcv, or "undef" if no message has
79 arrived yet. It isn't cleared by "mreset".
82 This module exports any of its functions only on request.
85 Each byte of the data string is converted into its bits sequence, with
86 bits of highest weight coming first. All those bits sequences are put
87 into the same order as the characters occur in the string.
89 The header is composed by the "utf8" bit (if the data has to be decoded
90 to UTF-8), the "sign" bit (if sender gives its PID in the header), and
91 then 24 bits representing the sender's PID (with highest weight coming
92 first) if the "sign" bit is set.
94 The emitter computes then the longuest sequence of successives 0 (say,
95 m) and 1 (n) in the concatenation of the header and the data. A
96 signature is then chosen :
98 - If m > n, we take n+1 times 1 follewed by one 0 ;
99 - Otherwise, we take m+1 times 0 follewed by one 1.
101 The signal is then formed by concatenating the signature, the header,
102 the data bits and the reversed signature (i.e. the bits of the signature
103 in the reverse order).
105 a ... a b | u s [ p23 ... p0 ] | ... data ... | b a ... a
106 signature | header | data | reversed signature
108 The receiver knows that the signature has been sent when it has catched
109 at least one 0 and one 1. The signal is completely transferred when it
110 has received for the first time the whole reversed signature.
113 This type of IPC is highly unreliable. Send little data at slow speed if
114 you want it to reach its goal.
116 "SIGUSR{1,2}" seem to interrupt sleep, so it's not a good idea to
117 transfer data to a sleeping process.
120 Carp (standard since perl 5), POSIX (idem), Time::HiRes (since perl
121 5.7.3) and utf8 (since perl 5.6) are required.
124 perlipc for information about signals in perl.
126 For truly useful IPC, search for shared memory, pipes and semaphores.
129 Vincent Pit, "<perl at profvince.com>"
131 You can contact me by mail or on #perl @ FreeNode (Prof_Vince).
134 Please report any bugs or feature requests to "bug-ipc-morsesignals at
135 rt.cpan.org", or through the web interface at
136 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IPC-MorseSignals>. I
137 will be notified, and then you'll automatically be notified of progress
138 on your bug as I make changes.
141 You can find documentation for this module with the perldoc command.
143 perldoc IPC::MorseSignals
146 Thanks for the inspiration, mofino ! I hope this module will fill all
150 Copyright 2007 Vincent Pit, all rights reserved.
152 This program is free software; you can redistribute it and/or modify it
153 under the same terms as Perl itself.