CQI/Server.pm

filter: Statementstmt 50.0% 24 / 48 Branchbran 10.0% 2 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 40.4% 36 / 89 SCAR 37.8
CC
11
Cov
35%
cqi_server
110.0
cqi_server_available
2.1
BEGIN
1.1
CRAP 44.0
« CQI/Client.pm ↑ Summary CQI.pm »
1 package CWB::CQI::Server;
2 # -*-cperl-*-
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use CWB::CQI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # export CQi server startup functions
12 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 our @EXPORT = qw(cqi_server cqi_server_available);
14  
15 =head1 NAME
16  
17 CWB::CQI::Server - launch private CQPserver on local machine
18  
19  
20 =head1 SYNOPSIS
21  
22 use CWB::CQI::Server;
23 use CWB::CQI::Client;
24  
25 if (cqi_server_available()) {
26 my @details = cqi_server();
27 cqi_connect(@details);
28 ...
29 }
30  
31 =head1 DESCRIPTION
32  
33 The B<CWB::CQI::Server> module can be used to launch a private CQPserver
34 on the local machine, which B<CWB::CQI::Client> can then connect to.
35  
36 Note that this is only possible if a suitable version of the IMS Open Corpus Workbench
37 and the B<CWB> Perl module have been installed. Availability must therefore be
38 checked with the B<cqi_server_available> function before calling B<cqi_server>.
39  
40  
41 =head1 FUNCTIONS
42  
43 =over 4
44  
45 =cut
46  
47 3 our $CQPserver = undef;
48 3 ▶if (eval 'use CWB 3.000_000; 1') {
Subroutine BEGIN: called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if (eval "use CWB 3.000_000; 1") 0 3 3
49 0 ▶ $CQPserver = $CWB::CQPserver
Branch: 1 branch
BranchTrueFalseTotal
if -x $CWB::CQPserver 0 0 0
50 if -x $CWB::CQPserver;
51 }
52  
53 =item I<$ok> = B<cqi_server_available>();
54  
55 Returns a B<true> value if a suitable CQPserver binary is installed on the local machine and
56 can be started with the B<cqi_server> function.
57  
58 =cut
59  
60 sub cqi_server_available {
61 2 ▶ return (defined $CQPserver) ? 1 : 0;
Subroutine cqi_server_available: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $CWB::CQI::Server::CQPserver ? : 0 2 2
62 }
63  
64 =item (I<$user>, I<$passwd>, I<$host>, I<$port>) = B<cqi_server>();
65  
66 =item I<@details> = B<cqi_server>(I<$flags>);
67  
68 C<cqi_server()> searches for a free port on the local machine, then
69 launches a single-user B<CQPserver> process and returns the connection details
70 required by the B<cqi_connect> function from B<CWB::CQI::Client> (in the appropriate order).
71 The simplest way to establish a connection with a private, local CQPserver is
72  
73 cqi_connect(cqi_server());
74  
75 Be sure to check with B<cqi_server_available> whether the required C<cqpserver>
76 command-line program is available first.
77  
78 An optional argument to B<cqi_server> is appended to the C<cqpserver> command-line flags
79 and can be used to specify further start-up options (e.g. to read a macro definition file).
80 Keep in mind that arguments containing shell metacharacters need to be quoted appropriately.
81  
82 B<WARNING:> Since B<CQPserver> runs as a separate process in the background, it is
83 important to establish a connection B<as soon as possible>. If the user's
84 program aborts before B<cqi_connect> is called and contacts the new CQPserver,
85 this process will accept further connections from other users (on the local machine),
86 which might compromise confidential data.
87  
88 =cut
89  
90 #
91 #
92 # Start CQPserver in the background and return (host, port, user, passwd) list for cqi_connect()
93 # An init file is generated which adds a random user/passwd to the server's user list,
94 # so you can connect to the newly created server with the user/passwd combination returned
95 # by cqi_server() only. (NB uses '-I' at the moment, so .cqprc won't be read)
96 #
97 #
98 sub cqi_server {
99 0 ▶ my $user = "cqi_server_$$";
Subroutine cqi_server: not called CC=10 SCAR=47.0
Pod documented
100 0 my $passwd = "pass" . int rand(42000);
101 0 my $flags = "-1 -L -q "; # single-client server, localhost only (for security reasons)
102 0 ▶ $flags .= "@_" if @_; # append optional command-line flags
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
103  
104 0 ▶ croak "CQPserver is not installed on this machine"
Branch: 1 branch
BranchTrueFalseTotal
unless cqi_server_available() 0 0 0
105 unless cqi_server_available();
106  
107 # generate temporary user list file for CQPserver
108 0 my $passfile = "/tmp/CQI::Server.$$";
109 0 my $fh = new FileHandle "> $passfile";
110 0 ▶ croak "Can't create temporary user list file. Aborting."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
111 unless defined $fh;
112 0 print $fh "user $user \"$passwd\";\n";
113 0 $fh->close;
114 0 chmod 0600, $passfile; # so no one can spy on us
115  
116 # scan for free port (using rand() so two servers invoked at the same time won't collide)
117 0 my $port = 10000 + int rand(2000);
118 my %in_use =
119 0 map {$_ => 1}
120 0 ▶ map {(/\*\.([0-9]+)/) ? $1 : 0}
Branch: 1 branch
BranchTrueFalseTotal
/\*\.([0-9]+)/ ? : 0 0 0
121 `netstat -a -n | grep LISTEN`;
122 0 ▶ while ($port < 60000 and $in_use{$port}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port < 60000 and $in_use{$port}) 0 0 0
Condition0%0/3
$port < 60000 and $in_use{$port}
!ll&&!rl&&r
000
MC/DC0%0/2
$port < 60000 and $in_use{$port}
$port < 60000 $in_use{$port}
Truth table0%0/3
A and B
A= $port < 60000B= $in_use{$port}
ABresult
0X0
100
111
123 0 $port += rand(20); # jump randomly to avoid collisions
124 }
125 0 ▶ croak "Can't find free port for CQPserver. Abort."
Branch: 1 branch
BranchTrueFalseTotal
unless $port < 60000 0 0 0
126 unless $port < 60000;
127  
128 # now start CQPserver on this port
129 0 ▶ croak "CQPserver failed to launch: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
if system "$CQPserver $flags -P $port -I $passfile >/dev/null 2>&1" or $? != 0 0 0 0
Condition0%0/3
system "$CQPserver $flags -P $port -I $passfile >/dev/null 2>&1" or $? != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
system "$CQPserver $flags -P $port -I $passfile >/dev/null 2>&1" or $? != 0
system "$CQPserver $flags -P $port -I $passfile >/dev/null 2>&1" $? != 0
Truth table0%0/3
A or B
A= system "$CQPserver $flags -P $port -I $passfile >/dev/null 2>&1"B= $? != 0
ABresult
1X1
011
000
130 if system "$CQPserver $flags -P $port -I $passfile >/dev/null 2>&1" or $? != 0;
131  
132 # delete user list file
133 0 unlink $passfile;
134  
135 # return connection information suitable for cqi_connect()
136 0 return $user, $passwd, "localhost", $port;
137 }
138  
139  
140 3 1;
141  
142 __END__
« CQI/Client.pm ↑ Summary CQI.pm »