USRP Hardware Driver and USRP Manual  Version: 003.008.002-0-ge9d11b35
UHD and USRP Manual
rpc_common.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2013 Ettus Research LLC
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef INCLUDED_RPC_COMMON_HPP
19 #define INCLUDED_RPC_COMMON_HPP
20 
21 #define USE_BINARY_ARCHIVE 0
22 
23 #include <string>
24 #include <iostream>
25 #include <sstream>
26 #include <vector>
27 #if (USE_BINARY_ARCHIVE)
28  #include <boost/archive/binary_oarchive.hpp>
29  #include <boost/archive/binary_iarchive.hpp>
30 #else
31  #include <boost/archive/text_oarchive.hpp>
32  #include <boost/archive/text_iarchive.hpp>
33 #endif
34 
35 namespace uhd { namespace usrprio_rpc {
36 
37 //[Over-the-wire] IDs
38 typedef boost::int32_t func_id_t;
39 typedef boost::uint64_t client_id_t;
40 
41 #define build_client_id(host_id, process_id) \
42  ((static_cast<boost::uint64_t>(host_id) << 32) | static_cast<boost::uint64_t>(process_id))
43 #define get_process_id_from_client_id(client_id) \
44  (static_cast<boost::int32_t>(client_id))
45 #define get_host_id_from_client_id(client_id) \
46  (static_cast<boost::uint32_t>(client_id >> 32))
47 
48 //[Over-the-wire] Handshake format
49 struct hshake_args_t {
50  boost::uint32_t version;
51  boost::uint32_t oldest_comp_version;
52  boost::int32_t boost_archive_version;
53  client_id_t client_id;
54 };
55 
56 //[Over-the-wire] Header for RPC request and response
58 public:
59  func_id_t func_id;
60  client_id_t client_id;
61  boost::uint32_t func_args_size;
62 
63  static bool match_function(const func_args_header_t& a, const func_args_header_t& b) {
64  return ((a.func_id == b.func_id) && (a.client_id == b.client_id));
65  }
66 };
67 
68 //[Internal] Storage for RPC header and arguments
70 public:
72  std::vector<char> data;
73 };
74 
75 //[Internal] Serializer for RPC input parameters
77 public:
78  func_args_writer_t() : _stream(), _archive(_stream, boost::archive::no_header) {}
79 
80  template<typename data_t>
81  void push(const data_t& d) {
82  _archive << d;
83  }
84 
85  template<typename data_t>
86  func_args_writer_t& operator<< (const data_t& data) {
87  push(data);
88  return *this;
89  }
90 
91  void store(std::vector<char>& data) const {
92  const std::string& str = _stream.str();
93  data.resize(str.length());
94  data.assign((char*)str.c_str(), ((char*)str.c_str()) + str.length());
95  }
96 
97 private:
98  std::ostringstream _stream;
99 #if (USE_BINARY_ARCHIVE)
100  boost::archive::binary_oarchive _archive;
101 #else
102  boost::archive::text_oarchive _archive;
103 #endif
104 };
105 
106 //[Internal] Deserializer for RPC output parameters
108 public:
109  func_args_reader_t() : _stream(), _archive() {}
110 
111  template<typename data_t>
112  void pull(data_t& d) const {
113  if (_archive) (*_archive) >> d;
114  }
115 
116  template<typename data_t>
117  const func_args_reader_t& operator>> (data_t& data) const {
118  pull(data);
119  return *this;
120  }
121 
122  void load(const std::vector<char>& data) {
123  _stream.str(std::string(data.begin(), data.end()));
124 #if (USE_BINARY_ARCHIVE)
125  _archive.reset(new boost::archive::binary_iarchive(_stream, boost::archive::no_header));
126 #else
127  _archive.reset(new boost::archive::text_iarchive(_stream, boost::archive::no_header));
128 #endif
129  }
130 
131 private:
132  std::istringstream _stream;
133 #if (USE_BINARY_ARCHIVE)
134  boost::scoped_ptr<boost::archive::binary_iarchive> _archive;
135 #else
136  boost::scoped_ptr<boost::archive::text_iarchive> _archive;
137 #endif
138 };
139 
141 public:
142  static boost::int32_t get_version() {
143  #if (USE_BINARY_ARCHIVE)
144  typedef boost::archive::binary_oarchive archive_t;
145  #else
146  typedef boost::archive::text_oarchive archive_t;
147  #endif
148  std::ostringstream stream;
149  archive_t dummy_archive(stream, boost::archive::no_header);
150  return static_cast<boost::int32_t>(dummy_archive.get_library_version());
151  }
152 };
153 
154 }}
155 
156 #undef USE_BINARY_ARCHIVE
157 
158 #endif /* INCLUDED_RPC_COMMON_HPP */
Definition: rpc_common.hpp:76
void pull(data_t &d) const
Definition: rpc_common.hpp:112
void push(const data_t &d)
Definition: rpc_common.hpp:81
static boost::int32_t get_version()
Definition: rpc_common.hpp:142
Definition: rpc_common.hpp:107
client_id_t client_id
Definition: rpc_common.hpp:53
Definition: rpc_common.hpp:49
boost::int32_t boost_archive_version
Definition: rpc_common.hpp:52
Definition: rpc_common.hpp:57
boost::uint32_t func_args_size
Definition: rpc_common.hpp:61
static bool match_function(const func_args_header_t &a, const func_args_header_t &b)
Definition: rpc_common.hpp:63
Definition: convert.hpp:28
func_args_header_t header
Definition: rpc_common.hpp:71
void store(std::vector< char > &data) const
Definition: rpc_common.hpp:91
void load(const std::vector< char > &data)
Definition: rpc_common.hpp:122
boost::uint32_t version
Definition: rpc_common.hpp:50
func_args_writer_t()
Definition: rpc_common.hpp:78
boost::uint64_t client_id_t
Definition: rpc_common.hpp:39
boost::int32_t func_id_t
Definition: rpc_common.hpp:38
client_id_t client_id
Definition: rpc_common.hpp:60
boost::uint32_t oldest_comp_version
Definition: rpc_common.hpp:51
std::vector< char > data
Definition: rpc_common.hpp:72
func_id_t func_id
Definition: rpc_common.hpp:59
func_args_reader_t()
Definition: rpc_common.hpp:109
Definition: rpc_common.hpp:69