henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/libjingle/xmpp/pingtask.h" |
| 12 | |
kwiberg | 322c4a0 | 2016-04-30 02:40:22 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 15 | #include "webrtc/libjingle/xmpp/constants.h" |
| 16 | #include "webrtc/base/logging.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 17 | |
| 18 | namespace buzz { |
| 19 | |
| 20 | PingTask::PingTask(buzz::XmppTaskParentInterface* parent, |
| 21 | rtc::MessageQueue* message_queue, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 22 | uint32_t ping_period_millis, |
| 23 | uint32_t ping_timeout_millis) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 24 | : buzz::XmppTask(parent, buzz::XmppEngine::HL_SINGLE), |
| 25 | message_queue_(message_queue), |
| 26 | ping_period_millis_(ping_period_millis), |
| 27 | ping_timeout_millis_(ping_timeout_millis), |
| 28 | next_ping_time_(0), |
| 29 | ping_response_deadline_(0) { |
| 30 | ASSERT(ping_period_millis >= ping_timeout_millis); |
| 31 | } |
| 32 | |
| 33 | bool PingTask::HandleStanza(const buzz::XmlElement* stanza) { |
| 34 | if (!MatchResponseIq(stanza, Jid(STR_EMPTY), task_id())) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | if (stanza->Attr(buzz::QN_TYPE) != buzz::STR_RESULT && |
| 39 | stanza->Attr(buzz::QN_TYPE) != buzz::STR_ERROR) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | QueueStanza(stanza); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | // This task runs indefinitely and remains in either the start or blocked |
| 48 | // states. |
| 49 | int PingTask::ProcessStart() { |
| 50 | if (ping_period_millis_ < ping_timeout_millis_) { |
| 51 | LOG(LS_ERROR) << "ping_period_millis should be >= ping_timeout_millis"; |
| 52 | return STATE_ERROR; |
| 53 | } |
| 54 | const buzz::XmlElement* stanza = NextStanza(); |
| 55 | if (stanza != NULL) { |
| 56 | // Received a ping response of some sort (don't care what it is). |
| 57 | ping_response_deadline_ = 0; |
| 58 | } |
| 59 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 60 | uint32_t now = rtc::Time(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 61 | |
| 62 | // If the ping timed out, signal. |
| 63 | if (ping_response_deadline_ != 0 && now >= ping_response_deadline_) { |
| 64 | SignalTimeout(); |
| 65 | return STATE_ERROR; |
| 66 | } |
| 67 | |
| 68 | // Send a ping if it's time. |
| 69 | if (now >= next_ping_time_) { |
kwiberg | 322c4a0 | 2016-04-30 02:40:22 -0700 | [diff] [blame] | 70 | std::unique_ptr<buzz::XmlElement> stanza( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 71 | MakeIq(buzz::STR_GET, Jid(STR_EMPTY), task_id())); |
| 72 | stanza->AddElement(new buzz::XmlElement(QN_PING)); |
| 73 | SendStanza(stanza.get()); |
| 74 | |
| 75 | ping_response_deadline_ = now + ping_timeout_millis_; |
| 76 | next_ping_time_ = now + ping_period_millis_; |
| 77 | |
| 78 | // Wake ourselves up when it's time to send another ping or when the ping |
| 79 | // times out (so we can fire a signal). |
| 80 | message_queue_->PostDelayed(ping_timeout_millis_, this); |
| 81 | message_queue_->PostDelayed(ping_period_millis_, this); |
| 82 | } |
| 83 | |
| 84 | return STATE_BLOCKED; |
| 85 | } |
| 86 | |
| 87 | void PingTask::OnMessage(rtc::Message* msg) { |
| 88 | // Get the task manager to run this task so we can send a ping or signal or |
| 89 | // process a ping response. |
| 90 | Wake(); |
| 91 | } |
| 92 | |
| 93 | } // namespace buzz |