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