ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/MP/DataConn.pm
Revision: 1.4
Committed: Sun Nov 8 23:49:40 2009 UTC (14 years, 6 months ago) by root
Branch: MAIN
Changes since 1.3: +41 -13 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     AnyEvent::MP::DataConn - create socket connections between nodes
4    
5     =head1 SYNOPSIS
6    
7     use AnyEvent::MP::DataConn;
8    
9     =head1 DESCRIPTION
10    
11     This module can be used to create socket connections between the local and
12     a remote node in the aemp network. The socket can be used freely for any
13     purpose, and in most cases, this mechanism is a good way to transport big
14     chunks of binary data.
15    
16     The connections created by this module use the same security mechanisms
17     as normal AEMP connections (secure authentication, optional use of TLS),
18     and in fact, use the same listening port as AEMP connections, so when two
19     nodes can reach each other, they can, with high probability, creare a data
20     connection between them.
21    
22     The protocol is, however, not the AEMP transport protocol, so this will
23     only work between nodes implementing the "aemp-dataconn" protocol.
24    
25     =head1 FUNCTIONS
26    
27     =over 4
28    
29     =cut
30    
31     package AnyEvent::MP::DataConn;
32    
33     use common::sense;
34     use Carp ();
35     use POSIX ();
36    
37     use AnyEvent ();
38     use AnyEvent::Util ();
39    
40     use AnyEvent::MP;
41 root 1.2 use AnyEvent::MP::Kernel ();
42     use AnyEvent::MP::Global ();
43    
44     our $ID = "a";
45     our %STATE;
46    
47 root 1.3 # another node tells us to await a connection
48     sub _expect {
49 root 1.4 my ($id, $port, $timeout, $initspec) = @_;
50 root 1.2
51 root 1.3 $STATE{$id} = {
52     id => $id,
53     to => (AE::timer $timeout, 0, sub {
54 root 1.4 $STATE{$id}{done}(undef);
55 root 1.3 }),
56     done => sub {
57     my ($hdl, $error) = @_;
58    
59     %{delete $STATE{$id}} = ();
60 root 1.2
61 root 1.3 if (defined $hdl) {
62     my ($func, @args) = @$initspec;
63     (AnyEvent::MP::Kernel::load_func $func)->(@args, $hdl);
64 root 1.4 } else {
65     kil $port, AnyEvent::MP::DataConn:: => $error;
66 root 1.3 }
67 root 1.2 },
68     };
69     }
70 root 1.1
71 root 1.3 # AEMP::Transport call for dataconn-connections
72 root 1.1 sub _inject {
73     my ($conn, $error) = @_;
74 root 1.2
75 root 1.3 my $hdl = defined $error ? undef : delete $conn->{hdl};
76     my $id = $conn->{local_greeting}{dataconn_id} || $conn->{remote_greeting}{dataconn_id}
77     or return;
78    
79     $conn->destroy;
80    
81 root 1.4 ($STATE{$id} or return)->{done}($hdl, $error);
82 root 1.1 }
83    
84 root 1.3 # actively connect to some other node
85 root 1.2 sub _connect {
86 root 1.3 my ($id, $node) = @_;
87 root 1.2
88     my $state = $STATE{$id}
89     or return;
90    
91 root 1.3 my $addr = $AnyEvent::MP::Global::addr{$node};
92 root 1.2
93     @$addr
94 root 1.4 or return $state->{done}(undef, "$node: no listeners found");
95 root 1.2
96 root 1.3 # I love hardcoded constants !
97 root 1.2 $state->{next} = AE::timer 0, 2, sub {
98     my $endpoint = shift @$addr
99     or return delete $state->{next};
100    
101     my ($host, $port) = AnyEvent::Socket::parse_hostport $endpoint
102     or return;
103    
104     my $transport; $transport = AnyEvent::MP::Transport::mp_connect
105     $host, $port,
106     protocol => "aemp-dataconn",
107 root 1.3 local_greeting => { dataconn_id => $id },
108     sub { $transport->destroy }, #TODO: destroys handshaked conenctions too early
109 root 1.2 ;
110     };
111     }
112    
113     =item AnyEvent::MP::DataConn::connect_to $node, $timeout, [$initfunc, @initdata], $cb->($handle)
114 root 1.1
115     Creates a socket connection between the local node and the node C<$node>
116 root 1.4 (which can also be specified as a port). One of the nodes must have
117     listening ports ("binds").
118 root 1.1
119     When the connection could be successfully created, the C<$initfunc>
120     will be called with the given C<@initdata> on the remote node (similar
121     to C<snd_to_func> or C<spawn>), and the C<AnyEvent::Handle> object
122 root 1.2 representing the remote connection end as additional argument.
123 root 1.1
124 root 1.2 Also, the callback given as last argument will be called with the
125     AnyEvent::Handle object for the local side.
126 root 1.1
127 root 1.2 The AnyEvent::Handle objects will be in a "quiescent" state - you could rip
128 root 1.1 out the file handle and forget about it, but it is recommended to use it,
129 root 1.2 as the security settings might have called for a TLS connection. If you
130 root 1.1 opt to use it, you at least have to set an C<on_error> callback.
131    
132 root 1.2 In case of any error (timeout etc.), nothing will be called on
133     the remote side, and the local port will be C<kil>'ed with an C<<
134 root 1.1 AnyEvent::MP::DataConn => "error message" >> kill reason.
135    
136 root 1.4 The timeout should be large enough to cover at least four network
137     round-trips and one message round-trip.
138    
139     Example: on node1, establish a connection to node2 and send a line of text,
140     one node2, provide a receiver function.
141    
142     # node1, code executes in some port context
143     AnyEvent::MP::DataConn::connect_to "node2", 5, ["pkg::receiver", 1], sub {
144     my ($hdl) = @_;
145     warn "connection established, sending line.\n"
146     $hdl->push_write ("blabla\n")
147     };
148    
149     # node2
150     sub pkg::receiver {
151     my ($one, $hdl) = @_;
152     warn "connection established, wait for a line...\n"
153    
154     $hdl->push_read (line => sub {
155     warn "received a line: $_[1]\n";
156     undef $hdl;
157     });
158     }
159    
160 root 1.1 =cut
161    
162     sub connect_to($$$;@) {
163 root 1.4 my ($node, $timeout, $initspec, $cb) = @_;
164 root 1.1 my $port = $SELF;
165     $node = node_of $node;
166    
167 root 1.2 my $id = (++$ID) . "\@$NODE";
168 root 1.1
169 root 1.2 # damn, why do my simple state hashes resemble objects so quickly
170     my $state = $STATE{$id} = {
171 root 1.3 id => (++$ID) . "\@$NODE",
172     to => (AE::timer $timeout, 0, sub {
173 root 1.4 $STATE{$id}{done}(undef, "$node: unable to establish connection within $timeout seconds");
174 root 1.2 }),
175 root 1.3 done => sub {
176     my ($hdl, $error) = @_;
177    
178     delete $AnyEvent::MP::Global::ON_SETUP{$id};
179     %{delete $STATE{$id}} = ();
180    
181     if (defined $hdl) {
182 root 1.4 $cb->($hdl);
183 root 1.3 } else {
184     kil $port, AnyEvent::MP::DataConn:: => $error;
185     }
186 root 1.2 },
187 root 1.1 };
188    
189 root 1.2 if (AnyEvent::MP::Kernel::port_is_local $node) {
190 root 1.1 return kil $port, AnyEvent::MP::DataConn:: =>
191     "connect_to does not yet support local/local connections, please bug me about it";
192 root 1.2
193 root 1.1 } else {
194 root 1.4 AnyEvent::MP::Kernel::snd_to_func $node, AnyEvent::MP::DataConn::_expect:: => $id, $port, $timeout, $initspec;
195 root 1.2
196     $state->{wait} = sub {
197     if (my $addr = $AnyEvent::MP::Global::addr{$node}) {
198     delete $AnyEvent::MP::Global::ON_SETUP{$id};
199    
200     # continue connect
201     if (@$addr) {
202     # node has listeners, so connect
203 root 1.3 _connect $id, $node;
204 root 1.2 } else {
205     # no listeners, ask it to connect to us
206 root 1.4 AnyEvent::MP::Kernel::snd_to_func $node, AnyEvent::MP::DataConn::_connect:: => $id, $NODE;
207 root 1.2 }
208     } else {
209     # wait for the next global setup handshake
210 root 1.4 # due to the round-trip at the beginning, this should never be necessary
211 root 1.2 $AnyEvent::MP::Global::ON_SETUP{$id} = $state->{wait};
212     };
213 root 1.1 };
214 root 1.2
215 root 1.4 # we actually have to make sure that the connection arrives after the expect message, and
216     # the easiest way to do this is to use an rpc call.
217     AnyEvent::MP::Kernel::snd_on $node, port { $state->{wait}() };
218 root 1.1 }
219     }
220    
221     =back
222    
223     =head1 SEE ALSO
224    
225     L<AnyEvent::MP>.
226    
227     =head1 AUTHOR
228    
229     Marc Lehmann <schmorp@schmorp.de>
230     http://home.schmorp.de/
231    
232     =cut
233    
234     1
235