| 1 | package Net::SeedServe::Server; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Net::SeedServe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use IO::All; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Time::HiRes qw(usleep); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Net::SeedServe::Server - Perl module for implementing a seed server. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | None yet. Consult the code, and the examples in the tests directory. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 METHODS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head2 $obj = Net::SeedServe::Server->new(status_file => $status_filename); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Initialises a new object with the status filename. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub new | ||||||||||||||||||||||||||||
| 27 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 7 | my $class = shift; | |||||||||||||||||||||||||||
| 29 | 7 | my $self = {}; | |||||||||||||||||||||||||||
| 30 | 7 | bless $self, $class; | |||||||||||||||||||||||||||
| 31 | 7 | $self->_init(@_); | |||||||||||||||||||||||||||
| 32 | 7 | return $self; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub _init | ||||||||||||||||||||||||||||
| 36 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 37 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 38 | 7 | my %args = (@_); | |||||||||||||||||||||||||||
| 39 | 7 | ▶ | $self->{'status_file'} = $args{'status_file'} or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | die "Unknown status file!"; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 7 | return 0; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head2 $server->start() | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Starts the server on a port starting from port 3,000. Returns a hash ref | ||||||||||||||||||||||||||||
| 48 | containing the port. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub start | ||||||||||||||||||||||||||||
| 53 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 55 | 6 | my $status_file = $self->{'status_file'}; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 6 | for(my $port = 3000; ; $port++) | |||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 10 | unlink($status_file); | |||||||||||||||||||||||||||
| 60 | 10 | my $fork_pid = fork(); | |||||||||||||||||||||||||||
| 61 | 10 | ▶ | if (!defined($fork_pid)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | die "Fork was not successful!"; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 10 | ▶ | if (! $fork_pid) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | # The child will start the service. | ||||||||||||||||||||||||||||
| 68 | 3 | my $server = | |||||||||||||||||||||||||||
| 69 | Net::SeedServe->new( | ||||||||||||||||||||||||||||
| 70 | 'status_file' => $status_file, | ||||||||||||||||||||||||||||
| 71 | 'port' => $port, | ||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | eval | ||||||||||||||||||||||||||||
| 75 | 3 | { | |||||||||||||||||||||||||||
| 76 | 3 | $server->loop(); | |||||||||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||||||||||
| 78 | 3 | ▶ | if ($@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||
| 80 | 3 | exit(-1); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else | ||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | # The parent will try to find the child's status | ||||||||||||||||||||||||||||
| 86 | 7 | my $text; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 7 | ▶ | while (! ( defined($text) and $text =~ /\n\z/) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $text and $text =~ /\n\z/
MC/DC50%1/2
defined $text and $text =~ /\n\z/
defined $text
$text =~ /\n\z/
Truth table66%2/3
A and B
A= defined $textB= $text =~ /\n\z/
| |||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||
| 90 | 7 | ▶ | while (! -f $status_file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 31 | usleep(5000); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 7 | usleep(5000); | |||||||||||||||||||||||||||
| 95 | 7 | $text = io()->file($status_file)->getline(); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 7 | ▶ | if ($text eq "Status:Success\tPort:$port\tPID:$fork_pid\n") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | # The game is on - the service is running and everything's OK. | ||||||||||||||||||||||||||||
| 100 | 3 | $self->{'port'} = $port; | |||||||||||||||||||||||||||
| 101 | 3 | $self->{'server_pid'} = $fork_pid; | |||||||||||||||||||||||||||
| 102 | 3 | return +{ 'port' => $port }; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | else | ||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 4 | waitpid($fork_pid, 0); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 $server->connect(status_file => $status_filename) | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Connects to an existing Server whose status file is $status_filename. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub connect | ||||||||||||||||||||||||||||
| 119 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | my $status_file = $self->{'status_file'}; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | my $text = io()->file($status_file)->getline(); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | ▶ | if ($text !~ /^Status:Success\tPort:(\d+)\tPID:(\d+)$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 0 | die "Invalid status file."; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | my $port = $1; | |||||||||||||||||||||||||||
| 132 | 1 | $self->{'server_pid'} = $2; | |||||||||||||||||||||||||||
| 133 | # TODO ? | ||||||||||||||||||||||||||||
| 134 | # Add sanity checks. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | $self->{'port'} = $port; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | return { 'port' => $port, }; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head2 $server->stop() | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Stops the service by killing the listening process. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub stop | ||||||||||||||||||||||||||||
| 148 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 3 | my $pid = $self->{'server_pid'}; | |||||||||||||||||||||||||||
| 152 | 3 | kill("TERM", $pid); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 3 | waitpid($pid, 0); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub _ok_transact | ||||||||||||||||||||||||||||
| 158 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_ok_transact: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 159 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 160 | 10 | my $msg = shift; | |||||||||||||||||||||||||||
| 161 | 10 | my $port = $self->{'port'}; | |||||||||||||||||||||||||||
| 162 | 10 | my $conn = io("localhost:$port"); | |||||||||||||||||||||||||||
| 163 | 10 | $conn->print("$msg\n"); | |||||||||||||||||||||||||||
| 164 | 10 | my $response = $conn->getline(); | |||||||||||||||||||||||||||
| 165 | 10 | ▶ | if ($response eq "OK\n") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 10 | return 0; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else | ||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 0 | die "Invalid response - $response."; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 $server->clear(); | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Sends a clear transaction that clears the seeds of the seed server. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | sub clear | ||||||||||||||||||||||||||||
| 182 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 184 | 2 | return $self->_ok_transact("CLEAR"); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 $server->enqueue(\@seeds); | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Enqueues several seeds in the server to be served next. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | sub enqueue | ||||||||||||||||||||||||||||
| 194 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
enqueue: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 195 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 196 | 8 | my $seeds = shift; | |||||||||||||||||||||||||||
| 197 | 8 | ▶ | if (grep { $_ !~ /^\d+$/ } @$seeds) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||
| 199 | 0 | die "Invalid seed."; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 8 | return $self->_ok_transact("ENQUEUE " . join("", map { "$_," } @$seeds)); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 7 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||