| 1 | # -*- Perl -*- | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # File: POE/Event/Message/UniqueID.pm | ||||||||||||||||||||||||||||||||
| 4 | # Desc: Generate a guaranteed unique message identifier | ||||||||||||||||||||||||||||||||
| 5 | # Date: Mon Oct 10 10:10:59 2005 | ||||||||||||||||||||||||||||||||
| 6 | # Stat: Prototype, Experimental | ||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||
| 8 | package POE::Event::Message::UniqueID; | ||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 3 | our $PACK = __PACKAGE__; | |||||||||||||||||||||||||||||||
| 14 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||
| 15 | ### @ISA = qw( ); | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use POSIX; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use Time::HiRes; # comment this line to use CORE::time() and rand() | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | # uncomment to use gettimeofday() -- the better choice | ||||||||||||||||||||||||||||||||
| 20 | 3 | my($host,$pid); | |||||||||||||||||||||||||||||||
| 21 | 3 | my($time,$ident,@prior) = ("","",()); | |||||||||||||||||||||||||||||||
| 22 | 3 | my($secs,$msecs,$rand) = (0,0,0); | |||||||||||||||||||||||||||||||
| 23 | 3 | my($errs,$mesg,$count) = (0,"",0); | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | sub import | ||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | { my($class,@args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | 3 | $class->buildIdentityGenerator( @args ); # How will we create IDs today? | |||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 3 | *generate = \&genUniqueIdent; | |||||||||||||||||||||||||||||||
| 31 | 3 | *generateUniqueID = \&genUniqueIdent; | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | sub buildIdentityGenerator | ||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | { my($class,$debug) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
buildIdentityGenerator: called CC=6 SCAR=21.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 3 | $host = eval { (POSIX::uname)[1] }; | |||||||||||||||||||||||||||||||
| 37 | 3 | $pid = $$; | |||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | print "-" x 55 ."\n" if $debug; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | if ( exists $INC{'Time/HiRes.pm'} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | print "Will use Time::HiRes::time() to generate IDs\n" if $debug; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | eval('sub genUniqueIdent { | ||||||||||||||||||||||||||||||
|
Subroutine
genUniqueIdent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 45 | ($secs,$msecs) = Time::HiRes::gettimeofday(); | ||||||||||||||||||||||||||||||||
| 46 | ( $host ."-". unpack("H*", pack("N*", $secs,$msecs,$pid)) ); | ||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | '); | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | print "Will use CORE::time() and rand() to generate IDs\n" if $debug; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | print "(note that there is a slight chance of duplicates)\n" if $debug; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 0 | eval('sub genUniqueIdent { | |||||||||||||||||||||||||||||||
| 55 | my $limit = 1000000; # good distribution for uniqueness? | ||||||||||||||||||||||||||||||||
| 56 | ($time,$rand) = ( CORE::time(), rand($limit) ); | ||||||||||||||||||||||||||||||||
| 57 | ( $host ."-". unpack("H*", pack("N*", $time,$rand,$pid)) ); | ||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | '); | ||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | 3 | return; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 3 | my $dupErrCount; | |||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | sub dupErrCount { $dupErrCount }; | ||||||||||||||||||||||||||||||
|
Subroutine
dupErrCount: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | sub verifyGenerateUniqueID | ||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | { my($class,$debug,$max) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
verifyGenerateUniqueID: called CC=12 SCAR=30.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | $debug ||= 0; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$debug ||= 0
MC/DC0%0/1
$debug ||= 0
$debug
Truth table50%1/2
A
A= $debug
| |||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | $max ||= 1000; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$max ||= 1000
MC/DC0%0/1
$max ||= 1000
$max
Truth table50%1/2
A
A= $max
| |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | if ($debug) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | 0 | print "-" x 55 ."\n"; | |||||||||||||||||||||||||||||||
| 75 | 0 | print "Generating $max IDs to verify uniqueness...\n"; | |||||||||||||||||||||||||||||||
| 76 | 0 | print " host = $host\n"; | |||||||||||||||||||||||||||||||
| 77 | 0 | print " pid = $pid\n"; | |||||||||||||||||||||||||||||||
| 78 | 0 | print " time = ". time() ."\n"; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 1 | foreach (1..$max) { | |||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 1000 | $ident = genUniqueIdent(); # This is a generated subroutine | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 1000 | ▶ | if ( grep(/^$ident$/, @prior) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 0 | ($errs,$mesg) = ($errs +1, "duplicate ID!"); | |||||||||||||||||||||||||||||||
| 87 | 0 | print " ident = $ident $mesg\n"; | |||||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||||||
| 89 | 1000 | ($mesg) = ("unique ID!"); | |||||||||||||||||||||||||||||||
| 90 | 1000 | push @prior, $ident; | |||||||||||||||||||||||||||||||
| 91 | 1000 | ▶ | if ($count) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 999 | ▶ | print " ident = $ident $mesg\n" if $debug > 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | print " ident = $ident (example)\n" if $debug > 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | 1000 | $count++; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | 1 | $dupErrCount = $errs; | |||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 1 | print "-" x 55 ."\n"; | |||||||||||||||||||||||||||||||
| 102 | 1 | print "IDs Generated: $count Duplicates: $errs\n"; | |||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | if ( $errs and exists $INC{'Time/HiRes.pm'} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$errs and exists $INC{"Time/HiRes.pm"}
MC/DC0%0/2
$errs and exists $INC{"Time/HiRes.pm"}
$errs
exists $INC{"Time/HiRes.pm"}
Truth table33%1/3
A and B
A= $errsB= exists $INC{"Time/HiRes.pm"}
| |||||||||||||||||||||||||||||||||
| 105 | 0 | print "OUCH: the unique ID generator needs some work!!\n"; | |||||||||||||||||||||||||||||||
| 106 | } elsif ( $errs ) { | ||||||||||||||||||||||||||||||||
| 107 | 0 | print "Oops: Try using 'Time::HiRes' for better results!\n"; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 1 | print "-" x 55 ."\n"; | |||||||||||||||||||||||||||||||
| 110 | 1 | return; | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | #_________________________ | ||||||||||||||||||||||||||||||||
| 113 | 3 | 1; # Required by require() | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||||