Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Unified Diff: google_apis/gcm/engine/heartbeat_manager.h

Issue 118133003: [GCM] Add heartbeat manager and reconnection logic due to heartbeat failure (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: google_apis/gcm/engine/heartbeat_manager.h
diff --git a/google_apis/gcm/engine/heartbeat_manager.h b/google_apis/gcm/engine/heartbeat_manager.h
new file mode 100644
index 0000000000000000000000000000000000000000..d7eeda2592126d3609f9f84921516b11dc1d7fd4
--- /dev/null
+++ b/google_apis/gcm/engine/heartbeat_manager.h
@@ -0,0 +1,81 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_
+#define GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_
+
+#include "base/callback.h"
+#include "base/logging.h"
+#include "base/memory/weak_ptr.h"
+#include "base/timer/timer.h"
+#include "google_apis/gcm/base/gcm_export.h"
+
+namespace mcs_proto {
+class HeartbeatConfig;
+}
+
+namespace gcm {
+
+// A heartbeat management class, capable of sending and handling heartbeat
+// receipt/failures and triggering reconnection as necessary.
+class GCM_EXPORT HeartbeatManager {
+ public:
+ HeartbeatManager();
+ ~HeartbeatManager();
+
+ // Start the heartbeat logic.
+ // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to
+ // send new heartbeats. Only one heartbeat can be outstanding at a time.
+ void Start(const base::Closure& send_heartbeat_callback,
+ const base::Closure& trigger_reconnect_callback);
+
+ // Stop the timer. Start(..) must be called again to begin sending heartbeats
+ // afterwards.
+ void Stop();
+
+ // Reset the heartbeat timer. It is valid to call this even if no heartbeat
+ // is associated with the ack (for example if another signal is used to
+ // determine that the connection is alive).
+ void OnHeartbeatAcked();
+
+ // Updates the current heartbeat interval.
+ void UpdateHeartbeatConfig(const mcs_proto::HeartbeatConfig& config);
+
+ // Returns the next scheduled heartbeat time. If non-null and less than
+ // base::Timeticks::Now(), a heartbeat ack is pending. If null, the heartbeat
+ // manager is stopped.
jianli 2013/12/20 23:10:03 What does it mean if the returned value is non-nul
Nicolas Zea 2013/12/26 22:55:02 That's the time that the heartbeat will be sent. R
+ base::TimeTicks GetNextHeartbeatTime() const;
+
+ protected:
+ // Helper method to send heartbeat on timer trigger.
+ void OnHeartbeatTriggered();
+
+ private:
+ // Restarts the heartbeat timer.
+ void RestartTimer();
+
+ // Whether the last heartbeat ping sent has been acknowledged or not.
+ bool waiting_for_ack_;
+
+ // The current heartbeat interval.
+ base::TimeDelta heartbeat_interval_;
+ // The most recent server-provided heartbeat interval (0 if none has been
+ // provided).
+ int32 server_interval_ms_;
+
+ // Timer for triggering heartbeats.
+ base::Timer heartbeat_timer_;
+
+ // Callbacks for interacting with the the connection.
+ base::Closure send_heartbeat_callback_;
+ base::Closure trigger_reconnect_callback_;
+
+ base::WeakPtrFactory<HeartbeatManager> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(HeartbeatManager);
+};
+
+} // namespace gcm
+
+#endif // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_

Powered by Google App Engine
This is Rietveld 408576698