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+ }
0 commit comments