File Coverage

blib/lib/Net/Async/Tangence/Server.pm
Criterion Covered Total %
statement 38 43 88.3
branch 1 2 50.0
condition n/a
subroutine 12 13 92.3
pod 3 5 60.0
total 54 63 85.7


line stmt bran cond sub pod time code
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 6     6   1164813 use v5.14;
  6         69  
9 6     6   33 use warnings;
  6         27  
  6         224  
10              
11 6     6   3332 use IO::Async::Listener '0.36';
  6         62449  
  6         222  
12 6     6   52 use base qw( IO::Async::Listener );
  6         13  
  6         647  
13              
14 6     6   41 use Carp;
  6         13  
  6         314  
15              
16 6     6   2736 use Net::Async::Tangence::ServerProtocol;
  6         17  
  6         2440  
17              
18             =head1 NAME
19              
20             C - serve C clients using C
21              
22             =head1 DESCRIPTION
23              
24             This subclass of L accepts L client
25             connections.
26              
27             =cut
28              
29             =head1 PARAMETERS
30              
31             The following named parameters may be passed to C or C:
32              
33             =over 8
34              
35             =item registry => Tangence::Registry
36              
37             The L for the server's objects.
38              
39             =back
40              
41             =cut
42              
43             sub _init
44             {
45 5     5   100766 my $self = shift;
46 5         15 my ( $params ) = @_;
47              
48             $params->{handle_constructor} = sub {
49 6     6   504 my $self = shift;
50              
51             return Net::Async::Tangence::ServerProtocol->new(
52             registry => $self->{registry},
53             on_closed => $self->_capture_weakself( sub {
54 2         24 my $self = shift;
55 2         21 $self->remove_child( $_[0] );
56 6         81 } ),
57             );
58 5         68 };
59              
60 5         65 $self->SUPER::_init( $params );
61              
62 5 50       131 $self->{registry} = delete $params->{registry} if exists $params->{registry};
63             }
64              
65             sub on_accept
66             {
67 6     6 1 511 my $self = shift;
68 6         19 my ( $conn ) = @_;
69              
70 6         43 $self->add_child( $conn );
71             }
72              
73             # Useful for testing
74             sub make_new_connection
75             {
76 4     4 0 33977 my $self = shift;
77 4         16 my ( $sock ) = @_;
78              
79             # Mass cheating
80 4         15 my $conn = $self->{handle_constructor}->( $self );
81              
82 4         516 $conn->configure( handle => $sock );
83 4         771 $self->on_accept( $conn );
84              
85 4         1361 return $conn;
86             }
87              
88             # More testing utilities
89             sub accept_stdio
90             {
91 0     0 0 0 my $self = shift;
92              
93 0         0 my $conn = $self->{handle_constructor}->( $self );
94              
95 0         0 $conn->configure(
96             read_handle => \*STDIN,
97             write_handle => \*STDOUT,
98             );
99 0         0 $self->on_accept( $conn );
100              
101 0         0 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 message is received from the client, this method
116             should return a L 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             {
125 6     6 1 118 my $self = shift;
126 6         53 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 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             {
147 6     6 1 54 return 1;
148             }
149              
150             =head1 AUTHOR
151              
152             Paul Evans
153              
154             =cut
155              
156             0x55AA;