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