Home | History | Annotate | Download | only in xmpp
      1 /*
      2  * libjingle
      3  * Copyright 2011, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_XMPP_PUBSUBCLIENT_H_
     29 #define TALK_XMPP_PUBSUBCLIENT_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 
     34 #include "talk/xmpp/jid.h"
     35 #include "talk/xmpp/pubsubtasks.h"
     36 #include "webrtc/base/sigslot.h"
     37 #include "webrtc/base/sigslotrepeater.h"
     38 #include "webrtc/base/task.h"
     39 
     40 // Easy to use clients built on top of the tasks for XEP-0060
     41 // (http://xmpp.org/extensions/xep-0060.html).
     42 
     43 namespace buzz {
     44 
     45 class Jid;
     46 class XmlElement;
     47 class XmppTaskParentInterface;
     48 
     49 // An easy-to-use pubsub client that handles the three tasks of
     50 // getting, publishing, and listening for updates.  Tied to a specific
     51 // pubsub jid and node.  All you have to do is RequestItems, listen
     52 // for SignalItems and PublishItems.
     53 class PubSubClient : public sigslot::has_slots<> {
     54  public:
     55   PubSubClient(XmppTaskParentInterface* parent,
     56                const Jid& pubsubjid,
     57                const std::string& node)
     58     : parent_(parent),
     59       pubsubjid_(pubsubjid),
     60       node_(node) {}
     61 
     62   const std::string& node() const { return node_; }
     63 
     64   // Requests the <pubsub><items>, which will be returned via
     65   // SignalItems, or SignalRequestError if there is a failure.  Should
     66   // auto-subscribe.
     67   void RequestItems();
     68   // Fired when either <pubsub><items> are returned or when
     69   // <event><items> are received.
     70   sigslot::signal2<PubSubClient*,
     71                    const std::vector<PubSubItem>&> SignalItems;
     72   // Signal (this, error stanza)
     73   sigslot::signal2<PubSubClient*,
     74                    const XmlElement*> SignalRequestError;
     75   // Signal (this, task_id, item, error stanza)
     76   sigslot::signal4<PubSubClient*,
     77                    const std::string&,
     78                    const XmlElement*,
     79                    const XmlElement*> SignalPublishError;
     80   // Signal (this, task_id, item)
     81   sigslot::signal3<PubSubClient*,
     82                    const std::string&,
     83                    const XmlElement*> SignalPublishResult;
     84   // Signal (this, task_id, error stanza)
     85   sigslot::signal3<PubSubClient*,
     86                    const std::string&,
     87                    const XmlElement*> SignalRetractError;
     88   // Signal (this, task_id)
     89   sigslot::signal2<PubSubClient*,
     90                    const std::string&> SignalRetractResult;
     91 
     92   // Publish an item.  Takes ownership of payload.
     93   void PublishItem(const std::string& itemid,
     94                    XmlElement* payload,
     95                    std::string* task_id_out);
     96   // Publish an item.  Takes ownership of children.
     97   void PublishItem(const std::string& itemid,
     98                    const std::vector<XmlElement*>& children,
     99                    std::string* task_id_out);
    100   // Retract (delete) an item.
    101   void RetractItem(const std::string& itemid,
    102                    std::string* task_id_out);
    103 
    104   // Get the publisher nick if it exists from the pubsub item.
    105   const std::string GetPublisherNickFromPubSubItem(const XmlElement* item_elem);
    106 
    107  private:
    108   void OnRequestError(IqTask* task,
    109                       const XmlElement* stanza);
    110   void OnRequestResult(PubSubRequestTask* task,
    111                        const std::vector<PubSubItem>& items);
    112   void OnReceiveUpdate(PubSubReceiveTask* task,
    113                        const std::vector<PubSubItem>& items);
    114   void OnPublishResult(PubSubPublishTask* task);
    115   void OnPublishError(IqTask* task,
    116                       const XmlElement* stanza);
    117   void OnRetractResult(PubSubRetractTask* task);
    118   void OnRetractError(IqTask* task,
    119                       const XmlElement* stanza);
    120 
    121   XmppTaskParentInterface* parent_;
    122   Jid pubsubjid_;
    123   std::string node_;
    124 };
    125 
    126 }  // namespace buzz
    127 
    128 #endif  // TALK_XMPP_PUBSUBCLIENT_H_
    129