summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/include/gnuradio/thrift_server_template.h
blob: 31c1316c63ad278d69b88b28b309d1efc3051cfc (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
/* -*- c++ -*- */
/*
 * Copyright 2015 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifndef THRIFT_SERVER_TEMPLATE_H
#define THRIFT_SERVER_TEMPLATE_H

#include <gnuradio/rpcserver_thrift.h>
#include <gnuradio/thrift_application_base.h>
#include <iostream>

#include <thrift/server/TSimpleServer.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PlatformThreadFactory.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <ControlPort.h>

using namespace apache;

namespace {
  static const unsigned int ETHERNET_HEADER_SIZE(14);
  static const unsigned int IP_HEADER_SIZE(20);
  static const unsigned int TCP_HEADER_SIZE(32);
  static const unsigned int ETHERNET_TYPICAL_MTU(1500);
  static const unsigned int ALRIGHT_DEFAULT_BUFFER_SIZE(
    ETHERNET_TYPICAL_MTU - ETHERNET_HEADER_SIZE - IP_HEADER_SIZE - TCP_HEADER_SIZE);
}

template<typename TserverBase, typename TserverClass, typename TImplClass, typename TThriftClass>
class thrift_server_template : public thrift_application_base<TserverBase, TImplClass>
{
public:
  thrift_server_template(TImplClass* _this,
                         const std::string& contolPortName,
                         const std::string& endpointName);
  ~thrift_server_template();

protected:
  TserverBase* i_impl();
  friend class thrift_application_base<TserverBase, TImplClass>;

  TserverBase* d_server;
  const std::string d_contolPortName, d_endpointName;

private:

  /**
   * Custom TransportFactory that allows you to override the default Thrift buffer size
   * of 512 bytes.
   *
   */
  class TBufferedTransportFactory : public thrift::transport::TTransportFactory
  {
  public:
    TBufferedTransportFactory() : bufferSize(ALRIGHT_DEFAULT_BUFFER_SIZE) {;}
    TBufferedTransportFactory(const unsigned int _bufferSize) : bufferSize(_bufferSize) {;}

    virtual ~TBufferedTransportFactory() {}

    virtual boost::shared_ptr<thrift::transport::TTransport> getTransport(
        boost::shared_ptr<thrift::transport::TTransport> trans)
    {
      return boost::shared_ptr<thrift::transport::TTransport>
        (new thrift::transport::TBufferedTransport(trans, bufferSize));
    }
  private:
    unsigned int bufferSize;
  };
};

template<typename TserverBase, typename TserverClass, typename TImplClass, typename TThriftClass>
thrift_server_template<TserverBase, TserverClass, TImplClass, TThriftClass>::thrift_server_template
(TImplClass* _this, const std::string& controlPortName, const std::string& endpointName)
  : thrift_application_base<TserverBase, TImplClass>(_this),
    d_contolPortName(controlPortName),
    d_endpointName(endpointName)
{
  //std::cerr << "thrift_server_template: ctor" << std::endl;

  boost::shared_ptr<TserverClass> handler(new TserverClass());

  boost::shared_ptr<thrift::TProcessor>
    processor(new GNURadio::ControlPortProcessor(handler));

  boost::shared_ptr<thrift::transport::TServerTransport>
    serverTransport(new thrift::transport::TServerSocket(9090));

  boost::shared_ptr<thrift::transport::TTransportFactory>
    transportFactory(new thrift_server_template::TBufferedTransportFactory());

  boost::shared_ptr<thrift::protocol::TProtocolFactory>
    protocolFactory(new thrift::protocol::TBinaryProtocolFactory());

  if(thrift_application_base<TserverBase, TImplClass>::d_default_num_thrift_threads <= 1) {
    // "Thrift: Single-threaded server"
    thrift_application_base<TserverBase, TImplClass>::d_thriftserver =
      new thrift::server::TSimpleServer(processor, serverTransport,
                                        transportFactory, protocolFactory);
  }
  else {
    // std::cout << "Thrift Multi-threaded server : " << d_default_num_thrift_threads << std::endl;
    boost::shared_ptr<thrift::concurrency::ThreadManager> threadManager
      (thrift::concurrency::ThreadManager::newSimpleThreadManager
       (thrift_application_base<TserverBase, TImplClass>::d_default_num_thrift_threads));

    boost::shared_ptr<thrift::concurrency::PlatformThreadFactory> threadFactory
      (boost::shared_ptr<thrift::concurrency::PlatformThreadFactory>
       (new thrift::concurrency::PlatformThreadFactory()));

    threadManager->threadFactory(threadFactory);

    threadManager->start();

    thrift_application_base<TserverBase, TImplClass>::d_thriftserver =
      new thrift::server::TThreadPoolServer(processor, serverTransport,
                                            transportFactory, protocolFactory,
                                            threadManager);
  }

  d_server = handler.get();
}

template<typename TserverBase, typename TserverClass, typename TImplClass, typename TThriftClass>
thrift_server_template<TserverBase, TserverClass,TImplClass, TThriftClass>::~thrift_server_template()
{
}

template<typename TserverBase, typename TserverClass, typename TImplClass, typename TThriftClass>
TserverBase* thrift_server_template<TserverBase, TserverClass, TImplClass, TThriftClass>::i_impl()
{
  //std::cerr << "thrift_server_template: i_impl" << std::endl;
  return d_server;
}

#endif /* THRIFT_SERVER_TEMPLATE_H */