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.openr66.thrift;
21  
22  import org.apache.thrift.server.TServer;
23  import org.apache.thrift.server.TThreadPoolServer;
24  import org.apache.thrift.transport.TServerSocket;
25  import org.apache.thrift.transport.TServerTransport;
26  import org.apache.thrift.transport.TTransportException;
27  import org.waarp.common.future.WaarpFuture;
28  import org.waarp.common.logging.WaarpLogger;
29  import org.waarp.common.logging.WaarpLoggerFactory;
30  import org.waarp.thrift.r66.R66Service;
31  
32  import java.net.InetAddress;
33  import java.net.InetSocketAddress;
34  import java.net.UnknownHostException;
35  
36  /**
37   * Main Thrift server service
38   */
39  public class R66ThriftServerService implements Runnable {
40    /**
41     * Internal Logger
42     */
43    private static final WaarpLogger logger =
44        WaarpLoggerFactory.getLogger(R66ThriftServerService.class);
45  
46    protected int port = 4266;
47    protected TServerTransport serverTransport;
48    protected TServer server;
49    protected final WaarpFuture serviceReady;
50  
51    public R66ThriftServerService(final WaarpFuture serviceReady,
52                                  final int port) {
53      this.serviceReady = serviceReady;
54      this.port = port;
55    }
56  
57    public final boolean awaitInitialization() {
58      if (serviceReady != null) {
59        serviceReady.awaitOrInterruptible();
60        return serviceReady.isSuccess();
61      }
62      return true;
63    }
64  
65    @Override
66    public void run() {
67      try {
68        logger.warn("Will start Thrift service on port: " + port);
69        final byte[] local = { 127, 0, 0, 1 };
70        InetAddress addr;
71        try {
72          addr = InetAddress.getByAddress(local);
73        } catch (final UnknownHostException e) {
74          try {
75            addr = InetAddress.getLocalHost();
76          } catch (final UnknownHostException e1) {
77            logger.error("Cannot start the Thrift service", e1);
78            serviceReady.setFailure(e);
79            releaseResources();
80            return;
81          }
82        }
83        final InetSocketAddress address = new InetSocketAddress(addr, port);
84        serverTransport = new TServerSocket(address);
85        final R66Service.Processor<R66EmbeddedServiceImpl> processor =
86            new R66Service.Processor<R66EmbeddedServiceImpl>(
87                new R66EmbeddedServiceImpl());
88        server = new TThreadPoolServer(
89            new TThreadPoolServer.Args(serverTransport).processor(processor));
90        serviceReady.setSuccess();
91        server.serve();
92      } catch (final TTransportException e) {
93        logger.error("An error occurs during initialization of Thrift support",
94                     e);
95        serviceReady.setFailure(e);
96        releaseResources();
97      }
98    }
99  
100   public final void releaseResources() {
101     if (server != null) {
102       logger.debug("Stop Thrift Server");
103       server.stop();
104     }
105     if (serverTransport != null) {
106       logger.debug("Stop Thrift Transport");
107       serverTransport.close();
108     }
109     logger.debug("Thrift stopped");
110   }
111 }