ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Base.pm
Revision: 1.21
Committed: Wed Aug 12 21:39:58 2009 UTC (14 years, 9 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.20: +0 -0 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

# Content
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 =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 =head1 GLOBALS
16
17 =over 4
18
19 =cut
20
21 package AnyEvent::MP::Base;
22
23 use common::sense;
24 use Carp ();
25 use MIME::Base64 ();
26
27 use AE ();
28
29 use AnyEvent::MP::Node;
30 use AnyEvent::MP::Transport;
31
32 use base "Exporter";
33
34 our $VERSION = '0.4';
35 our @EXPORT = qw(
36 %NODE %PORT %PORT_DATA %REG $UNIQ $RUNIQ $ID add_node load_func
37
38 NODE $NODE node_of snd kil _any_
39 resolve_node initialise_node
40 );
41
42 our $DEFAULT_PORT = "4040";
43
44 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
48 =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 our $WARN = sub {
58 my $msg = $_[0];
59 $msg =~ s/\n$//;
60 warn "$msg\n";
61 };
62
63 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 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 sub gen_uniq {
100 asciibits pack "wNa*", $$, time, nonce 2
101 }
102
103 our $PUBLIC = 0;
104 our $SLAVE = 0;
105
106 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 our %NODE; # node id to transport mapping, or "undef", for local node
112 our (%PORT, %PORT_DATA); # local ports
113
114 our %RMON; # local ports monitored by remote nodes ($RMON{noderef}{portid} == cb)
115 our %LMON; # monitored _local_ ports
116
117 our %LISTENER;
118
119 our $SRCNODE; # holds the sending node during _inject
120
121 sub NODE() {
122 $NODE
123 }
124
125 sub node_of($) {
126 my ($noderef, undef) = split /#/, $_[0], 2;
127
128 $noderef
129 }
130
131 sub _ANY_() { 1 }
132 sub _any_() { \&_ANY_ }
133
134 sub TRACE() { 0 }
135
136 sub _inject {
137 warn "RCV $SRCNODE->{noderef} -> @_\n" if TRACE;#d#
138 &{ $PORT{+shift} or return };
139 }
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 # new node, check validity
153 my $node;
154
155 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
166 AnyEvent::Socket::parse_address $host
167 or Carp::croak "$noderef: not a resolved node reference ('$_' contains unresolved address)";
168 }
169
170 # TODO: for indirect sends, use a different class
171 $node = new AnyEvent::MP::Node::Direct $noderef;
172 }
173
174 $NODE{$_} = $node
175 for $noderef, split /,/, $noderef;
176
177 $node
178 }
179
180 sub snd(@) {
181 my ($noderef, $portid) = split /#/, shift, 2;
182
183 warn "SND $noderef <- $portid @_\n" if TRACE;#d#
184
185 ($NODE{$noderef} || add_node $noderef)
186 ->send (["$portid", @_]);
187 }
188
189 sub kil(@) {
190 my ($noderef, $portid) = split /#/, shift, 2;
191
192 length $portid
193 or Carp::croak "$noderef#$portid: killing a node port is not allowed, caught";
194
195 ($NODE{$noderef} || add_node $noderef)
196 ->kill ("$portid", @_);
197 }
198
199 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 sub initialise_node(@) {
268 my ($noderef, @others) = @_;
269
270 if ($noderef =~ /^slave\/(.*)$/) {
271 $SLAVE = AE::cv;
272 my $name = $1;
273 $name = $NODE unless length $name;
274 $noderef = AE::cv;
275 $noderef->send ("slave/$name");
276
277 @others
278 or Carp::croak "seed nodes must be specified for slave nodes";
279
280 } else {
281 $PUBLIC = 1;
282 $noderef = resolve_node $noderef;
283 }
284
285 @others = map $_->recv, map +(resolve_node $_), @others;
286
287 $NODE = $noderef->recv;
288
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 # TODO: urgs
299 my $node = add_node $tp->{remote_node};
300 $node->{trial}{accept} = $tp;
301 },
302 ;
303 }
304
305 (add_node $_)->connect for @others;
306
307 if ($SLAVE) {
308 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 $SLAVE = 1;
312 }
313 }
314
315 #############################################################################
316 # self node code
317
318 sub load_func($) {
319 my $func = $_[0];
320
321 unless (defined &$func) {
322 my $pkg = $func;
323 do {
324 $pkg =~ s/::[^:]+$//
325 or return sub { die "unable to resolve '$func'" };
326 eval "require $pkg";
327 } until defined &$func;
328 }
329
330 \&$func
331 }
332
333 our %node_req = (
334 # internal services
335
336 # 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 $node->send (["", kil => $portid, @_]);
347 });
348 },
349 kil => sub {
350 my $cbs = delete $SRCNODE->{lmon}{+shift}
351 or return;
352
353 $_->(@_) for @$cbs;
354 },
355 # node changed its name (for slave nodes)
356 iam => sub {
357 $SRCNODE->{noderef} = $_[0];
358 $NODE{$_[0]} = $SRCNODE;
359 },
360
361 # public services
362
363 # relay message to another node / generic echo
364 relay => sub {
365 &snd;
366 },
367 relay_multiple => sub {
368 snd @$_ for @_
369 },
370
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 $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self noderef => $NODE;
385 $PORT{""} = sub {
386 my $tag = shift;
387 eval { &{ $node_req{$tag} ||= load_func $tag } };
388 $WARN->("error processing node message: $@") if $@;
389 };
390
391 =back
392
393 =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