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.Bootstrap;
19  import io.netty.channel.ChannelFuture;
20  import io.netty.channel.ChannelInitializer;
21  import io.netty.channel.EventLoopGroup;
22  import io.netty.channel.nio.NioEventLoopGroup;
23  import io.netty.channel.sctp.SctpChannel;
24  import io.netty.channel.sctp.SctpChannelOption;
25  import io.netty.channel.sctp.nio.NioSctpChannel;
26  import io.netty.example.sctp.SctpEchoClientHandler;
27  import io.netty.util.internal.SocketUtils;
28  
29  import java.net.InetAddress;
30  import java.net.InetSocketAddress;
31  
32  /**
33   * SCTP Echo Client with multi-homing support.
34   */
35  public final class SctpMultiHomingEchoClient {
36  
37      private static final String CLIENT_PRIMARY_HOST = System.getProperty("host.primary", "127.0.0.1");
38      private static final String CLIENT_SECONDARY_HOST = System.getProperty("host.secondary", "127.0.0.2");
39  
40      private static final int CLIENT_PORT = Integer.parseInt(System.getProperty("port.local", "8008"));
41  
42      private static final String SERVER_REMOTE_HOST = System.getProperty("host.remote", "127.0.0.1");
43      private static final int SERVER_REMOTE_PORT = Integer.parseInt(System.getProperty("port.remote", "8007"));
44  
45      public static void main(String[] args) throws Exception {
46          // Configure the client.
47          EventLoopGroup group = new NioEventLoopGroup();
48          try {
49              Bootstrap b = new Bootstrap();
50              b.group(group)
51               .channel(NioSctpChannel.class)
52               .option(SctpChannelOption.SCTP_NODELAY, true)
53               .handler(new ChannelInitializer<SctpChannel>() {
54                   @Override
55                   public void initChannel(SctpChannel ch) throws Exception {
56                       ch.pipeline().addLast(
57  //                             new LoggingHandler(LogLevel.INFO),
58                               new SctpEchoClientHandler());
59                   }
60               });
61  
62              InetSocketAddress localAddress = SocketUtils.socketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT);
63              InetAddress localSecondaryAddress = SocketUtils.addressByName(CLIENT_SECONDARY_HOST);
64  
65              InetSocketAddress remoteAddress = SocketUtils.socketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT);
66  
67              // Bind the client channel.
68              ChannelFuture bindFuture = b.bind(localAddress).sync();
69  
70              // Get the underlying sctp channel
71              SctpChannel channel = (SctpChannel) bindFuture.channel();
72  
73              // Bind the secondary address.
74              // Please note that, bindAddress in the client channel should be done before connecting if you have not
75              // enable Dynamic Address Configuration. See net.sctp.addip_enable kernel param
76              channel.bindAddress(localSecondaryAddress).sync();
77  
78              // Finish connect
79              ChannelFuture connectFuture = channel.connect(remoteAddress).sync();
80  
81              // Wait until the connection is closed.
82              connectFuture.channel().closeFuture().sync();
83          } finally {
84              // Shut down the event loop to terminate all threads.
85              group.shutdownGracefully();
86          }
87      }
88  }