2013-07-10 02:45:36 +02:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
|
|
#include "talk/xmpp/pingtask.h"
|
|
|
|
|
2014-08-13 19:26:08 +02:00
|
|
|
#include "talk/xmpp/constants.h"
|
2014-07-29 19:36:52 +02:00
|
|
|
#include "webrtc/base/logging.h"
|
|
|
|
#include "webrtc/base/scoped_ptr.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
namespace buzz {
|
|
|
|
|
|
|
|
PingTask::PingTask(buzz::XmppTaskParentInterface* parent,
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::MessageQueue* message_queue,
|
2013-07-10 02:45:36 +02:00
|
|
|
uint32 ping_period_millis,
|
|
|
|
uint32 ping_timeout_millis)
|
|
|
|
: buzz::XmppTask(parent, buzz::XmppEngine::HL_SINGLE),
|
|
|
|
message_queue_(message_queue),
|
|
|
|
ping_period_millis_(ping_period_millis),
|
|
|
|
ping_timeout_millis_(ping_timeout_millis),
|
|
|
|
next_ping_time_(0),
|
|
|
|
ping_response_deadline_(0) {
|
|
|
|
ASSERT(ping_period_millis >= ping_timeout_millis);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PingTask::HandleStanza(const buzz::XmlElement* stanza) {
|
|
|
|
if (!MatchResponseIq(stanza, Jid(STR_EMPTY), task_id())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stanza->Attr(buzz::QN_TYPE) != buzz::STR_RESULT &&
|
|
|
|
stanza->Attr(buzz::QN_TYPE) != buzz::STR_ERROR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QueueStanza(stanza);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This task runs indefinitely and remains in either the start or blocked
|
|
|
|
// states.
|
|
|
|
int PingTask::ProcessStart() {
|
|
|
|
if (ping_period_millis_ < ping_timeout_millis_) {
|
|
|
|
LOG(LS_ERROR) << "ping_period_millis should be >= ping_timeout_millis";
|
|
|
|
return STATE_ERROR;
|
|
|
|
}
|
|
|
|
const buzz::XmlElement* stanza = NextStanza();
|
|
|
|
if (stanza != NULL) {
|
|
|
|
// Received a ping response of some sort (don't care what it is).
|
|
|
|
ping_response_deadline_ = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
uint32 now = rtc::Time();
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// If the ping timed out, signal.
|
|
|
|
if (ping_response_deadline_ != 0 && now >= ping_response_deadline_) {
|
|
|
|
SignalTimeout();
|
|
|
|
return STATE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a ping if it's time.
|
|
|
|
if (now >= next_ping_time_) {
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::scoped_ptr<buzz::XmlElement> stanza(
|
2013-07-10 02:45:36 +02:00
|
|
|
MakeIq(buzz::STR_GET, Jid(STR_EMPTY), task_id()));
|
|
|
|
stanza->AddElement(new buzz::XmlElement(QN_PING));
|
|
|
|
SendStanza(stanza.get());
|
|
|
|
|
|
|
|
ping_response_deadline_ = now + ping_timeout_millis_;
|
|
|
|
next_ping_time_ = now + ping_period_millis_;
|
|
|
|
|
|
|
|
// Wake ourselves up when it's time to send another ping or when the ping
|
|
|
|
// times out (so we can fire a signal).
|
|
|
|
message_queue_->PostDelayed(ping_timeout_millis_, this);
|
|
|
|
message_queue_->PostDelayed(ping_period_millis_, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATE_BLOCKED;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void PingTask::OnMessage(rtc::Message* msg) {
|
2013-07-10 02:45:36 +02:00
|
|
|
// Get the task manager to run this task so we can send a ping or signal or
|
|
|
|
// process a ping response.
|
|
|
|
Wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace buzz
|