GNU Radio Manual and C++ API Reference  3.7.13.4
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/prefs.h>
27 #include <gnuradio/logger.h>
29 #include <iostream>
30 
31 #include <thrift/server/TSimpleServer.h>
32 #include <thrift/server/TThreadPoolServer.h>
33 #include <thrift/concurrency/ThreadManager.h>
34 #include <thrift/concurrency/PlatformThreadFactory.h>
35 #include <thrift/transport/TServerSocket.h>
36 #include <thrift/transport/TBufferTransports.h>
37 #include "thrift/ControlPort.h"
38 
39 using namespace apache;
40 
41 template<typename TserverBase, typename TserverClass, typename TImplClass>
42 class thrift_server_template : public thrift_application_base<TserverBase, TImplClass>
43 {
44 public:
45  thrift_server_template(TImplClass* _this);
47 
48 protected:
49  TserverBase* i_impl();
50  friend class thrift_application_base<TserverBase, TImplClass>;
51 
52 private:
53  boost::shared_ptr<TserverClass> d_handler;
54  boost::shared_ptr<thrift::TProcessor> d_processor;
55  boost::shared_ptr<thrift::transport::TServerTransport> d_serverTransport;
56  boost::shared_ptr<thrift::transport::TTransportFactory> d_transportFactory;
57  boost::shared_ptr<thrift::protocol::TProtocolFactory> d_protocolFactory;
58  /**
59  * Custom TransportFactory that allows you to override the default Thrift buffer size
60  * of 512 bytes.
61  *
62  */
63  class TBufferedTransportFactory : public thrift::transport::TTransportFactory
64  {
65  public:
66  TBufferedTransportFactory(const unsigned int _bufferSize) : bufferSize(_bufferSize) {;}
67 
68  virtual ~TBufferedTransportFactory() {}
69 
70  virtual boost::shared_ptr<thrift::transport::TTransport> getTransport(
71  boost::shared_ptr<thrift::transport::TTransport> trans)
72  {
73  return boost::shared_ptr<thrift::transport::TTransport>
74  (new thrift::transport::TBufferedTransport(trans, bufferSize));
75  }
76  private:
77  unsigned int bufferSize;
78  };
79 };
80 
81 template<typename TserverBase, typename TserverClass, typename TImplClass>
83  : thrift_application_base<TserverBase, TImplClass>(_this),
84  d_handler(new TserverClass()),
85  d_processor(new GNURadio::ControlPortProcessor(d_handler)),
86  d_serverTransport(),
87  d_transportFactory(),
88  d_protocolFactory(new thrift::protocol::TBinaryProtocolFactory())
89 {
90  gr::logger_ptr logger, debug_logger;
91  gr::configure_default_loggers(logger, debug_logger, "controlport");
92 
93  unsigned int port, nthreads, buffersize;
94  std::string thrift_config_file = 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>
104  (gr::prefs::singleton()->get_long("thrift", "port",
105  thrift_application_base<TserverBase,
106  TImplClass>::d_default_thrift_port));
107  nthreads = static_cast<unsigned int>
108  (gr::prefs::singleton()->get_long("thrift", "nthreads",
109  thrift_application_base<TserverBase,
110  TImplClass>::d_default_num_thrift_threads));
111  buffersize = static_cast<unsigned int>
112  (gr::prefs::singleton()->get_long("thrift", "buffersize",
113  thrift_application_base<TserverBase,
114  TImplClass>::d_default_thrift_buffer_size));
115 
116  d_serverTransport.reset(new thrift::transport::TServerSocket(port));
117 
118  d_transportFactory.reset(new thrift_server_template::TBufferedTransportFactory(buffersize));
119 
120  if(nthreads <= 1) {
121  // "Thrift: Single-threaded server"
122  //std::cout << "Thrift Single-threaded server" << std::endl;
124  (new thrift::server::TSimpleServer(d_processor, d_serverTransport,
125  d_transportFactory, d_protocolFactory));
126  }
127  else {
128  //std::cout << "Thrift Multi-threaded server : " << d_nthreads << std::endl;
129  boost::shared_ptr<thrift::concurrency::ThreadManager> threadManager
130  (thrift::concurrency::ThreadManager::newSimpleThreadManager(nthreads));
131 
132  threadManager->threadFactory
133  (boost::shared_ptr<thrift::concurrency::PlatformThreadFactory>
134  (new thrift::concurrency::PlatformThreadFactory()));
135 
136  threadManager->start();
137 
139  (new thrift::server::TThreadPoolServer(d_processor, d_serverTransport,
140  d_transportFactory, d_protocolFactory,
141  threadManager));
142  }
143 }
144 
145 template<typename TserverBase, typename TserverClass, typename TImplClass>
147 {
148 }
149 
150 template<typename TserverBase, typename TserverClass, typename TImplClass>
152 {
153  //std::cerr << "thrift_server_template: i_impl" << std::endl;
154 
155  return d_handler.get();
156 }
157 
158 #endif /* THRIFT_SERVER_TEMPLATE_H */
thrift_server_template(TImplClass *_this)
Definition: thrift_server_template.h:82
TserverBase * i_impl()
Definition: thrift_server_template.h:151
Definition: thrift_server_template.h:42
~thrift_server_template()
Definition: thrift_server_template.h:146
static const unsigned int d_default_num_thrift_threads
Definition: thrift_application_base.h:143
void * logger_ptr
Definition: logger.h:696
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:79
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
static const unsigned int d_default_thrift_port
Definition: thrift_application_base.h:136
static prefs * singleton()
static const unsigned int d_default_thrift_buffer_size
Definition: thrift_application_base.h:149