Forwarder.java revision 5926723f82fbdd9b523193e05f901784904b6d38
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 final ConnectionHandler connectionHandler = 96 new ConnectionHandler(localSocket, remoteSocket); 97 98 /** 99 * We have to close the sockets after the ConnectionHandler finishes, so we 100 * don't get "Too may open files" exception. We also remove the ConnectionHandler 101 * from the collection to avoid memory issues. 102 * */ 103 ConnectionHandler.OnFinishedCallback callback = 104 new ConnectionHandler.OnFinishedCallback() { 105 @Override 106 public void onFinished() { 107 removeConncetionHandler(connectionHandler); 108 } 109 }; 110 connectionHandler.registerOnConnectionHandlerFinishedCallback(callback); 111 112 mConnectionHandlers.add(connectionHandler); 113 connectionHandler.start(); 114 } 115 } 116 } 117 118 private synchronized void removeConncetionHandler(ConnectionHandler connectionHandler) { 119 if (mConnectionHandlers.remove(connectionHandler)) { 120 Log.d(LOG_TAG, "removeConnectionHandler(): removed"); 121 } else { 122 Log.d(LOG_TAG, "removeConnectionHandler(): not in the collection"); 123 } 124 } 125 126 public void finish() { 127 try { 128 mServerSocket.close(); 129 } catch (IOException e) { 130 Log.e(LOG_TAG, "mPort=" + mPort, e); 131 } 132 133 synchronized (this) { 134 mIsRunning = false; 135 } 136 137 for (ConnectionHandler connectionHandler : mConnectionHandlers) { 138 connectionHandler.stop(); 139 } 140 mConnectionHandlers.clear(); 141 } 142}