Net-Async-Tangence

 view release on metacpan or  search on metacpan

lib/Net/Async/Tangence/Client.pm  view on Meta::CPAN


   $rootobj = $client->connect_url( $url, %args )->get

Connects to a C<Tangence> server at the given URL. The returned L<Future> will
yield the root object proxy once it has been obtained.

Takes the following named arguments:

=over 8

=item on_registry => CODE

=item on_root => CODE

Invoked once the registry and root object proxies have been obtained from the
server. See the documentation the L<Tangence::Client> C<tangence_connected>
method.

=item family => STRING

Optional. May be set to C<inet4> or C<inet6> to force IPv4 or IPv6 if
relevant. Ignored by C<exec:> and C<unix:> schemes.

=back

The following URL schemes are recognised:

=over 4

=cut

sub connect_url
{
   my $self = shift;
   my ( $url, %args ) = @_;

   my $uri = ( blessed $url && $url->isa( "URI" ) ) ? $url : URI->new( $url );

   my $scheme = $uri->scheme;

   if( $scheme =~ m/\+/ ) {
      $scheme =~ s/^circle\+// or croak "Found a + within URL scheme that is not 'circle+'";
   }

   # Legacy name
   $scheme = "sshexec" if $scheme eq "ssh";

   my $authority = $uri->authority;

   my $path      = $uri->path;
   # Path will start with a leading /; we need to trim that
   $path =~ s{^/}{};

   my $query     = $uri->query;
   defined $query or $query = "";

   my $f;

   if( $scheme eq "exec" ) {
      # $query will contain args to exec - split them on +
      $f = $self->connect_exec( [ $path, split m/\+/, $query ], %args );
   }
   elsif( $scheme eq "tcp" ) {
      $f = $self->connect_tcp( $authority, %args );
   }
   elsif( $scheme eq "unix" ) {
      $f = $self->connect_unix( $path, %args );
   }
   else {
      my $connectorpkg = "Net::Async::Tangence::Client::via::$scheme";
      ( my $connectorfile = "$connectorpkg.pm" ) =~ s{::}{/}g;
      if( eval { require $connectorfile } and
            my $code = $connectorpkg->can( 'connect' ) ) {
         $f = $code->( $self, $uri, %args );
      }
      else {
         croak "Unrecognised URL scheme name '$scheme'";
      }
   }

   return $f->then( sub {
      my $on_root = $args{on_root};

      my $root_f = $self->new_future;

      $self->tangence_connected( %args,
         on_root => sub {
            my ( $root ) = @_;

            $on_root->( $root ) if $on_root;
            $root_f->done( $root );
         },
      );

      $root_f;
   });
}

=item * exec

Directly executes the server as a child process. This is largely provided for
testing purposes, as the server will only run for this one client; it will
exit when the client disconnects.

 exec:///path/to/command?with+arguments

The URL's path should point to the required command, and the query string will
be split on C<+> signs and used as the arguments. The authority section of the
URL will be ignored, so may be left empty.

=cut

sub connect_exec
{
   my $self = shift;
   my ( $command ) = @_;

   my $loop = $self->get_loop;

   pipe( my $myread, my $childwrite ) or croak "Cannot pipe - $!";
   pipe( my $childread, my $mywrite ) or croak "Cannoe pipe - $!";



( run in 0.894 second using v1.01-cache-2.11-cpan-71847e10f99 )