1 /*------------------------------------------------------------------------------
  2 Name:      MsgQueueEntry.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 
  7 #ifndef _UTIL_QUEUE_MSGQUEUEENRY_H
  8 #define _UTIL_QUEUE_MSGQUEUEENRY_H
  9 
 10 #include <util/xmlBlasterDef.h>
 11 #include <util/Timestamp.h>
 12 #include <util/MessageUnit.h>
 13 #include <util/MethodName.h>
 14 #include <util/qos/ConnectQos.h>
 15 #include <client/qos/PublishQos.h>
 16 #include <client/qos/PublishReturnQos.h>
 17 #include <util/ReferenceCounterBase.h>
 18 #include <util/qos/StatusQosData.h>
 19 #include <util/qos/QueryQosData.h>
 20 #include <util/key/QueryKeyData.h>
 21 //#include <util/msgUtil.h> // from xmlBlaster C library
 22 //#include <socket/xmlBlasterSocket.h> // from xmlBlaster C library ::encodeMsgUnit(&msgUnit, debug);
 23 
 24 // circular dependency I_ConnectionsHandler -> Queue -> MsgQueueEntry
 25 #ifndef _UTIL_DISPATCH_ICONNECTIONSHANDLER_H
 26 namespace org { namespace xmlBlaster { namespace util { namespace dispatch {
 27 class I_ConnectionsHandler;
 28 }}}}
 29 #endif
 30 
 31 
 32 
 33 /**
 34  * Class embedding messages or information to be stored on the client queues
 35  * Note that all content is copied when passed to the constructors.
 36  * This way this queue entry is the owner of the content (and therefore will
 37  * delete it when its destructor is called).
 38  *
 39  * @author <a href='mailto:laghi@swissinfo.org'>Michele Laghi</a>
 40  */
 41 namespace org { namespace xmlBlaster { namespace util { namespace queue {
 42 
 43 /**
 44  * Holds arbitrary raw data and its length
 45  */
 46 typedef struct {
 47    size_t dataLen; // size_t is the unsigned integer size result of a sizeof operator. Change to uint32_t ?
 48    char *data;
 49 } BlobHolder;
 50 
 51 class Dll_Export MsgQueueEntry : public ReferenceCounterBase
 52 {
 53 protected:
 54    std::string ME;
 55    org::xmlBlaster::util::Global& global_;
 56    //mutable org::xmlBlaster::util::Global& global_;
 57    org::xmlBlaster::util::I_Log& log_;
 58    int priority_;
 59    bool persistent_;
 60    org::xmlBlaster::util::Timestamp uniqueId_;
 61    std::string embeddedType_;
 62    std::string logId_;
 63    org::xmlBlaster::util::MessageUnit* msgUnit_;
 64    /* TODO: Change that connectQos, queryQos all derive from QosData and are transported inside msgUnit */
 65    org::xmlBlaster::util::qos::ConnectQosRef connectQos_;
 66 
 67    /**
 68     * Specific return value for connect(). 
 69     */
 70    mutable org::xmlBlaster::util::qos::ConnectReturnQosRef connectReturnQos_;
 71    /**
 72     * Specific return value for publish(). 
 73     */
 74    mutable const org::xmlBlaster::client::qos::PublishReturnQos* publishReturnQos_;
 75    /**
 76     * Return status for subscribe() etc. 
 77     */
 78    mutable org::xmlBlaster::util::qos::StatusQosData* statusQosData_;
 79 
 80    /**
 81     * Holds the serialized information which is returned by getEmbeddedObject(),
 82     * encoded according to embeddedType
 83     */
 84    mutable BlobHolder blobHolder_;
 85    
 86 public:
 87 
 88     /**
 89      * Constructor suited for operations like publishes
 90      * @param msgUnit We take a clone of it
 91      * @param embeddedType Describes the type of serialization of the embedded object to be able
 92      *        to restore it later, something like "MSG_RAW|publish"
 93      */
 94     MsgQueueEntry(org::xmlBlaster::util::Global& global,
 95                   const org::xmlBlaster::util::MessageUnit& msgUnit,
 96                   const std::string& embeddedType,
 97                   int priority,
 98                   bool persistent,
 99                   org::xmlBlaster::util::Timestamp uniqueId = TimestampFactory::getInstance().getTimestamp());
100 
101     /**
102      * Constructor suited for operations like connect
103      * @param connectQos We take a clone of it
104      */
105     MsgQueueEntry(org::xmlBlaster::util::Global& global,
106                   const org::xmlBlaster::util::qos::ConnectQosRef& connectQos,
107                   const std::string& embeddedType,
108                   int priority,
109                   bool persistent,
110                   org::xmlBlaster::util::Timestamp uniqueId = TimestampFactory::getInstance().getTimestamp());
111 
112 
113     /**
114      * Constructor suited for operations like subscribe and unSubscribe
115      * @param queryKeyData We take a clone of it
116      * @param queryQosData We take a clone of it
117      */
118     MsgQueueEntry(org::xmlBlaster::util::Global& global,
119                   const org::xmlBlaster::util::key::QueryKeyData& queryKeyData,
120                   const org::xmlBlaster::util::qos::QueryQosData& queryQosData,
121                   const std::string& embeddedType,
122                   int priority,
123                   bool persistent,
124                   org::xmlBlaster::util::Timestamp uniqueId = TimestampFactory::getInstance().getTimestamp());
125 
126 
127     virtual ~MsgQueueEntry();
128 
129     org::xmlBlaster::util::Global& getGlobal() const { return global_; }
130 
131     void copy(const MsgQueueEntry& entry);
132 
133     /**
134      * copy constructor
135      */
136     MsgQueueEntry(const MsgQueueEntry& entry);
137 
138     MsgQueueEntry& operator =(const MsgQueueEntry& entry);
139 
140     inline bool operator == (const MsgQueueEntry& entry)
141     {
142        if (priority_ != entry.priority_) return false;
143        return (uniqueId_ == entry.uniqueId_);
144     }
145 
146     /**
147      * returns true if the current object is lower than the entry passed as
148      * an argument.
149      */
150     inline bool operator < (const MsgQueueEntry& entry)
151     {
152        if (priority_ < entry.priority_) return true;
153        if (priority_ > entry.priority_) return false;
154        return (uniqueId_ > entry.uniqueId_);
155     }
156 
157     /**
158      * Create a new entry of myself. 
159      * @return The cloned entry, is is allocated with new and it is your responsibility to delete it
160      */
161     virtual MsgQueueEntry *getClone() const = 0;
162 
163    /**
164     * Allows to query the priority of this entry.
165     * This is the highest order precedence in the sorted queue
166     * @return The priority
167     */
168    int getPriority() const;
169 
170    /**
171     * Returns true if the entry is persistent (persistent), false otherwise.
172     */
173    bool isPersistent() const;
174 
175    /**
176     * Set the sender name into the QoS. 
177     */
178    void setSender(org::xmlBlaster::util::SessionNameRef sender);
179 
180    /**
181     * This is the second order criteria in the queue
182     * @return The unique Id of this entry.
183     */
184    org::xmlBlaster::util::Timestamp getUniqueId() const;
185 
186    /**
187     * The serialized data with MSG_RAW (identical to the SOCKET protocol serialization). 
188     * @return The content of this queue entry (the embedded object). In
189     * persistent queues this is the data which is stored as a blob.
190     * Returns a BlobHolder instance containing the serialized message (identical serialization as in SOCKET protocol)
191     * May return 0.
192     */
193    virtual const void* getEmbeddedObject() const;
194 
195    /**
196     * gets the embeddedType of the object embedded in this entry.
197     * @return String in namespace MethodName, the identifier which tells the I_EntryFactory how to
198     *         deserialize this entry, e.g. "MSG_RAW|publish"
199     */
200    virtual std::string getEmbeddedType() const;
201    
202    /**
203     * @return for example org::xmlBlaster::util::MethodName::PUBLISH
204     */
205    virtual std::string getMethodName() const;
206    
207    virtual bool isConnect() const;
208 
209    virtual bool isPublish() const;
210 
211    virtual bool isSubscribe() const;
212 
213    virtual bool isUnSubscribe() const;
214 
215    virtual bool isErase() const;
216 
217 
218    /**
219     * Return a human readable identifier for logging output.
220     * <p>
221     * See the derived class for a syntax description.
222     * </p>
223     */
224    std::string getLogId() const;
225 
226    /**
227     * returns the size in bytes of this entry.
228     */
229    virtual size_t getSizeInBytes() const;
230 
231    /**
232     * Access the MessageUnit in case it is a Publish.
233     * @return NULL if not publish
234     */
235    org::xmlBlaster::util::MessageUnit& getMsgUnit() const;
236 
237    //org::xmlBlaster::util::qos::QueryQosData& getQueryQosData() const;
238    //org::xmlBlaster::util::key::QueryKeyData& getQueryKeyData() const;
239 
240 
241 
242    // this should actually be in another interface but since it is an only method we put it here.
243    virtual const MsgQueueEntry& send(org::xmlBlaster::util::dispatch::I_ConnectionsHandler&) const; // = 0;
244    
245    virtual std::string toXml(const std::string& indent="") const; // = 0;
246 
247 };
248 
249 }}}} // namespace
250 
251 #endif


syntax highlighted by Code2HTML, v. 0.9.1