Forwarder.java revision ef9a2175ee29b9a9c84ddd0a02d1ec80ab095222
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 17package com.android.dumprendertree2.forwarder; 18 19import android.util.Log; 20 21import java.io.IOException; 22import java.net.ServerSocket; 23import java.net.Socket; 24import java.util.HashSet; 25import java.util.Set; 26 27/** 28 * A port forwarding server. Listens on localhost on specified port and forwards the tcp 29 * communications to external socket via adb networking proxy. 30 */ 31public class Forwarder extends Thread { 32 private static final String LOG_TAG = "Forwarder"; 33 34 private int mPort; 35 private String mRemoteMachineIpAddress; 36 37 private Boolean mIsRunning = false; 38 private ServerSocket mServerSocket; 39 40 private Set<ConnectionHandler> mConnectionHandlers = new HashSet<ConnectionHandler>(); 41 42 public Forwarder(int port, String remoteMachineIpAddress) { 43 mPort = port; 44 mRemoteMachineIpAddress = remoteMachineIpAddress; 45 } 46 47 @Override 48 public void start() { 49 Log.i(LOG_TAG, "start(): Starting fowarder on port: " + mPort); 50 51 try { 52 mServerSocket = new ServerSocket(mPort); 53 } catch (IOException e) { 54 Log.e(LOG_TAG, "mPort=" + mPort, e); 55 return; 56 } 57 58 mIsRunning = true; 59 super.start(); 60 } 61 62 @Override 63 public void run() { 64 while (true) { 65 synchronized (this) { 66 if (!mIsRunning) { 67 return; 68 } 69 70 /** These sockets will be closed when Forwarder.stop() is called */ 71 Socket localSocket; 72 Socket remoteSocket; 73 try { 74 localSocket = mServerSocket.accept(); 75 remoteSocket = AdbUtils.getSocketToRemoteMachine(mRemoteMachineIpAddress, 76 mPort); 77 } catch (IOException e) { 78 /** This most likely means that mServerSocket is already closed */ 79 Log.w(LOG_TAG, "mPort=" + mPort, e); 80 return; 81 } 82 83 if (remoteSocket == null) { 84 try { 85 localSocket.close(); 86 } catch (IOException e) { 87 Log.e(LOG_TAG, "mPort=" + mPort, e); 88 } 89 90 Log.e(LOG_TAG, "run(): mPort= " + mPort + " Failed to start forwarding from " + 91 localSocket); 92 continue; 93 } 94 95 ConnectionHandler forwarder = new ConnectionHandler(localSocket, remoteSocket); 96 mConnectionHandlers.add(forwarder); 97 forwarder.start(); 98 99 } 100 } 101 } 102 103 public void finish() { 104 try { 105 mServerSocket.close(); 106 } catch (IOException e) { 107 Log.e(LOG_TAG, "mPort=" + mPort, e); 108 } 109 110 synchronized (this) { 111 mIsRunning = false; 112 } 113 114 for (ConnectionHandler connectionHandler : mConnectionHandlers) { 115 connectionHandler.stop(); 116 } 117 mConnectionHandlers.clear(); 118 } 119}