Search in sources :

Example 1 with NioChannel

use of suite.net.nio.NioChannelFactory.NioChannel in project suite by stupidsing.

the class NioDispatcherTest method testTextExchange.

@Test
public void testTextExchange() throws IOException {
    String hello = "HELLO";
    Charset charset = Constants.charset;
    Source<NioChannel> source = () -> {
        BufferedNioChannel channel = new BufferedNioChannel();
        channel.onConnected.wire(sender -> {
            String s = hello + "\n";
            channel.send(To.bytes(s));
        });
        channel.onReceive.wire(channel::send);
        return NioChannelFactory.buffered(channel);
    };
    NioDispatcher<NioChannel> dispatcher = new NioDispatcherImpl<>(source);
    dispatcher.start();
    try (Closeable closeServer = dispatcher.listen(5151);
        Socket socket = new Socket("localhost", 5151);
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        InputStreamReader isr = new InputStreamReader(is, charset);
        BufferedReader reader = new BufferedReader(isr);
        PrintWriter writer = new PrintWriter(os)) {
        String m = "testing nio";
        writer.println(m);
        writer.flush();
        assertEquals(hello, reader.readLine());
        assertEquals(m, reader.readLine());
    } finally {
        dispatcher.stop();
    }
}
Also used : OutputStream(java.io.OutputStream) PrintWriter(java.io.PrintWriter) Socket(java.net.Socket) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Constants(suite.Constants) Source(suite.util.FunUtil.Source) Bytes(suite.primitive.Bytes) Thread_(suite.util.Thread_) IOException(java.io.IOException) Test(org.junit.Test) To(suite.util.To) NioChannel(suite.net.nio.NioChannelFactory.NioChannel) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) BufferedNioChannel(suite.net.nio.NioChannelFactory.BufferedNioChannel) Iterate(suite.util.FunUtil.Iterate) Charset(java.nio.charset.Charset) Closeable(java.io.Closeable) BufferedReader(java.io.BufferedReader) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Closeable(java.io.Closeable) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) BufferedNioChannel(suite.net.nio.NioChannelFactory.BufferedNioChannel) NioChannel(suite.net.nio.NioChannelFactory.NioChannel) BufferedNioChannel(suite.net.nio.NioChannelFactory.BufferedNioChannel) RequestResponseNioChannel(suite.net.nio.NioChannelFactory.RequestResponseNioChannel) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

BufferedReader (java.io.BufferedReader)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 Charset (java.nio.charset.Charset)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TimeUnit (java.util.concurrent.TimeUnit)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Test (org.junit.Test)1 Constants (suite.Constants)1 BufferedNioChannel (suite.net.nio.NioChannelFactory.BufferedNioChannel)1 NioChannel (suite.net.nio.NioChannelFactory.NioChannel)1 RequestResponseNioChannel (suite.net.nio.NioChannelFactory.RequestResponseNioChannel)1 Bytes (suite.primitive.Bytes)1