PLS/Server.pm

filter: Statementstmt 30.0% 34 / 113 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 44.0% 11 / 25 Podpod 0.0% 0 / 4 totaltotal 23.4% 45 / 192 CC 23 SCAR 32.1
CC
23
Cov
21%
recv
90.0
__ANON__
20.0
__ANON__
20.0
CRAP 24.7
« PLS/Parser/Element/Subroutine.pm ↑ Summary PLS/Server/Method/Workspace.pm »
1 package PLS::Server;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use AnyEvent::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Coro;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Coro::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use PLS::Server::Request::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use PLS::Server::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 PLS::Server
20  
21 =head1 DESCRIPTION
22  
23 Perl Language Server
24  
25 This server communicates to a language client through STDIN/STDOUT.
26  
27 =head2 SYNOPSIS
28  
29 my $server = PLS::Server->new();
30 $server->run() # never returns
31  
32 =cut
33  
34 sub new
35 {
36 0 ▶ my ($class) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
37  
38 0 return bless {}, $class;
39 }
40  
41 sub recv
42 {
43 0 ▶ my ($self) = @_;
Subroutine recv: not called CC=9 SCAR=45.0
Pod undocumented
44  
45 0 my %headers;
46 my $line;
47 0 my $buffer;
48  
49 0 ▶ while (sysread STDIN, $buffer, 1)
Branch: 1 branch
BranchTrueFalseTotal
if (sysread STDIN, $buffer, 1) 0 0 0
50 {
51 0 $line .= $buffer;
52 0 ▶ last if $line eq "\r\n";
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "\r\n" 0 0 0
53 0 ▶ next unless $line =~ /\r\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\r\n$/ 0 0 0
54 0 $line =~ s/^\s+|\s+$//g;
55 0 my ($field, $value) = split /: /, $line;
56 0 $headers{$field} = $value;
57 0 $line = '';
58 } ## end while (sysread STDIN, $buffer...)
59  
60 0 my $size = $headers{'Content-Length'};
61 0 ▶ die 'no Content-Length header provided' unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 0 0
62  
63 0 my $raw;
64 0 my $bytes_read = -1;
65 0 my $total_read = 0;
66  
67 0 ▶ while ($bytes_read)
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read) 0 0 0
68 {
69 0 $bytes_read = sysread STDIN, $raw, $size - $total_read, $total_read;
70 0 ▶ die "failed to read: $!" unless (defined $bytes_read);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes_read 0 0 0
71  
72 0 $total_read += $bytes_read;
73 } ## end while ($bytes_read)
74  
75 0 ▶ die 'content length does not match header' if ($total_read != $size);
Branch: 1 branch
BranchTrueFalseTotal
if $total_read != $size 0 0 0
76 0 my $content = decode_json $raw;
77  
78 0 ▶ if (length $content->{method})
Branch: 1 branch
BranchTrueFalseTotal
if (length $content->{'method'}) { } 0 0 0
79 {
80 0 return PLS::Server::Request::Factory->new($content);
81 }
82 else
83 {
84 0 return PLS::Server::Response->new($content);
85 }
86 } ## end sub recv
87  
88 sub send
89 {
90 0 ▶ my ($self, $response) = @_;
Subroutine send: not called CC=1 SCAR=6.9
Pod undocumented
91  
92 0 my $json = $response->serialize;
93 0 my $size = length $json;
94  
95 0 syswrite STDOUT, "Content-Length: $size\r\n\r\n$json";
96 } ## end sub send
97  
98 sub run
99 {
100 0 ▶ my ($self) = @_;
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
101  
102 0 my $client_requests = Coro::Channel->new;
103 0 my $server_responses = Coro::Channel->new;
104 0 my $server_requests = Coro::Channel->new;
105 0 my $client_responses = Coro::Channel->new;
106  
107 0 $self->{server_requests} = $server_requests;
108 0 $self->{running_coros} = {};
109  
110 0 my $last_request_id = 0;
111 0 my @pending_requests;
112  
113 async
114 {
115 # check for requests and service them
116 0 ▶ while (my $request = $client_requests->get)
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (my $request = $client_requests->get) 0 0 0
117 {
118 my $coro = async
119 {
120 0 ▶ my $response = $request->service($self);
Subroutine __ANON__: not called CC=4 SCAR=30.0
121 0 ▶ delete $self->{running_coros}{$request->{id}} if (length $request->{id});
Branch: 1 branch
BranchTrueFalseTotal
if length $request->{"id"} 0 0 0
122 0 ▶ return if (not blessed($response) or not $response->isa('PLS::Server::Response'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($response) and $response->isa("PLS::Server::Response") 0 0 0
Condition0%0/3
builtin::blessed($response) and $response->isa("PLS::Server::Response")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($response) and $response->isa("PLS::Server::Response")
builtin::blessed($response) $response->isa("PLS::Server::Response")
Truth table0%0/3
A and B
A= builtin::blessed($response)B= $response->isa("PLS::Server::Response")
ABresult
0X0
100
111
123 0 $server_responses->put($response);
124 0 };
125  
126 0 ▶ $self->{running_coros}{$request->{id}} = $coro if (length $request->{id});
Branch: 1 branch
BranchTrueFalseTotal
if length $request->{"id"} 0 0 0
127  
128 0 Coro::cede();
129 } ## end while (my $request = $client_requests...)
130 0 };
131  
132 async
133 {
134 # check for responses and send them
135 0 ▶ while (my $response = $server_responses->get)
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $response = $server_responses->get) 0 0 0
136 {
137 0 ▶ async { $self->send($response) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 Coro::cede();
139 }
140 0 };
141  
142 async
143 {
144 0 ▶ while (my $request = $server_requests->get)
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (my $request = $server_requests->get) 0 0 0
145 {
146 0 ▶ if ($request->{notification})
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{'notification'}) { } 0 0 0
147 {
148 0 delete $request->{notification};
149 }
150 else
151 {
152 0 $request->{id} = ++$last_request_id;
153 0 push @pending_requests, $request;
154 }
155  
156 0 ▶ async { $self->send($request) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 Coro::cede();
158 } ## end while (my $request = $server_requests...)
159 0 };
160  
161 async
162 {
163 0 ▶ while (my $response = $client_responses->get)
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $response = $client_responses->get) 0 0 0
164 {
165 0 ▶ my $request = first { $_->{id} == $response->{id} } @pending_requests;
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 ▶ next if (not blessed($request) or not $request->isa('PLS::Server::Request'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($request) and $request->isa("PLS::Server::Request") 0 0 0
Condition0%0/3
builtin::blessed($request) and $request->isa("PLS::Server::Request")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($request) and $request->isa("PLS::Server::Request")
builtin::blessed($request) $request->isa("PLS::Server::Request")
Truth table0%0/3
A and B
A= builtin::blessed($request)B= $request->isa("PLS::Server::Request")
ABresult
0X0
100
111
167 0 @pending_requests = grep { $_->{id} != $response->{id} } @pending_requests;
168  
169 0 ▶ async { $request->handle_response($response, $self) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 0 Coro::cede();
171 } ## end while (my $response = $client_responses...)
172 0 };
173  
174 my $io_watcher = AnyEvent->io(
175 fh => \*STDIN,
176 poll => 'r',
177 cb => sub {
178 0 ▶ my $message = $self->recv();
Subroutine __ANON__: not called CC=4 SCAR=30.0
179 0 ▶ return unless blessed($message);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($message) 0 0 0
180  
181 0 ▶ $client_requests->put($message) if $message->isa('PLS::Server::Request');
Branch: 1 branch
BranchTrueFalseTotal
if $message->isa("PLS::Server::Request") 0 0 0
182 0 ▶ $client_responses->put($message) if $message->isa('PLS::Server::Response');
Branch: 1 branch
BranchTrueFalseTotal
if $message->isa("PLS::Server::Response") 0 0 0
183 }
184 0 );
185  
186 0 AnyEvent::Loop::run();
187 } ## end sub run
188  
189 1 1;
« PLS/Parser/Element/Subroutine.pm ↑ Summary PLS/Server/Method/Workspace.pm »