ConnectionHandler.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.io.InputStream;
23import java.io.OutputStream;
24import java.net.Socket;
25
26/**
27 * Worker class for {@link Forwarder}. A ConnectionHandler will be created once the Forwarder
28 * accepts an incoming connection, and it will then forward the incoming/outgoing streams to a
29 * connection already proxied by adb networking (see also {@link AdbUtils}).
30 */
31public class ConnectionHandler {
32
33    private static final String LOG_TAG = "ConnectionHandler";
34
35    public static interface OnFinishedCallback {
36        public void onFinished();
37    }
38
39    private class SocketPipeThread extends Thread {
40
41        private Socket mInSocket, mOutSocket;
42
43        public SocketPipeThread(Socket inSocket, Socket outSocket) {
44            mInSocket = inSocket;
45            mOutSocket = outSocket;
46        }
47
48        @Override
49        public void run() {
50            InputStream is;
51            OutputStream os;
52            try {
53                synchronized (this) {
54                    is = mInSocket.getInputStream();
55                    os = mOutSocket.getOutputStream();
56                }
57            } catch (IOException e) {
58                Log.w(LOG_TAG, this.toString(), e);
59                return;
60            }
61
62            byte[] buffer = new byte[4096];
63            int length;
64            while (true) {
65                try {
66                    if ((length = is.read(buffer)) <= 0) {
67                        break;
68                    }
69                    os.write(buffer, 0, length);
70                } catch (IOException e) {
71                    /** This exception means one of the streams is closed */
72                    Log.v(LOG_TAG, this.toString(), e);
73                    break;
74                }
75            }
76
77            ConnectionHandler.this.stop();
78            mOnFinishedCallback.onFinished();
79        }
80
81        @Override
82        public String toString() {
83            return "SocketPipeThread:\n" + mInSocket + "\n=>\n" + mOutSocket;
84        }
85    }
86
87    private Socket mFromSocket, mToSocket;
88    private SocketPipeThread mFromToPipe, mToFromPipe;
89
90    private OnFinishedCallback mOnFinishedCallback;
91
92    public ConnectionHandler(Socket fromSocket, Socket toSocket) {
93        mFromSocket = fromSocket;
94        mToSocket = toSocket;
95        mFromToPipe = new SocketPipeThread(mFromSocket, mToSocket);
96        mToFromPipe = new SocketPipeThread(mToSocket, mFromSocket);
97    }
98
99    public void registerOnConnectionHandlerFinishedCallback(OnFinishedCallback callback) {
100        mOnFinishedCallback = callback;
101    }
102
103    public void start() {
104        mFromToPipe.start();
105        mToFromPipe.start();
106    }
107
108    public void stop() {
109        shutdown(mFromSocket);
110        shutdown(mToSocket);
111    }
112
113    private void shutdown(Socket socket) {
114        synchronized (mFromToPipe) {
115            synchronized (mToFromPipe) {
116                /** This will stop the while loop in the run method */
117                try {
118                    if (!socket.isInputShutdown()) {
119                        socket.shutdownInput();
120                    }
121                } catch (IOException e) {
122                    Log.e(LOG_TAG, "mFromToPipe=" + mFromToPipe + " mToFromPipe=" + mToFromPipe, e);
123                }
124                try {
125                    if (!socket.isOutputShutdown()) {
126                        socket.shutdownOutput();
127                    }
128                } catch (IOException e) {
129                    Log.e(LOG_TAG, "mFromToPipe=" + mFromToPipe + " mToFromPipe=" + mToFromPipe, e);
130                }
131                try {
132                    if (!socket.isClosed()) {
133                        socket.close();
134                    }
135                } catch (IOException e) {
136                    Log.e(LOG_TAG, "mFromToPipe=" + mFromToPipe + " mToFromPipe=" + mToFromPipe, e);
137                }
138            }
139        }
140    }
141}