Forwarder.java revision 56d7e400ece64591685c8a21dbb82a94a7bd8010
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        synchronized (this) {
51            if (mIsRunning) {
52                Log.w(LOG_TAG, "start(): Forwarder on port: " + mPort + " already running! NOOP.");
53                return;
54            }
55        }
56
57        try {
58            mServerSocket = new ServerSocket(mPort);
59        } catch (IOException e) {
60            Log.e(LOG_TAG, "mPort=" + mPort, e);
61            return;
62        }
63
64        mIsRunning = true;
65        super.start();
66    }
67
68    @Override
69    public void run() {
70        while (true) {
71            synchronized (this) {
72                if (!mIsRunning) {
73                    return;
74                }
75
76                /** These sockets will be closed when Forwarder.stop() is called */
77                Socket localSocket;
78                Socket remoteSocket;
79                try {
80                    localSocket = mServerSocket.accept();
81                    remoteSocket = AdbUtils.getSocketToRemoteMachine(mRemoteMachineIpAddress,
82                            mPort);
83                } catch (IOException e) {
84                    /** This most likely means that mServerSocket is already closed */
85                    Log.w(LOG_TAG + "mPort=" + mPort, e);
86                    return;
87                }
88
89                if (remoteSocket == null) {
90                    try {
91                        localSocket.close();
92                    } catch (IOException e) {
93                        Log.e(LOG_TAG, "mPort=" + mPort, e);
94                    }
95
96                    Log.e(LOG_TAG, "run(): mPort= " + mPort + " Failed to start forwarding from " +
97                            localSocket);
98                    continue;
99                }
100
101                ConnectionHandler forwarder = new ConnectionHandler(localSocket, remoteSocket);
102                mConnectionHandlers.add(forwarder);
103                forwarder.start();
104
105            }
106        }
107    }
108
109    public void finish() {
110        synchronized (this) {
111            if (!mIsRunning) {
112                return;
113            }
114        }
115
116        try {
117            mServerSocket.close();
118        } catch (IOException e) {
119            Log.e(LOG_TAG, "mPort=" + mPort, e);
120        }
121
122        synchronized (this) {
123            mIsRunning = false;
124        }
125
126        for (ConnectionHandler connectionHandler : mConnectionHandlers) {
127            connectionHandler.stop();
128        }
129        mConnectionHandlers.clear();
130    }
131}