OpenSDN source code
TJSONProtocol.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef _SANDESH_PROTOCOL_TJSONPROTOCOL_H_
7 #define _SANDESH_PROTOCOL_TJSONPROTOCOL_H_ 1
8 
9 #include <string.h>
10 #include "TVirtualProtocol.h"
11 
12 #include <boost/shared_ptr.hpp>
13 #include <boost/tokenizer.hpp>
14 #include <boost/algorithm/string.hpp>
15 #include <boost/algorithm/string/replace.hpp>
16 #include <boost/container/set.hpp>
17 #include <base/time_util.h>
18 
19 namespace contrail { namespace sandesh { namespace protocol {
20 
24 class TJSONProtocol : public TVirtualProtocol<TJSONProtocol> {
25  private:
27  { UNINIT
29  , LIST
30  , SET
31  , MAP
33  };
34 
35  public:
36  TJSONProtocol(boost::shared_ptr<TTransport> trans)
38  , trans_(trans.get())
41  , reader_(*trans)
42  {
43  TType ttypes_arr[] = { T_STRUCT, T_MAP, T_SET, T_LIST, T_SANDESH };
44  complexTypeSet[T_STRUCT] = true;
45  complexTypeSet[T_MAP] = true;
46  complexTypeSet[T_SET] = true;
47  complexTypeSet[T_LIST] = true;
48  write_state_.push_back(UNINIT);
49  }
50 
51  static const int32_t DEFAULT_STRING_LIMIT = 256;
52  static const int32_t DEFAULT_STRING_PREFIX_SIZE = 16;
53 
54  void setStringSizeLimit(int32_t string_limit) {
55  string_limit_ = string_limit;
56  }
57 
58  void setStringPrefixSize(int32_t string_prefix_size) {
59  string_prefix_size_ = string_prefix_size;
60  }
61 
62  static std::string escapeJSONControlCharsInternal(const std::string& str) {
63  std::string xmlstr;
64  xmlstr.reserve(str.length());
65  for (std::string::const_iterator it = str.begin();
66  it != str.end(); ++it) {
67  switch(*it) {
68  case '&': xmlstr += "&amp;"; break;
69  case '\'': xmlstr += "&apos;"; break;
70  case '<': xmlstr += "&lt;"; break;
71  case '>': xmlstr += "&gt;"; break;
72  default: xmlstr += *it;
73  }
74  }
75  return xmlstr;
76  }
77 
78  static std::string escapeJSONControlChars(const std::string& str) {
79  if (strpbrk(str.c_str(), "&'<>") != NULL) {
81  } else {
82  return str;
83  }
84  }
85 
86  static void unescapeJSONControlChars(std::string& str) {
87  boost::algorithm::replace_all(str, "&amp;", "&");
88  boost::algorithm::replace_all(str, "&apos;", "\'");
89  boost::algorithm::replace_all(str, "&lt;", "<");
90  boost::algorithm::replace_all(str, "&gt;", ">");
91  }
92 
97  int32_t writeMessageBegin(const std::string& name,
98  const TMessageType messageType,
99  const int32_t seqid);
100 
101  int32_t writeMessageEnd();
102 
103  int32_t writeStructBegin(const char* name);
104 
105  int32_t writeStructEnd();
106 
107  int32_t writeSandeshBegin(const char* name);
108 
109  int32_t writeSandeshEnd();
110 
112 
114 
115  int32_t writeFieldBegin(const char* name,
116  const TType fieldType,
117  const int16_t fieldId,
118  const std::map<std::string, std::string> *const amap = NULL);
119 
120  int32_t writeFieldEnd();
121 
122  int32_t writeFieldStop();
123 
124  int32_t writeMapBegin(const TType keyType,
125  const TType valType,
126  const uint32_t size);
127 
128  int32_t writeMapEnd();
129 
130  int32_t writeListBegin(const TType elemType,
131  const uint32_t size);
132 
133  int32_t writeListEnd();
134 
135  int32_t writeSetBegin(const TType elemType,
136  const uint32_t size);
137 
138  int32_t writeSetEnd();
139 
140  int32_t writeBool(const bool value);
141 
142  int32_t writeByte(const int8_t byte);
143 
144  int32_t writeI16(const int16_t i16);
145 
146  int32_t writeI32(const int32_t i32);
147 
148  int32_t writeI64(const int64_t i64);
149 
150  int32_t writeU16(const uint16_t u16);
151 
152  int32_t writeU32(const uint32_t u32);
153 
154  int32_t writeU64(const uint64_t u64);
155 
156  int32_t writeIPV4(const uint32_t ip4);
157 
158  int32_t writeIPADDR(const boost::asio::ip::address& ipaddress);
159 
160  int32_t writeDouble(const double dub);
161 
162  int32_t writeString(const std::string& str);
163 
164  int32_t writeBinary(const std::string& str);
165 
166  int32_t writeJSON(const std::string& str);
167 
169 
173  void setSandeshEnd(bool val) {
174  sandesh_end_ = val;
175  }
176 
178 
179  public:
180 
182  trans_(&trans),
183  hasData_(false),
184  has2Data_(false),
185  firstRead_(false) {
186  }
187 
188  uint8_t read() {
189  if (hasData_) {
190  hasData_ = false;
191  }
192  else {
193  if (has2Data_) {
194  if (firstRead_) {
195  has2Data_ = false;
196  firstRead_ = false;
197  return data2_[1];
198  } else {
199  firstRead_ = true;
200  return data2_[0];
201  }
202  } else {
203  trans_->readAll(&data_, 1);
204  }
205  }
206  return data_;
207  }
208 
209  uint8_t peek() {
210  if (!hasData_) {
211  trans_->readAll(&data_, 1);
212  }
213  hasData_ = true;
214  return data_;
215  }
216 
217  uint8_t peek2() {
218  bool first = false;
219  if (!has2Data_) {
220  trans_->readAll(data2_, 2);
221  first = true;
222  }
223  has2Data_ = true;
224  return first ? data2_[0] : data2_[1];
225  }
226 
227  private:
229  bool hasData_;
230  uint8_t data_;
231  bool has2Data_;
232  uint8_t data2_[2];
234  };
235 
236  private:
237  typedef boost::tokenizer<boost::char_separator<char> >
239  void indentUp();
240  void indentDown();
241  int32_t writePlain(const std::string& str);
242  int32_t writeIndented(const std::string& str);
243 
244  static const std::string& fieldTypeName(TType type);
245  static TType getTypeIDForTypeName(const std::string &name);
246 
248 
249  int32_t string_limit_;
251 
252  std::string indent_str_;
253 
254  static const int indent_inc = 2;
255 
256  std::vector<write_state_t> write_state_;
257 
259 
261 
262  std::vector<bool> is_struct_begin_list_;
263 
264  std::vector<bool> is_list_begin_list_;
265 
267 
269 
271 
273 
275 
277 
279 
281 
283 
285 
287 
289 
291 
293 
295 
297 
298  std::vector<std::string> current_sandesh_context_;
299 
300  std::vector<bool> is_first_element_context_;
301 
302  std::vector<bool> is_primitive_element_list_;
303 
304  std::vector<bool> is_map_val_primitive_;
305 
306  std::vector<bool> in_map_val_context_;
307  std::vector<std::string> xml_state_;
309  std::map<TType, bool> complexTypeSet;
310 
311 };
312 
317  public:
320 
321  boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
322  return boost::shared_ptr<TProtocol>(new TJSONProtocol(trans));
323  }
324 
325 };
326 
327 }}} // contrail::sandesh::protocol
328 
329 #endif // #ifndef _SANDESH_PROTOCOL_TJSONPROTOCOL_H_
330 
331 
boost::shared_ptr< TProtocol > getProtocol(boost::shared_ptr< TTransport > trans)
int32_t writeJSON(const std::string &str)
int32_t writePlain(const std::string &str)
int32_t writeI32(const int32_t i32)
static std::string escapeJSONControlChars(const std::string &str)
Definition: TJSONProtocol.h:78
int32_t writeListBegin(const TType elemType, const uint32_t size)
int32_t writeSandeshBegin(const char *name)
std::vector< std::string > xml_state_
static TType getTypeIDForTypeName(const std::string &name)
int32_t writeBinary(const std::string &str)
int32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size)
static const std::string & fieldTypeName(TType type)
int32_t writeIPV4(const uint32_t ip4)
int32_t writeI16(const int16_t i16)
int32_t writeString(const std::string &str)
static std::string escapeJSONControlCharsInternal(const std::string &str)
Definition: TJSONProtocol.h:62
std::vector< std::string > current_sandesh_context_
int32_t writeI64(const int64_t i64)
int32_t writeStructBegin(const char *name)
static void unescapeJSONControlChars(std::string &str)
Definition: TJSONProtocol.h:86
static const int32_t DEFAULT_STRING_PREFIX_SIZE
Definition: TJSONProtocol.h:52
boost::tokenizer< boost::char_separator< char > > tokenizer
std::vector< write_state_t > write_state_
int32_t writeU64(const uint64_t u64)
int32_t writeIndented(const std::string &str)
int32_t writeFieldBegin(const char *name, const TType fieldType, const int16_t fieldId, const std::map< std::string, std::string > *const amap=NULL)
int32_t writeUUID(const boost::uuids::uuid &uuid)
int32_t writeByte(const int8_t byte)
int32_t writeU16(const uint16_t u16)
int32_t writeIPADDR(const boost::asio::ip::address &ipaddress)
TJSONProtocol(boost::shared_ptr< TTransport > trans)
Definition: TJSONProtocol.h:36
int32_t writeMessageBegin(const std::string &name, const TMessageType messageType, const int32_t seqid)
int32_t writeSetBegin(const TType elemType, const uint32_t size)
int32_t writeU32(const uint32_t u32)
int32_t writeDouble(const double dub)
void setStringSizeLimit(int32_t string_limit)
Definition: TJSONProtocol.h:54
void setStringPrefixSize(int32_t string_prefix_size)
Definition: TJSONProtocol.h:58
int32_t readAll(uint8_t *buf, uint32_t len)
Definition: TTransport.h:128
uint8_t type
Definition: load_balance.h:2
Definition: regex.h:11
boost::uuids::uuid uuid