ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Base.pm
Revision: 1.12
Committed: Wed Aug 5 19:55:58 2009 UTC (14 years, 10 months ago) by root
Branch: MAIN
Changes since 1.11: +14 -8 lines
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.01';
35 our @EXPORT = qw(
36 %NODE %PORT %PORT_DATA %REG $UNIQ $ID add_node
37
38 NODE $NODE node_of snd kil _any_
39 resolve_node initialise_node
40 );
41
42 our $DEFAULT_SECRET;
43 our $DEFAULT_PORT = "4040";
44
45 our $CONNECT_INTERVAL = 5; # new connect every 5s, at least
46 our $CONNECT_TIMEOUT = 30; # includes handshake
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 default_secret {
82 unless (defined $DEFAULT_SECRET) {
83 if (open my $fh, "<$ENV{HOME}/.aemp-secret") {
84 sysread $fh, $DEFAULT_SECRET, -s $fh;
85 } else {
86 $DEFAULT_SECRET = nonce 32;
87 }
88 }
89
90 $DEFAULT_SECRET
91 }
92
93 sub gen_uniq {
94 my $uniq = pack "wN", $$, time;
95 $uniq = MIME::Base64::encode_base64 $uniq, "";
96 $uniq =~ s/=+$//;
97 $uniq
98 }
99
100 our $UNIQ = gen_uniq; # per-process/node unique cookie
101 our $ID = "a";
102 our $PUBLIC = 0;
103 our $NODE = unpack "H*", nonce 16;
104
105 our %NODE; # node id to transport mapping, or "undef", for local node
106 our (%PORT, %PORT_DATA); # local ports
107
108 our %RMON; # local ports monitored by remote nodes ($RMON{noderef}{portid} == cb)
109 our %LMON; # monitored _local_ ports
110
111 our %REG; # registered port names
112
113 our %LISTENER;
114
115 our $SRCNODE; # holds the sending node during _inject
116
117 sub NODE() {
118 $NODE
119 }
120
121 sub node_of($) {
122 my ($noderef, undef) = split /#/, $_[0], 2;
123
124 $noderef
125 }
126
127 sub _ANY_() { 1 }
128 sub _any_() { \&_ANY_ }
129
130 sub _inject {
131 &{ $PORT{+shift} or return };
132 }
133
134 sub add_node {
135 my ($noderef) = @_;
136
137 return $NODE{$noderef}
138 if exists $NODE{$noderef};
139
140 for (split /,/, $noderef) {
141 return $NODE{$noderef} = $NODE{$_}
142 if exists $NODE{$_};
143 }
144
145 # new node, check validity
146
147 for (split /,/, $noderef) {
148 my ($host, $port) = AnyEvent::Socket::parse_hostport $_
149 or Carp::croak "$noderef: not a resolved node reference ('$_' not parsable)";
150
151 $port > 0
152 or Carp::croak "$noderef: not a resolved node reference ('$_' contains non-numeric port)";
153
154 AnyEvent::Socket::parse_address $host
155 or Carp::croak "$noderef: not a resolved node reference ('$_' contains unresolved address)";
156 }
157
158 # TODO: for indirect sends, use a different class
159 my $node = new AnyEvent::MP::Node::Direct $noderef;
160
161 $NODE{$_} = $node
162 for $noderef, split /,/, $noderef;
163
164 $node
165 }
166
167 sub snd(@) {
168 my ($noderef, $port) = split /#/, shift, 2;
169
170 ($NODE{$noderef} || add_node $noderef)
171 ->send ([$port, @_]);
172 }
173
174 sub kil(@) {
175 my ($noderef, $port) = split /#/, shift, 2;
176
177 length $port or Carp::cluck "yuk\n";#d#
178 length $port
179 or Carp::croak "$noderef: killing a node port is not allowed, caught";
180
181 ($NODE{$noderef} || add_node $noderef)
182 ->kill ($port, @_);
183 }
184
185 sub resolve_node($) {
186 my ($noderef) = @_;
187
188 my $cv = AE::cv;
189 my @res;
190
191 $cv->begin (sub {
192 my %seen;
193 my @refs;
194 for (sort { $a->[0] <=> $b->[0] } @res) {
195 push @refs, $_->[1] unless $seen{$_->[1]}++
196 }
197 shift->send (join ",", @refs);
198 });
199
200 $noderef = $DEFAULT_PORT unless length $noderef;
201
202 my $idx;
203 for my $t (split /,/, $noderef) {
204 my $pri = ++$idx;
205
206 if ($t =~ /^\d*$/) {
207 require POSIX;
208 my $nodename = (POSIX::uname ())[1];
209
210 $cv->begin;
211 AnyEvent::Socket::resolve_sockaddr $nodename, $t || "aemp=$DEFAULT_PORT", "tcp", 0, undef, sub {
212 for (@_) {
213 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3];
214 push @res, [
215 $pri += 1e-5,
216 AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service
217 ];
218 }
219 $cv->end;
220 };
221
222 # my (undef, undef, undef, undef, @ipv4) = gethostbyname $nodename;
223 #
224 # for (@ipv4) {
225 # push @res, [
226 # $pri,
227 # AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $_, $t || $DEFAULT_PORT,
228 # ];
229 # }
230 } else {
231 my ($host, $port) = AnyEvent::Socket::parse_hostport $t, "aemp=$DEFAULT_PORT"
232 or Carp::croak "$t: unparsable transport descriptor";
233
234 $cv->begin;
235 AnyEvent::Socket::resolve_sockaddr $host, $port, "tcp", 0, undef, sub {
236 for (@_) {
237 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr $_->[3];
238 push @res, [
239 $pri += 1e-5,
240 AnyEvent::Socket::format_hostport AnyEvent::Socket::format_address $host, $service
241 ];
242 }
243 $cv->end;
244 }
245 }
246 }
247
248 $cv->end;
249
250 $cv
251 }
252
253 sub initialise_node($;@) {
254 my ($noderef, @others) = @_;
255
256 if ($noderef =~ /^slave\/(.*)$/) {
257 my $name = $1;
258 $name = $UNIQ unless length $name;
259 $noderef = AE::cv;
260 $noderef->send ("slave/$name");
261 } else {
262 $noderef = resolve_node $noderef;
263 }
264
265 @others = map $_->recv, map +(resolve_node $_), @others;
266
267 $NODE = $noderef->recv;
268
269 for my $t (split /,/, $NODE) {
270 $NODE{$t} = $NODE{""};
271
272 my ($host, $port) = AnyEvent::Socket::parse_hostport $t;
273
274 $LISTENER{$t} = AnyEvent::MP::Transport::mp_server $host, $port,
275 sub {
276 my ($tp) = @_;
277
278 # TODO: urgs
279 my $node = add_node $tp->{remote_node};
280 $node->{trial}{accept} = $tp;
281 },
282 ;
283 }
284
285 $PUBLIC = 1;
286 }
287
288 #############################################################################
289 # self node code
290
291 our %node_req = (
292 # monitoring
293 mon0 => sub { # disable monitoring
294 my $portid = shift;
295 my $node = $SRCNODE;
296 $NODE{""}->unmonitor ($portid, delete $node->{rmon}{$portid});
297 },
298 mon1 => sub { # enable monitoring
299 my $portid = shift;
300 my $node = $SRCNODE;
301 $NODE{""}->monitor ($portid, $node->{rmon}{$portid} = sub {
302 $node->send (["", kil => $portid, @_]);
303 });
304 },
305 kil => sub {
306 my $cbs = delete $SRCNODE->{lmon}{+shift}
307 or return;
308
309 $_->(@_) for @$cbs;
310 },
311
312 # well-known-port lookup
313 lookup => sub {
314 my $name = shift;
315 my $port = $REG{$name};
316 #TODO: check vailidity
317 snd @_, $port;
318 },
319
320 # relay message to another node / generic echo
321 relay => sub {
322 &snd;
323 },
324
325 # random garbage
326 eval => sub {
327 my @res = eval shift;
328 snd @_, "$@", @res if @_;
329 },
330 time => sub {
331 snd @_, AE::time;
332 },
333 devnull => sub {
334 #
335 },
336 );
337
338 $NODE{""} = $NODE{$NODE} = new AnyEvent::MP::Node::Self noderef => $NODE;
339 $PORT{""} = sub { &{ $node_req{+shift} or return } };
340
341 =back
342
343 =head1 SEE ALSO
344
345 L<AnyEvent::MP>.
346
347 =head1 AUTHOR
348
349 Marc Lehmann <schmorp@schmorp.de>
350 http://home.schmorp.de/
351
352 =cut
353
354 1
355