FileDocCategorySizeDatePackage
ProxyServer.javaAPI DocAndroid 5.1 API8960Thu Mar 12 22:22:42 GMT 2015com.android.proxyhandler

ProxyServer

public class ProxyServer extends Thread
hide

Fields Summary
private static final String
CONNECT
private static final String
HTTP_OK
private static final String
TAG
private ExecutorService
threadExecutor
public boolean
mIsRunning
private ServerSocket
serverSocket
private int
mPort
private com.android.net.IProxyPortListener
mCallback
Constructors Summary
public ProxyServer()

        threadExecutor = Executors.newCachedThreadPool();
        mPort = -1;
        mCallback = null;
    
Methods Summary
public intgetPort()

        return mPort;
    
public booleanisBound()

        return (mPort != -1);
    
public voidrun()

        try {
            serverSocket = new ServerSocket(0);

            if (serverSocket != null) {
                setPort(serverSocket.getLocalPort());

                while (mIsRunning) {
                    try {
                        Socket socket = serverSocket.accept();
                        // Only receive local connections.
                        if (socket.getInetAddress().isLoopbackAddress()) {
                            ProxyConnection parser = new ProxyConnection(socket);

                            threadExecutor.execute(parser);
                        } else {
                            socket.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (SocketException e) {
            Log.e(TAG, "Failed to start proxy server", e);
        } catch (IOException e1) {
            Log.e(TAG, "Failed to start proxy server", e1);
        }

        mIsRunning = false;
    
public synchronized voidsetCallback(com.android.net.IProxyPortListener callback)

        if (mPort != -1) {
            try {
                callback.setProxyPort(mPort);
            } catch (RemoteException e) {
                Log.w(TAG, "Proxy failed to report port to PacManager", e);
            }
        }
        mCallback = callback;
    
public synchronized voidsetPort(int port)

        if (mCallback != null) {
            try {
                mCallback.setProxyPort(port);
            } catch (RemoteException e) {
                Log.w(TAG, "Proxy failed to report port to PacManager", e);
            }
        }
        mPort = port;
    
public synchronized voidstartServer()

        mIsRunning = true;
        start();
    
public synchronized voidstopServer()

        mIsRunning = false;
        if (serverSocket != null) {
            try {
                serverSocket.close();
                serverSocket = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }