13
13
// limitations under the License.
14
14
package com .google .devtools .build .lib .worker ;
15
15
16
- import static java .nio .charset .StandardCharsets .UTF_8 ;
17
-
16
+ import com .google .common .collect .ImmutableList ;
18
17
import com .google .devtools .build .lib .worker .WorkerProtocol .Input ;
19
18
import com .google .devtools .build .lib .worker .WorkerProtocol .WorkRequest ;
20
19
import com .google .devtools .build .lib .worker .WorkerProtocol .WorkResponse ;
23
22
import com .google .protobuf .ByteString ;
24
23
import com .google .protobuf .util .JsonFormat ;
25
24
import com .google .protobuf .util .JsonFormat .Printer ;
26
- import java .io .BufferedReader ;
27
25
import java .io .BufferedWriter ;
28
26
import java .io .EOFException ;
29
27
import java .io .IOException ;
30
- import java .io .InputStream ;
31
- import java .io .InputStreamReader ;
32
- import java .io .OutputStream ;
33
- import java .io .OutputStreamWriter ;
34
- import java .util .ArrayList ;
35
28
import java .util .List ;
36
29
37
- /** Sample implementation of the Worker Protocol using JSON to communicate with Bazel. */
38
- public class JsonExampleWorkerProtocolImpl implements ExampleWorkerProtocol {
39
- private final Printer jsonPrinter =
40
- JsonFormat .printer ().omittingInsignificantWhitespace ().includingDefaultValueFields ();
30
+ /** Implementation of the Worker Protocol using JSON to communicate with Bazel. */
31
+ public final class JsonWorkerMessageProcessor implements WorkRequestHandler .WorkerMessageProcessor {
32
+ /** Reader for reading the WorkResponse. */
41
33
private final JsonReader reader ;
34
+ /** Printer for printing the WorkRequest. */
35
+ private final Printer jsonPrinter ;
36
+ /** Writer for writing the WorkRequest to the worker. */
42
37
private final BufferedWriter jsonWriter ;
43
38
44
- public JsonExampleWorkerProtocolImpl (InputStream stdin , OutputStream stdout ) {
45
- reader = new JsonReader (new BufferedReader (new InputStreamReader (stdin , UTF_8 )));
39
+ /** Constructs a {@code WorkRequestHandler} that reads and writes JSON. */
40
+ public JsonWorkerMessageProcessor (JsonReader reader , BufferedWriter jsonWriter ) {
41
+ this .reader = reader ;
46
42
reader .setLenient (true );
47
- jsonWriter = new BufferedWriter (new OutputStreamWriter (stdout , UTF_8 ));
43
+ this .jsonWriter = jsonWriter ;
44
+ jsonPrinter =
45
+ JsonFormat .printer ().omittingInsignificantWhitespace ().includingDefaultValueFields ();
48
46
}
49
47
50
- private static ArrayList <String > readArguments (JsonReader reader ) throws IOException {
48
+ private static ImmutableList <String > readArguments (JsonReader reader ) throws IOException {
51
49
reader .beginArray ();
52
- ArrayList <String > arguments = new ArrayList <> ();
50
+ ImmutableList . Builder <String > argumentsBuilder = ImmutableList . builder ();
53
51
while (reader .hasNext ()) {
54
- arguments .add (reader .nextString ());
52
+ argumentsBuilder .add (reader .nextString ());
55
53
}
56
54
reader .endArray ();
57
- return arguments ;
55
+ return argumentsBuilder . build () ;
58
56
}
59
57
60
- private static ArrayList <Input > readInputs (JsonReader reader ) throws IOException {
58
+ private static ImmutableList <Input > readInputs (JsonReader reader ) throws IOException {
61
59
reader .beginArray ();
62
- ArrayList <Input > inputs = new ArrayList <> ();
60
+ ImmutableList . Builder <Input > inputsBuilder = ImmutableList . builder ();
63
61
while (reader .hasNext ()) {
64
62
String digest = null ;
65
63
String path = null ;
@@ -81,19 +79,25 @@ private static ArrayList<Input> readInputs(JsonReader reader) throws IOException
81
79
path = reader .nextString ();
82
80
break ;
83
81
default :
84
- throw new IOException ( name + " is an incorrect field in input" ) ;
82
+ continue ;
85
83
}
86
84
}
87
85
reader .endObject ();
88
- inputs .add (
89
- Input .newBuilder ().setDigest (ByteString .copyFromUtf8 (digest )).setPath (path ).build ());
86
+ Input .Builder inputBuilder = Input .newBuilder ();
87
+ if (digest != null ) {
88
+ inputBuilder .setDigest (ByteString .copyFromUtf8 (digest ));
89
+ }
90
+ if (path != null ) {
91
+ inputBuilder .setPath (path );
92
+ }
93
+ inputsBuilder .add (inputBuilder .build ());
90
94
}
91
95
reader .endArray ();
92
- return inputs ;
96
+ return inputsBuilder . build () ;
93
97
}
94
98
95
99
@ Override
96
- public WorkRequest readRequest () throws IOException {
100
+ public WorkRequest readWorkRequest () throws IOException {
97
101
List <String > arguments = null ;
98
102
List <Input > inputs = null ;
99
103
Integer requestId = null ;
@@ -104,24 +108,24 @@ public WorkRequest readRequest() throws IOException {
104
108
switch (name ) {
105
109
case "arguments" :
106
110
if (arguments != null ) {
107
- throw new IOException ("Work request cannot have more than one list of arguments" );
111
+ throw new IOException ("WorkRequest cannot have more than one ' arguments' field " );
108
112
}
109
113
arguments = readArguments (reader );
110
114
break ;
111
115
case "inputs" :
112
116
if (inputs != null ) {
113
- throw new IOException ("Work request cannot have more than one list of inputs" );
117
+ throw new IOException ("WorkRequest cannot have more than one ' inputs' field " );
114
118
}
115
119
inputs = readInputs (reader );
116
120
break ;
117
121
case "requestId" :
118
122
if (requestId != null ) {
119
- throw new IOException ("Work request cannot have more than one requestId" );
123
+ throw new IOException ("WorkRequest cannot have more than one requestId" );
120
124
}
121
125
requestId = reader .nextInt ();
122
126
break ;
123
127
default :
124
- throw new IOException ( name + " is an incorrect field in work request" ) ;
128
+ break ;
125
129
}
126
130
}
127
131
reader .endObject ();
@@ -143,17 +147,13 @@ public WorkRequest readRequest() throws IOException {
143
147
}
144
148
145
149
@ Override
146
- public void writeResponse (WorkResponse response ) throws IOException {
150
+ public void writeWorkResponse (WorkResponse response ) throws IOException {
147
151
jsonPrinter .appendTo (response , jsonWriter );
148
152
jsonWriter .flush ();
149
153
}
150
154
151
155
@ Override
152
- public void close () {
153
- try {
154
- jsonWriter .close ();
155
- } catch (IOException e ) {
156
- System .err .printf ("Could not close json writer. %s" , e );
157
- }
156
+ public void close () throws IOException {
157
+ jsonWriter .close ();
158
158
}
159
159
}
0 commit comments