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 |
|
|
use AnyEvent::MP::Kernel; |
42 |
|
|
|
43 |
|
|
sub _inject { |
44 |
|
|
my ($conn, $error) = @_; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
=item AnyEvent::MP::DataConn::connect_to $node, $timeout, [$initfunc, @initdata], @localmsg |
48 |
|
|
|
49 |
|
|
Creates a socket connection between the local node and the node C<$node> |
50 |
|
|
(which can also be specified as a port). |
51 |
|
|
|
52 |
|
|
When the connection could be successfully created, the C<$initfunc> |
53 |
|
|
will be called with the given C<@initdata> on the remote node (similar |
54 |
|
|
to C<snd_to_func> or C<spawn>), and the C<AnyEvent::Handle> object |
55 |
|
|
representing the connection as additional argument. |
56 |
|
|
|
57 |
|
|
Also, the local port in which context C<connect_to> was called (C<$SELF>) |
58 |
|
|
will receive a message with contents C<@localmsg>, again with the |
59 |
|
|
AnyEvent::Handle object tacked to the end. |
60 |
|
|
|
61 |
|
|
The AnyEvent::Handle object will be in a "quiescent" state - you could rip |
62 |
|
|
out the file handle and forget about it, but it is recommended to use it, |
63 |
|
|
as the security settings might have called for a TLS connection. IF you |
64 |
|
|
opt to use it, you at least have to set an C<on_error> callback. |
65 |
|
|
|
66 |
|
|
In case of any error (or timeout), nothing will be called on the |
67 |
|
|
remote side, and the local port will be C<kil>'ed with an C<< |
68 |
|
|
AnyEvent::MP::DataConn => "error message" >> kill reason. |
69 |
|
|
|
70 |
|
|
=cut |
71 |
|
|
|
72 |
|
|
sub connect_to($$$;@) { |
73 |
|
|
my ($node, $timeout, $initspec, @localmsg) = @_; |
74 |
|
|
my $port = $SELF; |
75 |
|
|
$node = node_of $node; |
76 |
|
|
|
77 |
|
|
my %state; |
78 |
|
|
|
79 |
|
|
$state{to} = AE::timer $timeout, 0, sub { |
80 |
|
|
%state = (); |
81 |
|
|
kil $port, AnyEvent::MP::DataConn:: => "$node: unable to establish connection within $timeout seconds"; |
82 |
|
|
}; |
83 |
|
|
|
84 |
|
|
my $continue_connect = sub { |
85 |
|
|
use Data::Dumper; |
86 |
|
|
warn Dumper $NODE{$node};#d# |
87 |
|
|
}; |
88 |
|
|
|
89 |
|
|
if (port_is_local $node) { |
90 |
|
|
return kil $port, AnyEvent::MP::DataConn:: => |
91 |
|
|
"connect_to does not yet support local/local connections, please bug me about it"; |
92 |
|
|
} elsif (node_is_up $node) { |
93 |
|
|
$continue_connect->(); |
94 |
|
|
} else { |
95 |
|
|
$state{mon_nodes} = mon_nodes sub { |
96 |
|
|
return unless $_[0] eq $node && $_[1]; |
97 |
|
|
delete $state{mon_nodes}; |
98 |
|
|
$continue_connect->() |
99 |
|
|
}; |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
=back |
104 |
|
|
|
105 |
|
|
=head1 SEE ALSO |
106 |
|
|
|
107 |
|
|
L<AnyEvent::MP>. |
108 |
|
|
|
109 |
|
|
=head1 AUTHOR |
110 |
|
|
|
111 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
112 |
|
|
http://home.schmorp.de/ |
113 |
|
|
|
114 |
|
|
=cut |
115 |
|
|
|
116 |
|
|
1 |
117 |
|
|
|