ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/Base.pm
Revision: 1.6
Committed: Mon Aug 3 22:05:55 2009 UTC (14 years, 10 months ago) by root
Branch: MAIN
Changes since 1.5: +12 -5 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     our $VERSION = '0.01';
35 root 1.5 our @EXPORT = qw(
36     NODE $NODE snd del _any_
37     become_slave become_public
38     );
39 root 1.1
40     our $DEFAULT_SECRET;
41    
42     our $CONNECT_INTERVAL = 5; # new connect every 5s, at least
43     our $CONNECT_TIMEOUT = 30; # includes handshake
44    
45 root 1.4 =item $AnyEvent::MP::Base::WARN
46    
47     This value is called with an error or warning message, when e.g. a connection
48     could not be created, authorisation failed and so on.
49    
50     The default simply logs the message to STDERR.
51    
52     =cut
53    
54 root 1.3 our $WARN = sub {
55     warn "$_[0]\n";
56     };
57    
58 root 1.1 sub nonce($) {
59     my $nonce;
60    
61     if (open my $fh, "</dev/urandom") {
62     sysread $fh, $nonce, $_[0];
63     } else {
64     # shit...
65     our $nonce_init;
66     unless ($nonce_init++) {
67     srand time ^ $$ ^ unpack "%L*", qx"ps -edalf" . qx"ipconfig /all";
68     }
69    
70     $nonce = join "", map +(chr rand 256), 1 .. $_[0]
71     }
72    
73     $nonce
74     }
75    
76     sub default_secret {
77     unless (defined $DEFAULT_SECRET) {
78     if (open my $fh, "<$ENV{HOME}/.aemp-secret") {
79     sysread $fh, $DEFAULT_SECRET, -s $fh;
80     } else {
81 root 1.2 $DEFAULT_SECRET = nonce 32;
82 root 1.1 }
83     }
84    
85     $DEFAULT_SECRET
86     }
87    
88 root 1.6 sub gen_uniq {
89     my $uniq = pack "wN", $$, time;
90     $uniq = MIME::Base64::encode_base64 $uniq, "";
91     $uniq =~ s/=+$//;
92     $uniq
93     }
94    
95     our $UNIQ = gen_uniq; # per-process/node unique cookie
96 root 1.1 our $ID = "a";
97     our $PUBLIC = 0;
98     our $NODE = $$;
99     our $PORT;
100    
101     our %NODE; # node id to transport mapping, or "undef", for local node
102     our %PORT; # local ports
103 root 1.5
104     our %RMON; # local ports monitored by remote nodes ($RMON{noderef}{portid} == cb)
105     our %LMON; # monitored _local_ ports
106    
107 root 1.1 our %WKP;
108     our %LISTENER; # local transports
109    
110 root 1.5 our $SRCNODE; # holds the sending node during _inject
111    
112 root 1.1 sub NODE() { $NODE }
113    
114     sub _ANY_() { 1 }
115     sub _any_() { \&_ANY_ }
116    
117     sub _inject {
118 root 1.5 &{ $PORT{+shift} or return };
119 root 1.1 }
120    
121     sub add_node {
122     my ($noderef) = @_;
123    
124     return $NODE{$noderef}
125     if exists $NODE{$noderef};
126    
127     for (split /,/, $noderef) {
128     return $NODE{$noderef} = $NODE{$_}
129     if exists $NODE{$_};
130     }
131    
132     # for indirect sends, use a different class
133     my $node = new AnyEvent::MP::Node::Direct $noderef;
134    
135     $NODE{$_} = $node
136     for $noderef, split /,/, $noderef;
137    
138     $node
139     }
140    
141     sub snd(@) {
142     my ($noderef, $port) = split /#/, shift, 2;
143    
144 root 1.5 ($NODE{$noderef} || add_node $noderef)
145     ->send ([$port, @_]);
146     }
147    
148     sub del($) {
149     my ($noderef, $port) = split /#/, shift, 2;
150    
151     delete $PORT{$port};
152    
153     my $mon = delete $LMON{$port}
154     or return;
155 root 1.1
156 root 1.5 $_->() for values %$mon;
157 root 1.1 }
158    
159     sub become_public {
160     return if $PUBLIC;
161    
162     my $noderef = join ",", @_;
163     my @args = @_;
164    
165     $NODE = (AnyEvent::MP::Node::normalise_noderef $noderef)->recv;
166    
167     for my $t (split /,/, $NODE) {
168     $NODE{$t} = $NODE{""};
169    
170     my ($host, $port) = AnyEvent::Socket::parse_hostport $t;
171    
172     $LISTENER{$t} = AnyEvent::MP::Transport::mp_server $host, $port,
173     @args,
174     sub {
175     my ($tp) = @_;
176    
177 root 1.5 # TODO: urgs
178     my $node = add_node $tp->{remote_node};
179     $node->{trial}{accept} = $tp;
180 root 1.1 },
181     ;
182     }
183    
184     $PUBLIC = 1;
185     }
186    
187     #############################################################################
188     # self node code
189    
190 root 1.5 our %node_req = (
191     # monitoring
192     mon0 => sub { # disable monitoring
193     my $portid = shift;
194     my $node = $SRCNODE;
195     $NODE{""}->unmonitor ($portid, delete $node->{rmon}{$portid});
196     },
197     mon1 => sub { # enable monitoring
198     my $portid = shift;
199     my $node = $SRCNODE;
200     $NODE{""}->monitor ($portid, $node->{rmon}{$portid} = sub {
201     $node->send (["", exit => $portid]);
202     });
203     },
204     exit => sub {
205     my $cbs = delete $SRCNODE->{lmon}{$_[0]}
206     or return;
207    
208     $_->() for @$cbs;
209     },
210    
211     # well-known-port lookup
212     wkp => sub {
213     my $wkname = shift;
214     snd @_, $WKP{$wkname};
215     },
216    
217     # relay message to another node / generic echo
218     relay => sub {
219     &snd;
220     },
221    
222     # random garbage
223     eval => sub {
224     my @res = eval shift;
225     snd @_, "$@", @res if @_;
226     },
227     time => sub {
228     snd @_, AE::time;
229     },
230     devnull => sub {
231     #
232     },
233     );
234    
235 root 1.1 $NODE{""} = new AnyEvent::MP::Node::Self noderef => $NODE;
236 root 1.5 $PORT{""} = sub { &{ $node_req{+shift} or return } };
237 root 1.1
238 root 1.4 =back
239    
240 root 1.1 =head1 SEE ALSO
241    
242     L<AnyEvent::MP>.
243    
244     =head1 AUTHOR
245    
246     Marc Lehmann <schmorp@schmorp.de>
247     http://home.schmorp.de/
248    
249     =cut
250    
251     1
252