GNU Radio Manual and C++ API Reference  3.8.4.0
The Free & Open Software Radio Ecosystem
thrift_server_template.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2015 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef THRIFT_SERVER_TEMPLATE_H
24 #define THRIFT_SERVER_TEMPLATE_H
25 
26 #include <gnuradio/config.h>
27 #include <gnuradio/logger.h>
28 #include <gnuradio/prefs.h>
31 #include <iostream>
32 
33 #include "thrift/ControlPort.h"
34 #ifdef THRIFT_HAS_THREADFACTORY_H
35 #include <thrift/concurrency/ThreadFactory.h>
36 #else
37 #include <thrift/concurrency/PlatformThreadFactory.h>
38 #endif
39 #include <thrift/concurrency/ThreadManager.h>
40 #include <thrift/server/TSimpleServer.h>
41 #include <thrift/server/TThreadPoolServer.h>
42 #include <thrift/transport/TBufferTransports.h>
43 #include <thrift/transport/TServerSocket.h>
44 
45 using namespace apache;
46 
47 template <typename TserverBase, typename TserverClass, typename TImplClass>
48 class thrift_server_template : public thrift_application_base<TserverBase, TImplClass>
49 {
50 public:
51  thrift_server_template(TImplClass* _this);
53 
54 protected:
55  TserverBase* i_impl();
56  friend class thrift_application_base<TserverBase, TImplClass>;
57 
58 private:
59  // typename necessary in C++11 for dependent types:
60  typename gr::rpc_sptr<TserverClass>::t d_handler;
61  gr::rpc_sptr<thrift::TProcessor>::t d_processor;
62  gr::rpc_sptr<thrift::transport::TServerTransport>::t d_serverTransport;
63  gr::rpc_sptr<thrift::transport::TTransportFactory>::t d_transportFactory;
64  gr::rpc_sptr<thrift::protocol::TProtocolFactory>::t d_protocolFactory;
65  /**
66  * Custom TransportFactory that allows you to override the default Thrift buffer size
67  * of 512 bytes.
68  *
69  */
70  class TBufferedTransportFactory : public thrift::transport::TTransportFactory
71  {
72  public:
73  TBufferedTransportFactory(const unsigned int _bufferSize)
74  : bufferSize(_bufferSize)
75  {
76  ;
77  }
78 
79  virtual ~TBufferedTransportFactory() {}
80 
81  virtual gr::rpc_sptr<thrift::transport::TTransport>::t
82  getTransport(gr::rpc_sptr<thrift::transport::TTransport>::t trans)
83  {
84  return gr::rpc_sptr<thrift::transport::TTransport>::t(
85  new thrift::transport::TBufferedTransport(trans, bufferSize));
86  }
87 
88  private:
89  unsigned int bufferSize;
90  };
91 };
92 
93 template <typename TserverBase, typename TserverClass, typename TImplClass>
95  TImplClass* _this)
96  : thrift_application_base<TserverBase, TImplClass>(_this),
97  d_handler(new TserverClass()),
98  d_processor(new GNURadio::ControlPortProcessor(d_handler)),
99  d_serverTransport(),
100  d_transportFactory(),
101  d_protocolFactory(new thrift::protocol::TBinaryProtocolFactory())
102 {
103  gr::logger_ptr logger, debug_logger;
104  gr::configure_default_loggers(logger, debug_logger, "controlport");
105 
106  unsigned int port, nthreads, buffersize;
107  std::string thrift_config_file =
108  gr::prefs::singleton()->get_string("ControlPort", "config", "");
109 
110  if (thrift_config_file.length() > 0) {
111  gr::prefs::singleton()->add_config_file(thrift_config_file);
112  }
113 
114  // Collect configuration options from the Thrift config file;
115  // defaults if the config file doesn't exist or list the specific
116  // options.
117  port = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
118  "thrift",
119  "port",
121  nthreads = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
122  "thrift",
123  "nthreads",
125  buffersize = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
126  "thrift",
127  "buffersize",
129 
130  d_serverTransport.reset(new thrift::transport::TServerSocket(port));
131 
132  d_transportFactory.reset(
133  new thrift_server_template::TBufferedTransportFactory(buffersize));
134 
135  if (nthreads <= 1) {
136  // "Thrift: Single-threaded server"
137  // std::cout << "Thrift Single-threaded server" << std::endl;
139  new thrift::server::TSimpleServer(
140  d_processor, d_serverTransport, d_transportFactory, d_protocolFactory));
141  } else {
142  // std::cout << "Thrift Multi-threaded server : " << d_nthreads << std::endl;
143  gr::rpc_sptr<thrift::concurrency::ThreadManager>::t threadManager(
144  thrift::concurrency::ThreadManager::newSimpleThreadManager(nthreads));
145 
146 #ifdef THRIFT_HAS_THREADFACTORY_H
147  threadManager->threadFactory(gr::rpc_sptr<thrift::concurrency::ThreadFactory>::t(
148  new thrift::concurrency::ThreadFactory()));
149 #else
150  threadManager->threadFactory(
151  gr::rpc_sptr<thrift::concurrency::PlatformThreadFactory>::t(
152  new thrift::concurrency::PlatformThreadFactory()));
153 #endif
154 
155  threadManager->start();
156 
158  new thrift::server::TThreadPoolServer(d_processor,
159  d_serverTransport,
160  d_transportFactory,
161  d_protocolFactory,
162  threadManager));
163  }
164 }
165 
166 template <typename TserverBase, typename TserverClass, typename TImplClass>
168 {
169 }
170 
171 template <typename TserverBase, typename TserverClass, typename TImplClass>
173 {
174  // std::cerr << "thrift_server_template: i_impl" << std::endl;
175 
176  return d_handler.get();
177 }
178 
179 #endif /* THRIFT_SERVER_TEMPLATE_H */
thrift_server_template(TImplClass *_this)
Definition: thrift_server_template.h:94
TserverBase * i_impl()
Definition: thrift_server_template.h:172
Definition: thrift_server_template.h:48
~thrift_server_template()
Definition: thrift_server_template.h:167
virtual long get_long(const std::string &section, const std::string &option, long default_val)
If option exists and value can be converted to long, return it; else default_val. ...
Base class for a Thrift application with a singleton with instance function thrift_application_base::...
Definition: thrift_application_base.h:86
void add_config_file(const std::string &configfile)
GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, const std::string name)
virtual const std::string get_string(const std::string &section, const std::string &option, const std::string &default_val)
If option exists return associated value; else default_val.
Definition: thrift_application_base.h:39
log4cpp::Category * logger_ptr
GR_LOG macrosThese macros wrap the standard LOG4CPP_LEVEL macros. The availablie macros are: LOG_DEBU...
Definition: logger.h:71
static prefs * singleton()