File Coverage

blib/lib/Ace/SocketServer.pm
Criterion Covered Total %
statement 68 162 41.9
branch 0 48 0.0
condition 4 22 18.1
subroutine 22 34 64.7
pod 0 8 0.0
total 94 274 34.3


line stmt bran cond sub pod time code
1             package Ace::SocketServer;
2              
3             require 5.004;
4 4     4   806 use strict;
  4         8  
  4         195  
5 4     4   29 use Carp 'croak','cluck';
  4         6  
  4         274  
6 4     4   20 use Ace qw(rearrange STATUS_WAITING STATUS_PENDING STATUS_ERROR);
  4         21  
  4         239  
7 4     4   4152 use IO::Socket;
  4         115484  
  4         20  
8 4     4   2789 use Digest::MD5 'md5_hex';
  4         11  
  4         310  
9              
10 4     4   23 use vars '$VERSION';
  4         11  
  4         258  
11             $VERSION = '1.01';
12              
13 4     4   26 use constant DEFAULT_USER => 'anonymous'; # anonymous user
  4         9  
  4         449  
14 4     4   23 use constant DEFAULT_PASS => 'guest'; # anonymous password
  4         8  
  4         266  
15 4     4   21 use constant DEFAULT_TIMEOUT => 120; # two minute timeout on queries
  4         11  
  4         229  
16              
17             # header information
18 4     4   19 use constant HEADER => 'L5a30';
  4         7  
  4         1556  
19 4     4   22 use constant HEADER_LEN => 5*4+30;
  4         9  
  4         213  
20 4     4   23 use constant ACESERV_MSGREQ => "ACESERV_MSGREQ";
  4         63  
  4         188  
21 4     4   22 use constant ACESERV_MSGDATA => "ACESERV_MSGDATA";
  4         8  
  4         188  
22 4     4   76 use constant WORDORDER_MAGIC => 0x12345678;
  4         9  
  4         207  
23              
24             # Server only, it may just be sending or a reply or it may be sending an
25             # instruction, such as "operation refused".
26 4     4   21 use constant ACESERV_MSGOK => "ACESERV_MSGOK";
  4         5  
  4         187  
27 4     4   21 use constant ACESERV_MSGENCORE => "ACESERV_MSGENCORE";
  4         5  
  4         170  
28 4     4   20 use constant ACESERV_MSGFAIL => "ACESERV_MSGFAIL";
  4         9  
  4         175  
29 4     4   22 use constant ACESERV_MSGKILL => "ACESERV_MSGKILL";
  4         6  
  4         182  
30              
31 4     4   63 use constant ACESERV_CLIENT_HELLO => "bonjour";
  4         15  
  4         207  
32 4     4   19 use constant ACESERV_SERVER_HELLO => "et bonjour a vous";
  4         8  
  4         7323  
