From: Vincent Pit Date: Sun, 29 Jun 2008 16:35:33 +0000 (+0200) Subject: Importing IPC-MorseSignals-0.05.tar.gz X-Git-Tag: v0.05^0 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FIPC-MorseSignals.git;a=commitdiff_plain;h=fcfeb2180a98d41e14a848f7bb8ba0d05b297c52 Importing IPC-MorseSignals-0.05.tar.gz --- diff --git a/Changes b/Changes index 4b26a56..58b6335 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,13 @@ Revision history for IPC-MorseSignals +0.05 2007-08-18 16:50 UTC + + Add : m{send,recv} will croak() if any of their arguments is invalid. + + Chg : The requirements to pass the speed test were lowered. + + Doc : Typos in POD. + + Fix : You can now send "0" as a valid message. + + Fix : I lied, t/02-sigusr.t wasn't really gone. + + Fix : while ($speed > 1) { $speed /= 2 } ok($speed >= 1); never fails. + 0.04 2007-08-17 14:45 UTC + Add : Test for SUGUSR{1,2} in Makefile.PL. + Add : Unicode support. Enabled by passing utf8 => 1 to m{send,recv}. @@ -8,7 +16,7 @@ Revision history for IPC-MorseSignals + Doc : Prof_Vince, 'truely' isn't a word. + Fix : t/11-speed.t didn't fail properly (as if those tests don't fail enough yet!). - + Rem : t/02-sigusr.t, as it just seems to fail everywhere. + + Fix : t/02-sigusr.t didn't check properly the returned value. 0.03 2007-08-16 16:20 UTC + Chg : Better t/10-base.t... Or at least, I hope so. diff --git a/META.yml b/META.yml index 0950b53..2373575 100644 --- a/META.yml +++ b/META.yml @@ -1,14 +1,16 @@ --- #YAML:1.0 name: IPC-MorseSignals -version: 0.04 +version: 0.05 abstract: Communicate between processes with Morse signals. license: perl generated_by: ExtUtils::MakeMaker version 6.36 distribution_type: module requires: + Carp: 0 POSIX: 0 Test::More: 0 Time::HiRes: 0 + utf8: 0 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.2.html version: 1.2 diff --git a/Makefile.PL b/Makefile.PL index 6e26a00..4a1f945 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -28,9 +28,11 @@ WriteMakefile( ABSTRACT_FROM => 'lib/IPC/MorseSignals.pm', PL_FILES => {}, PREREQ_PM => { + 'Carp' => 0, 'POSIX' => 0, 'Test::More' => 0, 'Time::HiRes' => 0, + 'utf8' => 0, }, dist => { PREOP => 'pod2text lib/IPC/MorseSignals.pm > $(DISTVNAME)/README', diff --git a/README b/README index 222504f..b2925f0 100644 --- a/README +++ b/README @@ -2,7 +2,7 @@ NAME IPC::MorseSignals - Communicate between processes with Morse signals. VERSION - Version 0.04 + Version 0.05 SYNOPSIS use IPC::MorseSignals qw/msend mrecv/; @@ -36,14 +36,14 @@ FUNCTIONS is set, the string will first be encoded in UTF-8. In this case, you must turn it on for "mrecv" as well. Default speed is 512, don't set it too low or the target will miss bits and the whole message will be - crippled. The "utf8" flag is turned off by default; + crippled. The "utf8" flag is turned off by default. "mrecv" mrecv $callback [, utf => $utf8 ] Takes as its first 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 + "USR1" and "USR2" signal handlers. Basically, you want to use it like this : local @SIG{qw/USR1 USR2/} = mrecv sub { ... }; @@ -59,11 +59,11 @@ PROTOCOL 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 string. The emitter - computes then the longuest sequence of successives 0 (say, "m") and 1 - ("n"). A signature is then chosen : + computes then the longuest sequence of successives 0 (say, m) and 1 (n). + A signature is then chosen : - If C(m > n), we take "n+1" times 1 follewed by 1 0 ; - Otherwise, we take "m+1" times 0 follewed by 1 1. + - If m > n, we take n+1 times 1 follewed by one 0 ; + - Otherwise, we take m+1 times 0 follewed by one 1. 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 @@ -77,12 +77,12 @@ CAVEATS This type of IPC is highly unreliable. Send little data at slow speed if you want it to reach its goal. - SIGUSR{1,2} seem to interrupt sleep, so it's not a good idea to transfer - data to a sleeping process. + "SIGUSR{1,2}" seem to interrupt sleep, so it's not a good idea to + transfer data to a sleeping process. DEPENDENCIES - POSIX (standard since perl 5) and Time::HiRes (standard since perl - 5.7.3) are required. + Carp (standard since perl 5), POSIX (idem), Time::HiRes (since perl + 5.7.3) and utf8 (since perl 5.6) are required. SEE ALSO perlipc for information about signals in perl. diff --git a/lib/IPC/MorseSignals.pm b/lib/IPC/MorseSignals.pm index 5f61e31..0c32482 100644 --- a/lib/IPC/MorseSignals.pm +++ b/lib/IPC/MorseSignals.pm @@ -5,8 +5,9 @@ use warnings; use utf8; -use Time::HiRes qw/usleep/; +use Carp qw/croak/; use POSIX qw/SIGUSR1 SIGUSR2/; +use Time::HiRes qw/usleep/; =head1 NAME @@ -14,11 +15,11 @@ IPC::MorseSignals - Communicate between processes with Morse signals. =head1 VERSION -Version 0.04 +Version 0.05 =cut -our $VERSION = '0.04'; +our $VERSION = '0.05'; =head1 SYNOPSIS @@ -49,15 +50,17 @@ But, seriously, use something else for your IPC. :) msend $msg, $pid [, speed => $speed, utf8 => $utf8 ] -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. If the C flag is set, the string will first be encoded in UTF-8. In this case, you must turn it on for L as well. -Default speed is 512, don't set it too low or the target will miss bits and the whole message will be crippled. The C flag is turned off by default; +Sends the string C<$msg> to the process C<$pid> (or to all the processes C<@$pid> if C<$pid> is an array ref) at C<$speed> bits per second. If the C flag is set, the string will first be encoded in UTF-8. In this case, you must turn it on for L as well. +Default speed is 512, don't set it too low or the target will miss bits and the whole message will be crippled. The C flag is turned off by default. =cut sub msend { my ($msg, $pid, @o) = @_; my @pid = (ref $pid eq 'ARRAY') ? @$pid : $pid; - return unless @pid && $msg && !(@o % 2); + return unless defined $msg && length $msg; + croak 'No PID was supplied' unless @pid; + croak 'Optional arguments must be passed as key => value pairs' if @o % 2; my %opts = @o; $opts{speed} ||= 512; $opts{utf8} ||= 0; @@ -93,7 +96,7 @@ sub msend { mrecv $callback [, utf => $utf8 ] -Takes as its first 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 : +Takes as its first argument the callback triggered when a complete message is received, and returns two code references that should replace C and C signal handlers. Basically, you want to use it like this : local @SIG{qw/USR1 USR2/} = mrecv sub { ... }; @@ -103,7 +106,8 @@ Turn on the utf8 flag if you know that the incoming strings are expected to be i sub mrecv { my ($cb, @o) = @_; - return unless $cb && !(@o % 2); + croak 'No callback was specified' unless $cb; + croak 'Optional arguments must be passed as key => value pairs' if @o % 2; my %opts = @o; $opts{utf8} ||= 0; my ($bits, $state, $c, $n, $end) = ('', 0, undef, 0, ''); @@ -154,13 +158,13 @@ $EXPORT_TAGS{'all'} = \@EXPORT_OK; =head1 PROTOCOL -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 string. The emitter computes then the longuest sequence of successives 0 (say, C) and 1 (C). A signature is then chosen : +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 string. The emitter computes then the longuest sequence of successives 0 (say, m) and 1 (n). A signature is then chosen : =over 4 -=item If C(m > n), we take C times 1 follewed by C<1> 0 ; +=item - If m > n, we take n+1 times 1 follewed by one 0 ; -=item Otherwise, we take C times 0 follewed by C<1> 1. +=item - Otherwise, we take m+1 times 0 follewed by one 1. =back @@ -172,11 +176,11 @@ The receiver knows that the signature has been sent when it has catched at least This type of IPC is highly unreliable. Send little data at slow speed if you want it to reach its goal. -SIGUSR{1,2} seem to interrupt sleep, so it's not a good idea to transfer data to a sleeping process. +C seem to interrupt sleep, so it's not a good idea to transfer data to a sleeping process. =head1 DEPENDENCIES -L (standard since perl 5) and L (standard since perl 5.7.3) are required. +L (standard since perl 5), L (idem), L (since perl 5.7.3) and L (since perl 5.6) are required. =head1 SEE ALSO diff --git a/samples/bench.pl b/samples/bench.pl index d45e950..4e4d57f 100755 --- a/samples/bench.pl +++ b/samples/bench.pl @@ -73,17 +73,19 @@ SPEED: } tryspeed 4, 1; -tryspeed 4, 5; -tryspeed 4, 10; -tryspeed 4, 50; +tryspeed 4, 4; +tryspeed 4, 16; +tryspeed 4, 64; +tryspeed 4, 256; tryspeed 16, 1; -tryspeed 16, 5; -tryspeed 16, 10; +tryspeed 16, 4; +tryspeed 16, 16; +tryspeed 16, 64; tryspeed 64, 1; -tryspeed 64, 5; -tryspeed 64, 10; +tryspeed 64, 4; +tryspeed 64, 16; tryspeed 256, 1; -tryspeed 256, 5; +tryspeed 256, 4; tryspeed 1024, 1; print STDERR "=== Summary ===\n"; diff --git a/t/10-base.t b/t/10-base.t index 3eb877c..782e0b2 100644 --- a/t/10-base.t +++ b/t/10-base.t @@ -11,8 +11,7 @@ sub trysend { my $speed = 2 ** 16; my $ok = 0; SPEED: - while (($speed > 1) && !$ok) { - $speed /= 2; + while ((($speed /= 2) >= 1) && !$ok) { my $pid = fork; if (!defined $pid) { die "$desc: fork() failed : $!"; diff --git a/t/11-unicode.t b/t/11-unicode.t index f0c19e4..2c6704a 100644 --- a/t/11-unicode.t +++ b/t/11-unicode.t @@ -14,8 +14,7 @@ sub trysend8 { my $ok = 0; $desc .= ' (unicode)'; SPEED: - while (($speed > 1) && !$ok) { - $speed /= 2; + while ((($speed /= 2) >= 1) && !$ok) { my $pid = fork; if (!defined $pid) { die "$desc: fork() failed : $!"; diff --git a/t/12-speed.t b/t/12-speed.t index 7d035b3..95a7e6d 100644 --- a/t/12-speed.t +++ b/t/12-speed.t @@ -1,6 +1,6 @@ #!perl -T -use Test::More tests => 12; +use Test::More tests => 6; use POSIX qw/SIGINT SIGTERM SIGKILL EXIT_SUCCESS EXIT_FAILURE WIFEXITED WEXITSTATUS/; @@ -9,15 +9,15 @@ use IPC::MorseSignals qw/msend mrecv/; my @res; sub tryspeed { - my ($l, $n) = @_; + my ($l, $n, $optional) = @_; my $speed = 2 ** 16; my $ok = 0; my @alpha = ('a' .. 'z'); my $msg = join '', map { $alpha[rand @alpha] } 1 .. $l; my $desc; - while (($speed > 1) && ($ok < $n)) { - $speed /= 2; + while ((($speed /= 2) >= 1) && ($ok < $n)) { $desc = "$n sends of $l bytes at $speed bits/s"; + $desc .= ' (mandatory)' unless $optional; $ok = 0; diag("try $desc..."); TRY: @@ -51,22 +51,26 @@ TRY: } } $desc = "$l bytes sent $n times"; - ok($speed >= 1, $desc); + ok($speed >= 1, $desc) unless $optional; push @res, $desc . (($speed) ? ' at ' . $speed . ' bits/s' : ' failed'); } -tryspeed 4, 1; -tryspeed 4, 5; -tryspeed 4, 10; -tryspeed 4, 50; -tryspeed 16, 1; -tryspeed 16, 5; -tryspeed 16, 10; -tryspeed 64, 1; -tryspeed 64, 5; -tryspeed 64, 10; -tryspeed 256, 1; -tryspeed 1024, 1; +tryspeed 4, 1; +tryspeed 4, 4; +tryspeed 4, 16; +tryspeed 16, 1; +tryspeed 16, 4; +tryspeed 64, 1; + +tryspeed 4, 64, 1; +tryspeed 16, 16, 1; +tryspeed 64, 4, 1; +tryspeed 256, 1, 1; +tryspeed 1024, 1, 1; diag '=== Summary ==='; -diag $_ for @res; +diag $_ for sort { + my ($l1, $n1) = $a =~ /(\d+)\D+(\d+)/; + my ($l2, $n2) = $b =~ /(\d+)\D+(\d+)/; + $l1 <=> $l2 || $n1 <=> $n2 +} @res;