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