ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Kernel.pm
(Generate patch)

Comparing AnyEvent-MP/MP/Kernel.pm (file contents):
Revision 1.77 by root, Thu Mar 1 20:19:43 2012 UTC vs.
Revision 1.78 by root, Fri Mar 2 19:19:21 2012 UTC

24package AnyEvent::MP::Kernel; 24package AnyEvent::MP::Kernel;
25 25
26use common::sense; 26use common::sense;
27use POSIX (); 27use POSIX ();
28use Carp (); 28use Carp ();
29use MIME::Base64 ();
30 29
31use AE (); 30use AE ();
32 31
33use AnyEvent::MP::Node; 32use AnyEvent::MP::Node;
34use AnyEvent::MP::Transport; 33use AnyEvent::MP::Transport;
113 } 112 }
114 113
115 \&$func 114 \&$func
116} 115}
117 116
117my @alnum = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z');
118
118sub nonce($) { 119sub nonce($) {
119 my $nonce;
120
121 if (open my $fh, "</dev/urandom") {
122 sysread $fh, $nonce, $_[0];
123 } else {
124 # shit...
125 $nonce = join "", map +(chr rand 256), 1 .. $_[0] 120 join "", map chr rand 256, 1 .. $_[0]
126 }
127
128 $nonce
129} 121}
130 122
131# binary to alphanumeric 123sub nonce62($) {
132sub alnumbits($) { 124 join "", map $alnum[rand 62], 1 .. $_[0]
133 (my $data = MIME::Base64::encode_base64 $_[0], "") =~ y%+/=%-_%d;
134 $data
135} 125}
136 126
137sub gen_uniq { 127sub gen_uniq {
138 # 48 bits happen to fit perfectly into 8 chars 128 my $now = AE::now;
139 # we add one more char to get 54 bits. 129 (join "",
140 substr +(alnumbits pack "nna*", $$ & 0xffff, time & 0xffff, nonce 3), 0, 9 130 map $alnum[$_],
131 $$ / 62 % 62,
132 $$ % 62,
133 (int $now ) % 62,
134 (int $now * 100) % 62,
135 (int $now * 10000) % 62,
136 ) . nonce62 4;
141} 137}
142 138
143our $CONFIG; # this node's configuration 139our $CONFIG; # this node's configuration
144 140
145our $RUNIQ; # remote uniq value 141our $RUNIQ; # remote uniq value
158our $LISTENER; # our listeners, as arrayref 154our $LISTENER; # our listeners, as arrayref
159 155
160our $SRCNODE; # holds the sending node _object_ during _inject 156our $SRCNODE; # holds the sending node _object_ during _inject
161 157
162sub _init_names { 158sub _init_names {
159 # ~54 bits, for local port names, lowercase $ID appended
163 $UNIQ = gen_uniq; 160 $UNIQ = gen_uniq;
164 $RUNIQ = substr +(alnumbits nonce 64/8), 0, 10; # for remote port names &c, should be longer than $UNIQ 161
162 # ~59 bits, for remote port names, one longer than $UNIQ and uppercase at the end to avoid clashes
163 $RUNIQ = nonce62 10;
164 $RUNIQ =~ s/(.)$/\U$1/;
165
165 $NODE = "anon/$RUNIQ"; 166 $NODE = "anon/$RUNIQ";
166} 167}
167 168
168_init_names; 169_init_names;
169 170
170sub NODE() { 171sub NODE() {
597# "'g" -> node -> undef 598# "'g" -> node -> undef
598# ... 599# ...
599# 600#
600 601
601# used on all nodes: 602# used on all nodes:
602our $MASTER; # the global node we bind ourselves to, unless we are global ourselves 603our $MASTER; # the global node we bind ourselves to, unless we are global ourselves
603our $GLOBAL_MON; 604our $MASTER_MON;
604our $GLOBAL_TIMER;
605our %LOCAL_DB; # this node database 605our %LOCAL_DB; # this node database
606 606
607# used on global nodes:
608our %GLOBAL_DB; # all local databases, merged 607our %GLOBAL_DB; # all local databases, merged - empty on non-global nodes
609our %LOCAL_DBS; # local databases of all global nodes
610our %GLOBAL_DBS; # global db
611our %GLOBAL_SLAVE; # nodes that are our slaves
612 608
613#our $sv_ne_json = JSON::XS->new->canonical; 609#our $sv_ne_json = JSON::XS->new->canonical;
614# 610#
615#sub sv_ne($$) { 611#sub sv_ne($$) {
616# $sv_ne_json->encode ($_[0]) ne $sv_ne_json->encode ($_[1]) 612# $sv_ne_json->encode ($_[0]) ne $sv_ne_json->encode ($_[1])
617#} 613#}
618
619# should be only on globals, but...
620$node_req{g_global} = sub {
621 if ($GLOBAL) {
622 &g_clr ($SRCNODE->{id});
623 $SRCNODE->{transport}{remote_greeting}{global} = 1;
624 delete $GLOBAL_SLAVE{$SRCNODE->{id}};
625 snd $SRCNODE->{id}, g_set => \%LOCAL_DB;
626 }
627};
628
629sub other_globals() {
630 grep $_ ne $NODE && node_is_up $_, keys %{ $GLOBAL_DB{"'g"} }
631}
632 614
633# local database management 615# local database management
634sub ldb_set($$;$) { 616sub ldb_set($$;$) {
635 warn "ldb_set<@_>\n";#d# 617 warn "ldb_set<@_>\n";#d#
636 if (@_ > 2) { 618 if (@_ > 2) {
698 master_set $_; 680 master_set $_;
699 return; 681 return;
700 } 682 }
701 } 683 }
702 684
703 $GLOBAL_MON = mon_nodes sub { 685 $MASTER_MON = mon_nodes sub {
704 return unless $_[1]; # we are only interested in node-ups 686 return unless $_[1]; # we are only interested in node-ups
705 return unless $NODE_SEED{$_[0]}; # we are only interested in seed nodes 687 return unless $NODE_SEED{$_[0]}; # we are only interested in seed nodes
706 688
707 master_set $_[0]; 689 master_set $_[0];
708 690
709 $GLOBAL_MON = mon_nodes sub { 691 $MASTER_MON = mon_nodes sub {
710 if ($_[0] eq $MASTER && !$_[1]) { 692 if ($_[0] eq $MASTER && !$_[1]) {
711 undef $MASTER; 693 undef $MASTER;
712 master_search (); 694 master_search ();
713 } 695 }
714 }; 696 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines