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);
}
}
});
}
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);
}
}
});
}
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();
}
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);
}
}
}
});
}
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) {
}
}
});
}
Aggregations