blob: 3b1515d2e234a8271cfc1ecc12a2e48c0199f41c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.server;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.io.ByteStreams;
import com.google.devtools.build.lib.unix.LocalClientSocket;
import com.google.devtools.build.lib.unix.LocalSocketAddress;
import com.google.devtools.build.lib.util.io.RecordingOutErr;
import com.google.devtools.build.lib.util.io.StreamDemultiplexer;
import com.google.devtools.build.lib.vfs.Path;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
/**
* A client to test RPCServer.
*/
public class RPCTestingClient {
private final RecordingOutErr outErr;
private final Path socketFile;
/**
* Create a client to RPCServer. {@code socketFile} must be a file
* on disk; this will not work with the in-memory file system.
*/
public RPCTestingClient(RecordingOutErr outErr, Path socketFile) {
this.socketFile = socketFile;
this.outErr = outErr;
}
public int sendRequest(String command, String... params)
throws Exception {
String request = command;
for (String param : params) {
request += "\0" + param;
}
return sendRequest(request);
}
public int sendRequest(String request) throws Exception {
LocalClientSocket connection = new LocalClientSocket();
connection.connect(new LocalSocketAddress(socketFile.getPathFile()));
try {
OutputStream out = connection.getOutputStream();
byte[] requestBytes = request.getBytes(UTF_8);
byte[] requestLength = new byte[4];
requestLength[0] = (byte) (requestBytes.length << 24);
requestLength[1] = (byte) ((requestBytes.length << 16) & 0xff);
requestLength[2] = (byte) ((requestBytes.length << 8) & 0xff);
requestLength[3] = (byte) (requestBytes.length & 0xff);
out.write(requestLength);
out.write(requestBytes);
out.flush();
connection.shutdownOutput();
OutputStream stdout = outErr.getOutputStream();
OutputStream stderr = outErr.getErrorStream();
ByteArrayOutputStream control = new ByteArrayOutputStream();
StreamDemultiplexer demux = new StreamDemultiplexer((byte) 1,
stdout, stderr, control);
ByteStreams.copy(connection.getInputStream(), demux);
demux.flush();
byte[] controlBytes = control.toByteArray();
return (((int) controlBytes[0]) << 24)
+ (((int) controlBytes[1]) << 16)
+ (((int) controlBytes[2]) << 8)
+ ((int) controlBytes[3]);
} finally {
connection.close();
}
}
}
|