File Coverage

blib/lib/AnyEvent/SKKServ.pm
Criterion Covered Total %
statement 18 43 41.8
branch 0 10 0.0
condition n/a
subroutine 6 14 42.8
pod 2 2 100.0
total 26 69 37.6


line stmt bran cond sub pod time code
1             package AnyEvent::SKKServ;
2 1     1   740 use strict;
  1         2  
  1         36  
3 1     1   6 use warnings;
  1         1  
  1         34  
4 1     1   30 use 5.008005;
  1         13  
  1         48  
5             our $VERSION = '0.01';
6              
7 1     1   1204 use AnyEvent::Handle;
  1         29357  
  1         43  
8 1     1   1132 use AnyEvent::Socket ();
  1         15772  
  1         59  
9              
10             use constant {
11 1         593 CLIENT_END => '0',
12             CLIENT_REQUEST => '1',
13             CLIENT_VERSION => '2',
14             CLIENT_HOST => '3',
15              
16             SERVER_ERROR => '0',
17             SERVER_FOUND => '1',
18             SERVER_NOT_FOUND => '4',
19             SERVER_FULL => '9',
20 1     1   30 };
  1         3  
21              
22             sub new {
23 0     0 1   my ($class, %args) = @_;
24             return bless {
25             host => undef,
26             port => 55100,
27              
28             on_error => sub {
29 0     0     $_[0]->push_write(SERVER_ERROR . "\n");
30             },
31             on_end => sub {
32 0     0     undef $_[0];
33             },
34             on_version => sub {
35 0     0     $_[0]->push_write("$VERSION:anyevent_skkserv ");
36             },
37             on_host => sub {
38             # unimplemented
39 0     0     $_[0]->push_write("hostname:addr:...: ");
40             },
41 0     0     on_request => sub {},
42 0           %args,
43             }, $class;
44             }
45              
46             sub run {
47 0     0 1   my $self = shift;
48             AnyEvent::Socket::tcp_server $self->{host}, $self->{port}, sub {
49 0 0   0     my ($fh, $host, $port) = @_ or die "connection failed: $!";
50 0           my $hdl; $hdl = AnyEvent::Handle->new(
51             fh => $fh,
52             on_eof => sub {
53 0           $_[0]->destroy;
54             },
55             on_error => sub {
56 0           $_[0]->destroy;
57             },
58 0           );
59             $hdl->on_read(sub {
60             $hdl->push_read(chunk => 1, sub {
61 0           my ($hdl, $command) = @_;
62 0 0         if ($command eq CLIENT_END) {
    0          
    0          
    0          
63 0           $self->{on_end}->(@_);
64             } elsif ($command eq CLIENT_REQUEST) {
65             $hdl->push_read(regex => qr/\x20/, sub {
66 0           chop $_[1];
67 0           $self->{on_request}->(@_);
68 0           });
69             } elsif ($command eq CLIENT_VERSION) {
70 0           $self->{on_version}->(@_);
71             } elsif ($command eq CLIENT_HOST) {
72 0           $self->{on_host}->(@_);
73             } else {
74 0           $self->{on_error}->(@_);
75             }
76 0           });
77 0           });
78 0           };
79             }
80              
81             1;
82             __END__