View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.example.sctp.multihoming;
17  
18  import io.netty.bootstrap.ServerBootstrap;
19  import io.netty.channel.ChannelFuture;
20  import io.netty.channel.ChannelInitializer;
21  import io.netty.channel.ChannelOption;
22  import io.netty.channel.EventLoopGroup;
23  import io.netty.channel.MultiThreadIoEventLoopGroup;
24  import io.netty.channel.nio.NioIoHandler;
25  import io.netty.channel.sctp.SctpChannel;
26  import io.netty.channel.sctp.SctpServerChannel;
27  import io.netty.channel.sctp.nio.NioSctpServerChannel;
28  import io.netty.example.sctp.SctpEchoServerHandler;
29  import io.netty.handler.logging.LogLevel;
30  import io.netty.handler.logging.LoggingHandler;
31  import io.netty.util.internal.SocketUtils;
32  
33  import java.net.InetAddress;
34  import java.net.InetSocketAddress;
35  
36  /**
37   * SCTP Echo Server with multi-homing support.
38   */
39  public final class SctpMultiHomingEchoServer {
40  
41      private static final String SERVER_PRIMARY_HOST = System.getProperty("host.primary", "127.0.0.1");
42      private static final String SERVER_SECONDARY_HOST = System.getProperty("host.secondary", "127.0.0.2");
43  
44      private static final int SERVER_PORT = Integer.parseInt(System.getProperty("port", "8007"));
45  
46      public static void main(String[] args) throws Exception {
47          // Configure the server.
48          EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
49          EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
50          try {
51              ServerBootstrap b = new ServerBootstrap();
52              b.group(bossGroup, workerGroup)
53               .channel(NioSctpServerChannel.class)
54               .option(ChannelOption.SO_BACKLOG, 100)
55               .handler(new LoggingHandler(LogLevel.INFO))
56               .childHandler(new ChannelInitializer<SctpChannel>() {
57                   @Override
58                   public void initChannel(SctpChannel ch) throws Exception {
59                       ch.pipeline().addLast(
60  //                             new LoggingHandler(LogLevel.INFO),
61                               new SctpEchoServerHandler());
62                   }
63               });
64  
65              InetSocketAddress localAddress = SocketUtils.socketAddress(SERVER_PRIMARY_HOST, SERVER_PORT);
66              InetAddress localSecondaryAddress = SocketUtils.addressByName(SERVER_SECONDARY_HOST);
67  
68              // Bind the server to primary address.
69              ChannelFuture bindFuture = b.bind(localAddress).sync();
70  
71              //Get the underlying sctp channel
72              SctpServerChannel channel = (SctpServerChannel) bindFuture.channel();
73  
74              //Bind the secondary address
75              ChannelFuture connectFuture = channel.bindAddress(localSecondaryAddress).sync();
76  
77              // Wait until the connection is closed.
78              connectFuture.channel().closeFuture().sync();
79          } finally {
80              // Shut down all event loops to terminate all threads.
81              bossGroup.shutdownGracefully();
82              workerGroup.shutdownGracefully();
83          }
84      }
85  }