Search in sources :

Example 1 with NamedRunnable

use of okhttp3.internal.NamedRunnable in project okhttp by square.

the class SocksProxy method service.

private void service(final Socket from) {
    executor.execute(new NamedRunnable("SocksProxy %s", from.getRemoteSocketAddress()) {

        @Override
        protected void execute() {
            try {
                BufferedSource fromSource = Okio.buffer(Okio.source(from));
                BufferedSink fromSink = Okio.buffer(Okio.sink(from));
                hello(fromSource, fromSink);
                acceptCommand(from.getInetAddress(), fromSource, fromSink);
            } catch (IOException e) {
                logger.log(Level.WARNING, name + " failed", e);
                Util.closeQuietly(from);
            }
        }
    });
}
Also used : BufferedSink(okio.BufferedSink) IOException(java.io.IOException) NamedRunnable(okhttp3.internal.NamedRunnable) BufferedSource(okio.BufferedSource)

Example 2 with NamedRunnable

use of okhttp3.internal.NamedRunnable in project okhttp by square.

the class SocksProxy method play.

public void play() throws IOException {
    serverSocket = new ServerSocket(0);
    executor.execute(new NamedRunnable("SocksProxy %s", serverSocket.getLocalPort()) {

        @Override
        protected void execute() {
            try {
                while (true) {
                    Socket socket = serverSocket.accept();
                    connectionCount.incrementAndGet();
                    service(socket);
                }
            } catch (SocketException e) {
                logger.info(name + " done accepting connections: " + e.getMessage());
            } catch (IOException e) {
                logger.log(Level.WARNING, name + " failed unexpectedly", e);
            }
        }
    });
}
Also used : SocketException(java.net.SocketException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) NamedRunnable(okhttp3.internal.NamedRunnable) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 3 with NamedRunnable

use of okhttp3.internal.NamedRunnable in project okhttp by square.

the class Crawler method parallelDrainQueue.

private void parallelDrainQueue(int threadCount) {
    ExecutorService executor = Executors.newFixedThreadPool(threadCount);
    for (int i = 0; i < threadCount; i++) {
        executor.execute(new NamedRunnable("Crawler %s", i) {

            @Override
            protected void execute() {
                try {
                    drainQueue();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executor.shutdown();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) NamedRunnable(okhttp3.internal.NamedRunnable) IOException(java.io.IOException)

Example 4 with NamedRunnable

use of okhttp3.internal.NamedRunnable in project okhttp by square.

the class MockWebServer method start.

/**
   * Starts the server and binds to the given socket address.
   *
   * @param inetSocketAddress the socket address to bind the server on
   */
private synchronized void start(InetSocketAddress inetSocketAddress) throws IOException {
    if (started)
        throw new IllegalStateException("start() already called");
    started = true;
    executor = Executors.newCachedThreadPool(Util.threadFactory("MockWebServer", false));
    this.inetSocketAddress = inetSocketAddress;
    serverSocket = serverSocketFactory.createServerSocket();
    // Reuse if the user specified a port
    serverSocket.setReuseAddress(inetSocketAddress.getPort() != 0);
    serverSocket.bind(inetSocketAddress, 50);
    port = serverSocket.getLocalPort();
    executor.execute(new NamedRunnable("MockWebServer %s", port) {

        @Override
        protected void execute() {
            try {
                logger.info(MockWebServer.this + " starting to accept connections");
                acceptConnections();
            } catch (Throwable e) {
                logger.log(Level.WARNING, MockWebServer.this + " failed unexpectedly", e);
            }
            // Release all sockets and all threads, even if any close fails.
            closeQuietly(serverSocket);
            for (Iterator<Socket> s = openClientSockets.iterator(); s.hasNext(); ) {
                closeQuietly(s.next());
                s.remove();
            }
            for (Iterator<Http2Connection> s = openConnections.iterator(); s.hasNext(); ) {
                closeQuietly(s.next());
                s.remove();
            }
            dispatcher.shutdown();
            executor.shutdown();
        }

        private void acceptConnections() throws Exception {
            while (true) {
                Socket socket;
                try {
                    socket = serverSocket.accept();
                } catch (SocketException e) {
                    logger.info(MockWebServer.this + " done accepting connections: " + e.getMessage());
                    return;
                }
                SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy();
                if (socketPolicy == DISCONNECT_AT_START) {
                    dispatchBookkeepingRequest(0, socket);
                    socket.close();
                } else {
                    openClientSockets.add(socket);
                    serveConnection(socket);
                }
            }
        }
    });
}
Also used : SocketException(java.net.SocketException) Iterator(java.util.Iterator) NamedRunnable(okhttp3.internal.NamedRunnable) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ProtocolException(java.net.ProtocolException) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) RealWebSocket(okhttp3.internal.ws.RealWebSocket) Socket(java.net.Socket)

Example 5 with NamedRunnable

use of okhttp3.internal.NamedRunnable in project okhttp by square.

the class Http2Connection method pushDataLater.

/**
   * Eagerly reads {@code byteCount} bytes from the source before launching a background task to
   * process the data.  This avoids corrupting the stream.
   */
void pushDataLater(final int streamId, final BufferedSource source, final int byteCount, final boolean inFinished) throws IOException {
    final Buffer buffer = new Buffer();
    // Eagerly read the frame before firing client thread.
    source.require(byteCount);
    source.read(buffer, byteCount);
    if (buffer.size() != byteCount)
        throw new IOException(buffer.size() + " != " + byteCount);
    pushExecutor.execute(new NamedRunnable("OkHttp %s Push Data[%s]", hostname, streamId) {

        @Override
        public void execute() {
            try {
                boolean cancel = pushObserver.onData(streamId, buffer, byteCount, inFinished);
                if (cancel)
                    writer.rstStream(streamId, ErrorCode.CANCEL);
                if (cancel || inFinished) {
                    synchronized (Http2Connection.this) {
                        currentPushRequests.remove(streamId);
                    }
                }
            } catch (IOException ignored) {
            }
        }
    });
}
Also used : Buffer(okio.Buffer) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) NamedRunnable(okhttp3.internal.NamedRunnable)

Aggregations

IOException (java.io.IOException)5 NamedRunnable (okhttp3.internal.NamedRunnable)5 InterruptedIOException (java.io.InterruptedIOException)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 SocketException (java.net.SocketException)2 ProtocolException (java.net.ProtocolException)1 CertificateException (java.security.cert.CertificateException)1 Iterator (java.util.Iterator)1 ExecutorService (java.util.concurrent.ExecutorService)1 SSLSocket (javax.net.ssl.SSLSocket)1 RealWebSocket (okhttp3.internal.ws.RealWebSocket)1 Buffer (okio.Buffer)1 BufferedSink (okio.BufferedSink)1 BufferedSource (okio.BufferedSource)1