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, 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 "sign" flag is unset (default is set),
38 the PID of the sender won't be shipped with the packet. UTF-8 encoded
39 strings are automatically detected. The "utf8" bit of the packet message
40 is turned on, so that the receiver can encode them appropriately.
43 mrecv %SIG [, cb => $callback ]
45 Takes as its first argument the %SIG hash and returns a hash reference
46 that represent the current state of the receiver. %SIG's fields 'USR1'
47 and 'USR2' will be replaced by the receiver's callbacks. "cb" specifies
48 the callback to trigger each time a complete message has arrived.
49 Basically, you want to use it like this :
51 my $rcv = mrecv local %SIG, cb => sub { ... };
53 In the callback, $_[0] is the sender's PID (or 0 if the sender wanted to
54 stay anonymous) and $_[1] is the message received.
59 Resets the state of the receiver $rcv. Useful to abort transfers.
64 Returns true if the receiver $rcv is currently busy with incoming data,
70 Holds the PID of the last process that sent data to the receiver $rcv, 0
71 if that process was anonymous, or "undef" if no message has arrived yet.
72 It isn't cleared by "mreset".
77 Holds the last message received by $rcv, or "undef" if no message has
78 arrived yet. It isn't cleared by "mreset".
81 This module exports any of its functions only on request.
84 Each byte of the data string is converted into its bits sequence, with
85 bits of highest weight coming first. All those bits sequences are put
86 into the same order as the characters occur in the string.
88 The header is composed by the "utf8" bit (if the data has to be decoded
89 to UTF-8), the "sign" bit (if sender gives its PID in the header), and
90 then 24 bits representing the sender's PID (with highest weight coming
91 first) if the "sign" bit is set.
93 The emitter computes then the longuest sequence of successives 0 (say,
94 m) and 1 (n) in the concatenation of the header and the data. A
95 signature is then chosen :
97 - If m > n, we take n+1 times 1 followed by one 0 ;
98 - Otherwise, we take m+1 times 0 followed by one 1.
100 The signal is then formed by concatenating the signature, the header,
101 the data bits and the reversed signature (i.e. the bits of the signature
102 in the reverse order).
104 a ... a b | u s [ p23 ... p0 ] | ... data ... | b a ... a
105 signature | header | data | reversed signature
107 The receiver knows that the signature has been sent when it has catched
108 at least one 0 and one 1. The signal is completely transferred when it
109 has received for the first time the whole reversed signature.
112 This type of IPC is highly unreliable. Send little data at slow speed if
113 you want it to reach its goal.
115 "SIGUSR{1,2}" seem to interrupt sleep, so it's not a good idea to
116 transfer data to a sleeping process.
119 Carp (standard since perl 5), POSIX (idem), utf8 (since perl 5.6),
120 Encode (since perl 5.7.3) and Time::HiRes (idem) are required.
123 perlipc for information about signals in perl.
125 For truly useful IPC, search for shared memory, pipes and semaphores.
128 Vincent Pit, "<perl at profvince.com>"
130 You can contact me by mail or on #perl @ FreeNode (Prof_Vince).
133 Please report any bugs or feature requests to "bug-ipc-morsesignals at
134 rt.cpan.org", or through the web interface at
135 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IPC-MorseSignals>. I
136 will be notified, and then you'll automatically be notified of progress
137 on your bug as I make changes.
140 You can find documentation for this module with the perldoc command.
142 perldoc IPC::MorseSignals
145 Thanks for the inspiration, mofino ! I hope this module will fill all
149 Copyright 2007 Vincent Pit, all rights reserved.
151 This program is free software; you can redistribute it and/or modify it
152 under the same terms as Perl itself.