ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Base.pm
Revision: 1.20
Committed: Wed Aug 12 00:19:03 2009 UTC (14 years, 9 months ago) by root
Branch: MAIN
Changes since 1.19: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     AnyEvent::MP::Base - basis for AnyEvent::MP and Coro::MP
4    
5     =head1 SYNOPSIS
6    
7     # use AnyEvent::MP or Coro::MP instead
8    
9 root 1.3 =head1 DESCRIPTION
10    
11     This module provides most of the basic functionality of AnyEvent::MP,
12     exposed through higher level interfaces such as L<AnyEvent::MP> and
13     L<Coro::MP>.
14    
15 root 1.4 =head1 GLOBALS
16    
17     =over 4
18    
19 root 1.1 =cut
20    
21     package AnyEvent::MP::Base;
22    
23     use common::sense;
24     use Carp ();
25 root 1.6 use MIME::Base64 ();
26 root 1.1
27     use AE ();
28    
29 root 1.6 use AnyEvent::MP::Node;
30     use AnyEvent::MP::Transport;
31    
32 root 1.1 use base "Exporter";
33    
34 root 1.17 our $VERSION = '0.4';
35 root 1.5 our @EXPORT = qw(
36 root 1.15 %NODE %PORT %PORT_DATA %REG $UNIQ $RUNIQ $ID add_node load_func
37 root 1.8
38     NODE $NODE node_of snd kil _any_
39 root 1.12 resolve_node initialise_node
40 root 1.5 );
41 root 1.1
42 root 1.10 our $DEFAULT_PORT = "4040";
43 root 1.1
44 root 1.20 our $CONNECT_INTERVAL = 2; # new connect every 2s, at least
45     our $CONNECT_TIMEOUT = 5; # activity timeout
46     our $MONITOR_TIMEOUT = 15; # fail monitoring after this time
47 root 1.1
48 root 1.4 =item $AnyEvent::MP::Base::WARN
49    
50     This value is called with an error or warning message, when e.g. a connection
51     could not be created, authorisation failed and so on.
52    
53     The default simply logs the message to STDERR.
54    
55     =cut
56    
57 root 1.3 our $WARN = sub {
58 root 1.8 my $msg = $_[0];
59     $msg =~ s/\n$//;
60     warn "$msg\n";
61 root 1.3 };
62    
63 root 1.1 sub nonce($) {
64     my $nonce;
65    
66     if (open my $fh, "</dev/urandom") {
67     sysread $fh, $nonce, $_[0];
68     } else {
69     # shit...
70     our $nonce_init;
71     unless ($nonce_init++) {
72     srand time ^ $$ ^ unpack "%L*", qx"ps -edalf" . qx"ipconfig /all";
73     }
74    
75     $nonce = join "", map +(chr rand 256), 1 .. $_[0]
76     }
77    
78     $nonce
79     }
80    
81 root 1.14 sub asciibits($) {
82     my $data = $_[0];
83    
84     if (eval "use Math::GMP 2.05; 1") {
85     $data = Math::GMP::get_str_gmp (
86     (Math::GMP::new_from_scalar_with_base (+(unpack "H*", $data), 16)),
87     62
88     );
89     } else {
90     $data = MIME::Base64::encode_base64 $data, "";
91     $data =~ s/=//;
92     $data =~ s/\//s/g;
93     $data =~ s/\+/p/g;
94     }
95    
96     $data
97     }
98    
99 root 1.6 sub gen_uniq {
100 root 1.14 asciibits pack "wNa*", $$, time, nonce 2
101 root 1.6 }
102    
103 root 1.1 our $PUBLIC = 0;
104 root 1.13 our $SLAVE = 0;
105 root 1.1
106 root 1.15 our $NODE = asciibits nonce 16;
107     our $RUNIQ = $NODE; # remote uniq value
108     our $UNIQ = gen_uniq; # per-process/node unique cookie
109     our $ID = "a";
110    
111 root 1.1 our %NODE; # node id to transport mapping, or "undef", for local node
112 root 1.8 our (%PORT, %PORT_DATA); # local ports
113 root 1.5
114     our %RMON; # local ports monitored by remote nodes ($RMON{noderef}{portid} == cb)
115     our %LMON; # monitored _local_ ports
116    
117 root 1.8 our %LISTENER;
118 root 1.1
119 root 1.5 our $SRCNODE; # holds the sending node during _inject
120    
121 root 1.8 sub NODE() {
122     $NODE
123     }
124    
125     sub node_of($) {
126     my ($noderef, undef) = split /#/, $_[0], 2;
127    
128     $noderef
129     }
130 root 1.1
131     sub _ANY_() { 1 }
132     sub _any_() { \&_ANY_ }
133    
134 root 1.16 sub TRACE() { 0 }
135 root 1.13
136 root 1.1 sub _inject {
137 root 1.14 warn "RCV $SRCNODE->{noderef} -> @_\n" if TRACE;#d#
138 root 1.5 &{ $PORT{+shift} or return };
139 root 1.1 }
140    
141     sub add_node {
142     my ($noderef) = @_;
143    
144     return $NODE{$noderef}
145     if exists $NODE{$noderef};
146    
147     for (split /,/, $noderef) {
148     return $NODE{$noderef} = $NODE{$_}
149     if exists $NODE{$_};
150     }
151    
152 root 1.10 # new node, check validity
153 root 1.13 my $node;
154 root 1.10
155 root 1.13 if ($noderef =~ /^slave\/.+$/) {
156     $node = new AnyEvent::MP::Node::Slave $noderef;
157    
158     } else {
159     for (split /,/, $noderef) {
160     my ($host, $port) = AnyEvent::Socket::parse_hostport $_
161     or Carp::croak "$noderef: not a resolved node reference ('$_' not parsable)";
162    
163     $port > 0
164     or Carp::croak "$noderef: not a resolved node reference ('$_' contains non-numeric port)";
165 root 1.10
166 root 1.13 AnyEvent::Socket::parse_address $host
167     or Carp::croak "$noderef: not a resolved node reference ('$_' contains unresolved address)";
168     }
169 root 1.10
170 root 1.13 # TODO: for indirect sends, use a different class
171     $node = new AnyEvent::MP::Node::Direct $noderef;
172 root 1.10 }
173    
174 root 1.1 $NODE{$_} = $node
175     for $noderef, split /,/, $noderef;
176    
177     $node
178     }
179    
180     sub snd(@) {
181 root 1.13 my ($noderef, $portid) = split /#/, shift, 2;
182    
183 root 1.14 warn "SND $noderef <- $portid @_\n" if TRACE;#d#
184 root 1.1
185 root 1.5 ($NODE{$noderef} || add_node $noderef)
186 root 1.13 ->send (["$portid", @_]);
187 root 1.5 }
188    
189 root 1.8 sub kil(@) {
190 root 1.13 my ($noderef, $portid) = split /#/, shift, 2;
191 root 1.5
192 root 1.13 length $portid
193     or Carp::croak "$noderef#$portid: killing a node port is not allowed, caught";
194 root 1.10
195 root 1.7 ($NODE{$noderef} || add_node $noderef)
196 root 1.13 ->kill ("$portid", @_);
197 root 1.1 }
198    
199 root 1.10 sub resolve_node($) {
200     my ($noderef) = @_;
201    
202     my $cv = AE::cv;
203     my @res;
204    
205     $cv->begin (sub {
206     my %seen;
207     my @refs;
208     for (sort { $a->[0] <=> $b->[0] } @res) {
209     push @refs, $_->[1] unless $seen{$_->[1]}++
210     }
211     shift->send (join ",", @refs);
212     });
213    
214     $noderef = $DEFAULT_PORT unless length $noderef;
215    
216     my $idx;
217     for my $t (split /,/, $noderef) {
218     my $pri = ++$idx;
219    
220     if ($t =~ /^\d*$/) {
221     require POSIX;
222     my $nodename = (POSIX::uname ())[1];
223    
224     $cv->begin;
225     AnyEvent::Socket::resolve_sockaddr $nodename, $t || "aemp=$DEFAULT_PORT", "tcp", 0, undef, sub {
226     for (@_) {
227     my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3];
228     push @res, [
229     $pri += 1e-5,
230     AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service
231     ];
232     }
233     $cv->end;
234     };
235    
236     # my (undef, undef, undef, undef, @ipv4) = gethostbyname $nodename;
237     #
238     # for (@ipv4) {
239     # push @res, [
240     # $pri,
241     # AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $_, $t || $DEFAULT_PORT,
242     # ];
243     # }
244     } else {
245     my ($host, $port) = AnyEvent::Socket::parse_hostport $t, "aemp=$DEFAULT_PORT"
246     or Carp::croak "$t: unparsable transport descriptor";
247    
248     $cv->begin;
249     AnyEvent::Socket::resolve_sockaddr $host, $port, "tcp", 0, undef, sub {
250     for (@_) {
251     my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3];
252     push @res, [
253     $pri += 1e-5,
254     AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service
255     ];
256     }
257     $cv->end;
258     }
259     }
260     }
261    
262     $cv->end;
263    
264     $cv
265     }
266    
267 root 1.16 sub initialise_node(@) {
268 root 1.12 my ($noderef, @others) = @_;
269 root 1.1
270 root 1.12 if ($noderef =~ /^slave\/(.*)$/) {
271 root 1.13 $SLAVE = AE::cv;
272 root 1.12 my $name = $1;
273 root 1.14 $name = $NODE unless length $name;
274 root 1.12 $noderef = AE::cv;
275     $noderef->send ("slave/$name");
276 root 1.13
277     @others
278     or Carp::croak "seed nodes must be specified for slave nodes";
279    
280 root 1.12 } else {
281 root 1.13 $PUBLIC = 1;
282 root 1.12 $noderef = resolve_node $noderef;
283     }
284    
285     @others = map $_->recv, map +(resolve_node $_), @others;
286 root 1.1
287 root 1.12 $NODE = $noderef->recv;
288 root 1.1
289     for my $t (split /,/, $NODE) {
290     $NODE{$t} = $NODE{""};
291    
292     my ($host, $port) = AnyEvent::Socket::parse_hostport $t;
293    
294     $LISTENER{$t} = AnyEvent::MP::Transport::mp_server $host, $port,
295     sub {
296     my ($tp) = @_;
297    
298 root 1.5 # TODO: urgs
299     my $node = add_node $tp->{remote_node};
300     $node->{trial}{accept} = $tp;
301 root 1.1 },
302     ;
303     }
304    
305 root 1.13 (add_node $_)->connect for @others;
306    
307     if ($SLAVE) {
308 root 1.18 my $timeout = AE::timer $CONNECT_TIMEOUT, 0, sub { $SLAVE->() };
309     $SLAVE->recv
310     or Carp::croak "AnyEvent::MP: unable to enter slave mode, unable to connect to a seednode.\n";
311 root 1.13 $SLAVE = 1;
312     }
313 root 1.1 }
314    
315     #############################################################################
316     # self node code
317    
318 root 1.15 sub load_func($) {
319     my $func = $_[0];
320    
321     unless (defined &$func) {
322     my $pkg = $func;
323     do {
324     $pkg =~ s/::[^:]+$//
325 root 1.18 or return sub { die "unable to resolve '$func'" };
326 root 1.15 eval "require $pkg";
327     } until defined &$func;
328     }
329    
330     \&$func
331     }
332    
333 root 1.5 our %node_req = (
334 root 1.13 # internal services
335    
336 root 1.5 # monitoring
337     mon0 => sub { # disable monitoring
338     my $portid = shift;
339     my $node = $SRCNODE;
340     $NODE{""}->unmonitor ($portid, delete $node->{rmon}{$portid});
341     },
342     mon1 => sub { # enable monitoring
343     my $portid = shift;
344     my $node = $SRCNODE;
345     $NODE{""}->monitor ($portid, $node->{rmon}{$portid} = sub {
346 root 1.8 $node->send (["", kil => $portid, @_]);
347 root 1.5 });
348     },
349 root 1.7 kil => sub {
350 root 1.8 my $cbs = delete $SRCNODE->{lmon}{+shift}
351 root 1.5 or return;
352    
353 root 1.8 $_->(@_) for @$cbs;
354 root 1.5 },
355 root 1.13 # node changed its name (for slave nodes)
356     iam => sub {
357     $SRCNODE->{noderef} = $_[0];
358     $NODE{$_[0]} = $SRCNODE;
359     },
360    
361     # public services
362 root 1.5
363     # relay message to another node / generic echo
364     relay => sub {
365     &snd;
366     },
367 root 1.15 relay_multiple => sub {
368     snd @$_ for @_
369     },
370 root 1.5
371     # random garbage
372     eval => sub {
373     my @res = eval shift;
374     snd @_, "$@", @res if @_;
375     },
376     time => sub {
377     snd @_, AE::time;
378     },
379     devnull => sub {
380     #
381     },
382     );
383    
384 root 1.9 $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self noderef => $NODE;
385 root 1.15 $PORT{""} = sub {
386     my $tag = shift;
387     eval { &{ $node_req{$tag} ||= load_func $tag } };
388     $WARN->("error processing node message: $@") if $@;
389     };
390 root 1.1
391 root 1.4 =back
392    
393 root 1.1 =head1 SEE ALSO
394    
395     L<AnyEvent::MP>.
396    
397     =head1 AUTHOR
398    
399     Marc Lehmann <schmorp@schmorp.de>
400     http://home.schmorp.de/
401    
402     =cut
403    
404     1
405