1 package Object::Remote::ConnectionServer;
3 use Scalar::Util qw(blessed weaken);
4 use Module::Runtime qw(use_module);
6 use Object::Remote::Logging qw( :log :dlog );
14 return $_[0] if blessed($_[0]);
16 IO::Socket::UNIX->new(
19 ) or die "Couldn't liten to $_[0]: $!";
23 log_debug { "adding connection server to run loop because the trigger has executed" };
25 Object::Remote->current_loop
28 on_read_ready => sub { $self->_listen_ready($fh) }
33 has connection_args => (
34 is => 'ro', default => sub { [] }
37 has connection_callback => (
38 is => 'ro', default => sub { sub { shift } }
42 log_debug { "A connection server has been built; calling want_run on run loop" };
43 Object::Remote->current_loop->want_run;
47 log_debug { "Connection server is calling run_while_wanted on the run loop" };
48 Object::Remote->current_loop->run_while_wanted;
53 log_debug { "Got a connection, calling accept on the file handle" };
54 my $new = $fh->accept or die "Couldn't accept: $!";
55 log_trace { "Setting file handle non-blocking" };
58 log_trace { "Creating a new connection with the remote node" };
59 my $c = use_module('Object::Remote::Connection')->new(
60 receive_from_fh => $new,
62 on_close => $f, # and so will die $c
63 @{$self->connection_args}
64 )->${\$self->connection_callback};
65 $f->on_ready(sub { undef($c) });
66 log_trace { "marking the future as done" };
67 $c->ready_future->done;
68 Dlog_trace { "Sending 'Shere' to socket $_" } $new;
69 print $new "Shere\n" or die "Couldn't send to new socket: $!";
70 log_debug { "Connection has been fully handled" };
76 log_debug { "A connection server is being destroyed; global destruction: '$gd'" };
78 log_trace { "Removing the connection server IO watcher from run loop" };
79 Object::Remote->current_loop
81 handle => $self->listen_on,
84 if ($self->listen_on->can('hostpath')) {
85 log_debug { my $p = $self->listen_on->hostpath; "Removing '$p' from the filesystem" };
86 unlink($self->listen_on->hostpath);
88 log_trace { "calling want_stop on the run loop" };
89 Object::Remote->current_loop->want_stop;