33              
34             sub connect {
35 4     4 0 46 my $class = shift;
36 4         33 my ($host,$port,$timeout,$user,$pass) = rearrange(['HOST','PORT','TIMEOUT','USER','PASS'],@_);
37 4   50     33 $user ||= DEFAULT_USER;
38 4   50     25 $pass ||= DEFAULT_PASS;
39 4   50     12 $timeout ||= DEFAULT_TIMEOUT;
40 4   50     52 my $s = IO::Socket::INET->new("$host:$port") ||
41             return _error("Couldn't establish connection");
42 0         0 my $self = bless { socket => $s,
43             client_id => 0, # client ID provided by server
44             timeout => $timeout,
45             },$class;
46 0 0       0 return unless $self->_handshake($user,$pass);
47 0         0 $self->{status} = STATUS_WAITING;
48 0         0 $self->{encoring} = 0;
49 0         0 return $self;
50             }
51              
52             sub DESTROY {
53 0     0   0 my $self = shift;
54 0 0       0 return if $self->{last_msg} eq ACESERV_MSGKILL;
55 0         0 $self->_send_msg('quit');
56             # Is _recv_msg() bringing things down in flames? Maybe!
57 0         0 my ($msg,$body) = $self->_recv_msg('strip');
58 0 0 0     0 warn "Did not get expected ACESERV_MSGKILL message, got $msg instead"
59             if defined($msg) and $msg ne ACESERV_MSGKILL;
60             }
61              
62 0     0 0 0 sub encore { return shift->{encoring} }
63              
64 0     0 0 0 sub status { shift->{status} }
65              
66 0     0 0 0 sub error { $Ace::Error; }
67              
68             sub query {
69 0     0 0 0 my $self = shift;
70 0         0 my ($request,$parse) = @_;
71 0 0       0 warn "query($request)" if Ace->debug;
72 0 0       0 unless ($self->_send_msg($request,$parse)) {
73 0         0 $self->{status} = STATUS_ERROR;
74 0         0 return _error("Write to socket server failed: $!");
75             }
76 0         0 $self->{status} = STATUS_PENDING;
77 0         0 $self->{encoring} = 0;
78 0         0 return 1;
79             }
80              
81             sub read {
82 0     0 0 0 my $self = shift;
83 0 0       0 return _error("No pending query") unless $self->status == STATUS_PENDING;
84 0 0 0     0 $self->_do_encore || return if $self->encore;
85             # call select() here to time out
86              
87 0 0       0 if ($self->{timeout}) {
88 0         0 my $rdr = '';
89 0         0 vec($rdr,fileno($self->{socket}),1) = 1;
90 0         0 my $result = select($rdr,undef,undef,$self->{timeout});
91 0 0       0 return _error("Query timed out") unless $result;
92             }
93              
94 0         0 my ($msg,$body) = $self->_recv_msg;
95 0 0       0 return unless defined $msg;
96 0         0 $msg =~ s/\0.+$//; # socketserver bug workaround: get rid of junk in message
97 0 0 0     0 if ($msg eq ACESERV_MSGOK or $msg eq ACESERV_MSGFAIL) {
    0          
98 0         0 $self->{status} = STATUS_WAITING;
99 0         0 $self->{encoring} = 0;
100             } elsif ($msg eq ACESERV_MSGENCORE) {
101 0         0 $self->{status} = STATUS_PENDING; # not strictly necessary, but helpful to document
102 0         0 $self->{encoring} = 1;
103             } else {
104 0         0 $self->{status} = STATUS_ERROR;
105 0         0 return _error($body);
106             }
107 0         0 return $body;
108             }
109              
110             sub write {
111 0     0 0 0 my $self = shift;
112 0         0 my $data = shift;
113 0 0       0 unless ($self->_send_msg($data,1)) {
114 0         0 $self->{status} = STATUS_ERROR;
115 0         0 return _error("Write to socket server failed: $!");
116             }
117 0         0 $self->{status} = STATUS_PENDING;
118 0         0 $self->{encoring} = 0;
119 0         0 return 1;
120             }
121              
122             sub _error {
123 4     4   509218181 $Ace::Error = shift;
124 4         33 return;
125             }
126              
127             # return socket (read only)
128 0     0 0   sub socket { $_[0]->{socket} }
129              
130             # ----------------------------- low level -------------------------------
131             sub _do_encore {
132 0     0     my $self = shift;
133 0 0         unless ($self->_send_msg('encore')) {
134 0           $self->{status} = STATUS_ERROR;
135 0           return _error("Write to socket server failed: $!");
136             }
137 0           $self->{status} = STATUS_PENDING;
138 0           return 1;
139             }
140             sub _handshake {
141 0     0     my $self = shift;
142 0           my ($user,$pass) = @_;
143 0           $self->_send_msg(ACESERV_CLIENT_HELLO);
144 0           my ($msg,$nonce) = $self->_recv_msg('strip');
145 0 0         return unless $msg eq ACESERV_MSGOK;
146             # hash username and password
147 0           my $authdigest = md5_hex(md5_hex($user . $pass).$nonce);
148 0           $self->_send_msg("$user $authdigest");
149 0           my $body;
150 0           ($msg,$body) = $self->_recv_msg('strip');
151 0 0         return _error("server: $body") unless $body eq ACESERV_SERVER_HELLO;
152 0           return 1;
153             }
154              
155             sub _send_msg {
156 0     0     my ($self,$msg,$parse) = @_;
157 0 0         return unless my $sock = $self->{socket};
158 0           local $SIG{'PIPE'} = 'IGNORE';
159 0           $msg .= "\0"; # add terminating null
160 0           my $request;
161 0 0         if ($parse) {
162 0           $request = ACESERV_MSGDATA;
163             } else {
164 0 0         $request = $msg eq "encore\0" ? ACESERV_MSGENCORE : ACESERV_MSGREQ;
165             }
166 0           my $header = pack HEADER,WORDORDER_MAGIC,length($msg),0,$self->{client_id},0,$request;
167 0           print $sock $header,$msg;
168             }
169              
170             sub _recv_msg {
171 0     0     my $self = shift;
172 0           my $strip_null = shift;
173 0 0         return unless my $sock = $self->{socket};
174 0           my ($header,$body);
175 0           my $bytes = CORE::read($sock,$header,HEADER_LEN);
176 0 0         unless ($bytes > 0) {
177 0           $self->{status} = STATUS_ERROR;
178 0           return _error("Connection closed by remote server: $!");
179             }
180 0           my ($magic,$length,$junk1,$clientID,$junk2,$msg) = unpack HEADER,$header;
181 0   0       $self->{client_id} ||= $clientID;
182 0           $msg =~ s/\0*$//;
183 0           $self->{last_msg} = $msg;
184 0 0         if ($length > 0) {
185 0 0         return _error("read of body failed: $!" )
186             unless CORE::read($sock,$body,$length);
187 0 0 0       $body =~ s/\0*$// if defined($strip_null) && $strip_null;
188 0           return ($msg,$body);
189             } else {
190 0           return $msg;
191             }
192             }
193              
194             1;
195              
196             __END__