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();
}
}
Aggregations