ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/lib/AnyEvent/MCP/Server/Connect.pm
Revision: 1.2
Committed: Fri Oct 16 08:15:32 2026 UTC (4 weeks, 5 days ago) by root
Branch: MAIN
Changes since 1.1: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     AnyEvent::MCP::Server::Connect - reverse-connecting MCP JSONL server
4    
5     =head1 DESCRIPTION
6    
7     This JSONL server connects to a multibroker's HTTP/extension listener using
8     C<connect =E<gt> "http://host:port/token/alias">. It sends
9     C<["mcp-connect", token, alias]> and then ordinary MCP JSONL messages.
10     The TCP initiator remains the MCP server; the multibroker is the MCP client.
11    
12     =head1 METHODS
13    
14     =over
15    
16     =item $server->start
17    
18     Attempt a connection immediately. Retry failures every second, up to sixty
19     attempts, and start a new retry period after an established connection closes.
20     Exhaustion closes the server. Outstanding requests are cancelled, not replayed.
21    
22     =item $server->close
23    
24     Cancel pending connections and retries, close streams and cancel requests.
25     Connecting remotely grants no managed child-shutdown authority.
26    
27     =back
28    
29     =cut
30    
31     package AnyEvent::MCP::Server::Connect;
32    
33     use common::sense;
34     use parent "AnyEvent::MCP::Server::JSONL";
35     use AnyEvent::MCP::Extension ();
36     use AnyEvent::Socket ();
37     use AnyEvent ();
38     use JSON::XS ();
39     use Scalar::Util qw(weaken);
40    
41     sub new {
42     my ($class, %kv) = @_;
43    
44     my $self = $class->SUPER::new (%kv);
45     $self->{endpoint} = [AnyEvent::MCP::Extension::parse_url $self->{connect}];
46     my $on_close = $self->{on_connection_close};
47     weaken (my $weak = $self);
48     $self->{on_connection_close} = sub {
49     $on_close->(@_) if $on_close;
50     $weak->start if $weak && !$weak->{_closed};
51     };
52    
53     $self
54     }
55    
56     sub start {
57     my ($self) = @_;
58    
59     return if $self->{retry};
60     delete $self->{_closed};
61     $self->{attempts} = 0;
62     weaken $self;
63     $self->{retry} = AE::timer 0, 1, sub {
64     return unless $self;
65     return if $self->{connector};
66    
67     if (++$self->{attempts} > 60) {
68     AE::log warn => "MCP reverse connection: giving up after sixty attempts";
69     $self->close;
70     return
71     }
72    
73     my ($host, $port, $token, $alias) = @{$self->{endpoint}};
74     $self->{connector} = AnyEvent::Socket::tcp_connect $host, $port, sub {
75     delete $self->{connector} if $self;
76     my ($fh) = @_;
77     return unless $self && !$self->{_closed};
78    
79     unless ($fh) {
80 root 1.2 AE::log debug => "MCP reverse connection '$alias': $!";
81 root 1.1 return
82     }
83    
84     delete $self->{retry};
85     my $handle = $self->accept ($fh);
86     $handle->push_write (JSON::XS::encode_json (["mcp-connect", $token, $alias]) . "\n");
87     }, sub { 1 };
88     };
89     }
90    
91     sub request_idle_exit {
92     undef
93     }
94    
95     sub close {
96     my ($self) = @_;
97    
98     delete @$self{qw(retry connector)};
99     $self->SUPER::close;
100     }
101    
102     1;