|
1 | 1 | package com.baeldung.java.nio2.async; |
2 | 2 |
|
3 | | -import org.junit.After; |
4 | | -import org.junit.Before; |
5 | | -import org.junit.Test; |
6 | | - |
| 3 | +import java.io.File; |
7 | 4 | import java.io.IOException; |
| 5 | +import java.net.InetSocketAddress; |
| 6 | +import java.nio.ByteBuffer; |
| 7 | +import java.nio.channels.AsynchronousServerSocketChannel; |
| 8 | +import java.nio.channels.AsynchronousSocketChannel; |
| 9 | +import java.util.concurrent.ExecutionException; |
| 10 | +import java.util.concurrent.Future; |
8 | 11 |
|
9 | | -import static org.junit.Assert.assertEquals; |
| 12 | +public class AsyncEchoServer { |
| 13 | + private AsynchronousServerSocketChannel serverChannel; |
| 14 | + private Future<AsynchronousSocketChannel> acceptResult; |
| 15 | + private AsynchronousSocketChannel clientChannel; |
10 | 16 |
|
11 | | -public class AsyncEchoTest { |
| 17 | + public AsyncEchoServer() { |
| 18 | + try { |
| 19 | + serverChannel = AsynchronousServerSocketChannel.open(); |
| 20 | + InetSocketAddress hostAddress = new InetSocketAddress("localhost", 4999); |
| 21 | + serverChannel.bind(hostAddress); |
| 22 | + acceptResult = serverChannel.accept(); |
| 23 | + } catch (IOException e) { |
| 24 | + e.printStackTrace(); |
| 25 | + } |
| 26 | + } |
12 | 27 |
|
13 | | - Process server; |
14 | | - AsyncEchoClient client; |
| 28 | + public void runServer() { |
| 29 | + try { |
| 30 | + clientChannel = acceptResult.get(); |
| 31 | + if ((clientChannel != null) && (clientChannel.isOpen())) { |
| 32 | + while (true) { |
15 | 33 |
|
16 | | - @Before |
17 | | - public void setup() throws IOException, InterruptedException { |
18 | | - server = AsyncEchoServer2.start(); |
19 | | - client = AsyncEchoClient.getInstance(); |
20 | | - } |
| 34 | + ByteBuffer buffer = ByteBuffer.allocate(32); |
| 35 | + Future<Integer> readResult = clientChannel.read(buffer); |
| 36 | + |
| 37 | + //do other things as operation continues in background |
| 38 | + readResult.get(); |
| 39 | + |
| 40 | + buffer.flip(); |
| 41 | + String message = new String(buffer.array()).trim(); |
| 42 | + if (message.equals("bye")) { |
| 43 | + break; // while loop |
| 44 | + } |
| 45 | + buffer = ByteBuffer.wrap(new String(message).getBytes()); |
| 46 | + Future<Integer> writeResult = clientChannel.write(buffer); |
| 47 | + //run other code |
| 48 | + writeResult.get(); |
| 49 | + buffer.clear(); |
| 50 | + |
| 51 | + } // while() |
| 52 | + |
| 53 | + clientChannel.close(); |
| 54 | + serverChannel.close(); |
| 55 | + |
| 56 | + } |
| 57 | + } catch (InterruptedException | ExecutionException | IOException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + } |
21 | 60 |
|
22 | | - @Test |
23 | | - public void givenServerClient_whenServerEchosMessage_thenCorrect() throws Exception { |
24 | | - String resp1 = client.sendMessage("hello"); |
25 | | - String resp2 = client.sendMessage("world"); |
26 | | - assertEquals("hello", resp1); |
27 | | - assertEquals("world", resp2); |
28 | 61 | } |
29 | 62 |
|
30 | | - @After |
31 | | - public void teardown() throws IOException { |
32 | | - server.destroy(); |
33 | | - client.stop(); |
| 63 | + public static void main(String[] args) { |
| 64 | + AsyncEchoServer server = new AsyncEchoServer(); |
| 65 | + server.runServer(); |
34 | 66 | } |
| 67 | + public static Process start() throws IOException, InterruptedException { |
| 68 | + String javaHome = System.getProperty("java.home"); |
| 69 | + String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; |
| 70 | + String classpath = System.getProperty("java.class.path"); |
| 71 | + String className = AsyncEchoServer.class.getCanonicalName(); |
35 | 72 |
|
| 73 | + ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className); |
| 74 | + |
| 75 | + return builder.start(); |
| 76 | + } |
36 | 77 | } |
0 commit comments