blob: d81390cedad3438812ae3d1b6b28cde05ef9c134 [file] [log] [blame]
Brett Chabot33513a52011-11-09 18:20:04 -08001/*
2 * Copyright (C) 2011 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 */
16package com.android.tradefed.command;
17
18import com.android.tradefed.util.ArrayUtil;
19
20import java.io.BufferedReader;
21import java.io.IOException;
22import java.io.InputStreamReader;
23import java.io.PrintWriter;
24import java.net.InetAddress;
25import java.net.Socket;
26import java.net.UnknownHostException;
27
28/**
29 * Class for sending remote commands to another TF process via sockets.
30 */
31public class RemoteClient {
32
33 private final Socket mSocket;
34 private final PrintWriter mWriter;
35 private final BufferedReader mReader;
36
37 /**
38 * @param port
39 * @throws IOException
40 * @throws UnknownHostException
41 */
42 RemoteClient(int port) throws UnknownHostException, IOException {
43 String hostName = InetAddress.getLocalHost().getHostName();
44 mSocket = new Socket(hostName, port);
45 mWriter = new PrintWriter(mSocket.getOutputStream(), true);
46 mReader = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
47 }
48
49 private synchronized boolean sendCommand(String... cmd) throws IOException {
50 // TODO: use a more standard data protocol - such as Json
51 mWriter.println(ArrayUtil.join(RemoteManager.DELIM, (Object[])cmd));
52 String response = mReader.readLine();
53 return response != null && Boolean.parseBoolean(response);
54 }
55
56 public static RemoteClient connect(int port) throws UnknownHostException, IOException {
57 return new RemoteClient(port);
58 }
59
60 /**
61 * Send a 'add this device to global ignore filter' command
62 * @param serial
63 * @throws IOException
64 */
65 public boolean sendFilterDevice(String serial) throws IOException {
66 return sendCommand(RemoteManager.FILTER, serial);
67 }
68
69 /**
70 * Send a 'remove this device from global ignore filter' command
71 * @param serial
72 * @throws IOException
73 */
74 public boolean sendUnfilterDevice(String serial) throws IOException {
75 return sendCommand(RemoteManager.UNFILTER, serial);
76 }
77
78 /**
Brett Chabot33513a52011-11-09 18:20:04 -080079 * Send a 'add command' command.
80 *
81 * @param commandArgs
82 */
83 public boolean sendAddCommand(long totalTime, String... commandArgs) throws IOException {
84 String[] fullList = ArrayUtil.buildArray(new String[] {RemoteManager.ADD_COMMAND,
85 Long.toString(totalTime)}, commandArgs);
86 return sendCommand(fullList);
87 }
88
89 /**
90 * Send a 'close connection' command
Omari Stephensc9e52682013-03-07 19:22:20 -080091 *
Brett Chabot33513a52011-11-09 18:20:04 -080092 * @throws IOException
93 */
94 public boolean sendClose() throws IOException {
95 return sendCommand(RemoteManager.CLOSE);
96 }
97
98 /**
99 * Close the connection to the {@link RemoteManager}.
100 */
101 public synchronized void close() {
102 if (mSocket != null) {
103 try {
104 mSocket.close();
105 } catch (IOException e) {
106 // ignore
107 }
108 }
109 if (mWriter != null) {
110 mWriter.close();
111 }
112 }
113}
114