-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
plugins { | ||
id("java-conventions") | ||
} | ||
|
||
dependencies { | ||
implementation(project(":core")) | ||
|
||
implementation(platform(libs.netty.bom)) | ||
implementation("io.netty:netty-all") | ||
} |
47 changes: 47 additions & 0 deletions
47
chapter04-transport/src/main/java/com/joebrothers/chapter04/NettyNioServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.joebrothers.chapter04; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.util.CharsetUtil; | ||
|
||
public class NettyNioServer { | ||
public void serve(int port) throws Exception { | ||
final ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", CharsetUtil.UTF_8)); | ||
final EventLoopGroup group = new NioEventLoopGroup(); | ||
|
||
try { | ||
final ServerBootstrap b = new ServerBootstrap(); | ||
b.group(group) | ||
.channel(NioServerSocketChannel.class) | ||
.localAddress(new InetSocketAddress(port)) | ||
.childHandler(new ChannelInitializer<>() { | ||
@Override | ||
protected void initChannel(Channel ch) throws Exception { | ||
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { | ||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
ctx.writeAndFlush(buf.duplicate()) | ||
.addListener(ChannelFutureListener.CLOSE); | ||
} | ||
}); | ||
} | ||
}); | ||
final ChannelFuture f = b.bind().sync(); | ||
f.channel().closeFuture().sync(); | ||
} finally { | ||
group.shutdownGracefully().sync(); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
chapter04-transport/src/main/java/com/joebrothers/chapter04/NettyOioServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.joebrothers.chapter04; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.oio.OioEventLoopGroup; | ||
import io.netty.channel.socket.oio.OioServerSocketChannel; | ||
import io.netty.util.CharsetUtil; | ||
|
||
public class NettyOioServer { | ||
public void serve(int port) throws Exception { | ||
final ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", CharsetUtil.UTF_8)); | ||
final EventLoopGroup group = new OioEventLoopGroup(); | ||
|
||
try { | ||
final ServerBootstrap b = new ServerBootstrap(); | ||
b.group(group) | ||
.channel(OioServerSocketChannel.class) | ||
.localAddress(new InetSocketAddress(port)) | ||
.childHandler(new ChannelInitializer<>() { | ||
@Override | ||
protected void initChannel(Channel ch) throws Exception { | ||
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { | ||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
ctx.writeAndFlush(buf.duplicate()) | ||
.addListener(ChannelFutureListener.CLOSE); | ||
} | ||
}); | ||
} | ||
}); | ||
final ChannelFuture f = b.bind().sync(); | ||
f.channel().closeFuture().sync(); | ||
} finally { | ||
group.shutdownGracefully().sync(); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
chapter04-transport/src/main/java/com/joebrothers/chapter04/PlainNioServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.joebrothers.chapter04; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.ServerSocket; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.SelectionKey; | ||
import java.nio.channels.Selector; | ||
import java.nio.channels.ServerSocketChannel; | ||
import java.nio.channels.SocketChannel; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
public class PlainNioServer { | ||
public void serve(int port) throws IOException { | ||
final ServerSocketChannel serverChannel = ServerSocketChannel.open(); | ||
serverChannel.configureBlocking(false); | ||
|
||
final ServerSocket ssocket = serverChannel.socket(); | ||
final InetSocketAddress address = new InetSocketAddress(port); | ||
ssocket.bind(address); | ||
|
||
final Selector selector = Selector.open(); | ||
serverChannel.register(selector, SelectionKey.OP_ACCEPT); | ||
final ByteBuffer msg = ByteBuffer.wrap("Hi!\r\n".getBytes()); | ||
|
||
for (; ; ) { | ||
try { | ||
selector.select(); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
// handle exception | ||
break; | ||
} | ||
|
||
final Set<SelectionKey> readyKeys = selector.selectedKeys(); | ||
final Iterator<SelectionKey> iterator = readyKeys.iterator(); | ||
while (iterator.hasNext()) { | ||
final SelectionKey key = iterator.next(); | ||
iterator.remove(); | ||
|
||
try { | ||
if (key.isAcceptable()) { | ||
final ServerSocketChannel server = (ServerSocketChannel) key.channel(); | ||
final SocketChannel client = server.accept(); | ||
client.configureBlocking(false); | ||
client.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, | ||
msg.duplicate()); | ||
System.out.println("Accepted connection from " + client); | ||
} | ||
|
||
if (key.isWritable()) { | ||
final SocketChannel client = (SocketChannel) key.channel(); | ||
final ByteBuffer buffer = (ByteBuffer) key.attachment(); | ||
while (buffer.hasRemaining()) { | ||
if (client.write(buffer) == 0) { | ||
break; | ||
} | ||
} | ||
|
||
client.close(); | ||
} | ||
} catch (IOException ex) { | ||
key.cancel(); | ||
try { | ||
key.channel().close(); | ||
} catch (IOException cex) { | ||
// ignore on close | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
chapter04-transport/src/main/java/com/joebrothers/chapter04/PlainOioServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.joebrothers.chapter04; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class PlainOioServer { | ||
public void serve(int port) throws IOException { | ||
final ServerSocket socket = new ServerSocket(port); | ||
try { | ||
for (; ; ) { | ||
final Socket clientSocket = socket.accept(); | ||
System.out.println("Accepted connection from" + clientSocket); | ||
|
||
new Thread(() -> { | ||
final OutputStream out; | ||
try { | ||
out = clientSocket.getOutputStream(); | ||
out.write("Hi!\r\n".getBytes(StandardCharsets.UTF_8)); | ||
out.flush(); | ||
clientSocket.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
clientSocket.close(); | ||
} catch (IOException ex) { | ||
// ignore | ||
} | ||
} | ||
}).start(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
chapter04-transport/src/main/java/com/joebrothers/chapter04/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@NonNullByDefault | ||
package com.joebrothers.chapter04; | ||
|
||
import com.joebrothers.annotation.NonNullByDefault; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters