1 /*------------------------------------------------------------------------------
  2 Name:      QosData.h
  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 ruff@swand.lake.de
 28  * @author laghi@swissinfo.org
 29  */
 30 
 31 #ifndef _UTIL_QOS_QOSDATA_H
 32 #define _UTIL_QOS_QOSDATA_H
 33 
 34 #include <util/xmlBlasterDef.h>
 35 #include <util/cluster/RouteInfo.h>
 36 #include <util/SessionName.h>
 37 #include <util/qos/ClientProperty.h>
 38 #include <util/PriorityEnum.h>
 39 #include <util/ReferenceCounterBase.h>
 40 #include <util/ReferenceHolder.h>
 41 #include <vector>
 42 #include <map>
 43 #include <string>
 44 
 45 namespace org { namespace xmlBlaster { namespace util { namespace qos {
 46 
 47 extern Dll_Export const bool DEFAULT_isSubscribable;
 48 extern Dll_Export const bool DEFAULT_isVolatile;
 49 extern Dll_Export const bool DEFAULT_persistent;
 50 extern Dll_Export const bool DEFAULT_forceUpdate;
 51 extern Dll_Export const bool DEFAULT_forceDestroy;
 52 
 53 // extern Dll_Export const bool DEFAULT_readonly;
 54 
 55 typedef std::vector<org::xmlBlaster::util::cluster::RouteInfo> RouteVector;
 56 
 57 class Dll_Export QosData : public org::xmlBlaster::util::ReferenceCounterBase
 58 {
 59 
 60 private:
 61    void init();
 62 
 63 protected:
 64    std::string  ME;
 65    org::xmlBlaster::util::Global& global_;
 66    org::xmlBlaster::util::I_Log&    log_;
 67    std::string  serialData_;
 68 
 69    /** the state of the message, defaults to "OK" if no state is returned */
 70    std::string state_; // = Constants::STATE_OK;
 71    /** Human readable information */
 72    std::string stateInfo_;
 73 
 74    /** 
 75     * The receive timestamp (UTC time),
 76     * when message arrived in requestBroker.publish() method.<br />
 77     * In nanoseconds elapsed since midnight, January 1, 1970 UTC
 78     */
 79    Timestamp rcvTimestamp_;
 80    bool rcvTimestampFound_; // = false;
 81 
 82    /** The priority of the message */
 83    org::xmlBlaster::util::PriorityEnum priority_; // = org::xmlBlaster::util::PriorityEnum.NORM_PRIORITY;
 84 
 85    /** Internal use only, is this message sent from the persistence layer? */
 86    bool fromPersistenceStore_; // = false;
 87 
 88    bool persistent_; // = DEFAULT_persistent;
 89 
 90    /**
 91     * ArrayList containing org::xmlBlaster::util::cluster::RouteInfo objects
 92     */
 93    RouteVector routeNodeList_;
 94 
 95 public:   typedef std::map<std::string, org::xmlBlaster::util::qos::ClientProperty> ClientPropertyMap;
 96 protected:
 97 
 98    ClientPropertyMap clientProperties_; 
 99 
100    /** the sender (publisher) of this message (unique loginName) */
101    mutable org::xmlBlaster::util::SessionNameRef sender_;
102 
103    void copy(const QosData& data);
104 
105    std::string dumpClientProperties(const std::string& extraOffset, bool clearText) const;
106 
107 public:
108    /**
109     * Constructs the specialized quality of service object for a publish() or update() call.
110     * @param The factory which knows how to serialize and parse me
111     */
112 //   long size_;
113 
114    QosData(org::xmlBlaster::util::Global& global, const std::string& serialData="");
115 
116    QosData(const QosData& data);
117 
118    QosData& operator=(const QosData& data);
119 
120    virtual ~QosData();
121 
122    /**
123     * @param state The state of an update message
124     */
125    void setState(const std::string& state);
126 
127    /**
128     * Access state of message on update().
129     * @return OK (Other values are not yet supported)
130     */
131    std::string getState() const;
132 
133    /**
134     * @param state The human readable state text of an update message
135     */
136    void setStateInfo(const std::string& stateInfo);
137 
138    /**
139     * Access state of message on update().
140     * @return The human readable info text
141     */
142    std::string getStateInfo() const;
143 
144    /**
145     * True if the message is OK on update(). 
146     */
147    bool isOk() const;
148 
149    /**
150     * True if the message was erased by timer or by a
151     * client invoking erase(). 
152     */
153    bool isErased() const;
154 
155    /**
156     * True if a timeout on this message occurred. 
157     * <p />
158     * Timeouts are spanned by the publisher and thrown by xmlBlaster
159     * on timeout to indicate for example
160     * STALE messages or any other user problem domain specific event.
161     */
162    bool isTimeout() const;
163 
164    /**
165     * True on cluster forward problems
166     */
167    bool isForwardError() const;
168 
169    /**
170     * Adds a new route hop to the QoS of this message. 
171     * The added routeInfo is assumed to be one stratum closer to the master
172     * So we will rearrange the stratum here. The given stratum in routeInfo
173     * is used to recalculate the other nodes as well.
174     */
175    void addRouteInfo(const org::xmlBlaster::util::cluster::RouteInfo& routeInfo);
176 
177    /**
178     * Check if the message has already been at the given node (circulating message). 
179     * @return How often the message has travelled the node already
180     */
181    int count(const org::xmlBlaster::util::cluster::NodeId& nodeId) const;
182 
183    /**
184     * Check if the message has already been at the given node (circulating message). 
185     * @return How often the message has travelled the node already
186     */
187    bool dirtyRead(org::xmlBlaster::util::cluster::NodeId nodeId) const;
188 
189    /**
190     * The approximate receive timestamp (UTC time),
191     * when message arrived in requestBroker.publish() method.<br />
192     * In milliseconds elapsed since midnight, January 1, 1970 UTC
193     */
194    void setRcvTimestamp(Timestamp rcvTimestamp);
195 
196    /** 
197     * The approximate receive timestamp (UTC time),
198     * when message arrived in requestBroker.publish() method.<br />
199     * In milliseconds elapsed since midnight, January 1, 1970 UTC
200     */
201    Timestamp getRcvTimestamp() const;
202 
203    /**
204     * Set timestamp to current time.
205     */
206    void touchRcvTimestamp();
207 
208    /**
209     * Add a client property. 
210     * @param clientProperty
211     * @see ClientProperty
212     */
213    void addClientProperty(const ClientProperty& clientProperty);
214 
215    /**
216     * Add a client property key and value. 
217     * If you use other character sets than US-ASCII you should set the type
218     * to Constants::TYPE_BLOB (which will force a Base64 encoding). 
219     * @param name  The key name in US-ASCII encoding
220     * @param value "vector<unsigned char>" and "unsigned char *" is treated as a blob
221     * @param type  Optionally you can force another type than "String",
222     *              for example Constants::TYPE_DOUBLE if the pointer contains
223     *              such a number as a string representation. 
224     * @param encoding How the data is transferred, org::xmlBlaster::util::Constants::ENCODING_BASE64 or ""
225     * @see ClientProperty::#ClientProperty
226     */
227    template <typename T_VALUE> void addClientProperty(
228             const std::string& name,
229             const T_VALUE& value,
230             const std::string& type="",
231             const std::string& encoding="",
232             const std::string& charset="");
233 
234    /**
235     * Access the value for the given name, if not found returns the defaultValue. 
236     * @return A copy of the given defaultValue if none was found
237     */
238    template <typename T_VALUE> T_VALUE getClientProperty(
239             const std::string& name,
240             const T_VALUE& defaultValue);
241         
242    bool hasClientProperty(const std::string& name) const;
243    const ClientPropertyMap& getClientProperties() const;
244    void setClientProperties(const ClientPropertyMap& cm);
245 
246    /**
247     * Access sender unified naming object.
248     * @return sessionName of sender or null if not known
249     */
250    org::xmlBlaster::util::SessionNameRef getSender() const;
251 
252    /**
253     * Access sender name.
254     * @param loginName of sender
255     */
256    void setSender(org::xmlBlaster::util::SessionNameRef sender) const;
257 
258    /**
259     * Dump state of this object into a XML ASCII std::string.
260     * <br>
261     * Needs to be implemented by derived classes.
262     * @param extraOffset indenting of tags for nice output
263     * @return internal state of the message QoS as a XML ASCII std::string
264     */
265    virtual std::string toXml(const std::string& extraOffset="") const;
266 
267    /**
268     * Allocate a clone, the derived classes need to implement this method. 
269     * @return The caller needs to free it with 'delete'.
270     */
271    virtual QosData* getClone() const;
272 
273     // the following where not present before ...
274    RouteVector getRouteNodes() const;
275 
276    void clearRoutes();
277 
278    int size() const;
279 
280    /**
281     * Message priority.
282     * @return priority 0-9
283     * @see org.xmlBlaster.util.def.PriorityEnum
284     */
285    org::xmlBlaster::util::PriorityEnum getPriority() const;
286 
287    /**
288     * Set message priority value, org::xmlBlaster::util::PriorityEnum.NORM_PRIORITY (5) is default. 
289     * org::xmlBlaster::util::PriorityEnum.MIN_PRIORITY (0) is slowest
290     * whereas org::xmlBlaster::util::PriorityEnum.MAX_PRIORITY (9) is highest priority.
291     * @see org.xmlBlaster.util.def.PriorityEnum
292     */
293    void setPriority(org::xmlBlaster::util::PriorityEnum priority);
294 
295    /**
296     * Internal use only, is this message sent from the persistence layer?
297     * @return true/false
298     */
299    bool isFromPersistenceStore() const;
300 
301    /**
302     * Internal use only, set if this message sent from the persistence layer
303     * @param true/false
304     */
305    void setFromPersistenceStore(bool fromPersistenceStore);
306 
307    /**
308     * @param persistent mark a message as persistent
309     */
310    void setPersistent(bool persistent);
311 
312    /**
313     * @return true/false
314     */
315    bool isPersistent() const;
316 };
317 
318 typedef org::xmlBlaster::util::ReferenceHolder<org::xmlBlaster::util::qos::QosData> QosDataRef;
319 
320 template <typename T_VALUE> void QosData::addClientProperty(
321              const std::string& name, const T_VALUE& value,
322              const std::string& type, const std::string& encoding,
323              const std::string& charset)
324 {
325    org::xmlBlaster::util::qos::ClientProperty clientProperty(name, value, type, encoding, charset);
326    clientProperties_.insert(ClientPropertyMap::value_type(name, clientProperty));   
327 }
328 
329 template <typename T_VALUE> T_VALUE QosData::getClientProperty(
330              const std::string& name, const T_VALUE& defaultValue)
331 {
332    ClientPropertyMap::const_iterator iter = clientProperties_.find(name);
333    if (iter != clientProperties_.end()) {
334       T_VALUE tmp;
335       (*iter).second.getValue(tmp);
336       return tmp;
337    }
338    return defaultValue;
339 }
340         
341 }}}}
342 
343 #endif


syntax highlighted by Code2HTML, v. 0.9.1