GNU Radio Manual and C++ API Reference 3.7.14.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/logger.h>
27#include <gnuradio/prefs.h>
29#include <iostream>
30
31#include "thrift/ControlPort.h"
32#include <thrift/concurrency/PlatformThreadFactory.h>
33#include <thrift/concurrency/ThreadManager.h>
34#include <thrift/server/TSimpleServer.h>
35#include <thrift/server/TThreadPoolServer.h>
36#include <thrift/transport/TBufferTransports.h>
37#include <thrift/transport/TServerSocket.h>
38
39using namespace apache;
40
41template <typename TserverBase, typename TserverClass, typename TImplClass>
42class thrift_server_template : public thrift_application_base<TserverBase, TImplClass>
43{
44public:
45 thrift_server_template(TImplClass* _this);
47
48protected:
49 TserverBase* i_impl();
50 friend class thrift_application_base<TserverBase, TImplClass>;
51
52private:
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)
67 : bufferSize(_bufferSize)
68 {
69 ;
70 }
71
72 virtual ~TBufferedTransportFactory() {}
73
74 virtual boost::shared_ptr<thrift::transport::TTransport>
75 getTransport(boost::shared_ptr<thrift::transport::TTransport> trans)
76 {
77 return boost::shared_ptr<thrift::transport::TTransport>(
78 new thrift::transport::TBufferedTransport(trans, bufferSize));
79 }
80
81 private:
82 unsigned int bufferSize;
83 };
84};
85
86template <typename TserverBase, typename TserverClass, typename TImplClass>
88 TImplClass* _this)
89 : thrift_application_base<TserverBase, TImplClass>(_this),
90 d_handler(new TserverClass()),
91 d_processor(new GNURadio::ControlPortProcessor(d_handler)),
92 d_serverTransport(),
93 d_transportFactory(),
94 d_protocolFactory(new thrift::protocol::TBinaryProtocolFactory())
95{
96 gr::logger_ptr logger, debug_logger;
97 gr::configure_default_loggers(logger, debug_logger, "controlport");
98
99 unsigned int port, nthreads, buffersize;
100 std::string thrift_config_file =
101 gr::prefs::singleton()->get_string("ControlPort", "config", "");
102
103 if (thrift_config_file.length() > 0) {
104 gr::prefs::singleton()->add_config_file(thrift_config_file);
105 }
106
107 // Collect configuration options from the Thrift config file;
108 // defaults if the config file doesn't exist or list the specific
109 // options.
110 port = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
111 "thrift",
112 "port",
114 nthreads = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
115 "thrift",
116 "nthreads",
118 buffersize = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
119 "thrift",
120 "buffersize",
122
123 d_serverTransport.reset(new thrift::transport::TServerSocket(port));
124
125 d_transportFactory.reset(
126 new thrift_server_template::TBufferedTransportFactory(buffersize));
127
128 if (nthreads <= 1) {
129 // "Thrift: Single-threaded server"
130 // std::cout << "Thrift Single-threaded server" << std::endl;
132 new thrift::server::TSimpleServer(
133 d_processor, d_serverTransport, d_transportFactory, d_protocolFactory));
134 } else {
135 // std::cout << "Thrift Multi-threaded server : " << d_nthreads << std::endl;
136 boost::shared_ptr<thrift::concurrency::ThreadManager> threadManager(
137 thrift::concurrency::ThreadManager::newSimpleThreadManager(nthreads));
138
139 threadManager->threadFactory(
140 boost::shared_ptr<thrift::concurrency::PlatformThreadFactory>(
141 new thrift::concurrency::PlatformThreadFactory()));
142
143 threadManager->start();
144
146 new thrift::server::TThreadPoolServer(d_processor,
147 d_serverTransport,
148 d_transportFactory,
149 d_protocolFactory,
150 threadManager));
151 }
152}
153
154template <typename TserverBase, typename TserverClass, typename TImplClass>
156{
157}
158
159template <typename TserverBase, typename TserverClass, typename TImplClass>
161{
162 // std::cerr << "thrift_server_template: i_impl" << std::endl;
163
164 return d_handler.get();
165}
166
167#endif /* THRIFT_SERVER_TEMPLATE_H */
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.
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.
void add_config_file(const std::string &configfile)
static prefs * singleton()
Base class for a Thrift application with a singleton with instance function thrift_application_base::...
Definition: thrift_application_base.h:87
Definition: thrift_server_template.h:43
TserverBase * i_impl()
Definition: thrift_server_template.h:160
~thrift_server_template()
Definition: thrift_server_template.h:155
thrift_server_template(TImplClass *_this)
Definition: thrift_server_template.h:87
Definition: thrift_application_base.h:39
void * logger_ptr
Definition: logger.h:696
GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, const std::string name)