summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/include/gnuradio/thrift_server_template.h
blob: 6cd8f63d15aa7471d03491d0ae57221199f333b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* -*- 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)
        {
            ;
        }

        virtual ~TBufferedTransportFactory() {}

        virtual std::shared_ptr<thrift::transport::TTransport>
        getTransport(std::shared_ptr<thrift::transport::TTransport> trans)
        {
            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 */