Home | History | Annotate | Download | only in adb
      1 /*
      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  */
     16 
     17 package com.android.adb;
     18 
     19 /* This class represents an adb socket.  adb supports multiple independent
     20  * socket connections to a single device.  Typically a socket is created
     21  * for each adb command that is executed.
     22  */
     23 public class AdbSocket {
     24 
     25     private final AdbDevice mDevice;
     26     private final int mId;
     27     private int mPeerId;
     28 
     29     public AdbSocket(AdbDevice device, int id) {
     30         mDevice = device;
     31         mId = id;
     32     }
     33 
     34     public int getId() {
     35         return mId;
     36     }
     37 
     38     public boolean open(String destination) {
     39         AdbMessage message = new AdbMessage();
     40         message.set(AdbMessage.A_OPEN, mId, 0, destination);
     41         if (! message.write(mDevice)) {
     42             return false;
     43         }
     44 
     45         synchronized (this) {
     46             try {
     47                 wait();
     48             } catch (InterruptedException e) {
     49                 return false;
     50             }
     51         }
     52         return true;
     53     }
     54 
     55     public void handleMessage(AdbMessage message) {
     56         switch (message.getCommand()) {
     57             case AdbMessage.A_OKAY:
     58                 mPeerId = message.getArg0();
     59                 synchronized (this) {
     60                     notify();
     61                 }
     62                 break;
     63             case AdbMessage.A_WRTE:
     64                 mDevice.log(message.getDataString());
     65                 sendReady();
     66                 break;
     67         }
     68     }
     69 
     70     private void sendReady() {
     71         AdbMessage message = new AdbMessage();
     72         message.set(AdbMessage.A_OKAY, mId, mPeerId);
     73         message.write(mDevice);
     74     }
     75 }
     76