Home | History | Annotate | Download | only in device
      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 com.android.hierarchyviewerlib.device;
     18 
     19 import com.android.ddmlib.IDevice;
     20 
     21 import java.io.BufferedReader;
     22 import java.io.BufferedWriter;
     23 import java.io.IOException;
     24 import java.io.InputStreamReader;
     25 import java.io.OutputStreamWriter;
     26 import java.net.InetSocketAddress;
     27 import java.net.Socket;
     28 import java.nio.channels.SocketChannel;
     29 
     30 /**
     31  * This class is used for connecting to a device in debug mode running the view
     32  * server.
     33  */
     34 public class DeviceConnection {
     35 
     36     // Now a socket channel, since socket channels are friendly with interrupts.
     37     private SocketChannel mSocketChannel;
     38 
     39     private BufferedReader mIn;
     40 
     41     private BufferedWriter mOut;
     42 
     43     public DeviceConnection(IDevice device) throws IOException {
     44         mSocketChannel = SocketChannel.open();
     45         int port = DeviceBridge.getDeviceLocalPort(device);
     46 
     47         if (port == -1) {
     48             throw new IOException();
     49         }
     50 
     51         mSocketChannel.connect(new InetSocketAddress("127.0.0.1", port)); //$NON-NLS-1$
     52         mSocketChannel.socket().setSoTimeout(40000);
     53     }
     54 
     55     public BufferedReader getInputStream() throws IOException {
     56         if (mIn == null) {
     57             mIn = new BufferedReader(new InputStreamReader(mSocketChannel.socket().getInputStream()));
     58         }
     59         return mIn;
     60     }
     61 
     62     public BufferedWriter getOutputStream() throws IOException {
     63         if (mOut == null) {
     64             mOut =
     65                     new BufferedWriter(new OutputStreamWriter(mSocketChannel.socket()
     66                             .getOutputStream()));
     67         }
     68         return mOut;
     69     }
     70 
     71     public Socket getSocket() {
     72         return mSocketChannel.socket();
     73     }
     74 
     75     public void sendCommand(String command) throws IOException {
     76         BufferedWriter out = getOutputStream();
     77         out.write(command);
     78         out.newLine();
     79         out.flush();
     80     }
     81 
     82     public void close() {
     83         try {
     84             if (mIn != null) {
     85                 mIn.close();
     86             }
     87         } catch (IOException e) {
     88         }
     89         try {
     90             if (mOut != null) {
     91                 mOut.close();
     92             }
     93         } catch (IOException e) {
     94         }
     95         try {
     96             mSocketChannel.close();
     97         } catch (IOException e) {
     98         }
     99     }
    100 }
    101