1 /*------------------------------------------------------------------------------
  2 Name:      SubscribeQos.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 
  7 /**
  8  * This class encapsulates the QoS of an subscribe() request.
  9  * <p />
 10  * see xmlBlaster/src/dtd/XmlQoS.xml
 11  * @see org.xmlBlaster.util.qos.QueryQosData
 12  * @see org.xmlBlaster.util.qos.QueryQosSaxFactory
 13  * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/interface.subscribe.html">subscribe interface</a>
 14  */
 15 
 16 #include <client/qos/SubscribeQos.h>
 17 #include <util/Global.h>
 18 
 19 using namespace std;
 20 using namespace org::xmlBlaster::util;
 21 using namespace org::xmlBlaster::util::qos;
 22 
 23 namespace org { namespace xmlBlaster { namespace client { namespace qos {
 24 
 25 SubscribeQos::SubscribeQos(Global& global) : GetQos(global)
 26 {
 27    ME = "SubscribeQos";
 28 }
 29 
 30 SubscribeQos::SubscribeQos(Global& global, const QueryQosData& data)
 31    : GetQos(global, data)
 32 {
 33    ME = "SubscribeQos";
 34 }
 35 
 36 SubscribeQos::SubscribeQos(const SubscribeQos& qos) : GetQos(qos)
 37 {
 38 }
 39 
 40 SubscribeQos& SubscribeQos::operator =(const SubscribeQos& qos)
 41 {
 42    data_ = qos.data_;
 43    return *this;
 44 }
 45 
 46 
 47 /**
 48  * Do we want to have an initial update on subscribe if the message
 49  * exists already?
 50  *
 51  * @return true if initial update wanted
 52  *         false if only updates on new publishes are sent
 53  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.qos.subscribe.initialUpdate.html">The engine.qos.subscribe.initialUpdate requirement</a>
 54  */
 55 void SubscribeQos::setWantInitialUpdate(bool initialUpdate)
 56 {
 57    data_.setWantInitialUpdate(initialUpdate);
 58 }
 59 
 60 /**
 61  * Do we want the callback messages of this subscription as oneway with <tt>updateOneway()</tt> or with
 62  * the acknowledged <tt>update()</tt>.
 63  * @param updateOneway Defaults to false.
 64  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.subscribe.html">The interface.subscribe requirement</a>
 65  * @see QueryQosData#setWantUpdateOneway(boolean)
 66  */
 67 void SubscribeQos::setWantUpdateOneway(bool updateOneway)
 68 {
 69    data_.setWantUpdateOneway(updateOneway);
 70 }
 71 
 72 /**
 73  * Are multiple subscribes allowed?
 74  * Defaults to true.
 75  * @return true Multiple subscribes deliver multiple updates
 76  *         false Ignore more than one subscribes on same oid
 77  */
 78 void SubscribeQos::setMultiSubscribe(bool multiSubscribe)
 79 {
 80    data_.setMultiSubscribe(multiSubscribe);
 81 }
 82 
 83 /**
 84  * false Inhibit the delivery of messages to myself if i have published it.
 85  */
 86 void SubscribeQos::setWantLocal(bool local)
 87 {
 88    data_.setWantLocal(local);
 89 }
 90 
 91 void SubscribeQos::setWantNotify(bool notifyOnErase)
 92 {
 93    data_.setWantNotify(notifyOnErase);
 94 }
 95 
 96 /**
 97  * Force the identifier (unique handle) for this subscription.
 98  * Usually you let the identifier be generated by xmlBlaster.
 99  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.qos.subscribe.id.html">The engine.qos.subscribe.id requirement</a>
100  */
101 void SubscribeQos::setSubscriptionId(const string& subscriptionId) const
102 {
103    data_.setSubscriptionId(subscriptionId);
104 }
105 
106 bool SubscribeQos::hasSubscriptionId() const {
107   return data_.getSubscriptionId().size() > 0;
108 }
109 
110 bool SubscribeQos::getMultiSubscribe() const {
111   return data_.getMultiSubscribe();
112 }
113 
114 std::string SubscribeQos::generateSubscriptionId(org::xmlBlaster::util::SessionNameRef sessionName, const org::xmlBlaster::client::key::SubscribeKey& subscribeKey)
115 {
116       if (sessionName->getPubSessionId() > 0 || !getMultiSubscribe()) {
117          // This key is assured to be the same on client restart
118          // a previous subscription in the server will have the same subscriptionId
119          // Benefit: If on client restart we are queueing the returned faked subscriptionId will
120          // match the later used one of the xmlBlaster server. We can easily use the subscriptionId
121          // as a key in client code hashtable to dispatch update() messages
122          // Note: multiSubscribe==false allows max one subscription on a topic, even it has
123          // different mime query plugins (the latest wins)
124          std::string url = subscribeKey.getUrl();
125          url = StringTrim::replaceAll(url, "'", "&apos;"); // to have valid xml (<subscribe id='bla'/>
126          setSubscriptionId(Constants::SUBSCRIPTIONID_PREFIX +
127                                sessionName->getRelativeName(true) + "-" +
128                                url);
129       }
130       else {
131          TimestampFactory& factory = TimestampFactory::getInstance();
132          Timestamp timestamp = factory.getTimestamp();
133          setSubscriptionId(Constants::SUBSCRIPTIONID_PREFIX +
134                                /* immutableId is also relativeName */
135                                /*is global_.getImmutableId() better than sessionName?? */
136                                sessionName->getRelativeName(true) + "-" +
137                                lexical_cast<string>(timestamp));
138       }
139       return data_.getSubscriptionId();
140    }
141 
142 void SubscribeQos::setPersistent(bool persistent) {
143    data_.setPersistent(persistent);
144 }
145 
146 }}}} // namespace


syntax highlighted by Code2HTML, v. 0.9.1