hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sample.cpp
Go to the documentation of this file.
1 
14 #include <sstream>
15 #include <iostream>
16 
17 #include "JsonMessageCover.hpp"
18 
19 void printTitle(const std::string& title)
20 {
21  std::cout << std::string(title.length(), '=') << std::endl << title << std::endl << std::string(title.length(), '=') << std::endl;
22 }
23 //-----------------------------------------------------------------------------
25 {
26  printTitle("Sample use serialize of JsonMessageCover");
27 
28  const std::string data = "Hello world!";
29  HCE::types::MessageType type = HCE::types::MessageType::mtSphinx;
30  unsigned int ttl = 234;
31 
32  HCE::JsonMessageCover jsonMessageCover;
33  jsonMessageCover.setType(type);
34  jsonMessageCover.setData(data);
35  jsonMessageCover.setTTL(ttl);
36 
37  std::string json;
38  std::cout << "serialize => " << std::boolalpha << jsonMessageCover.serialize(json) << std::endl;
39  std::cout << "isError => " << std::boolalpha << jsonMessageCover.isError() << std::endl;
40  if (jsonMessageCover.isError())
41  std::cout << "ErrorCode: " << jsonMessageCover.getErrorCode() << "\tErrorMsg: " << jsonMessageCover.getErrorMsg() << std::endl;
42 
43  std::cout << "JSON: " << json << std::endl;
44 }
45 //-----------------------------------------------------------------------------
47 {
48  printTitle("Sample use unserialize of JsonMessageCover");
49 
50  std::string json;
51 
52 #if JSON_USE_BASE64 == 1
53  std::cout << "JSON_USE_BASE64 == 1" << std::endl;
54  json = "{\"data\":\"SGVsbG8gd29ybGQh\",\"ttl\":234,\"type\":1}";
55 #else
56  std::cout << "JSON_USE_BASE64 != 1" << std::endl;
57  json = "{\"data\":\"Hello world!\",\"ttl\":234,\"type\":1}";
58 #endif
59 
60  HCE::JsonMessageCover jsonMessageCover;
61  std::cout << "unserialize => " << std::boolalpha << jsonMessageCover.unserialize(json) << std::endl;
62  std::cout << "isError => " << std::boolalpha << jsonMessageCover.isError() << std::endl;
63  if (jsonMessageCover.isError())
64  std::cout << "ErrorCode: " << jsonMessageCover.getErrorCode() << "\tErrorMsg: " << jsonMessageCover.getErrorMsg() << std::endl;
65 
66  std::cout << "Type: " << static_cast<unsigned int>(jsonMessageCover.getType()) << std::endl;
67  std::cout << "Data: " << jsonMessageCover.getData() << std::endl;
68  std::cout << "TTL: " << jsonMessageCover.getTTL() << std::endl;
69 }
70 //-----------------------------------------------------------------------------
72 {
73  printTitle("Sample use output stream for JsonMessageCover");
74 
75  const std::string data = "Hello world!";
76  HCE::types::MessageType type = HCE::types::MessageType::mtSphinx;
77  unsigned int ttl = 234;
78 
79  HCE::JsonMessageCover jsonMessageCover;
80  jsonMessageCover.setType(type);
81  jsonMessageCover.setData(data);
82  jsonMessageCover.setTTL(ttl);
83 
84  std::stringstream json;
85  json << jsonMessageCover;
86 
87  std::cout << "isError => " << std::boolalpha << jsonMessageCover.isError() << std::endl;
88  if (jsonMessageCover.isError())
89  std::cout << "ErrorCode: " << jsonMessageCover.getErrorCode() << "\tErrorMsg: " << jsonMessageCover.getErrorMsg() << std::endl;
90 
91  std::cout << "JSON: " << json.str() << std::endl;
92 }
93 //-----------------------------------------------------------------------------
95 {
96  printTitle("Sample use input stream for JsonMessageCover");
97 
98  std::stringstream json;
99 
100 #if JSON_USE_BASE64 == 1
101  std::cout << "JSON_USE_BASE64 == 1" << std::endl;
102  json << "{\"data\":\"SGVsbG8gd29ybGQh\",\"ttl\":234,\"type\":1}";
103 #else
104  std::cout << "JSON_USE_BASE64 != 1" << std::endl;
105  json << "{\"data\":\"Hello world!\",\"ttl\":234,\"type\":1}";
106 #endif
107 
108  HCE::JsonMessageCover jsonMessageCover;
109  json >> jsonMessageCover;
110 
111  std::cout << "isError => " << std::boolalpha << jsonMessageCover.isError() << std::endl;
112  if (jsonMessageCover.isError())
113  std::cout << "ErrorCode: " << jsonMessageCover.getErrorCode() << "\tErrorMsg: " << jsonMessageCover.getErrorMsg() << std::endl;
114 
115  std::cout << "Type: " << static_cast<unsigned int>(jsonMessageCover.getType()) << std::endl;
116  std::cout << "Data: " << jsonMessageCover.getData() << std::endl;
117  std::cout << "TTL: " << jsonMessageCover.getTTL() << std::endl;
118 }
119 //-----------------------------------------------------------------------------
121 {
122  printTitle("Sample error message for JsonMessageCover");
123 
124  std::stringstream json;
125 
126 #if JSON_USE_BASE64 == 1
127  std::cout << "JSON_USE_BASE64 == 1" << std::endl;
128  json << "{\"data\":\"SGVsbG8gd29ybGQh\",\"ttl\":234,\"typ";
129 #else
130  std::cout << "JSON_USE_BASE64 != 1" << std::endl;
131  json << "{\"data\":\"Hello world!\",\"ttl\":234,\"typ";
132 #endif
133 
134  HCE::JsonMessageCover jsonMessageCover;
135  json >> jsonMessageCover;
136 
137  std::cout << "isError => " << std::boolalpha << jsonMessageCover.isError() << std::endl;
138  if (jsonMessageCover.isError())
139  std::cout << "ErrorCode: " << jsonMessageCover.getErrorCode() << "\tErrorMsg: " << jsonMessageCover.getErrorMsg() << std::endl;
140 }
141 //-----------------------------------------------------------------------------
142 //-----------------------------------------------------------------------------
143 int main(void)
144 {
150 
151  return 0;
152 }
153