Home | History | Annotate | Download | only in forwarder
      1 /*
      2  * Copyright (C) 2009 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.dumprendertree.forwarder;
     18 
     19 import android.util.Log;
     20 
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.io.OutputStream;
     24 import java.net.Socket;
     25 
     26 /**
     27  *
     28  * Worker class for {@link ForwardServer}. A Forwarder will be created once the ForwardServer
     29  * accepts an incoming connection, and it will then forward the incoming/outgoing streams to a
     30  * connection already proxied by adb networking (see also {@link AdbUtils}).
     31  *
     32  */
     33 public class Forwarder {
     34 
     35     private ForwardServer server;
     36     private Socket from, to;
     37 
     38     private static final String LOGTAG = "Forwarder";
     39     private static final int BUFFER_SIZE = 16384;
     40 
     41     public Forwarder (Socket from, Socket to, ForwardServer server) {
     42         this.server = server;
     43         this.from = from;
     44         this.to = to;
     45         server.register(this);
     46     }
     47 
     48     public void start() {
     49         Thread outgoing = new Thread(new SocketPipe(from, to));
     50         Thread incoming = new Thread(new SocketPipe(to, from));
     51         outgoing.setName(LOGTAG);
     52         incoming.setName(LOGTAG);
     53         outgoing.start();
     54         incoming.start();
     55     }
     56 
     57     public void stop() {
     58         shutdown(from);
     59         shutdown(to);
     60     }
     61 
     62     private void shutdown(Socket socket) {
     63         try {
     64             socket.shutdownInput();
     65         } catch (IOException e) {
     66             Log.v(LOGTAG, "Socket#shutdownInput", e);
     67         }
     68         try {
     69             socket.shutdownOutput();
     70         } catch (IOException e) {
     71             Log.v(LOGTAG, "Socket#shutdownOutput", e);
     72         }
     73         try {
     74             socket.close();
     75         } catch (IOException e) {
     76             Log.v(LOGTAG, "Socket#close", e);
     77         }
     78     }
     79 
     80     private class SocketPipe implements Runnable {
     81 
     82         private Socket in, out;
     83 
     84         public SocketPipe(Socket in, Socket out) {
     85             this.in = in;
     86             this.out = out;
     87         }
     88 
     89         public void run() {
     90             try {
     91                 int length;
     92                 InputStream is = in.getInputStream();
     93                 OutputStream os = out.getOutputStream();
     94                 byte[] buffer = new byte[BUFFER_SIZE];
     95                 while ((length = is.read(buffer)) > 0) {
     96                     os.write(buffer, 0, length);
     97                 }
     98             } catch (IOException ioe) {
     99             } finally {
    100                 server.unregister(Forwarder.this);
    101             }
    102         }
    103 
    104         @Override
    105         public String toString() {
    106             return "SocketPipe{" + in + "=>" + out  + "}";
    107         }
    108     }
    109 }
    110