SixXS/TIC/Server/AnyEvent.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 92 CC 4 SCAR 18.7
CC
4
Cov
0%
client_read
20.0
run
2.0
push_client_read
2.0
CRAP 6.5
1 #!/usr/bin/perl
2  
3 package Net::SixXS::TIC::Server::AnyEvent;
4  
5 use v5.010;
6 use strict;
7 use warnings;
8  
9 use version; our $VERSION = version->declare("v0.1.1");
10  
11 use AnyEvent::Handle;
12 use AnyEvent::Socket 'tcp_server';
13 use Moose;
14  
15 use Net::SixXS;
16  
17 extends 'Net::SixXS::TIC::Server';
18  
19 has host => (
20 is => 'rw',
21 isa => 'Str',
22 required => 0,
23 default => sub { '0.0.0.0' },
24 );
25  
26 has port => (
27 is => 'rw',
28 isa => 'Int',
29 required => 0,
30 default => sub { 3874 },
31 );
32  
33 has tic_s_asocket => (
34 is => 'rw',
35 isa => 'Any',
36 required => 0,
37 );
38  
39 my $client_id = 0;
40  
41 sub run($)
42 {
43 my ($self) = @_;
44  
45 my $s = tcp_server $self->host, $self->port, sub {
46 my ($fh, $host, $port) = @_;
47  
48 my $id = $client_id++;
49 $self->debug("Accepted TIC client $id from $host:$port");
50 my $on_eof = sub {
51 $self->debug("EOF client $id from $host:$port");
52 delete $self->clients->{$id};
53 };
54 my $ae = AnyEvent::Handle->new(
55 fh => $fh,
56 on_eof => $on_eof,
57 on_error => $on_eof,
58 );
59 $self->clients->{$id} = { ae => $ae };
60 $self->greet_client($self->clients->{$id});
61 $self->push_client_read($id);
62 } or die ref($self).": could not listen on ".
63 $self->host.":".$self->port.": $!\n";
64 $self->tic_s_asocket($s);
65 }
66  
67 sub push_client_read($ $)
68 {
69 my ($self, $id) = @_;
70  
71 $self->clients->{$id}->{ae}->push_read(line =>
72 sub { $self->client_read($id, $_[0], $_[1], $_[2]) });
73 }
74  
75 sub client_read($ $ $ $ $)
76 {
77 my ($self, $id, $handle, $line, $eol) = @_;
78  
79 $self->debug("Read a line from client $id: $line");
80 my $c = $self->clients->{$id};
81 if (!defined $c) {
82 $self->debug("The client has gone away, it seems");
83 $handle->push_shutdown;
84 return;
85 }
86
87 if ($c->{shutdown}) {
88 $self->debug("Not accepting anything more from this client");
89 } else {
90 $self->run_command($c, [split /\s+/, $line]);
91 $handle->push_shutdown if $c->{shutdown};
92 }
93 $self->push_client_read($id);
94 }
95  
96 sub client_write_line($ $ $)
97 {
98 my ($self, $client, $line) = @_;
99  
100 $client->{ae}->push_write("$line\n");
101 }
102  
103 no Moose;
104 1;
105 __END__