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.nio.NioEventLoopGroup;
24  import io.netty.channel.sctp.SctpChannel;
25  import io.netty.channel.sctp.SctpServerChannel;
26  import io.netty.channel.sctp.nio.NioSctpServerChannel;
27  import io.netty.example.sctp.SctpEchoServerHandler;
28  import io.netty.handler.logging.LogLevel;
29  import io.netty.handler.logging.LoggingHandler;
30  import io.netty.util.internal.SocketUtils;
31  
32  import java.net.InetAddress;
33  import java.net.InetSocketAddress;
34  
35  /**
36   * SCTP Echo Server with multi-homing support.
37   */
38  public final class SctpMultiHomingEchoServer {
39  
40      private static final String SERVER_PRIMARY_HOST = System.getProperty("host.primary", "127.0.0.1");
41      private static final String SERVER_SECONDARY_HOST = System.getProperty("host.secondary", "127.0.0.2");
42  
43      private static final int SERVER_PORT = Integer.parseInt(System.getProperty("port", "8007"));
44  
45      public static void main(String[] args) throws Exception {
46          // Configure the server.
47          EventLoopGroup bossGroup = new NioEventLoopGroup(1);
48          EventLoopGroup workerGroup = new NioEventLoopGroup();
49          try {
50              ServerBootstrap b = new ServerBootstrap();
51              b.group(bossGroup, workerGroup)
52               .channel(NioSctpServerChannel.class)
53               .option(ChannelOption.SO_BACKLOG, 100)
54               .handler(new LoggingHandler(LogLevel.INFO))
55               .childHandler(new ChannelInitializer<SctpChannel>() {
56                   @Override
57                   public void initChannel(SctpChannel ch) throws Exception {
58                       ch.pipeline().addLast(
59  //                             new LoggingHandler(LogLevel.INFO),
60                               new SctpEchoServerHandler());
61                   }
62               });
63  
64              InetSocketAddress localAddress = SocketUtils.socketAddress(SERVER_PRIMARY_HOST, SERVER_PORT);
65              InetAddress localSecondaryAddress = SocketUtils.addressByName(SERVER_SECONDARY_HOST);
66  
67              // Bind the server to primary address.
68              ChannelFuture bindFuture = b.bind(localAddress).sync();
69  
70              //Get the underlying sctp channel
71              SctpServerChannel channel = (SctpServerChannel) bindFuture.channel();
72  
73              //Bind the secondary address
74              ChannelFuture connectFuture = channel.bindAddress(localSecondaryAddress).sync();
75  
76              // Wait until the connection is closed.
77              connectFuture.channel().closeFuture().sync();
78          } finally {
79              // Shut down all event loops to terminate all threads.
80              bossGroup.shutdownGracefully();
81              workerGroup.shutdownGracefully();
82          }
83      }
84  }