1 /*------------------------------------------------------------------------------
2 Name: QosData.cpp
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6
7 /**
8 * Data container handling of publish() and update() quality of services.
9 * <p />
10 * QoS Informations sent from the client to the server via the publish() method and back via the update() method<br />
11 * They are needed to control xmlBlaster and inform the client.
12 * <p />
13 * <p>
14 * This data holder is accessible through 4 decorators, each of them allowing a specialized view on the data:
15 * </p>
16 * <ul>
17 * <li>PublishQosServer Server side access</i>
18 * <li>PublishQos Client side access</i>
19 * <li>UpdateQosServer Server side access facade</i>
20 * <li>UpdateQos Client side access facade</i>
21 * </ul>
22 * <p>
23 * For the xml representation see MsgQosSaxFactory.
24 * </p>
25 * @see org.xmlBlaster.util.qos.MsgQosSaxFactory
26 * @see org.xmlBlaster.test.classtest.qos.MsgQosFactoryTest
27 * @author xmlBlaster@marcelruff.info
28 * @author michele@laghi.eu
29 */
30
31 #include <util/qos/QosData.h>
32 #include <util/Constants.h>
33 #include <util/lexical_cast.h>
34 #include <util/Global.h>
35 #include <util/PriorityEnum.h>
36
37 using namespace org::xmlBlaster::util;
38 using namespace org::xmlBlaster::util::cluster;
39
40 using namespace std;
41
42 namespace org { namespace xmlBlaster { namespace util { namespace qos {
43
44 const bool DEFAULT_isSubscribable = true;
45 const bool DEFAULT_isVolatile = false;
46 const bool DEFAULT_persistent = false;
47 const bool DEFAULT_forceUpdate = true;
48 const bool DEFAULT_forceDestroy = false;
49
50 void QosData::init()
51 {
52 state_ = Constants::STATE_OK;
53 stateInfo_ = "";
54 rcvTimestamp_ = 0;
55 rcvTimestampFound_ = false;
56 serialData_ = "";
57 priority_ = NORM_PRIORITY;
58 fromPersistenceStore_ = false;
59 persistent_ = DEFAULT_persistent;
60 }
61
62 void QosData::copy(const QosData& data)
63 {
64 SessionName *p = new SessionName(global_, data.getSender()->getAbsoluteName());
65 SessionNameRef r(p);
66 sender_ = r;
67
68 clientProperties_ = data.clientProperties_;
69 state_ = data.state_;
70 stateInfo_ = data.stateInfo_;
71 rcvTimestamp_ = data.rcvTimestamp_;
72 rcvTimestampFound_ = data.rcvTimestampFound_;
73 serialData_ = data.serialData_;
74 priority_ = data.priority_;
75 fromPersistenceStore_ = data.fromPersistenceStore_;
76 persistent_ = data.persistent_;
77 }
78
79
80 QosData::QosData(Global& global, const string& serialData)
81 : ME("QosData"),
82 global_(global),
83 log_(global.getLog("org.xmlBlaster.util.qos")),
84 routeNodeList_(),
85 clientProperties_(),
86 sender_(new SessionName(global))
87 {
88 init();
89 serialData_ = serialData;
90 }
91
92
93 QosData::QosData(const QosData& data)
94 : ME(data.ME),
95 global_(data.global_),
96 log_(data.log_),
97 routeNodeList_(data.routeNodeList_),
98 clientProperties_(),
99 sender_(0)
100 {
101 copy(data);
102 }
103
104 QosData& QosData::operator=(const QosData& data)
105 {
106 copy(data);
107 return *this;
108 }
109
110
111 QosData::~QosData()
112 {
113 }
114
115 void QosData::setState(const string& state)
116 {
117 state_ = state;
118 }
119
120 string QosData::getState() const
121 {
122 return state_;
123 }
124
125 void QosData::setStateInfo(const string& stateInfo)
126 {
127 stateInfo_ = stateInfo;
128 }
129
130 string QosData::getStateInfo() const
131 {
132 return stateInfo_;
133 }
134
135 bool QosData::isOk() const
136 {
137 return Constants::STATE_OK == state_;
138 }
139
140 bool QosData::isErased() const
141 {
142 return Constants::STATE_ERASED == state_;
143 }
144
145 bool QosData::isTimeout() const
146 {
147 return Constants::STATE_TIMEOUT == state_;
148 }
149
150 bool QosData::isForwardError() const
151 {
152 return Constants::STATE_FORWARD_ERROR == state_;
153 }
154
155 void QosData::addRouteInfo(const RouteInfo& routeInfo)
156 {
157 routeNodeList_.insert(routeNodeList_.end(), routeInfo);
158
159 // Set stratum to new values
160 int offset = routeInfo.getStratum();
161 if (offset < 0) offset = 0;
162
163 vector<RouteInfo>::reverse_iterator iter = routeNodeList_.rbegin();
164 while (iter != routeNodeList_.rend()) {
165 (*iter).setStratum(offset++);
166 iter++;
167 }
168 }
169
170 int QosData::count(const NodeId& nodeId) const
171 {
172 int cnt = 0;
173 if (routeNodeList_.empty()) return cnt;
174 vector<RouteInfo>::const_iterator iter = routeNodeList_.begin();
175 while (iter != routeNodeList_.end()) {
176 if ((*iter).getNodeId() == nodeId) cnt++;
177 iter++;
178 }
179 return cnt;
180 }
181
182 bool QosData::dirtyRead(NodeId nodeId) const
183 {
184 if (routeNodeList_.empty()) return false;
185 vector<RouteInfo>::const_iterator iter = routeNodeList_.begin();
186 while (iter != routeNodeList_.end()) {
187 if ((*iter).getNodeId() == nodeId) return (*iter).getDirtyRead();
188 }
189 return false;
190 }
191
192 void QosData::setRcvTimestamp(Timestamp rcvTimestamp)
193 {
194 rcvTimestamp_ = rcvTimestamp;
195 }
196
197 Timestamp QosData::getRcvTimestamp() const
198 {
199 return rcvTimestamp_;
200 }
201
202 void QosData::touchRcvTimestamp()
203 {
204 rcvTimestamp_ = TimestampFactory::getInstance().getTimestamp();
205 }
206
207 void QosData::addClientProperty(const ClientProperty& clientProperty)
208 {
209 clientProperties_.insert(ClientPropertyMap::value_type(clientProperty.getName(), clientProperty));
210 }
211
212 bool QosData::hasClientProperty(const string& name) const
213 {
214 return clientProperties_.count(name) > 0;
215 }
216
217 const QosData::ClientPropertyMap& QosData::getClientProperties() const
218 {
219 return clientProperties_;
220 }
221
222 void QosData::setClientProperties(const QosData::ClientPropertyMap& cm)
223 {
224 clientProperties_ = cm;
225 }
226
227 RouteVector QosData::getRouteNodes() const
228 {
229 return routeNodeList_;
230 }
231
232 void QosData::clearRoutes()
233 {
234 routeNodeList_.erase(routeNodeList_.begin(), routeNodeList_.end());
235 }
236
237 int QosData::size() const
238 {
239 return (int)toXml().size();
240 }
241
242 /**
243 * Message priority.
244 * @return priority 0-9
245 * @see org.xmlBlaster.util.def.Constants
246 */
247 PriorityEnum QosData::getPriority() const
248 {
249 return priority_;
250 }
251
252 /**
253 * Set message priority value, PriorityEnum.NORM_PRIORITY (5) is default.
254 * PriorityEnum.MIN_PRIORITY (0) is slowest
255 * whereas PriorityEnum.MAX_PRIORITY (9) is highest priority.
256 * @see org.xmlBlaster.util.def.Constants
257 */
258 void QosData::setPriority(PriorityEnum priority)
259 {
260 priority_ = priority;
261 }
262
263 /**
264 * Internal use only, is this message sent from the persistence layer?
265 * @return true/false
266 */
267 bool QosData::isFromPersistenceStore() const
268 {
269 return fromPersistenceStore_;
270 }
271
272 /**
273 * Internal use only, set if this message sent from the persistence layer
274 * @param true/false
275 */
276 void QosData::setFromPersistenceStore(bool fromPersistenceStore)
277 {
278 fromPersistenceStore_ = fromPersistenceStore;
279 }
280
281 /**
282 * @param persistent mark a message as persistent
283 */
284 void QosData::setPersistent(bool persistent)
285 {
286 persistent_ = persistent;
287 }
288
289 /**
290 * @return true/false
291 */
292 bool QosData::isPersistent() const
293 {
294 return persistent_;
295 }
296
297 SessionNameRef QosData::getSender() const
298 {
299 return sender_;
300 }
301
302 void QosData::setSender(org::xmlBlaster::util::SessionNameRef sender) const
303 {
304 sender_ = sender;
305 }
306
307 string QosData::dumpClientProperties(const string& extraOffset, bool clearText) const
308 {
309 string ret = "";
310 QosData::ClientPropertyMap::const_iterator iter = clientProperties_.begin();
311 while (iter != clientProperties_.end()) {
312 const ClientProperty& cp = (*iter).second;
313 ret += cp.toXml(extraOffset, clearText);
314 iter++;
315 }
316 return ret;
317 }
318
319 QosData* QosData::getClone() const
320 {
321 return new QosData(*this);
322 }
323
324 std::string QosData::toXml(const std::string& /*extraOffset*/) const
325 {
326 return "<error>QosData::toXml: PLEASE IMPLEMENT IN_BASE CLASS</error>";
327 }
328
329 }}}}
syntax highlighted by Code2HTML, v. 0.9.1