| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2010-2020 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Net::Async::Tangence::Server 0.16; | ||||||||||
| 7 | |||||||||||
| 8 | 7 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 7 | ▶ | use IO::Async::Listener '0.36'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 7 | ▶ | use base qw( IO::Async::Listener ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 7 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 7 | ▶ | use Net::Async::Tangence::ServerProtocol; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | C<Net::Async::Tangence::Server> - serve C<Tangence> clients using C<IO::Async> | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 24 | This subclass of L<IO::Async::Listener> accepts L<Tangence> client | ||||||||||
| 25 | connections. | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | =head1 PARAMETERS | ||||||||||
| 30 | |||||||||||
| 31 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||
| 32 | |||||||||||
| 33 | =over 8 | ||||||||||
| 34 | |||||||||||
| 35 | =item registry => Tangence::Registry | ||||||||||
| 36 | |||||||||||
| 37 | The L<Tangence::Registry> for the server's objects. | ||||||||||
| 38 | |||||||||||
| 39 | =back | ||||||||||
| 40 | |||||||||||
| 41 | =cut | ||||||||||
| 42 | |||||||||||
| 43 | sub _init | ||||||||||
| 44 | 6 | ▶ | { | ||||||||
|
Subroutine
_init: called CC=2 SCAR=6.9
| |||||||||||
| 45 | 6 | my $self = shift; | |||||||||
| 46 | 6 | my ( $params ) = @_; | |||||||||
| 47 | |||||||||||
| 48 | 7 | ▶ | $params->{handle_constructor} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 7 | my $self = shift; | |||||||||
| 50 | |||||||||||
| 51 | return Net::Async::Tangence::ServerProtocol->new( | ||||||||||
| 52 | registry => $self->{registry}, | ||||||||||
| 53 | 2 | ▶ | on_closed => $self->_capture_weakself( sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 2 | my $self = shift; | |||||||||
| 55 | 2 | $self->remove_child( $_[0] ); | |||||||||
| 56 | 7 | } ), | |||||||||
| 57 | ); | ||||||||||
| 58 | 6 | }; | |||||||||
| 59 | |||||||||||
| 60 | 6 | $self->SUPER::_init( $params ); | |||||||||
| 61 | |||||||||||
| 62 | 6 | ▶ | $self->{registry} = delete $params->{registry} if exists $params->{registry}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | sub on_accept | ||||||||||
| 66 | 7 | ▶ | { | ||||||||
|
Subroutine
on_accept: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 67 | 7 | my $self = shift; | |||||||||
| 68 | 7 | my ( $conn ) = @_; | |||||||||
| 69 | |||||||||||
| 70 | 7 | $self->add_child( $conn ); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | # Useful for testing | ||||||||||
| 74 | sub make_new_connection | ||||||||||
| 75 | 4 | ▶ | { | ||||||||
|
Subroutine
make_new_connection: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 76 | 4 | my $self = shift; | |||||||||
| 77 | 4 | my ( $sock ) = @_; | |||||||||
| 78 | |||||||||||
| 79 | # Mass cheating | ||||||||||
| 80 | 4 | my $conn = $self->{handle_constructor}->( $self ); | |||||||||
| 81 | |||||||||||
| 82 | 4 | $conn->configure( handle => $sock ); | |||||||||
| 83 | 4 | $self->on_accept( $conn ); | |||||||||
| 84 | |||||||||||
| 85 | 4 | return $conn; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | # More testing utilities | ||||||||||
| 89 | sub accept_stdio | ||||||||||
| 90 | 1 | ▶ | { | ||||||||
|
Subroutine
accept_stdio: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 91 | 1 | my $self = shift; | |||||||||
| 92 | |||||||||||
| 93 | 1 | my $conn = $self->{handle_constructor}->( $self ); | |||||||||
| 94 | |||||||||||
| 95 | 1 | $conn->configure( | |||||||||
| 96 | read_handle => \*STDIN, | ||||||||||
| 97 | write_handle => \*STDOUT, | ||||||||||
| 98 | ); | ||||||||||
| 99 | 1 | $self->on_accept( $conn ); | |||||||||
| 100 | |||||||||||
| 101 | 1 | return $conn; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | =head1 OVERRIDEABLE METHODS | ||||||||||
| 105 | |||||||||||
| 106 | The following methods are provided but intended to be overridden if the | ||||||||||
| 107 | implementing class wishes to provide different behaviour from the default. | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | =head2 conn_rootobj | ||||||||||
| 112 | |||||||||||
| 113 | $rootobj = $server->conn_rootobj( $conn, $identity ) | ||||||||||
| 114 | |||||||||||
| 115 | Invoked when a C<GETROOT> message is received from the client, this method | ||||||||||
| 116 | should return a L<Tangence::Object> as root object for the connection. | ||||||||||
| 117 | |||||||||||
| 118 | The default implementation will return the object with ID 1; i.e. the first | ||||||||||
| 119 | object created in the registry. | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | sub conn_rootobj | ||||||||||
| 124 | 7 | ▶ | { | ||||||||
|
Subroutine
conn_rootobj: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 125 | 7 | my $self = shift; | |||||||||
| 126 | 7 | return $self->{registry}->get_by_id( 1 ); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | =head2 conn_permits_registry | ||||||||||
| 130 | |||||||||||
| 131 | $allow = $server->conn_permits_registry( $conn ) | ||||||||||
| 132 | |||||||||||
| 133 | Invoked when a C<GETREGISTRY> message is received from the client on the given | ||||||||||
| 134 | connection object. This method should return a boolean to indicate whether the | ||||||||||
| 135 | client is allowed to access the object registry. | ||||||||||
| 136 | |||||||||||
| 137 | The default implementation always permits this, but an overridden method may | ||||||||||
| 138 | decide to disallow it in some situations. When disabled, a client will not be | ||||||||||
| 139 | able to gain access to any serverside objects other than the root object, and | ||||||||||
| 140 | (recursively) any other objects returned by methods, events or properties on | ||||||||||
| 141 | objects already known. This can be used as a security mechanism. | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||
| 144 | |||||||||||
| 145 | sub conn_permits_registry | ||||||||||
| 146 | 7 | ▶ | { | ||||||||
|
Subroutine
conn_permits_registry: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 147 | 7 | return 1; | |||||||||
| 148 | } | ||||||||||
| 149 | |||||||||||
| 150 | =head1 AUTHOR | ||||||||||
| 151 | |||||||||||
| 152 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 153 | |||||||||||
| 154 | =cut | ||||||||||
| 155 | |||||||||||
| 156 | 7 | 0x55AA; | |||||||||