|
1 | 1 | package com.redhat.hacbs.domainproxy.common; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
4 | | -import java.net.Socket; |
5 | | -import java.net.SocketException; |
6 | | -import java.net.SocketTimeoutException; |
7 | 4 | import java.nio.ByteBuffer; |
8 | | -import java.nio.channels.ClosedChannelException; |
| 5 | +import java.nio.channels.SelectionKey; |
| 6 | +import java.nio.channels.Selector; |
9 | 7 | import java.nio.channels.SocketChannel; |
| 8 | +import java.util.Iterator; |
10 | 9 |
|
11 | 10 | import org.jboss.logging.Logger; |
12 | 11 |
|
13 | 12 | public final class CommonIOUtil { |
14 | 13 |
|
15 | 14 | private static final Logger LOG = Logger.getLogger(CommonIOUtil.class); |
16 | | - private static final Object CLOSE_LOCK = new Object(); |
17 | 15 |
|
18 | | - public static Runnable createSocketToChannelWriter(final int byteBufferSize, final Socket socket, |
19 | | - final SocketChannel channel) { |
20 | | - // Write from socket to channel |
21 | | - return () -> { |
22 | | - Thread.currentThread().setName("SocketToChannelWriter"); |
23 | | - int r; |
24 | | - final byte[] buf = new byte[byteBufferSize]; |
25 | | - int bytesWritten = 0; |
26 | | - LOG.info("Writing from socket to channel"); |
27 | | - try { |
28 | | - while ((r = socket.getInputStream().read(buf)) > 0) { |
29 | | - LOG.infof("Read %d bytes from socket", r); |
30 | | - channel.write(ByteBuffer.wrap(buf, 0, r)); |
31 | | - LOG.infof("Wrote %d bytes to channel", r); |
32 | | - bytesWritten += r; |
33 | | - } |
34 | | - } catch (final ClosedChannelException ignore) { |
35 | | - LOG.info("Channel closed"); |
36 | | - } catch (final SocketException ignore) { |
37 | | - LOG.info("Socket closed"); |
38 | | - } catch (final SocketTimeoutException ignore) { |
39 | | - LOG.info("Socket timed out"); |
40 | | - } catch (final IOException e) { |
41 | | - LOG.errorf(e, "Error writing from socket to channel"); |
42 | | - } finally { |
43 | | - closeSocketAndChannel(socket, channel); |
44 | | - } |
45 | | - LOG.infof("Wrote %d bytes from socket to channel", bytesWritten); |
46 | | - }; |
| 16 | + public static final String LOCALHOST = "localhost"; |
| 17 | + public static final int SELECTOR_TIMEOUT_MS = 1000; |
| 18 | + |
| 19 | + private CommonIOUtil() { |
47 | 20 | } |
48 | 21 |
|
49 | | - public static Runnable createChannelToSocketWriter(final int byteBufferSize, final SocketChannel channel, |
50 | | - final Socket socket) { |
51 | | - // Write from channel to socket |
| 22 | + public static Runnable createChannelToChannelBiDirectionalHandler(final int byteBufferSize, |
| 23 | + final SocketChannel leftChannel, |
| 24 | + final SocketChannel rightChannel) { |
52 | 25 | return () -> { |
53 | | - Thread.currentThread().setName("ChannelToSocketWriter"); |
54 | | - int r; |
55 | | - final ByteBuffer buf = ByteBuffer.allocate(byteBufferSize); |
56 | | - int bytesWritten = 0; |
57 | | - LOG.info("Writing from channel to socket"); |
58 | | - try { |
59 | | - while ((r = channel.read(buf)) > 0) { |
60 | | - LOG.infof("Read %d bytes from channel", r); |
61 | | - buf.flip(); |
62 | | - socket.getOutputStream().write(buf.array(), buf.arrayOffset(), buf.remaining()); |
63 | | - LOG.infof("Wrote %d bytes to socket", r); |
64 | | - buf.clear(); |
65 | | - bytesWritten += r; |
| 26 | + Thread.currentThread().setName("ChannelToChannelBiDirectionalHandler"); |
| 27 | + int bytesRead = 0, bytesWritten = 0; |
| 28 | + final ByteBuffer buffer = ByteBuffer.allocate(byteBufferSize); |
| 29 | + try (final Selector selector = Selector.open()) { |
| 30 | + leftChannel.configureBlocking(false); |
| 31 | + rightChannel.configureBlocking(false); |
| 32 | + leftChannel.register(selector, SelectionKey.OP_READ); |
| 33 | + rightChannel.register(selector, SelectionKey.OP_WRITE); |
| 34 | + while (leftChannel.isOpen() && rightChannel.isOpen()) { |
| 35 | + if (selector.select(SELECTOR_TIMEOUT_MS) > 0) { |
| 36 | + final Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); |
| 37 | + while (keys.hasNext()) { |
| 38 | + final SelectionKey key = keys.next(); |
| 39 | + keys.remove(); |
| 40 | + if (key.isReadable()) { |
| 41 | + if (key.channel() == leftChannel) { |
| 42 | + final int bytes = transferData(leftChannel, rightChannel, buffer); |
| 43 | + if (bytes == 0) { |
| 44 | + return; |
| 45 | + } |
| 46 | + bytesRead += bytes; |
| 47 | + } else if (key.channel() == rightChannel) { |
| 48 | + final int bytes = transferData(rightChannel, leftChannel, buffer); |
| 49 | + if (bytes == 0) { |
| 50 | + return; |
| 51 | + } |
| 52 | + bytesRead += bytes; |
| 53 | + } |
| 54 | + } |
| 55 | + if (key.isWritable()) { |
| 56 | + if (key.channel() == leftChannel) { |
| 57 | + bytesWritten += transferData(leftChannel, rightChannel, buffer); |
| 58 | + } else if (key.channel() == rightChannel) { |
| 59 | + bytesWritten += transferData(rightChannel, leftChannel, buffer); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
66 | 64 | } |
67 | | - } catch (final ClosedChannelException ignore) { |
68 | | - LOG.info("Channel closed"); |
69 | | - } catch (final SocketException ignore) { |
70 | | - LOG.info("Socket closed"); |
71 | | - } catch (final SocketTimeoutException ignore) { |
72 | | - LOG.info("Socket timed out"); |
73 | 65 | } catch (final IOException e) { |
74 | | - LOG.errorf(e, "Error writing from channel to socket"); |
| 66 | + LOG.errorf(e, "Error in bi-directional channel handling"); |
75 | 67 | } finally { |
76 | | - closeSocketAndChannel(socket, channel); |
| 68 | + closeSocketChannel(leftChannel, rightChannel); |
| 69 | + LOG.infof("Read %d bytes between channels", bytesRead); |
| 70 | + LOG.infof("Wrote %d bytes between channels", bytesWritten); |
77 | 71 | } |
78 | | - LOG.infof("Wrote %d bytes from channel to socket", bytesWritten); |
79 | 72 | }; |
80 | 73 | } |
81 | 74 |
|
82 | | - private static void closeSocketAndChannel(final Socket socket, final SocketChannel channel) { |
83 | | - synchronized (CLOSE_LOCK) { // TODO is this necessary? |
84 | | - try { |
85 | | - if (channel != null && channel.isOpen()) { |
86 | | - channel.close(); |
87 | | - } |
88 | | - } catch (final IOException e) { |
89 | | - LOG.errorf(e, "Error closing channel"); |
| 75 | + private static int transferData(final SocketChannel sourceChannel, final SocketChannel destinationChannel, |
| 76 | + final ByteBuffer buffer) |
| 77 | + throws IOException { |
| 78 | + buffer.clear(); |
| 79 | + final int bytesRead = sourceChannel.read(buffer); |
| 80 | + if (bytesRead == -1) { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + buffer.flip(); |
| 84 | + while (buffer.hasRemaining()) { |
| 85 | + destinationChannel.write(buffer); |
| 86 | + } |
| 87 | + return bytesRead; |
| 88 | + } |
| 89 | + |
| 90 | + private static void closeSocketChannel(final SocketChannel sourceChannel, final SocketChannel destinationChannel) { |
| 91 | + try { |
| 92 | + if (sourceChannel != null && sourceChannel.isOpen()) { |
| 93 | + sourceChannel.close(); |
90 | 94 | } |
91 | | - try { |
92 | | - if (socket != null && !socket.isClosed()) { |
93 | | - socket.close(); |
94 | | - } |
95 | | - } catch (final IOException e) { |
96 | | - LOG.errorf(e, "Error closing socket"); |
| 95 | + } catch (final IOException e) { |
| 96 | + LOG.errorf(e, "Error closing source channel"); |
| 97 | + } |
| 98 | + try { |
| 99 | + if (destinationChannel != null && destinationChannel.isOpen()) { |
| 100 | + destinationChannel.close(); |
97 | 101 | } |
| 102 | + } catch (final IOException e) { |
| 103 | + LOG.errorf(e, "Error closing destination channel"); |
98 | 104 | } |
99 | 105 | } |
100 | 106 | } |
0 commit comments