TargetMonitor.java revision 693bab6f249797311a9c4bcd4c9d9c7cfd5ae8d3
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package vogar.monitor;
18
19import com.google.caliper.internal.gson.Gson;
20import com.google.caliper.internal.gson.JsonObject;
21import java.io.IOException;
22import java.io.PrintStream;
23import java.net.ServerSocket;
24import java.net.Socket;
25import vogar.Result;
26import vogar.target.Runner;
27
28/**
29 * Accepts a connection from the host process. Once connected, XML is sent over
30 * raw sockets.
31 */
32public class TargetMonitor {
33
34    private static final int ACCEPT_TIMEOUT_MILLIS = 10 * 1000;
35
36    private final Gson gson = new Gson();
37    private final String marker = "//00xx";
38
39    private final PrintStream writer;
40
41    private TargetMonitor(PrintStream writer) {
42        this.writer = writer;
43    }
44
45    public static TargetMonitor forPrintStream(PrintStream printStream) {
46        return new TargetMonitor(printStream);
47    }
48
49    public static TargetMonitor await(int port) {
50        try {
51            final ServerSocket serverSocket = new ServerSocket(port);
52            serverSocket.setSoTimeout(ACCEPT_TIMEOUT_MILLIS);
53            serverSocket.setReuseAddress(true);
54            final Socket socket = serverSocket.accept();
55            return new TargetMonitor(new PrintStream(socket.getOutputStream())) {
56                @Override public void close() throws IOException {
57                    socket.close();
58                    serverSocket.close();
59                }
60            };
61
62        } catch (IOException e) {
63            throw new RuntimeException("Failed to accept a monitor on localhost:" + port, e);
64        }
65    }
66
67    public void outcomeStarted(Runner runner, String outcomeName, String actionName) {
68        JsonObject jsonObject = new JsonObject();
69        jsonObject.addProperty("outcome", outcomeName);
70        if (runner != null) {
71            jsonObject.addProperty("runner", runner.getClass().getName());
72        }
73        writer.println(marker + gson.toJson(jsonObject));
74    }
75
76    public void output(String text) {
77        writer.print(text);
78    }
79
80    public void outcomeFinished(Result result) {
81        JsonObject jsonObject = new JsonObject();
82        jsonObject.addProperty("result", result.name());
83        writer.println(marker + gson.toJson(jsonObject));
84    }
85
86    public synchronized void close() throws IOException {}
87}
88