Commit | Line | Data |
20038dd8 |
1 | package App::Clifton::ConsoleService; |
2 | |
cd780129 |
3 | use Log::Contextual qw(:log); |
4 | use aliased 'App::Clifton::ConsoleService::Session'; |
5 | use IO::Async::Listener; |
6 | use IO::Socket::UNIX; |
20038dd8 |
7 | use Moo; |
8 | |
9 | extends 'App::Clifton::Service'; |
10 | |
cd780129 |
11 | has socket_location => (is => 'ro', default => sub { 'clifton.sock' }); |
12 | |
13 | has $_ => (is => 'lazy') for qw(listener session socket); |
14 | |
21a18863 |
15 | has server => (is => 'ro', required => 1, weak_ref => 1); |
16 | |
cd780129 |
17 | sub _build_socket { |
18 | my ($self) = @_; |
19 | my $location = $self->socket_location; |
20 | unlink($location) if -e $location; |
21 | IO::Socket::UNIX->new( |
22 | Local => $location, |
23 | Listen => 1, |
24 | ) or die "Cannot make UNIX socket - $!\n"; |
25 | } |
26 | |
27 | sub _build_listener { |
28 | my ($self) = @_; |
29 | $self->_new_child('IO::Async::Listener', { |
30 | on_stream => $self->_replace_weakself('_open_connection'), |
31 | handle => $self->socket, |
32 | }); |
33 | } |
34 | |
35 | sub _build_session { |
36 | my ($self) = @_; |
37 | Session->new->${\sub { |
38 | my ($session) = @_; |
21a18863 |
39 | # no need to weaken this since server is already weak |
40 | $session->lex_env->lexicals->{'$_SERVER'} = \$self->server; |
cd780129 |
41 | $session; |
42 | }}; |
43 | } |
44 | |
45 | sub BUILD { |
46 | my ($self) = @_; |
47 | $self->$_ for qw(listener session); |
48 | } |
49 | |
50 | sub _open_connection { |
51 | my ($self, $stream) = @_; |
52 | log_debug { "Connection opened" }; |
53 | $self->session->configure(transport => $stream); |
54 | $self->add_child($self->session); |
55 | } |
56 | |
20038dd8 |
57 | 1; |