use of org.apache.nifi.remote.io.socket.SocketChannelOutputStream in project nifi by apache.
the class SocketChannelSender method open.
@Override
public void open() throws IOException {
try {
if (channel == null) {
channel = SocketChannel.open();
channel.configureBlocking(false);
if (maxSendBufferSize > 0) {
channel.setOption(StandardSocketOptions.SO_SNDBUF, maxSendBufferSize);
final int actualSendBufSize = channel.getOption(StandardSocketOptions.SO_SNDBUF);
if (actualSendBufSize < maxSendBufferSize) {
logger.warn("Attempted to set Socket Send Buffer Size to " + maxSendBufferSize + " bytes but could only set to " + actualSendBufSize + "bytes. You may want to " + "consider changing the Operating System's maximum send buffer");
}
}
}
if (!channel.isConnected()) {
final long startTime = System.currentTimeMillis();
final InetSocketAddress socketAddress = new InetSocketAddress(InetAddress.getByName(host), port);
if (!channel.connect(socketAddress)) {
while (!channel.finishConnect()) {
if (System.currentTimeMillis() > startTime + timeout) {
throw new SocketTimeoutException("Timed out connecting to " + host + ":" + port);
}
try {
Thread.sleep(50L);
} catch (final InterruptedException e) {
}
}
}
if (logger.isDebugEnabled()) {
final SocketAddress localAddress = channel.getLocalAddress();
if (localAddress != null && localAddress instanceof InetSocketAddress) {
final InetSocketAddress inetSocketAddress = (InetSocketAddress) localAddress;
logger.debug("Connected to local port {}", new Object[] { inetSocketAddress.getPort() });
}
}
socketChannelOutput = new SocketChannelOutputStream(channel);
socketChannelOutput.setTimeout(timeout);
}
} catch (final IOException e) {
IOUtils.closeQuietly(channel);
throw e;
}
}
use of org.apache.nifi.remote.io.socket.SocketChannelOutputStream in project nifi by apache.
the class AbstractCacheServer method start.
@Override
public void start() throws IOException {
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(true);
serverSocketChannel.bind(new InetSocketAddress(port));
final Runnable runnable = new Runnable() {
@Override
public void run() {
while (true) {
final SocketChannel socketChannel;
try {
socketChannel = serverSocketChannel.accept();
logger.debug("Connected to {}", new Object[] { socketChannel });
} catch (final IOException e) {
if (!stopped) {
logger.error("{} unable to accept connection from remote peer due to {}", this, e.toString());
if (logger.isDebugEnabled()) {
logger.error("", e);
}
}
return;
}
final Runnable processInputRunnable = new Runnable() {
@Override
public void run() {
final InputStream rawInputStream;
final OutputStream rawOutputStream;
final String peer = socketChannel.socket().getInetAddress().getHostName();
try {
if (sslContext == null) {
rawInputStream = new SocketChannelInputStream(socketChannel);
rawOutputStream = new SocketChannelOutputStream(socketChannel);
} else {
final SSLSocketChannel sslSocketChannel = new SSLSocketChannel(sslContext, socketChannel, false);
sslSocketChannel.connect();
rawInputStream = new SSLSocketChannelInputStream(sslSocketChannel);
rawOutputStream = new SSLSocketChannelOutputStream(sslSocketChannel);
}
} catch (IOException e) {
logger.error("Cannot create input and/or output streams for {}", new Object[] { identifier }, e);
if (logger.isDebugEnabled()) {
logger.error("", e);
}
try {
socketChannel.close();
} catch (IOException swallow) {
}
return;
}
try (final InputStream in = new BufferedInputStream(rawInputStream);
final OutputStream out = new BufferedOutputStream(rawOutputStream)) {
final VersionNegotiator versionNegotiator = getVersionNegotiator();
ProtocolHandshake.receiveHandshake(in, out, versionNegotiator);
boolean continueComms = true;
while (continueComms) {
continueComms = listen(in, out, versionNegotiator.getVersion());
}
// client has issued 'close'
logger.debug("Client issued close on {}", new Object[] { socketChannel });
} catch (final SocketTimeoutException e) {
logger.debug("30 sec timeout reached", e);
} catch (final IOException | HandshakeException e) {
if (!stopped) {
logger.error("{} unable to communicate with remote peer {} due to {}", new Object[] { this, peer, e.toString() });
if (logger.isDebugEnabled()) {
logger.error("", e);
}
}
} finally {
processInputThreads.remove(Thread.currentThread());
}
}
};
final Thread processInputThread = new Thread(processInputRunnable);
processInputThread.setName("Distributed Cache Server Communications Thread: " + identifier);
processInputThread.setDaemon(true);
processInputThread.start();
processInputThreads.add(processInputThread);
}
}
};
final Thread thread = new Thread(runnable);
thread.setDaemon(true);
thread.setName("Distributed Cache Server: " + identifier);
thread.start();
}
Aggregations