GNU Radio Manual and C++ API Reference  3.10.9.1
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  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef THRIFT_SERVER_TEMPLATE_H
12 #define THRIFT_SERVER_TEMPLATE_H
13 
14 #include <gnuradio/config.h>
15 #include <gnuradio/logger.h>
16 #include <gnuradio/prefs.h>
18 #include <iostream>
19 
20 #include "thrift/ControlPort.h"
21 #ifdef THRIFT_HAS_THREADFACTORY_H
22 #include <thrift/concurrency/ThreadFactory.h>
23 #else
24 #include <thrift/concurrency/PlatformThreadFactory.h>
25 #endif
26 #include <thrift/concurrency/ThreadManager.h>
27 #include <thrift/server/TSimpleServer.h>
28 #include <thrift/server/TThreadPoolServer.h>
29 #include <thrift/transport/TBufferTransports.h>
30 #include <thrift/transport/TServerSocket.h>
31 
32 using namespace apache;
33 
34 template <typename TserverBase, typename TserverClass, typename TImplClass>
35 class thrift_server_template : public thrift_application_base<TserverBase, TImplClass>
36 {
37 public:
38  thrift_server_template(TImplClass* _this);
40 
41 protected:
42  TserverBase* i_impl();
43  friend class thrift_application_base<TserverBase, TImplClass>;
44 
45 private:
46  std::shared_ptr<TserverClass> d_handler;
47  std::shared_ptr<thrift::TProcessor> d_processor;
48  std::shared_ptr<thrift::transport::TServerTransport> d_serverTransport;
49  std::shared_ptr<thrift::transport::TTransportFactory> d_transportFactory;
50  std::shared_ptr<thrift::protocol::TProtocolFactory> d_protocolFactory;
51  /**
52  * Custom TransportFactory that allows you to override the default Thrift buffer size
53  * of 512 bytes.
54  *
55  */
56  class TBufferedTransportFactory : public thrift::transport::TTransportFactory
57  {
58  public:
59  TBufferedTransportFactory(const unsigned int _bufferSize)
60  : bufferSize(_bufferSize)
61  {
62  ;
63  }
64 
65  virtual ~TBufferedTransportFactory() {}
66 
67  virtual std::shared_ptr<thrift::transport::TTransport>
68  getTransport(std::shared_ptr<thrift::transport::TTransport> trans)
69  {
70  return std::shared_ptr<thrift::transport::TTransport>(
71  new thrift::transport::TBufferedTransport(trans, bufferSize));
72  }
73 
74  private:
75  unsigned int bufferSize;
76  };
77 };
78 
79 template <typename TserverBase, typename TserverClass, typename TImplClass>
81  TImplClass* _this)
82  : thrift_application_base<TserverBase, TImplClass>(_this),
83  d_handler(new TserverClass()),
84  d_processor(new GNURadio::ControlPortProcessor(d_handler)),
85  d_serverTransport(),
86  d_transportFactory(),
87  d_protocolFactory(new thrift::protocol::TBinaryProtocolFactory())
88 {
89  gr::logger_ptr logger, debug_logger;
90  gr::configure_default_loggers(logger, debug_logger, "controlport");
91 
92  unsigned int port, nthreads, buffersize;
93  std::string thrift_config_file =
94  gr::prefs::singleton()->get_string("ControlPort", "config", "");
95 
96  if (thrift_config_file.length() > 0) {
97  gr::prefs::singleton()->add_config_file(thrift_config_file);
98  }
99 
100  // Collect configuration options from the Thrift config file;
101  // defaults if the config file doesn't exist or list the specific
102  // options.
103  port = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
104  "thrift",
105  "port",
107  nthreads = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
108  "thrift",
109  "nthreads",
111  buffersize = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
112  "thrift",
113  "buffersize",
115 
116  d_serverTransport.reset(new thrift::transport::TServerSocket(port));
117 
118  d_transportFactory.reset(
119  new thrift_server_template::TBufferedTransportFactory(buffersize));
120 
121  if (nthreads <= 1) {
122  // "Thrift: Single-threaded server"
123  // std::cout << "Thrift Single-threaded server" << std::endl;
125  new thrift::server::TSimpleServer(
126  d_processor, d_serverTransport, d_transportFactory, d_protocolFactory));
127  } else {
128  // std::cout << "Thrift Multi-threaded server : " << d_nthreads << std::endl;
129  std::shared_ptr<thrift::concurrency::ThreadManager> threadManager(
130  thrift::concurrency::ThreadManager::newSimpleThreadManager(nthreads));
131 
132 #ifdef THRIFT_HAS_THREADFACTORY_H
133  threadManager->threadFactory(std::shared_ptr<thrift::concurrency::ThreadFactory>(
134  new thrift::concurrency::ThreadFactory()));
135 #else
136  threadManager->threadFactory(
137  std::shared_ptr<thrift::concurrency::PlatformThreadFactory>(
138  new thrift::concurrency::PlatformThreadFactory()));
139 #endif
140 
141  threadManager->start();
142 
144  new thrift::server::TThreadPoolServer(d_processor,
145  d_serverTransport,
146  d_transportFactory,
147  d_protocolFactory,
148  threadManager));
149  }
150 }
151 
152 template <typename TserverBase, typename TserverClass, typename TImplClass>
154 {
155 }
156 
157 template <typename TserverBase, typename TserverClass, typename TImplClass>
159 {
160  // std::cerr << "thrift_server_template: i_impl" << std::endl;
161 
162  return d_handler.get();
163 }
164 
165 #endif /* THRIFT_SERVER_TEMPLATE_H */
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.
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.
static prefs * singleton()
void add_config_file(const std::string &configfile)
Base class for a Thrift application with a singleton with instance function thrift_application_base::...
Definition: thrift_application_base.h:76
Definition: thrift_server_template.h:36
TserverBase * i_impl()
Definition: thrift_server_template.h:158
~thrift_server_template()
Definition: thrift_server_template.h:153
thrift_server_template(TImplClass *_this)
Definition: thrift_server_template.h:80
Definition: thrift_application_base.h:28
std::shared_ptr< logger > logger_ptr
Definition: logger.h:250
GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, const std::string &name)