| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/heartbeat_sender.h" | 5 #include "remoting/host/heartbeat_sender.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "remoting/base/constants.h" | 9 #include "remoting/base/constants.h" |
| 10 #include "remoting/host/host_config.h" |
| 10 #include "remoting/jingle_glue/iq_request.h" | 11 #include "remoting/jingle_glue/iq_request.h" |
| 11 #include "remoting/jingle_glue/jingle_client.h" | 12 #include "remoting/jingle_glue/jingle_client.h" |
| 12 #include "remoting/jingle_glue/jingle_thread.h" | 13 #include "remoting/jingle_glue/jingle_thread.h" |
| 13 #include "talk/xmpp/constants.h" | 14 #include "talk/xmpp/constants.h" |
| 14 #include "talk/xmllite/xmlelement.h" | 15 #include "talk/xmllite/xmlelement.h" |
| 15 | 16 |
| 16 namespace remoting { | 17 namespace remoting { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 const char * const kChromotingNamespace = "google:remoting"; | 20 const char * const kChromotingNamespace = "google:remoting"; |
| 20 const buzz::QName kHeartbeatQuery(true, kChromotingNamespace, "heartbeat"); | 21 const buzz::QName kHeartbeatQuery(true, kChromotingNamespace, "heartbeat"); |
| 21 const buzz::QName kHostIdAttr(true, kChromotingNamespace, "hostid"); | 22 const buzz::QName kHostIdAttr(true, kChromotingNamespace, "hostid"); |
| 22 | 23 |
| 23 // TODO(sergeyu): Make this configurable by the cloud. | 24 // TODO(sergeyu): Make this configurable by the cloud. |
| 24 const int64 kHeartbeatPeriodMs = 5 * 60 * 1000; // 5 minutes. | 25 const int64 kHeartbeatPeriodMs = 5 * 60 * 1000; // 5 minutes. |
| 25 } | 26 } |
| 26 | 27 |
| 27 HeartbeatSender::HeartbeatSender() | 28 HeartbeatSender::HeartbeatSender() |
| 28 : started_(false) { | 29 : started_(false) { |
| 29 } | 30 } |
| 30 | 31 |
| 31 void HeartbeatSender::Start(JingleClient* jingle_client, | 32 void HeartbeatSender::Start(HostConfig* config, JingleClient* jingle_client) { |
| 32 const std::string& host_id) { | |
| 33 DCHECK(jingle_client); | 33 DCHECK(jingle_client); |
| 34 DCHECK(!started_); | 34 DCHECK(!started_); |
| 35 | 35 |
| 36 started_ = true; | 36 started_ = true; |
| 37 | 37 |
| 38 jingle_client_ = jingle_client; | 38 jingle_client_ = jingle_client; |
| 39 host_id_ = host_id; | 39 config_ = config; |
| 40 | 40 |
| 41 jingle_client_->message_loop()->PostTask( | 41 jingle_client_->message_loop()->PostTask( |
| 42 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoStart)); | 42 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoStart)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void HeartbeatSender::DoStart() { | 45 void HeartbeatSender::DoStart() { |
| 46 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); | 46 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); |
| 47 | 47 |
| 48 request_.reset(new IqRequest(jingle_client_)); | 48 request_.reset(new IqRequest(jingle_client_)); |
| 49 request_->set_callback(NewCallback(this, &HeartbeatSender::ProcessResponse)); | 49 request_->set_callback(NewCallback(this, &HeartbeatSender::ProcessResponse)); |
| 50 | 50 |
| 51 jingle_client_->message_loop()->PostTask( | 51 jingle_client_->message_loop()->PostTask( |
| 52 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza)); | 52 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza)); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void HeartbeatSender::DoSendStanza() { | 55 void HeartbeatSender::DoSendStanza() { |
| 56 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); | 56 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); |
| 57 | 57 |
| 58 LOG(INFO) << "Sending heartbeat stanza to " << kChromotingBotJid; | 58 LOG(INFO) << "Sending heartbeat stanza to " << kChromotingBotJid; |
| 59 | 59 |
| 60 buzz::XmlElement* stanza = new buzz::XmlElement(kHeartbeatQuery); | 60 buzz::XmlElement* stanza = new buzz::XmlElement(kHeartbeatQuery); |
| 61 stanza->AddAttr(kHostIdAttr, host_id_); | 61 stanza->AddAttr(kHostIdAttr, config_->host_id()); |
| 62 request_->SendIq(buzz::STR_SET, kChromotingBotJid, stanza); | 62 request_->SendIq(buzz::STR_SET, kChromotingBotJid, stanza); |
| 63 | 63 |
| 64 // Schedule next heartbeat. | 64 // Schedule next heartbeat. |
| 65 jingle_client_->message_loop()->PostDelayedTask( | 65 jingle_client_->message_loop()->PostDelayedTask( |
| 66 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza), | 66 FROM_HERE, NewRunnableMethod(this, &HeartbeatSender::DoSendStanza), |
| 67 kHeartbeatPeriodMs); | 67 kHeartbeatPeriodMs); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void HeartbeatSender::ProcessResponse(const buzz::XmlElement* response) { | 70 void HeartbeatSender::ProcessResponse(const buzz::XmlElement* response) { |
| 71 if (response->Attr(buzz::QN_TYPE) == buzz::STR_ERROR) { | 71 if (response->Attr(buzz::QN_TYPE) == buzz::STR_ERROR) { |
| 72 LOG(ERROR) << "Received error in response to heartbeat: " | 72 LOG(ERROR) << "Received error in response to heartbeat: " |
| 73 << response->Str(); | 73 << response->Str(); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 } // namespace remoting | 77 } // namespace remoting |
| OLD | NEW |