Skip to content

Commit e9d3191

Browse files
committed
demo: Add netty example code
This demonstrates using Netty 4.x with junixsocket.
1 parent 0e87299 commit e9d3191

4 files changed

Lines changed: 141 additions & 0 deletions

File tree

junixsocket-demo/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
<artifactId>mina-core</artifactId>
9191
<version>2.2.3</version>
9292
</dependency>
93+
<dependency>
94+
<groupId>io.netty</groupId>
95+
<artifactId>netty-all</artifactId>
96+
<version>4.1.54.Final</version>
97+
</dependency>
9398
</dependencies>
9499
<build>
95100
<plugins>

junixsocket-demo/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
requires mina.core;
1717
requires static com.kohlschutter.annotations.compiletime;
1818
requires static org.eclipse.jdt.annotation;
19+
requires io.netty.all;
1920

2021
exports org.newsclub.net.unix.demo.rmi.services to java.rmi;
2122
exports org.newsclub.net.unix.demo.rmi.fd to java.rmi;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* junixsocket
3+
*
4+
* Copyright 2009-2023 Christian Kohlschütter
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.newsclub.net.unix.demo.netty;
19+
20+
import java.io.File;
21+
import java.nio.channels.spi.SelectorProvider;
22+
import java.util.concurrent.Executor;
23+
24+
import org.newsclub.net.unix.AFSocketAddress;
25+
import org.newsclub.net.unix.AFUNIXSelectorProvider;
26+
import org.newsclub.net.unix.AFUNIXSocketAddress;
27+
28+
import io.netty.bootstrap.ServerBootstrap;
29+
import io.netty.channel.ChannelFuture;
30+
import io.netty.channel.ChannelInitializer;
31+
import io.netty.channel.ChannelOption;
32+
import io.netty.channel.EventLoopGroup;
33+
import io.netty.channel.nio.NioEventLoopGroup;
34+
import io.netty.channel.socket.SocketChannel;
35+
import io.netty.channel.socket.nio.NioServerSocketChannel;
36+
37+
/**
38+
* Echos any incoming data.
39+
* <p>
40+
* Based on example code from <a href="https://netty.io/wiki/user-guide-for-4.x.html">Netty user
41+
* guide for 4.x</a>
42+
*/
43+
public class EchoServer {
44+
private final AFSocketAddress addr;
45+
46+
public EchoServer(AFSocketAddress addr) {
47+
this.addr = addr;
48+
}
49+
50+
public void run() throws Exception {
51+
SelectorProvider provider = AFUNIXSelectorProvider.provider();
52+
// SelectorProvider provider = AFTIPCSelectorProvider.provider();
53+
54+
// We need to specify our custom selector provider here (1), as well as in (3)
55+
EventLoopGroup bossGroup = new NioEventLoopGroup(0, (Executor) null, provider); // (1)
56+
EventLoopGroup workerGroup = new NioEventLoopGroup(0, (Executor) null, provider); // (1)
57+
try {
58+
ServerBootstrap b = new ServerBootstrap(); // (2)
59+
b.group(bossGroup, workerGroup) //
60+
.channelFactory(() -> new NioServerSocketChannel(provider)) // (3)
61+
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
62+
@Override
63+
public void initChannel(SocketChannel ch) throws Exception {
64+
ch.pipeline().addLast(new EchoServerHandler());
65+
}
66+
}) //
67+
.option(ChannelOption.SO_BACKLOG, 128) // (5)
68+
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
69+
70+
// Bind and start to accept incoming connections.
71+
ChannelFuture f = b.bind(addr).sync(); // (7)
72+
73+
// Wait until the server socket is closed.
74+
// In this example, this does not happen, but you can do that to gracefully
75+
// shut down your server.
76+
f.channel().closeFuture().sync();
77+
} finally {
78+
workerGroup.shutdownGracefully();
79+
bossGroup.shutdownGracefully();
80+
}
81+
}
82+
83+
public static void main(String[] args) throws Exception {
84+
File path = new File("/tmp/nettyecho");
85+
if (args.length > 0) {
86+
path = new File(args[0]);
87+
}
88+
89+
new EchoServer(AFUNIXSocketAddress.of(path)).run();
90+
// new EchoServer(AFTIPCSocketAddress.ofService(Scope.SCOPE_CLUSTER, 128, 3)).run();
91+
}
92+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* junixsocket
3+
*
4+
* Copyright 2009-2023 Christian Kohlschütter
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.newsclub.net.unix.demo.netty;
19+
20+
import io.netty.channel.ChannelHandlerContext;
21+
import io.netty.channel.ChannelInboundHandlerAdapter;
22+
23+
/**
24+
* Handles a server-side channel.
25+
* <p>
26+
* Based on example code from <a href="https://netty.io/wiki/user-guide-for-4.x.html">Netty user
27+
* guide for 4.x</a>
28+
*/
29+
class EchoServerHandler extends ChannelInboundHandlerAdapter { // (1)
30+
31+
@Override
32+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
33+
ctx.write(msg); // (1)
34+
ctx.flush(); // (2)
35+
}
36+
37+
@Override
38+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
39+
// Close the connection when an exception is raised.
40+
cause.printStackTrace();
41+
ctx.close();
42+
}
43+
}

0 commit comments

Comments
 (0)