Loading...
Searching...
No Matches
thrift_server_template.h
Go to the documentation of this file.
/* -*- c++ -*- */
/*
* Copyright 2015 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#ifndef THRIFT_SERVER_TEMPLATE_H
#define THRIFT_SERVER_TEMPLATE_H
#include <gnuradio/config.h>
#include <gnuradio/logger.h>
#include <gnuradio/prefs.h>
#include <gnuradio/thrift_application_base.h>
#include <iostream>
#include "thrift/ControlPort.h"
#ifdef THRIFT_HAS_THREADFACTORY_H
#include <thrift/concurrency/ThreadFactory.h>
#else
#include <thrift/concurrency/PlatformThreadFactory.h>
#endif
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TServerSocket.h>
using namespace apache;
template <typename TserverBase, typename TserverClass, typename TImplClass>
class thrift_server_template : public thrift_application_base<TserverBase, TImplClass>
{
public:
thrift_server_template(TImplClass* _this);
~thrift_server_template();
protected:
TserverBase* i_impl();
friend class thrift_application_base<TserverBase, TImplClass>;
private:
std::shared_ptr<TserverClass> d_handler;
std::shared_ptr<thrift::TProcessor> d_processor;
std::shared_ptr<thrift::transport::TServerTransport> d_serverTransport;
std::shared_ptr<thrift::transport::TTransportFactory> d_transportFactory;
std::shared_ptr<thrift::protocol::TProtocolFactory> d_protocolFactory;
/**
* Custom TransportFactory that allows you to override the default Thrift buffer size
* of 512 bytes.
*
*/
class TBufferedTransportFactory : public thrift::transport::TTransportFactory
{
public:
TBufferedTransportFactory(const unsigned int _bufferSize)
: bufferSize(_bufferSize)
{
;
}
~TBufferedTransportFactory() override {}
std::shared_ptr<thrift::transport::TTransport>
getTransport(std::shared_ptr<thrift::transport::TTransport> trans) override
{
return std::shared_ptr<thrift::transport::TTransport>(
new thrift::transport::TBufferedTransport(trans, bufferSize));
}
private:
unsigned int bufferSize;
};
};
template <typename TserverBase, typename TserverClass, typename TImplClass>
thrift_server_template<TserverBase, TserverClass, TImplClass>::thrift_server_template(
TImplClass* _this)
: thrift_application_base<TserverBase, TImplClass>(_this),
d_handler(new TserverClass()),
d_processor(new GNURadio::ControlPortProcessor(d_handler)),
d_serverTransport(),
d_transportFactory(),
d_protocolFactory(new thrift::protocol::TBinaryProtocolFactory())
{
gr::logger_ptr logger, debug_logger;
gr::configure_default_loggers(logger, debug_logger, "controlport");
unsigned int port, nthreads, buffersize;
std::string thrift_config_file =
gr::prefs::singleton()->get_string("ControlPort", "config", "");
if (thrift_config_file.length() > 0) {
gr::prefs::singleton()->add_config_file(thrift_config_file);
}
// Collect configuration options from the Thrift config file;
// defaults if the config file doesn't exist or list the specific
// options.
port = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
"thrift",
"port",
thrift_application_base<TserverBase, TImplClass>::d_default_thrift_port));
nthreads = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
"thrift",
"nthreads",
thrift_application_base<TserverBase, TImplClass>::d_default_num_thrift_threads));
buffersize = static_cast<unsigned int>(gr::prefs::singleton()->get_long(
"thrift",
"buffersize",
thrift_application_base<TserverBase, TImplClass>::d_default_thrift_buffer_size));
d_serverTransport.reset(new thrift::transport::TServerSocket(port));
d_transportFactory.reset(
new thrift_server_template::TBufferedTransportFactory(buffersize));
if (nthreads <= 1) {
// "Thrift: Single-threaded server"
// std::cout << "Thrift Single-threaded server" << std::endl;
thrift_application_base<TserverBase, TImplClass>::d_thriftserver.reset(
new thrift::server::TSimpleServer(
d_processor, d_serverTransport, d_transportFactory, d_protocolFactory));
} else {
// std::cout << "Thrift Multi-threaded server : " << d_nthreads << std::endl;
std::shared_ptr<thrift::concurrency::ThreadManager> threadManager(
thrift::concurrency::ThreadManager::newSimpleThreadManager(nthreads));
#ifdef THRIFT_HAS_THREADFACTORY_H
threadManager->threadFactory(std::shared_ptr<thrift::concurrency::ThreadFactory>(
new thrift::concurrency::ThreadFactory()));
#else
threadManager->threadFactory(
std::shared_ptr<thrift::concurrency::PlatformThreadFactory>(
new thrift::concurrency::PlatformThreadFactory()));
#endif
threadManager->start();
thrift_application_base<TserverBase, TImplClass>::d_thriftserver.reset(
new thrift::server::TThreadPoolServer(d_processor,
d_serverTransport,
d_transportFactory,
d_protocolFactory,
threadManager));
}
}
template <typename TserverBase, typename TserverClass, typename TImplClass>
thrift_server_template<TserverBase, TserverClass, TImplClass>::~thrift_server_template()
{
}
template <typename TserverBase, typename TserverClass, typename TImplClass>
TserverBase* thrift_server_template<TserverBase, TserverClass, TImplClass>::i_impl()
{
// std::cerr << "thrift_server_template: i_impl" << std::endl;
return d_handler.get();
}
#endif /* THRIFT_SERVER_TEMPLATE_H */
gr::prefs::get_string
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.
gr::prefs::get_long
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.
gr::prefs::add_config_file
void add_config_file(const std::string &configfile)
gr::prefs::singleton
static prefs * singleton()
thrift_application_base::d_default_thrift_buffer_size
static const unsigned int d_default_thrift_buffer_size
Definition thrift_application_base.h:145
thrift_application_base< TserverBase, TImplClass >::thrift_application_base
thrift_application_base(TImplClass *_app)
Definition thrift_application_base.h:190
thrift_application_base::d_thriftserver
std::unique_ptr< apache::thrift::server::TServer > d_thriftserver
Definition thrift_application_base.h:118
thrift_application_base::d_default_num_thrift_threads
static const unsigned int d_default_num_thrift_threads
Definition thrift_application_base.h:139
thrift_application_base::d_default_thrift_port
static const unsigned int d_default_thrift_port
Definition thrift_application_base.h:132
thrift_server_template::i_impl
TserverBase * i_impl()
Definition thrift_server_template.h:158
thrift_server_template::~thrift_server_template
~thrift_server_template()
Definition thrift_server_template.h:153
thrift_server_template::thrift_server_template
thrift_server_template(TImplClass *_this)
Definition thrift_server_template.h:80
config.h
logger.h
apache::thrift
Definition thrift_application_base.h:29
apache
Definition thrift_application_base.h:28
gr::logger_ptr
std::shared_ptr< logger > logger_ptr
Definition logger.h:250
gr::configure_default_loggers
GR_RUNTIME_API bool configure_default_loggers(gr::logger_ptr &l, gr::logger_ptr &d, const std::string &name)
prefs.h
thrift_application_base.h