Home | History | Annotate | Download | only in monitor
      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 
     17 package vogar.monitor;
     18 
     19 import com.google.gson.Gson;
     20 import com.google.gson.JsonObject;
     21 import java.io.IOException;
     22 import java.io.PrintStream;
     23 import java.net.ServerSocket;
     24 import java.net.Socket;
     25 import vogar.Result;
     26 import vogar.target.Runner;
     27 
     28 /**
     29  * Accepts a connection from the host process. Once connected, XML is sent over
     30  * raw sockets.
     31  */
     32 public 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(Class<? extends Runner> runnerClass, String outcomeName) {
     68         JsonObject jsonObject = new JsonObject();
     69         jsonObject.addProperty("outcome", outcomeName);
     70         if (runnerClass != null) {
     71             jsonObject.addProperty("runner", runnerClass.getName());
     72         }
     73         writer.print(marker + gson.toJson(jsonObject) + "\n");
     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.print(marker + gson.toJson(jsonObject) + "\n");
     84     }
     85 
     86     public synchronized void close() throws IOException {
     87         writer.close();
     88     }
     89 
     90     public void completedNormally(boolean completedNormally) {
     91         JsonObject jsonObject = new JsonObject();
     92         jsonObject.addProperty("completedNormally", completedNormally);
     93         writer.print(marker + gson.toJson(jsonObject) + "\n");
     94     }
     95 }
     96