1 /*-----------------------------------------------------------------------------
2 Name: MessageUnit.h
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Holding a message
6 -----------------------------------------------------------------------------*/
7
8 #ifndef _UTIL_MESSAGEUNIT_C
9 #define _UTIL_MESSAGEUNIT_C
10
11 #if defined(_WIN32)
12 #pragma warning(disable:4786)
13 #endif
14
15 #include <string>
16 #include <vector>
17 #include <cstring> // memcpy()
18 #include <util/Log.h>
19 #include <util/MessageUnit.h>
20
21 using namespace std;
22 using namespace org::xmlBlaster::util;
23 using namespace org::xmlBlaster::util::key;
24 using namespace org::xmlBlaster::util::qos;
25 using namespace org::xmlBlaster::client::qos;
26 using namespace org::xmlBlaster::client::key;
27
28 namespace org { namespace xmlBlaster { namespace util {
29
30 MessageUnit::MessageUnit(const KeyData &key,
31 unsigned long len,
32 const unsigned char * content,
33 const QosData &qos)
34 : key_(key.getClone()),
35 len_(len), content_(0),
36 qos_(qos.getClone()),
37 immutableSizeInBytes_(0)
38 {
39 if (len_ > 0) {
40 content_ = new unsigned char[len_];
41 memcpy(content_, content, len_);
42 }
43 }
44
45 /**
46 * Constructs a MessageUnit with a string.
47 */
48 MessageUnit::MessageUnit(const KeyData &key,
49 const string &content,
50 const QosData &qos)
51 : key_(key.getClone()),
52 len_(content.size()), content_(0),
53 qos_(qos.getClone()),
54 immutableSizeInBytes_(0)
55 {
56 if (len_ > 0) {
57 content_ = new unsigned char[len_];
58 memcpy(content_, content.c_str(), len_);
59 }
60 }
61
62
63 /**
64 * Constructs a MessageUnit with a string and a PublishQos object
65 */
66 MessageUnit::MessageUnit(const PublishKey& xmlKey,
67 const string &content,
68 PublishQos& publishQos)
69 : key_(xmlKey.getData().getClone()),
70 len_(content.size()),
71 content_(0),
72 qos_(publishQos.getData().getClone()),
73 immutableSizeInBytes_(0)
74 {
75 if (len_ > 0) {
76 content_ = new unsigned char[len_];
77 memcpy(content_, content.c_str(), len_);
78 }
79 }
80
81
82 /**
83 * Constructs the message unit.
84 */
85 MessageUnit::MessageUnit(const KeyData &xmlKey,
86 const vector<unsigned char> &contentVec,
87 const QosData &qos)
88 : key_(xmlKey.getClone()),
89 len_(contentVec.size()), content_(0),
90 qos_(qos.getClone()),
91 immutableSizeInBytes_(0)
92 {
93 if (len_ > 0) {
94 content_ = new unsigned char[len_];
95 for (unsigned int ii=0; ii<len_; ii++) {
96 content_[ii] = contentVec[ii];
97 }
98 }
99 }
100
101
102 /**
103 * Constructs the message unit by taking a PublishQos object.
104 */
105 MessageUnit::MessageUnit(const PublishKey &xmlKey,
106 const vector<unsigned char> &contentVec,
107 PublishQos& publishQos)
108 : key_(xmlKey.getData().getClone()),
109 len_(contentVec.size()), content_(0),
110 qos_(publishQos.getData().getClone()),
111 immutableSizeInBytes_(0)
112 {
113 if (len_ > 0) {
114 content_ = new unsigned char[len_];
115 for (unsigned int ii=0; ii<len_; ii++) {
116 content_[ii] = contentVec[ii];
117 }
118 }
119 }
120
121 /**
122 * Copy constructor
123 */
124 MessageUnit::MessageUnit(const MessageUnit& rhs)
125 : key_(rhs.getKeyRef()),
126 len_(rhs.getContentLen()),
127 content_(0),
128 qos_(rhs.getQosRef()),
129 immutableSizeInBytes_(rhs.getSizeInBytes())
130 {
131 if (len_ > 0) {
132 content_ = new unsigned char[len_];
133 memcpy(content_, rhs.getContent(), len_);
134 }
135 }
136
137 /**
138 * Assignment constructor
139 */
140 MessageUnit& MessageUnit::operator=(const MessageUnit& rhs)
141 {
142 if (this != &rhs) {
143 key_ = rhs.getKeyRef();
144 len_ = rhs.getContentLen();
145 if (len_ > 0) {
146 content_ = new unsigned char[len_];
147 memcpy(content_, rhs.getContent(), len_);
148 }
149 qos_ = rhs.getQosRef();
150 immutableSizeInBytes_ = rhs.getSizeInBytes();
151 }
152 return *this;
153 }
154
155 MessageUnit::~MessageUnit()
156 {
157 if (content_ != 0)
158 delete [] content_;
159 }
160
161 const KeyData& MessageUnit::getKey() const
162 {
163 return *key_;
164 }
165
166 const KeyDataRef MessageUnit::getKeyRef() const
167 {
168 return key_;
169 }
170
171 vector<unsigned char> MessageUnit::getContentVec() const
172 {
173 vector<unsigned char> vec;
174 if (len_ > 0) {
175 vec.reserve(len_);
176 for (unsigned int ii=0; ii<len_; ii++) {
177 vec.push_back(content_[ii]);
178 }
179 }
180 return vec;
181 }
182
183 size_t MessageUnit::getSizeInBytes() const
184 {
185 if (immutableSizeInBytes_ > 0) return immutableSizeInBytes_;
186 // See org.xmlBlaster.engine.MsgUnitWrapper.java
187 immutableSizeInBytes_ = 306 + len_ + key_->toXml().size() + qos_->toXml().size();
188 return immutableSizeInBytes_;
189 }
190
191 std::string MessageUnit::getContentStr() const {
192 if (len_ < 1) {
193 return "";
194 }
195 return std::string(reinterpret_cast<const char *>(content_), static_cast<std::string::size_type>(len_));
196 }
197
198 const QosData& MessageUnit::getQos() const
199 {
200 return *qos_;
201 }
202
203 const QosDataRef MessageUnit::getQosRef() const
204 {
205 return qos_;
206 }
207
208 string MessageUnit::toXml(const string &extraOffset) const
209 {
210 string ret;
211 string offset = Constants::OFFSET + extraOffset;
212
213 ret += offset + "<MessageUnit>";
214 ret += getKey().toXml(Constants::INDENT+extraOffset);
215 if (len_ > 0) {
216 ret += offset + " <content>" + getContentStr() + "</content>";
217 }
218 ret += getQos().toXml(Constants::INDENT+extraOffset);
219 ret += offset + "</MessageUnit>";
220 return ret;
221 }
222
223 }}} // namespace
224 #endif
syntax highlighted by Code2HTML, v. 0.9.1