OpenSDN source code
TXMLProtocol.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef _SANDESH_PROTOCOL_TXMLPROTOCOL_H_
7 #define _SANDESH_PROTOCOL_TXMLPROTOCOL_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 
17 namespace contrail { namespace sandesh { namespace protocol {
18 
22 class TXMLProtocol : public TVirtualProtocol<TXMLProtocol> {
23  private:
25  { UNINIT
27  , LIST
28  , SET
29  , MAP
31  };
32 
33  public:
34  TXMLProtocol(boost::shared_ptr<TTransport> trans)
36  , trans_(trans.get())
39  , reader_(*trans)
40  {
41  write_state_.push_back(UNINIT);
42  }
43 
44  static const int32_t DEFAULT_STRING_LIMIT = 256;
45  static const int32_t DEFAULT_STRING_PREFIX_SIZE = 16;
46 
47  void setStringSizeLimit(int32_t string_limit) {
48  string_limit_ = string_limit;
49  }
50 
51  void setStringPrefixSize(int32_t string_prefix_size) {
52  string_prefix_size_ = string_prefix_size;
53  }
54 
55  static std::string escapeXMLControlCharsInternal(const std::string& str) {
56  std::string xmlstr;
57  xmlstr.reserve(str.length());
58  for (std::string::const_iterator it = str.begin();
59  it != str.end(); ++it) {
60  switch(*it) {
61  case '&': xmlstr += "&amp;"; break;
62  case '\'': xmlstr += "&apos;"; break;
63  case '<': xmlstr += "&lt;"; break;
64  case '>': xmlstr += "&gt;"; break;
65  default: xmlstr += *it;
66  }
67  }
68  return xmlstr;
69  }
70 
71  static std::string escapeXMLControlChars(const std::string& str) {
72  if (strpbrk(str.c_str(), "&'<>") != NULL) {
74  } else {
75  return str;
76  }
77  }
78 
79  static void unescapeXMLControlChars(std::string& str) {
80  boost::algorithm::replace_all(str, "&amp;", "&");
81  boost::algorithm::replace_all(str, "&apos;", "\'");
82  boost::algorithm::replace_all(str, "&lt;", "<");
83  boost::algorithm::replace_all(str, "&gt;", ">");
84  }
85 
90  int32_t writeMessageBegin(const std::string& name,
91  const TMessageType messageType,
92  const int32_t seqid);
93 
94  int32_t writeMessageEnd();
95 
96  int32_t writeStructBegin(const char* name);
97 
98  int32_t writeStructEnd();
99 
100  int32_t writeSandeshBegin(const char* name);
101 
102  int32_t writeSandeshEnd();
103 
105 
107 
108  int32_t writeFieldBegin(const char* name,
109  const TType fieldType,
110  const int16_t fieldId,
111  const std::map<std::string, std::string> *const amap = NULL);
112 
113  int32_t writeFieldEnd();
114 
115  int32_t writeFieldStop();
116 
117  int32_t writeMapBegin(const TType keyType,
118  const TType valType,
119  const uint32_t size);
120 
121  int32_t writeMapEnd();
122 
123  int32_t writeListBegin(const TType elemType,
124  const uint32_t size);
125 
126  int32_t writeListEnd();
127 
128  int32_t writeSetBegin(const TType elemType,
129  const uint32_t size);
130 
131  int32_t writeSetEnd();
132 
133  int32_t writeBool(const bool value);
134 
135  int32_t writeByte(const int8_t byte);
136 
137  int32_t writeI16(const int16_t i16);
138 
139  int32_t writeI32(const int32_t i32);
140 
141  int32_t writeI64(const int64_t i64);
142 
143  int32_t writeU16(const uint16_t u16);
144 
145  int32_t writeU32(const uint32_t u32);
146 
147  int32_t writeU64(const uint64_t u64);
148 
149  int32_t writeIPV4(const uint32_t ip4);
150 
151  int32_t writeIPADDR(const boost::asio::ip::address& ipaddress);
152 
153  int32_t writeDouble(const double dub);
154 
155  int32_t writeString(const std::string& str);
156 
157  int32_t writeBinary(const std::string& str);
158 
159  int32_t writeXML(const std::string& str);
160 
162 
167  int32_t readMessageBegin(std::string& name,
168  TMessageType& messageType,
169  int32_t& seqid);
170 
171  int32_t readMessageEnd();
172 
173  int32_t readSandeshBegin(std::string& name);
174 
175  int32_t readSandeshEnd();
176 
177  int32_t readStructBegin(std::string& name);
178 
179  int32_t readStructEnd();
180 
182 
184 
185  int32_t readFieldBegin(std::string& name,
186  TType& fieldType,
187  int16_t& fieldId);
188 
189  int32_t readFieldEnd();
190 
191  int32_t readMapBegin(TType& keyType,
192  TType& valType,
193  uint32_t& size);
194 
195  int32_t readMapEnd();
196 
197  int32_t readListBegin(TType& elemType,
198  uint32_t& size);
199 
200  int32_t readListEnd();
201 
202  int32_t readSetBegin(TType& elemType,
203  uint32_t& size);
204 
205  int32_t readSetEnd();
206 
207  int32_t readBool(bool& value);
208 
209  // Provide the default readBool() implementation for std::vector<bool>
211 
212  int32_t readByte(int8_t& byte);
213 
214  int32_t readI16(int16_t& i16);
215 
216  int32_t readI32(int32_t& i32);
217 
218  int32_t readI64(int64_t& i64);
219 
220  int32_t readU16(uint16_t& u16);
221 
222  int32_t readU32(uint32_t& u32);
223 
224  int32_t readU64(uint64_t& u64);
225 
226  int32_t readIPV4(uint32_t& ip4);
227 
228  int32_t readIPADDR(boost::asio::ip::address& ipaddress);
229 
230  int32_t readDouble(double& dub);
231 
232  int32_t readString(std::string& str);
233 
234  int32_t readBinary(std::string& str);
235 
236  int32_t readXML(std::string& str);
237 
239 
241 
242  public:
243 
245  trans_(&trans),
246  hasData_(false),
247  has2Data_(false),
248  firstRead_(false) {
249  }
250 
251  uint8_t read() {
252  if (hasData_) {
253  hasData_ = false;
254  }
255  else {
256  if (has2Data_) {
257  if (firstRead_) {
258  has2Data_ = false;
259  firstRead_ = false;
260  return data2_[1];
261  } else {
262  firstRead_ = true;
263  return data2_[0];
264  }
265  } else {
266  trans_->readAll(&data_, 1);
267  }
268  }
269  return data_;
270  }
271 
272  uint8_t peek() {
273  if (!hasData_) {
274  trans_->readAll(&data_, 1);
275  }
276  hasData_ = true;
277  return data_;
278  }
279 
280  uint8_t peek2() {
281  bool first = false;
282  if (!has2Data_) {
283  trans_->readAll(data2_, 2);
284  first = true;
285  }
286  has2Data_ = true;
287  return first ? data2_[0] : data2_[1];
288  }
289 
290  private:
292  bool hasData_;
293  uint8_t data_;
294  bool has2Data_;
295  uint8_t data2_[2];
297  };
298 
299  private:
300  typedef boost::tokenizer<boost::char_separator<char> >
302 
303  int32_t readXMLSyntaxChar(uint8_t ch);
304 
305  int32_t readXMLSyntaxString(const std::string &str);
306 
307  int32_t readXMLString(std::string &str);
308 
309  int32_t readXMLTag(std::string &str, bool endTag = false);
310 
311  int32_t readXMLNumericChars(std::string &str);
312 
313  int32_t readXMLCDATA(std::string &str);
314 
315  template <typename NumberType>
316  int32_t readXMLInteger(NumberType &num);
317 
318  int32_t readXMLDouble(double &num);
319 
321 
322  void indentUp();
323  void indentDown();
324  int32_t writePlain(const std::string& str);
325  int32_t writeIndented(const std::string& str);
326 
327  static const std::string& fieldTypeName(TType type);
328  static TType getTypeIDForTypeName(const std::string &name);
329 
331 
332  int32_t string_limit_;
334 
335  std::string indent_str_;
336 
337  static const int indent_inc = 2;
338 
339  std::vector<write_state_t> write_state_;
340 
341  std::vector<std::string> xml_state_;
343 };
344 
349  public:
351  virtual ~TXMLProtocolFactory() {}
352 
353  boost::shared_ptr<TProtocol> getProtocol(boost::shared_ptr<TTransport> trans) {
354  return boost::shared_ptr<TProtocol>(new TXMLProtocol(trans));
355  }
356 
357 };
358 
359 }}} // contrail::sandesh::protocol
360 
361 #endif // #ifndef _SANDESH_PROTOCOL_TXMLPROTOCOL_H_
362 
363 
boost::shared_ptr< TProtocol > getProtocol(boost::shared_ptr< TTransport > trans)
Definition: TXMLProtocol.h:353
int32_t readFieldBegin(std::string &name, TType &fieldType, int16_t &fieldId)
int32_t readXMLCDATA(std::string &str)
int32_t writeBinary(const std::string &str)
int32_t readString(std::string &str)
int32_t readXMLNumericChars(std::string &str)
int32_t writeU32(const uint32_t u32)
int32_t readBinary(std::string &str)
int32_t readListBegin(TType &elemType, uint32_t &size)
int32_t writeStructBegin(const char *name)
int32_t readXMLInteger(NumberType &num)
static const int32_t DEFAULT_STRING_PREFIX_SIZE
Definition: TXMLProtocol.h:45
int32_t readMessageBegin(std::string &name, TMessageType &messageType, int32_t &seqid)
int32_t readMapBegin(TType &keyType, TType &valType, uint32_t &size)
std::vector< write_state_t > write_state_
Definition: TXMLProtocol.h:339
int32_t writeDouble(const double dub)
int32_t writeSetBegin(const TType elemType, const uint32_t size)
int32_t writeMessageBegin(const std::string &name, const TMessageType messageType, const int32_t seqid)
static void unescapeXMLControlChars(std::string &str)
Definition: TXMLProtocol.h:79
int32_t writeU16(const uint16_t u16)
static std::string escapeXMLControlCharsInternal(const std::string &str)
Definition: TXMLProtocol.h:55
TXMLProtocol(boost::shared_ptr< TTransport > trans)
Definition: TXMLProtocol.h:34
int32_t readStructBegin(std::string &name)
void setStringSizeLimit(int32_t string_limit)
Definition: TXMLProtocol.h:47
int32_t readXMLUuid(boost::uuids::uuid &uuid)
int32_t readIPADDR(boost::asio::ip::address &ipaddress)
int32_t writeString(const std::string &str)
int32_t writeIPADDR(const boost::asio::ip::address &ipaddress)
int32_t writeListBegin(const TType elemType, const uint32_t size)
int32_t readXMLString(std::string &str)
int32_t readXMLSyntaxString(const std::string &str)
int32_t writeI64(const int64_t i64)
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 writePlain(const std::string &str)
int32_t writeSandeshBegin(const char *name)
int32_t readUUID(boost::uuids::uuid &uuid)
static const std::string & fieldTypeName(TType type)
int32_t writeXML(const std::string &str)
int32_t writeIPV4(const uint32_t ip4)
int32_t writeIndented(const std::string &str)
int32_t readXMLTag(std::string &str, bool endTag=false)
void setStringPrefixSize(int32_t string_prefix_size)
Definition: TXMLProtocol.h:51
int32_t writeByte(const int8_t byte)
int32_t writeU64(const uint64_t u64)
int32_t writeI32(const int32_t i32)
static TType getTypeIDForTypeName(const std::string &name)
std::vector< std::string > xml_state_
Definition: TXMLProtocol.h:341
int32_t readSandeshBegin(std::string &name)
boost::tokenizer< boost::char_separator< char > > tokenizer
Definition: TXMLProtocol.h:301
int32_t writeBool(const bool value)
int32_t writeI16(const int16_t i16)
static std::string escapeXMLControlChars(const std::string &str)
Definition: TXMLProtocol.h:71
int32_t readSetBegin(TType &elemType, uint32_t &size)
int32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size)
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