View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.commandexec.server;
21  
22  import io.netty.bootstrap.ServerBootstrap;
23  import io.netty.channel.ChannelFuture;
24  import io.netty.channel.EventLoopGroup;
25  import io.netty.channel.nio.NioEventLoopGroup;
26  import io.netty.util.concurrent.DefaultEventExecutorGroup;
27  import io.netty.util.concurrent.EventExecutorGroup;
28  import org.waarp.commandexec.utils.LocalExecDefaultResult;
29  import org.waarp.common.logging.WaarpLoggerFactory;
30  import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
31  import org.waarp.common.utility.DetectionUtils;
32  import org.waarp.common.utility.WaarpNettyUtil;
33  import org.waarp.common.utility.WaarpThreadFactory;
34  
35  import java.net.InetAddress;
36  import java.net.InetSocketAddress;
37  
38  /**
39   * LocalExec server Main method.
40   */
41  public class LocalExecServer {
42  
43    static final EventLoopGroup workerGroup = new NioEventLoopGroup();
44    static final EventExecutorGroup executor =
45        new DefaultEventExecutorGroup(DetectionUtils.numberThreads(),
46                                      new WaarpThreadFactory("LocalExecServer"));
47  
48    /**
49     * Takes 3 optional arguments:<br>
50     * - no argument: implies 127.0.0.1 + 9999 port<br>
51     * - arguments:<br>
52     * "addresse" "port"<br>
53     * "addresse" "port" "default delay"<br>
54     *
55     * @param args
56     *
57     * @throws Exception
58     */
59    public static void main(final String[] args) throws Exception {
60      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
61          new WaarpSlf4JLoggerFactory(null));
62      int port = 9999;
63      final InetAddress addr;
64      long delay = LocalExecDefaultResult.MAXWAITPROCESS;
65      if (args.length >= 2) {
66        addr = InetAddress.getByName(args[0]);
67        port = Integer.parseInt(args[1]);
68        if (args.length > 2) {
69          delay = Long.parseLong(args[2]);
70        }
71      } else {
72        final byte[] loop = { 127, 0, 0, 1 };
73        addr = InetAddress.getByAddress(loop);
74      }
75      // Configure the server.
76      try {
77        final ServerBootstrap bootstrap = new ServerBootstrap();
78        WaarpNettyUtil.setServerBootstrap(bootstrap, workerGroup, workerGroup,
79                                          30000);
80  
81        // Configure the pipeline factory.
82        bootstrap.childHandler(new LocalExecServerInitializer(delay, executor));
83  
84        // Bind and start to accept incoming connections only on local address.
85        final ChannelFuture future =
86            bootstrap.bind(new InetSocketAddress(addr, port));
87  
88        // Wait until the server socket is closed.
89        future.channel().closeFuture().sync();
90      } finally {
91        // Shut down all event loops to terminate all threads.
92        workerGroup.shutdownGracefully();
93  
94        // Wait until all threads are terminated.
95        workerGroup.terminationFuture().sync();
96      }
97    }
98  }