View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2026 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 com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
29  
30  import io.netty.bootstrap.ServerBootstrap;
31  import io.netty.channel.ChannelFuture;
32  import io.netty.channel.ChannelInitializer;
33  import io.netty.channel.ChannelOption;
34  import io.netty.channel.EventLoopGroup;
35  import io.netty.channel.nio.NioEventLoopGroup;
36  import io.netty.channel.socket.SocketChannel;
37  import io.netty.channel.socket.nio.NioServerSocketChannel;
38  
39  /**
40   * Echos any incoming data.
41   * <p>
42   * Based on example code from <a href="https://netty.io/wiki/user-guide-for-4.x.html">Netty user
43   * guide for 4.x</a>
44   */
45  @SuppressWarnings("FutureReturnValueIgnored" /* errorprone */)
46  public class EchoServer {
47    private final AFSocketAddress addr;
48  
49    public EchoServer(AFSocketAddress addr) {
50      this.addr = addr;
51    }
52  
53    public void run() throws Exception {
54      SelectorProvider provider = AFUNIXSelectorProvider.provider();
55      // SelectorProvider provider = AFTIPCSelectorProvider.provider();
56  
57      // We need to specify our custom selector provider here (1), as well as in (3)
58      EventLoopGroup bossGroup = new NioEventLoopGroup(0, (Executor) null, provider); // (1)
59      EventLoopGroup workerGroup = new NioEventLoopGroup(0, (Executor) null, provider); // (1)
60      try {
61        ServerBootstrap b = new ServerBootstrap(); // (2)
62        b.group(bossGroup, workerGroup) //
63            .channelFactory(() -> new NioServerSocketChannel(provider)) // (3)
64            .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
65              @Override
66              public void initChannel(SocketChannel ch) throws Exception {
67                ch.pipeline().addLast(new EchoServerHandler());
68              }
69            }) //
70            .option(ChannelOption.SO_BACKLOG, 128) // (5)
71            .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
72  
73        // Bind and start to accept incoming connections.
74        ChannelFuture f = b.bind(addr).sync(); // (7)
75  
76        // Wait until the server socket is closed.
77        // In this example, this does not happen, but you can do that to gracefully
78        // shut down your server.
79        f.channel().closeFuture().sync();
80      } finally {
81        workerGroup.shutdownGracefully();
82        bossGroup.shutdownGracefully();
83      }
84    }
85  
86    @SuppressFBWarnings("PATH_TRAVERSAL_IN")
87    public static void main(String[] args) throws Exception {
88      File path = new File("/tmp/nettyecho");
89      if (args.length > 0) {
90        path = new File(args[0]);
91      }
92  
93      AFUNIXSocketAddress addr = AFUNIXSocketAddress.of(path);
94      System.out.println("Binding to " + addr);
95  
96      new EchoServer(addr).run();
97      // new EchoServer(AFTIPCSocketAddress.ofService(Scope.SCOPE_CLUSTER, 128, 3)).run();
98    }
99  }