You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/xmpp/pubsubclient.h"
  28. #include <string>
  29. #include <vector>
  30. #include "talk/xmpp/constants.h"
  31. #include "talk/xmpp/jid.h"
  32. #include "talk/xmpp/pubsubtasks.h"
  33. namespace buzz {
  34. void PubSubClient::RequestItems() {
  35. PubSubRequestTask* request_task =
  36. new PubSubRequestTask(parent_, pubsubjid_, node_);
  37. request_task->SignalResult.connect(this, &PubSubClient::OnRequestResult);
  38. request_task->SignalError.connect(this, &PubSubClient::OnRequestError);
  39. PubSubReceiveTask* receive_task =
  40. new PubSubReceiveTask(parent_, pubsubjid_, node_);
  41. receive_task->SignalUpdate.connect(this, &PubSubClient::OnReceiveUpdate);
  42. receive_task->Start();
  43. request_task->Start();
  44. }
  45. void PubSubClient::PublishItem(
  46. const std::string& itemid, XmlElement* payload, std::string* task_id_out) {
  47. std::vector<XmlElement*> children;
  48. children.push_back(payload);
  49. PublishItem(itemid, children, task_id_out);
  50. }
  51. void PubSubClient::PublishItem(
  52. const std::string& itemid, const std::vector<XmlElement*>& children,
  53. std::string* task_id_out) {
  54. PubSubPublishTask* publish_task =
  55. new PubSubPublishTask(parent_, pubsubjid_, node_, itemid, children);
  56. publish_task->SignalError.connect(this, &PubSubClient::OnPublishError);
  57. publish_task->SignalResult.connect(this, &PubSubClient::OnPublishResult);
  58. publish_task->Start();
  59. if (task_id_out) {
  60. *task_id_out = publish_task->task_id();
  61. }
  62. }
  63. void PubSubClient::RetractItem(
  64. const std::string& itemid, std::string* task_id_out) {
  65. PubSubRetractTask* retract_task =
  66. new PubSubRetractTask(parent_, pubsubjid_, node_, itemid);
  67. retract_task->SignalError.connect(this, &PubSubClient::OnRetractError);
  68. retract_task->SignalResult.connect(this, &PubSubClient::OnRetractResult);
  69. retract_task->Start();
  70. if (task_id_out) {
  71. *task_id_out = retract_task->task_id();
  72. }
  73. }
  74. void PubSubClient::OnRequestResult(PubSubRequestTask* task,
  75. const std::vector<PubSubItem>& items) {
  76. SignalItems(this, items);
  77. }
  78. void PubSubClient::OnRequestError(IqTask* task,
  79. const XmlElement* stanza) {
  80. SignalRequestError(this, stanza);
  81. }
  82. void PubSubClient::OnReceiveUpdate(PubSubReceiveTask* task,
  83. const std::vector<PubSubItem>& items) {
  84. SignalItems(this, items);
  85. }
  86. const XmlElement* GetItemFromStanza(const XmlElement* stanza) {
  87. if (stanza != NULL) {
  88. const XmlElement* pubsub = stanza->FirstNamed(QN_PUBSUB);
  89. if (pubsub != NULL) {
  90. const XmlElement* publish = pubsub->FirstNamed(QN_PUBSUB_PUBLISH);
  91. if (publish != NULL) {
  92. return publish->FirstNamed(QN_PUBSUB_ITEM);
  93. }
  94. }
  95. }
  96. return NULL;
  97. }
  98. void PubSubClient::OnPublishResult(PubSubPublishTask* task) {
  99. const XmlElement* item = GetItemFromStanza(task->stanza());
  100. SignalPublishResult(this, task->task_id(), item);
  101. }
  102. void PubSubClient::OnPublishError(IqTask* task,
  103. const XmlElement* error_stanza) {
  104. PubSubPublishTask* publish_task =
  105. static_cast<PubSubPublishTask*>(task);
  106. const XmlElement* item = GetItemFromStanza(publish_task->stanza());
  107. SignalPublishError(this, publish_task->task_id(), item, error_stanza);
  108. }
  109. void PubSubClient::OnRetractResult(PubSubRetractTask* task) {
  110. SignalRetractResult(this, task->task_id());
  111. }
  112. void PubSubClient::OnRetractError(IqTask* task,
  113. const XmlElement* stanza) {
  114. PubSubRetractTask* retract_task =
  115. static_cast<PubSubRetractTask*>(task);
  116. SignalRetractError(this, retract_task->task_id(), stanza);
  117. }
  118. } // namespace buzz