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.

736 lines
25 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2006, 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 <iostream>
  28. #include "libjingleplus.h"
  29. #ifdef WIN32
  30. #include "talk/base/win32socketserver.h"
  31. #endif
  32. #include "talk/base/physicalsocketserver.h"
  33. #include "talk/base/logging.h"
  34. #include "talk/examples/login/xmppauth.h"
  35. #include "talk/examples/login/xmppsocket.h"
  36. #include "talk/examples/login/xmpppump.h"
  37. #include "presencepushtask.h"
  38. #include "talk/app/status.h"
  39. #include "talk/app/message.h"
  40. #include "rostertask.h"
  41. #include "talk/app/iqtask.h"
  42. #include "talk/app/presenceouttask.h"
  43. #include "talk/app/receivemessagetask.h"
  44. #include "talk/app/rostersettask.h"
  45. #include "talk/app/sendmessagetask.h"
  46. enum {
  47. MSG_START,
  48. // main thread to worker
  49. MSG_LOGIN,
  50. MSG_DISCONNECT,
  51. MSG_SEND_PRESENCE,
  52. MSG_SEND_DIRECTED_PRESENCE,
  53. MSG_SEND_DIRECTED_MUC_PRESENCE,
  54. MSG_SEND_XMPP_MESSAGE,
  55. MSG_SEND_XMPP_IQ,
  56. MSG_UPDATE_ROSTER_ITEM,
  57. MSG_REMOVE_ROSTER_ITEM,
  58. // worker thread to main thread
  59. MSG_STATE_CHANGE,
  60. MSG_STATUS_UPDATE,
  61. MSG_STATUS_ERROR,
  62. MSG_ROSTER_REFRESH_STARTED,
  63. MSG_ROSTER_REFRESH_FINISHED,
  64. MSG_ROSTER_ITEM_UPDATED,
  65. MSG_ROSTER_ITEM_REMOVED,
  66. MSG_ROSTER_SUBSCRIBE,
  67. MSG_ROSTER_UNSUBSCRIBE,
  68. MSG_ROSTER_SUBSCRIBED,
  69. MSG_ROSTER_UNSUBSCRIBED,
  70. MSG_INCOMING_MESSAGE,
  71. MSG_IQ_COMPLETE,
  72. MSG_XMPP_INPUT,
  73. MSG_XMPP_OUTPUT
  74. };
  75. class LibjinglePlusWorker : public talk_base::MessageHandler,
  76. public XmppPumpNotify,
  77. public sigslot::has_slots<> {
  78. public:
  79. LibjinglePlusWorker(LibjinglePlus *ljp, LibjinglePlusNotify *notify) :
  80. worker_thread_(NULL), ljp_(ljp), notify_(notify),
  81. ppt_(NULL), rmt_(NULL), rt_(NULL), is_test_login_(false) {
  82. main_thread_.reset(new talk_base::AutoThread());
  83. #ifdef WIN32
  84. ss_.reset(new talk_base::Win32SocketServer(main_thread_.get()));
  85. main_thread_->set_socketserver(ss_.get());
  86. #endif
  87. pump_.reset(new XmppPump(this));
  88. pump_->client()->SignalLogInput.connect(this, &LibjinglePlusWorker::OnInputDebug);
  89. pump_->client()->SignalLogOutput.connect(this, &LibjinglePlusWorker::OnOutputDebug);
  90. //pump_->client()->SignalStateChange.connect(this, &LibjinglePlusWorker::OnStateChange);
  91. }
  92. ~LibjinglePlusWorker() {
  93. if (worker_thread_) {
  94. worker_thread_->Send(this, MSG_DISCONNECT);
  95. delete worker_thread_;
  96. }
  97. }
  98. virtual void OnMessage(talk_base::Message *msg) {
  99. switch (msg->message_id) {
  100. case MSG_START:
  101. LoginW();
  102. break;
  103. case MSG_DISCONNECT:
  104. DisconnectW();
  105. break;
  106. case MSG_SEND_XMPP_MESSAGE:
  107. SendXmppMessageW(static_cast<SendMessageData*>(msg->pdata)->m_);
  108. delete msg->pdata;
  109. break;
  110. case MSG_SEND_XMPP_IQ:
  111. SendXmppIqW(static_cast<SendIqData*>(msg->pdata)->to_jid_,
  112. static_cast<SendIqData*>(msg->pdata)->is_get_,
  113. static_cast<SendIqData*>(msg->pdata)->xml_element_);
  114. delete msg->pdata;
  115. break;
  116. case MSG_SEND_PRESENCE:
  117. SendPresenceW(static_cast<SendPresenceData*>(msg->pdata)->s_);
  118. delete msg->pdata;
  119. break;
  120. case MSG_SEND_DIRECTED_PRESENCE:
  121. SendDirectedPresenceW(static_cast<SendDirectedPresenceData*>(msg->pdata)->j_,
  122. static_cast<SendDirectedPresenceData*>(msg->pdata)->s_);
  123. delete msg->pdata;
  124. break;
  125. case MSG_SEND_DIRECTED_MUC_PRESENCE:
  126. SendDirectedMUCPresenceW(static_cast<SendDirectedMUCPresenceData*>(msg->pdata)->j_,
  127. static_cast<SendDirectedMUCPresenceData*>(msg->pdata)->s_,
  128. static_cast<SendDirectedMUCPresenceData*>(msg->pdata)->un_,
  129. static_cast<SendDirectedMUCPresenceData*>(msg->pdata)->ac_,
  130. static_cast<SendDirectedMUCPresenceData*>(msg->pdata)->am_,
  131. static_cast<SendDirectedMUCPresenceData*>(msg->pdata)->role_);
  132. delete msg->pdata;
  133. break;
  134. case MSG_UPDATE_ROSTER_ITEM:
  135. UpdateRosterItemW(static_cast<UpdateRosterItemData*>(msg->pdata)->jid_,
  136. static_cast<UpdateRosterItemData*>(msg->pdata)->n_,
  137. static_cast<UpdateRosterItemData*>(msg->pdata)->g_,
  138. static_cast<UpdateRosterItemData*>(msg->pdata)->grt_);
  139. delete msg->pdata;
  140. break;
  141. case MSG_REMOVE_ROSTER_ITEM:
  142. RemoveRosterItemW(static_cast<JidData*>(msg->pdata)->jid_);
  143. delete msg->pdata;
  144. break;
  145. case MSG_STATUS_UPDATE:
  146. OnStatusUpdateW(static_cast<SendPresenceData*>(msg->pdata)->s_);
  147. delete msg->pdata;
  148. break;
  149. case MSG_STATUS_ERROR:
  150. OnStatusErrorW(static_cast<StatusErrorData*>(msg->pdata)->stanza_);
  151. delete msg->pdata;
  152. break;
  153. case MSG_STATE_CHANGE:
  154. OnStateChangeW(static_cast<StateChangeData*>(msg->pdata)->s_);
  155. delete msg->pdata;
  156. break;
  157. case MSG_ROSTER_REFRESH_STARTED:
  158. OnRosterRefreshStartedW();
  159. break;
  160. case MSG_ROSTER_REFRESH_FINISHED:
  161. OnRosterRefreshFinishedW();
  162. break;
  163. case MSG_ROSTER_ITEM_UPDATED:
  164. OnRosterItemUpdatedW(static_cast<RosterItemData*>(msg->pdata)->ri_);
  165. delete msg->pdata;
  166. break;
  167. case MSG_ROSTER_ITEM_REMOVED:
  168. OnRosterItemRemovedW(static_cast<RosterItemData*>(msg->pdata)->ri_);
  169. delete msg->pdata;
  170. break;
  171. case MSG_ROSTER_SUBSCRIBE:
  172. OnRosterSubscribeW(static_cast<JidData*>(msg->pdata)->jid_);
  173. delete msg->pdata;
  174. break;
  175. case MSG_ROSTER_UNSUBSCRIBE:
  176. OnRosterUnsubscribeW(static_cast<JidData*>(msg->pdata)->jid_);
  177. delete msg->pdata;
  178. break;
  179. case MSG_ROSTER_SUBSCRIBED:
  180. OnRosterSubscribedW(static_cast<JidData*>(msg->pdata)->jid_);
  181. delete msg->pdata;
  182. break;
  183. case MSG_ROSTER_UNSUBSCRIBED:
  184. OnRosterUnsubscribedW(static_cast<JidData*>(msg->pdata)->jid_);
  185. delete msg->pdata;
  186. break;
  187. case MSG_INCOMING_MESSAGE:
  188. OnIncomingMessageW(static_cast<XmppMessageData*>(msg->pdata)->m_);
  189. delete msg->pdata;
  190. break;
  191. case MSG_IQ_COMPLETE:
  192. OnIqCompleteW(static_cast<IqCompleteData*>(msg->pdata)->success_,
  193. static_cast<IqCompleteData*>(msg->pdata)->stanza_);
  194. delete msg->pdata;
  195. break;
  196. case MSG_XMPP_OUTPUT:
  197. OnOutputDebugW(static_cast<StringData*>(msg->pdata)->s_);
  198. delete msg->pdata;
  199. break;
  200. case MSG_XMPP_INPUT:
  201. OnInputDebugW(static_cast<StringData*>(msg->pdata)->s_);
  202. delete msg->pdata;
  203. break;
  204. }
  205. }
  206. void Login(const std::string &jid, const std::string &password,
  207. const std::string &machine_address, bool is_test, bool cookie_auth) {
  208. is_test_login_ = is_test;
  209. xcs_.set_user(jid);
  210. if (cookie_auth) {
  211. xcs_.set_auth_cookie(password);
  212. } else {
  213. talk_base::InsecureCryptStringImpl pass;
  214. pass.password() = password;
  215. xcs_.set_pass(talk_base::CryptString(pass));
  216. }
  217. xcs_.set_host(is_test ? "google.com" : "gmail.com");
  218. xcs_.set_resource("libjingleplus");
  219. xcs_.set_server(talk_base::SocketAddress(machine_address, 5222));
  220. xcs_.set_use_tls(!is_test);
  221. if (is_test) {
  222. xcs_.set_allow_plain(true);
  223. }
  224. worker_thread_ = new talk_base::Thread(&pss_);
  225. worker_thread_->Start();
  226. worker_thread_->Send(this, MSG_START);
  227. }
  228. void SendXmppMessage(const buzz::XmppMessage &m) {
  229. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  230. worker_thread_->Post(this, MSG_SEND_XMPP_MESSAGE, new SendMessageData(m));
  231. }
  232. void SendXmppIq(const buzz::Jid &to_jid, bool is_get,
  233. const buzz::XmlElement *xml_element) {
  234. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  235. worker_thread_->Post(this, MSG_SEND_XMPP_IQ,
  236. new SendIqData(to_jid, is_get, xml_element));
  237. }
  238. void SendPresence(const buzz::Status & s) {
  239. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  240. worker_thread_->Post(this, MSG_SEND_PRESENCE, new SendPresenceData(s));
  241. }
  242. void SendDirectedPresence (const buzz::Jid &j, const buzz::Status &s) {
  243. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  244. worker_thread_->Post(this, MSG_SEND_DIRECTED_PRESENCE, new SendDirectedPresenceData(j,s));
  245. }
  246. void SendDirectedMUCPresence(const buzz::Jid &j, const buzz::Status &s,
  247. const std::string &un, const std::string &ac,
  248. const std::string &am, const std::string &role) {
  249. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  250. worker_thread_->Post(this, MSG_SEND_DIRECTED_MUC_PRESENCE, new SendDirectedMUCPresenceData(j,s,un,ac,am, role));
  251. }
  252. void UpdateRosterItem(const buzz::Jid & jid, const std::string & name,
  253. const std::vector<std::string> & groups, buzz::GrType grt) {
  254. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  255. worker_thread_->Post(this, MSG_UPDATE_ROSTER_ITEM, new UpdateRosterItemData(jid,name,groups,grt));
  256. }
  257. void RemoveRosterItemW(const buzz::Jid &jid) {
  258. buzz::RosterSetTask *rst = new buzz::RosterSetTask(pump_.get()->client());
  259. rst->Remove(jid);
  260. rst->Start();
  261. }
  262. void RemoveRosterItem(const buzz::Jid &jid) {
  263. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  264. worker_thread_->Post(this, MSG_REMOVE_ROSTER_ITEM, new JidData(jid));
  265. }
  266. void DoCallbacks() {
  267. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  268. talk_base::Message m;
  269. while (main_thread_->Get(&m, 0)) {
  270. main_thread_->Dispatch(&m);
  271. }
  272. }
  273. private:
  274. struct UpdateRosterItemData : public talk_base::MessageData {
  275. UpdateRosterItemData(const buzz::Jid &jid, const std::string &name,
  276. const std::vector<std::string> &groups, buzz::GrType grt) :
  277. jid_(jid), n_(name), g_(groups), grt_(grt) {}
  278. buzz::Jid jid_;
  279. std::string n_;
  280. std::vector<std::string> g_;
  281. buzz::GrType grt_;
  282. };
  283. void UpdateRosterItemW(const buzz::Jid &jid, const std::string &name,
  284. const std::vector<std::string> &groups, buzz::GrType grt) {
  285. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  286. buzz::RosterSetTask *rst = new buzz::RosterSetTask(pump_.get()->client());
  287. rst->Update(jid, name, groups, grt);
  288. rst->Start();
  289. }
  290. struct StringData : public talk_base::MessageData {
  291. StringData(std::string s) : s_(s) {}
  292. std::string s_;
  293. };
  294. void OnInputDebugW(const std::string &data) {
  295. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  296. if (notify_)
  297. notify_->OnXmppInput(data);
  298. }
  299. void OnInputDebug(const char *data, int len) {
  300. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  301. main_thread_->Post(this, MSG_XMPP_INPUT, new StringData(std::string(data,len)));
  302. if (notify_)
  303. notify_->WakeupMainThread();
  304. }
  305. void OnOutputDebugW(const std::string &data) {
  306. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  307. if (notify_)
  308. notify_->OnXmppOutput(data);
  309. }
  310. void OnOutputDebug(const char *data, int len) {
  311. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  312. main_thread_->Post(this, MSG_XMPP_OUTPUT, new StringData(std::string(data,len)));
  313. if (notify_)
  314. notify_->WakeupMainThread();
  315. }
  316. struct StateChangeData : public talk_base::MessageData {
  317. StateChangeData(buzz::XmppEngine::State state) : s_(state) {}
  318. buzz::XmppEngine::State s_;
  319. };
  320. void OnStateChange(buzz::XmppEngine::State state) {
  321. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  322. switch (state) {
  323. case buzz::XmppEngine::STATE_OPEN:
  324. ppt_ = new buzz::PresencePushTask(pump_.get()->client());
  325. ppt_->SignalStatusUpdate.connect(this,
  326. &LibjinglePlusWorker::OnStatusUpdate);
  327. ppt_->SignalStatusError.connect(this,
  328. &LibjinglePlusWorker::OnStatusError);
  329. ppt_->Start();
  330. rmt_ = new buzz::ReceiveMessageTask(pump_.get()->client(), buzz::XmppEngine::HL_ALL);
  331. rmt_->SignalIncomingMessage.connect(this, &LibjinglePlusWorker::OnIncomingMessage);
  332. rmt_->Start();
  333. rt_ = new buzz::RosterTask(pump_.get()->client());
  334. rt_->SignalRosterItemUpdated.connect(this, &LibjinglePlusWorker::OnRosterItemUpdated);
  335. rt_->SignalRosterItemRemoved.connect(this, &LibjinglePlusWorker::OnRosterItemRemoved);
  336. rt_->SignalSubscribe.connect(this, &LibjinglePlusWorker::OnRosterSubscribe);
  337. rt_->SignalUnsubscribe.connect(this, &LibjinglePlusWorker::OnRosterUnsubscribe);
  338. rt_->SignalSubscribed.connect(this, &LibjinglePlusWorker::OnRosterSubscribed);
  339. rt_->SignalUnsubscribed.connect(this, &LibjinglePlusWorker::OnRosterUnsubscribed);
  340. rt_->SignalRosterRefreshStarted.connect(this, &LibjinglePlusWorker::OnRosterRefreshStarted);
  341. rt_->SignalRosterRefreshFinished.connect(this, &LibjinglePlusWorker::OnRosterRefreshFinished);
  342. rt_->Start();
  343. rt_->RefreshRosterNow();
  344. break;
  345. }
  346. main_thread_->Post(this, MSG_STATE_CHANGE, new StateChangeData(state));
  347. if (notify_)
  348. notify_->WakeupMainThread();
  349. }
  350. void OnStateChangeW(buzz::XmppEngine::State state) {
  351. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  352. if (notify_)
  353. notify_->OnStateChange(state);
  354. }
  355. struct RosterItemData : public talk_base::MessageData {
  356. RosterItemData(const buzz::RosterItem &ri) : ri_(ri) {}
  357. buzz::RosterItem ri_;
  358. };
  359. void OnRosterItemUpdatedW(const buzz::RosterItem &ri) {
  360. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  361. if (notify_)
  362. notify_->OnRosterItemUpdated(ri);
  363. }
  364. void OnRosterItemUpdated(const buzz::RosterItem &ri, bool huh) {
  365. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  366. main_thread_->Post(this, MSG_ROSTER_ITEM_UPDATED, new RosterItemData(ri));
  367. if (notify_)
  368. notify_->WakeupMainThread();
  369. }
  370. void OnRosterItemRemovedW(const buzz::RosterItem &ri) {
  371. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  372. if (notify_)
  373. notify_->OnRosterItemRemoved(ri);
  374. }
  375. void OnRosterItemRemoved(const buzz::RosterItem &ri) {
  376. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  377. main_thread_->Post(this, MSG_ROSTER_ITEM_REMOVED, new RosterItemData(ri));
  378. if (notify_)
  379. notify_->WakeupMainThread();
  380. }
  381. struct JidData : public talk_base::MessageData {
  382. JidData(const buzz::Jid& jid) : jid_(jid) {}
  383. const buzz::Jid jid_;
  384. };
  385. void OnRosterSubscribeW(const buzz::Jid& jid) {
  386. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  387. if (notify_)
  388. notify_->OnRosterSubscribe(jid);
  389. }
  390. void OnRosterSubscribe(const buzz::Jid& jid) {
  391. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  392. main_thread_->Post(this, MSG_ROSTER_SUBSCRIBE, new JidData(jid));
  393. if (notify_)
  394. notify_->WakeupMainThread();
  395. }
  396. void OnRosterUnsubscribeW(const buzz::Jid &jid) {
  397. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  398. if (notify_)
  399. notify_->OnRosterUnsubscribe(jid);
  400. }
  401. void OnRosterUnsubscribe(const buzz::Jid &jid) {
  402. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  403. main_thread_->Post(this, MSG_ROSTER_UNSUBSCRIBE, new JidData(jid));
  404. if (notify_)
  405. notify_->WakeupMainThread();
  406. }
  407. void OnRosterSubscribedW(const buzz::Jid &jid) {
  408. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  409. if (notify_)
  410. notify_->OnRosterSubscribed(jid);
  411. }
  412. void OnRosterSubscribed(const buzz::Jid &jid) {
  413. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  414. main_thread_->Post(this, MSG_ROSTER_SUBSCRIBED, new JidData(jid));
  415. if (notify_)
  416. notify_->WakeupMainThread();
  417. }
  418. void OnRosterUnsubscribedW(const buzz::Jid &jid) {
  419. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  420. if (notify_)
  421. notify_->OnRosterUnsubscribed(jid);
  422. }
  423. void OnRosterUnsubscribed(const buzz::Jid &jid) {
  424. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  425. main_thread_->Post(this, MSG_ROSTER_UNSUBSCRIBED, new JidData(jid));
  426. if (notify_)
  427. notify_->WakeupMainThread();
  428. }
  429. void OnRosterRefreshStartedW() {
  430. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  431. if (notify_)
  432. notify_->OnRosterRefreshStarted();
  433. }
  434. void OnRosterRefreshStarted() {
  435. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  436. main_thread_->Post(this, MSG_ROSTER_REFRESH_STARTED);
  437. if (notify_)
  438. notify_->WakeupMainThread();
  439. }
  440. void OnRosterRefreshFinishedW() {
  441. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  442. if (notify_)
  443. notify_->OnRosterRefreshFinished();
  444. }
  445. void OnRosterRefreshFinished() {
  446. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  447. main_thread_->Post(this, MSG_ROSTER_REFRESH_FINISHED);
  448. if (notify_)
  449. notify_->WakeupMainThread();
  450. }
  451. struct XmppMessageData : talk_base::MessageData {
  452. XmppMessageData(const buzz::XmppMessage &m) : m_(m) {}
  453. buzz::XmppMessage m_;
  454. };
  455. void OnIncomingMessageW(const buzz::XmppMessage &msg) {
  456. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  457. if (notify_)
  458. notify_->OnMessage(msg);
  459. }
  460. void OnIncomingMessage(const buzz::XmppMessage &msg) {
  461. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  462. main_thread_->Post(this, MSG_INCOMING_MESSAGE, new XmppMessageData(msg));
  463. if (notify_)
  464. notify_->WakeupMainThread();
  465. }
  466. void OnStatusUpdateW (const buzz::Status &status) {
  467. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  468. if (notify_)
  469. notify_->OnStatusUpdate(status);
  470. }
  471. void OnStatusUpdate (const buzz::Status &status) {
  472. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  473. main_thread_->Post(this, MSG_STATUS_UPDATE, new SendPresenceData(status));
  474. if (notify_)
  475. notify_->WakeupMainThread();
  476. }
  477. struct StatusErrorData : talk_base::MessageData {
  478. StatusErrorData(const buzz::XmlElement &stanza) : stanza_(stanza) {}
  479. buzz::XmlElement stanza_;
  480. };
  481. void OnStatusErrorW (const buzz::XmlElement &stanza) {
  482. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  483. if (notify_)
  484. notify_->OnStatusError(stanza);
  485. }
  486. void OnStatusError (const buzz::XmlElement &stanza) {
  487. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  488. main_thread_->Post(this, MSG_STATUS_ERROR, new StatusErrorData(stanza));
  489. if (notify_)
  490. notify_->WakeupMainThread();
  491. }
  492. void LoginW() {
  493. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  494. XmppSocket* socket = new XmppSocket(true);
  495. pump_->DoLogin(xcs_, socket, is_test_login_ ? NULL : new XmppAuth());
  496. socket->SignalCloseEvent.connect(this,
  497. &LibjinglePlusWorker::OnXmppSocketClose);
  498. }
  499. void DisconnectW() {
  500. assert(talk_base::ThreadManager::CurrentThread() == worker_thread_);
  501. pump_->DoDisconnect();
  502. }
  503. void SendXmppMessageW(const buzz::XmppMessage &m) {
  504. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  505. buzz::SendMessageTask * smt = new buzz::SendMessageTask(pump_.get()->client());
  506. smt->Send(m);
  507. smt->Start();
  508. }
  509. void SendXmppIqW(const buzz::Jid &to_jid, bool is_get,
  510. const buzz::XmlElement *xml_element) {
  511. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  512. buzz::IqTask *iq_task = new buzz::IqTask(pump_.get()->client(),
  513. is_get, to_jid, const_cast<buzz::XmlElement *>(xml_element));
  514. iq_task->SignalDone.connect(this, &LibjinglePlusWorker::OnIqComplete);
  515. iq_task->Start();
  516. }
  517. struct IqCompleteData : public talk_base::MessageData {
  518. IqCompleteData(bool success, const buzz::XmlElement *stanza) :
  519. success_(success), stanza_(*stanza) {}
  520. bool success_;
  521. buzz::XmlElement stanza_;
  522. };
  523. void OnIqCompleteW(bool success, const buzz::XmlElement& stanza) {
  524. assert(talk_base::ThreadManager::CurrentThread() != worker_thread_);
  525. if (notify_)
  526. notify_->OnIqDone(success, stanza);
  527. }
  528. void OnIqComplete(bool success, const buzz::XmlElement *stanza) {
  529. assert(talk_base::ThreadManager::CurrentThread() == worker_thread_);
  530. main_thread_->Post(this, MSG_IQ_COMPLETE,
  531. new IqCompleteData(success, stanza));
  532. if (notify_)
  533. notify_->WakeupMainThread();
  534. }
  535. void SendPresenceW(const buzz::Status & s) {
  536. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  537. buzz::PresenceOutTask *pot = new buzz::PresenceOutTask(pump_.get()->client());
  538. pot->Send(s);
  539. pot->Start();
  540. }
  541. void SendDirectedMUCPresenceW(const buzz::Jid & j, const buzz::Status & s,
  542. const std::string &user_nick, const std::string &api_capability,
  543. const std::string &api_message, const std::string &role) {
  544. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  545. buzz::PresenceOutTask *pot = new buzz::PresenceOutTask(pump_.get()->client());
  546. pot->SendDirectedMUC(j,s,user_nick,api_capability,api_message, role);
  547. pot->Start();
  548. }
  549. void SendDirectedPresenceW(const buzz::Jid & j, const buzz::Status & s) {
  550. assert (talk_base::ThreadManager::CurrentThread() == worker_thread_);
  551. buzz::PresenceOutTask *pot = new buzz::PresenceOutTask(pump_.get()->client());
  552. pot->SendDirected(j,s);
  553. pot->Start();
  554. }
  555. void OnXmppSocketClose(int error) {
  556. notify_->OnSocketClose(error);
  557. }
  558. struct SendMessageData : public talk_base::MessageData {
  559. SendMessageData(const buzz::XmppMessage &m) : m_(m) {}
  560. buzz::XmppMessage m_;
  561. };
  562. struct SendIqData : public talk_base::MessageData {
  563. SendIqData(const buzz::Jid &jid, bool is_get, const buzz::XmlElement *m)
  564. : to_jid_(jid), is_get_(is_get), xml_element_(m) {}
  565. buzz::Jid to_jid_;
  566. bool is_get_;
  567. const buzz::XmlElement *xml_element_;
  568. };
  569. struct SendPresenceData : public talk_base::MessageData {
  570. SendPresenceData(const buzz::Status &s) : s_(s) {}
  571. buzz::Status s_;
  572. };
  573. struct SendDirectedPresenceData : public talk_base::MessageData {
  574. SendDirectedPresenceData(const buzz::Jid &j, const buzz::Status &s) : j_(j), s_(s) {}
  575. buzz::Jid j_;
  576. buzz::Status s_;
  577. };
  578. struct SendDirectedMUCPresenceData : public talk_base::MessageData {
  579. SendDirectedMUCPresenceData(const buzz::Jid &j, const buzz::Status &s,
  580. const std::string &un, const std::string &ac,
  581. const std::string &am, const std::string &role)
  582. : j_(j), s_(s), un_(un), ac_(ac), am_(am), role_(role) {}
  583. buzz::Jid j_;
  584. buzz::Status s_;
  585. std::string un_;
  586. std::string ac_;
  587. std::string am_;
  588. std::string role_;
  589. };
  590. talk_base::scoped_ptr<talk_base::Win32SocketServer> ss_;
  591. talk_base::scoped_ptr<talk_base::Thread> main_thread_;
  592. talk_base::Thread *worker_thread_;
  593. LibjinglePlus *ljp_;
  594. LibjinglePlusNotify *notify_;
  595. buzz::XmppClientSettings xcs_;
  596. talk_base::PhysicalSocketServer pss_;
  597. talk_base::scoped_ptr<XmppPump> pump_;
  598. buzz::PresencePushTask * ppt_;
  599. buzz::ReceiveMessageTask * rmt_;
  600. buzz::RosterTask * rt_;
  601. bool is_test_login_;
  602. };
  603. LibjinglePlus::LibjinglePlus(LibjinglePlusNotify *notify)
  604. {
  605. worker_ = new LibjinglePlusWorker(this, notify);
  606. }
  607. LibjinglePlus::~LibjinglePlus()
  608. {
  609. delete worker_;
  610. worker_ = NULL;
  611. }
  612. void LibjinglePlus::Login(const std::string &jid,
  613. const std::string &password,
  614. const std::string &machine_address,
  615. bool is_test, bool cookie_auth) {
  616. worker_->Login(jid, password, machine_address, is_test, cookie_auth);
  617. }
  618. void LibjinglePlus::SendPresence(const buzz::Status & s) {
  619. worker_->SendPresence(s);
  620. }
  621. void LibjinglePlus::SendDirectedPresence(const buzz::Jid & j, const buzz::Status & s) {
  622. worker_->SendDirectedPresence(j,s);
  623. }
  624. void LibjinglePlus::SendDirectedMUCPresence(const buzz::Jid & j,
  625. const buzz::Status & s, const std::string &user_nick,
  626. const std::string &api_capability, const std::string &api_message,
  627. const std::string &role) {
  628. worker_->SendDirectedMUCPresence(j,s,user_nick,api_capability,api_message,
  629. role);
  630. }
  631. void LibjinglePlus::SendXmppMessage(const buzz::XmppMessage & m) {
  632. worker_->SendXmppMessage(m);
  633. }
  634. void LibjinglePlus::SendXmppIq(const buzz::Jid &to_jid, bool is_get,
  635. const buzz::XmlElement *iq_element) {
  636. worker_->SendXmppIq(to_jid, is_get, iq_element);
  637. }
  638. void LibjinglePlus::DoCallbacks() {
  639. worker_->DoCallbacks();
  640. }