use of java.net.SocketAddress in project jetty.project by eclipse.
the class SelectorManager method chooseSelector.
private ManagedSelector chooseSelector(SelectableChannel channel) {
// Ideally we would like to have all connections from the same client end
// up on the same selector (to try to avoid smearing the data from a single
// client over all cores), but because of proxies, the remote address may not
// really be the client - so we have to hedge our bets to ensure that all
// channels don't end up on the one selector for a proxy.
ManagedSelector candidate1 = null;
if (channel != null) {
try {
if (channel instanceof SocketChannel) {
SocketAddress remote = ((SocketChannel) channel).getRemoteAddress();
if (remote instanceof InetSocketAddress) {
byte[] addr = ((InetSocketAddress) remote).getAddress().getAddress();
if (addr != null) {
int s = addr[addr.length - 1] & 0xFF;
candidate1 = _selectors[s % getSelectorCount()];
}
}
}
} catch (IOException x) {
LOG.ignore(x);
}
}
// The ++ increment here is not atomic, but it does not matter,
// so long as the value changes sometimes, then connections will
// be distributed over the available selectors.
long s = _selectorIndex++;
int index = (int) (s % getSelectorCount());
ManagedSelector candidate2 = _selectors[index];
if (candidate1 == null || candidate1.size() >= candidate2.size() * 2)
return candidate2;
return candidate1;
}
use of java.net.SocketAddress in project jetty.project by eclipse.
the class BadRequestLogHandlerTest method testLogHandler.
@Test(timeout = 4000)
public void testLogHandler() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
CaptureLog captureLog = new CaptureLog();
RequestLogHandler requestLog = new RequestLogHandler();
requestLog.setRequestLog(captureLog);
requestLog.setHandler(new HelloHandler());
server.setHandler(requestLog);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
InetAddress destAddr = InetAddress.getByName(host);
int port = connector.getLocalPort();
SocketAddress endpoint = new InetSocketAddress(destAddr, port);
Socket socket = new Socket();
socket.setSoTimeout(1000);
socket.connect(endpoint);
try (OutputStream out = socket.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
InputStream in = socket.getInputStream();
InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
StringReader request = new StringReader(requestHeader);
IO.copy(request, writer);
writer.flush();
StringWriter response = new StringWriter();
IO.copy(reader, response);
LOG.info("Response: {}", response);
} finally {
socket.close();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of java.net.SocketAddress in project jetty.project by eclipse.
the class BlockheadClient method connect.
/* (non-Javadoc)
* @see org.eclipse.jetty.websocket.common.test.IBlockheadClient#connect()
*/
@Override
public void connect() throws IOException {
InetAddress destAddr = InetAddress.getByName(destHttpURI.getHost());
int port = destHttpURI.getPort();
SocketAddress endpoint = new InetSocketAddress(destAddr, port);
socket = new Socket();
socket.setSoTimeout(timeout);
socket.connect(endpoint);
out = socket.getOutputStream();
in = socket.getInputStream();
}
use of java.net.SocketAddress in project k-9 by k9mail.
the class ImapConnection method connectToAddress.
private Socket connectToAddress(InetAddress address) throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {
String host = settings.getHost();
int port = settings.getPort();
String clientCertificateAlias = settings.getClientCertificateAlias();
if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) {
Log.d(LOG_TAG, "Connecting to " + host + " as " + address);
}
SocketAddress socketAddress = new InetSocketAddress(address, port);
Socket socket;
if (settings.getConnectionSecurity() == ConnectionSecurity.SSL_TLS_REQUIRED) {
socket = socketFactory.createSocket(null, host, port, clientCertificateAlias);
} else {
socket = new Socket();
}
socket.connect(socketAddress, socketConnectTimeout);
return socket;
}
use of java.net.SocketAddress in project flink by apache.
the class PartitionRequestClientHandler method decodeMsg.
private boolean decodeMsg(Object msg, boolean isStagedBuffer) throws Throwable {
final Class<?> msgClazz = msg.getClass();
// ---- Buffer --------------------------------------------------------
if (msgClazz == NettyMessage.BufferResponse.class) {
NettyMessage.BufferResponse bufferOrEvent = (NettyMessage.BufferResponse) msg;
RemoteInputChannel inputChannel = inputChannels.get(bufferOrEvent.receiverId);
if (inputChannel == null) {
bufferOrEvent.releaseBuffer();
cancelRequestFor(bufferOrEvent.receiverId);
return true;
}
return decodeBufferOrEvent(inputChannel, bufferOrEvent, isStagedBuffer);
} else // ---- Error ---------------------------------------------------------
if (msgClazz == NettyMessage.ErrorResponse.class) {
NettyMessage.ErrorResponse error = (NettyMessage.ErrorResponse) msg;
SocketAddress remoteAddr = ctx.channel().remoteAddress();
if (error.isFatalError()) {
notifyAllChannelsOfErrorAndClose(new RemoteTransportException("Fatal error at remote task manager '" + remoteAddr + "'.", remoteAddr, error.cause));
} else {
RemoteInputChannel inputChannel = inputChannels.get(error.receiverId);
if (inputChannel != null) {
if (error.cause.getClass() == PartitionNotFoundException.class) {
inputChannel.onFailedPartitionRequest();
} else {
inputChannel.onError(new RemoteTransportException("Error at remote task manager '" + remoteAddr + "'.", remoteAddr, error.cause));
}
}
}
} else {
throw new IllegalStateException("Received unknown message from producer: " + msg.getClass());
}
return true;
}
Aggregations