OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ |
| 6 #define GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/timer/timer.h" |
| 12 #include "google_apis/gcm/base/gcm_export.h" |
| 13 |
| 14 namespace mcs_proto { |
| 15 class HeartbeatConfig; |
| 16 } |
| 17 |
| 18 namespace gcm { |
| 19 |
| 20 // A heartbeat management class, capable of sending and handling heartbeat |
| 21 // receipt/failures and triggering reconnection as necessary. |
| 22 class GCM_EXPORT HeartbeatManager { |
| 23 public: |
| 24 HeartbeatManager(); |
| 25 ~HeartbeatManager(); |
| 26 |
| 27 // Start the heartbeat logic. |
| 28 // |send_heartbeat_callback_| is the callback the HeartbeatManager uses to |
| 29 // send new heartbeats. Only one heartbeat can be outstanding at a time. |
| 30 void Start(const base::Closure& send_heartbeat_callback, |
| 31 const base::Closure& trigger_reconnect_callback); |
| 32 |
| 33 // Stop the timer. Start(..) must be called again to begin sending heartbeats |
| 34 // afterwards. |
| 35 void Stop(); |
| 36 |
| 37 // Reset the heartbeat timer. It is valid to call this even if no heartbeat |
| 38 // is associated with the ack (for example if another signal is used to |
| 39 // determine that the connection is alive). |
| 40 void OnHeartbeatAcked(); |
| 41 |
| 42 // Updates the current heartbeat interval. |
| 43 void UpdateHeartbeatConfig(const mcs_proto::HeartbeatConfig& config); |
| 44 |
| 45 // Returns the next scheduled heartbeat time. A null time means |
| 46 // no heartbeat is pending. If non-null and less than the |
| 47 // current time (in ticks), the heartbeat has been triggered and an ack is |
| 48 // pending. |
| 49 base::TimeTicks GetNextHeartbeatTime() const; |
| 50 |
| 51 protected: |
| 52 // Helper method to send heartbeat on timer trigger. |
| 53 void OnHeartbeatTriggered(); |
| 54 |
| 55 private: |
| 56 // Restarts the heartbeat timer. |
| 57 void RestartTimer(); |
| 58 |
| 59 // Whether the last heartbeat ping sent has been acknowledged or not. |
| 60 bool waiting_for_ack_; |
| 61 |
| 62 // The current heartbeat interval. |
| 63 int heartbeat_interval_ms_; |
| 64 // The most recent server-provided heartbeat interval (0 if none has been |
| 65 // provided). |
| 66 int server_interval_ms_; |
| 67 |
| 68 // Timer for triggering heartbeats. |
| 69 base::Timer heartbeat_timer_; |
| 70 |
| 71 // Callbacks for interacting with the the connection. |
| 72 base::Closure send_heartbeat_callback_; |
| 73 base::Closure trigger_reconnect_callback_; |
| 74 |
| 75 base::WeakPtrFactory<HeartbeatManager> weak_ptr_factory_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(HeartbeatManager); |
| 78 }; |
| 79 |
| 80 } // namespace gcm |
| 81 |
| 82 #endif // GOOGLE_APIS_GCM_ENGINE_HEARTBEAT_MANAGER_H_ |
OLD | NEW |