OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 5 #ifndef GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
6 #define GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 6 #define GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
17 #include "google_apis/gcm/base/gcm_export.h" | 17 #include "google_apis/gcm/base/gcm_export.h" |
18 #include "google_apis/gcm/base/mcs_message.h" | 18 #include "google_apis/gcm/base/mcs_message.h" |
19 #include "google_apis/gcm/engine/connection_handler.h" | 19 #include "google_apis/gcm/engine/connection_handler.h" |
20 #include "google_apis/gcm/engine/rmq_store.h" | 20 #include "google_apis/gcm/engine/gcm_store.h" |
21 | 21 |
22 namespace google { | 22 namespace google { |
23 namespace protobuf { | 23 namespace protobuf { |
24 class MessageLite; | 24 class MessageLite; |
25 } // namespace protobuf | 25 } // namespace protobuf |
26 } // namespace google | 26 } // namespace google |
27 | 27 |
28 namespace mcs_proto { | 28 namespace mcs_proto { |
29 class LoginRequest; | 29 class LoginRequest; |
30 } | 30 } |
31 | 31 |
32 namespace gcm { | 32 namespace gcm { |
33 | 33 |
34 class ConnectionFactory; | 34 class ConnectionFactory; |
35 struct ReliablePacketInfo; | 35 struct ReliablePacketInfo; |
36 | 36 |
37 // An MCS client. This client is in charge of all communications with an | 37 // An MCS client. This client is in charge of all communications with an |
38 // MCS endpoint, and is capable of reliably sending/receiving GCM messages. | 38 // MCS endpoint, and is capable of reliably sending/receiving GCM messages. |
39 // NOTE: Not thread safe. This class should live on the same thread as that | 39 // NOTE: Not thread safe. This class should live on the same thread as that |
40 // network requests are performed on. | 40 // network requests are performed on. |
41 class GCM_EXPORT MCSClient { | 41 class GCM_EXPORT MCSClient { |
42 public: | 42 public: |
43 enum State { | 43 enum State { |
44 UNINITIALIZED, // Uninitialized. | 44 UNINITIALIZED, // Uninitialized. |
45 LOADED, // RMQ Load finished, waiting to connect. | 45 LOADED, // GCM Load finished, waiting to connect. |
46 CONNECTING, // Connection in progress. | 46 CONNECTING, // Connection in progress. |
47 CONNECTED, // Connected and running. | 47 CONNECTED, // Connected and running. |
48 }; | 48 }; |
49 | 49 |
50 // Callback for informing MCSClient status. It is valid for this to be | 50 // Callback for informing MCSClient status. It is valid for this to be |
51 // invoked more than once if a permanent error is encountered after a | 51 // invoked more than once if a permanent error is encountered after a |
52 // successful login was initiated. | 52 // successful login was initiated. |
53 typedef base::Callback< | 53 typedef base::Callback< |
54 void(bool success, | 54 void(bool success, |
55 uint64 restored_android_id, | 55 uint64 restored_android_id, |
56 uint64 restored_security_token)> InitializationCompleteCallback; | 56 uint64 restored_security_token)> InitializationCompleteCallback; |
57 // Callback when a message is received. | 57 // Callback when a message is received. |
58 typedef base::Callback<void(const MCSMessage& message)> | 58 typedef base::Callback<void(const MCSMessage& message)> |
59 OnMessageReceivedCallback; | 59 OnMessageReceivedCallback; |
60 // Callback when a message is sent (and receipt has been acknowledged by | 60 // Callback when a message is sent (and receipt has been acknowledged by |
61 // the MCS endpoint). | 61 // the MCS endpoint). |
62 // TODO(zea): pass some sort of structure containing more details about | 62 // TODO(zea): pass some sort of structure containing more details about |
63 // send failures. | 63 // send failures. |
64 typedef base::Callback<void(const std::string& message_id)> | 64 typedef base::Callback<void(const std::string& message_id)> |
65 OnMessageSentCallback; | 65 OnMessageSentCallback; |
66 | 66 |
67 MCSClient(ConnectionFactory* connection_factory, RMQStore* rmq_store); | 67 MCSClient(ConnectionFactory* connection_factory, GCMStore* gcm_store); |
68 virtual ~MCSClient(); | 68 virtual ~MCSClient(); |
69 | 69 |
70 // Initialize the client. Will load any previous id/token information as well | 70 // Initialize the client. Will load any previous id/token information as well |
71 // as unacknowledged message information from the RMQ storage, if it exists, | 71 // as unacknowledged message information from the GCM storage, if it exists, |
72 // passing the id/token information back via |initialization_callback| along | 72 // passing the id/token information back via |initialization_callback| along |
73 // with a |success == true| result. If no RMQ information is present (and | 73 // with a |success == true| result. If no GCM information is present (and |
74 // this is therefore a fresh client), a clean RMQ store will be created and | 74 // this is therefore a fresh client), a clean GCM store will be created and |
75 // values of 0 will be returned via |initialization_callback| with | 75 // values of 0 will be returned via |initialization_callback| with |
76 // |success == true|. | 76 // |success == true|. |
77 /// If an error loading the RMQ store is encountered, | 77 /// If an error loading the GCM store is encountered, |
78 // |initialization_callback| will be invoked with |success == false|. | 78 // |initialization_callback| will be invoked with |success == false|. |
79 void Initialize(const InitializationCompleteCallback& initialization_callback, | 79 void Initialize(const InitializationCompleteCallback& initialization_callback, |
80 const OnMessageReceivedCallback& message_received_callback, | 80 const OnMessageReceivedCallback& message_received_callback, |
81 const OnMessageSentCallback& message_sent_callback, | 81 const OnMessageSentCallback& message_sent_callback, |
82 const RMQStore::LoadResult& load_result); | 82 const GCMStore::LoadResult& load_result); |
83 | 83 |
84 // Logs the client into the server. Client must be initialized. | 84 // Logs the client into the server. Client must be initialized. |
85 // |android_id| and |security_token| are optional if this is not a new | 85 // |android_id| and |security_token| are optional if this is not a new |
86 // client, else they must be non-zero. | 86 // client, else they must be non-zero. |
87 // Successful login will result in |message_received_callback| being invoked | 87 // Successful login will result in |message_received_callback| being invoked |
88 // with a valid LoginResponse. | 88 // with a valid LoginResponse. |
89 // Login failure (typically invalid id/token) will shut down the client, and | 89 // Login failure (typically invalid id/token) will shut down the client, and |
90 // |initialization_callback| to be invoked with |success = false|. | 90 // |initialization_callback| to be invoked with |success = false|. |
91 void Login(uint64 android_id, uint64 security_token); | 91 void Login(uint64 android_id, uint64 security_token); |
92 | 92 |
93 // Sends a message, with or without reliable message queueing (RMQ) support. | 93 // Sends a message, with or without reliable message queueing (GCM) support. |
Nicolas Zea
2013/12/27 20:03:06
nit: leave as RMQ, here and below
fgorski
2013/12/27 22:20:39
Done, but I'll leave the one below as GCM, given t
| |
94 // Will asynchronously invoke the OnMessageSent callback regardless. | 94 // Will asynchronously invoke the OnMessageSent callback regardless. |
95 // TODO(zea): support TTL. | 95 // TODO(zea): support TTL. |
96 void SendMessage(const MCSMessage& message, bool use_rmq); | 96 void SendMessage(const MCSMessage& message, bool use_rmq); |
97 | 97 |
98 // Disconnects the client and permanently destroys the persistent RMQ store. | 98 // Disconnects the client and permanently destroys the persistent GCM store. |
99 // WARNING: This is permanent, and the client must be recreated with new | 99 // WARNING: This is permanent, and the client must be recreated with new |
100 // credentials afterwards. | 100 // credentials afterwards. |
101 void Destroy(); | 101 void Destroy(); |
102 | 102 |
103 // Returns the current state of the client. | 103 // Returns the current state of the client. |
104 State state() const { return state_; } | 104 State state() const { return state_; } |
105 | 105 |
106 private: | 106 private: |
107 typedef uint32 StreamId; | 107 typedef uint32 StreamId; |
108 typedef std::string PersistentId; | 108 typedef std::string PersistentId; |
109 typedef std::vector<StreamId> StreamIdList; | 109 typedef std::vector<StreamId> StreamIdList; |
110 typedef std::vector<PersistentId> PersistentIdList; | 110 typedef std::vector<PersistentId> PersistentIdList; |
111 typedef std::map<StreamId, PersistentId> StreamIdToPersistentIdMap; | 111 typedef std::map<StreamId, PersistentId> StreamIdToPersistentIdMap; |
112 typedef linked_ptr<ReliablePacketInfo> MCSPacketInternal; | 112 typedef linked_ptr<ReliablePacketInfo> MCSPacketInternal; |
113 | 113 |
114 // Resets the internal state and builds a new login request, acknowledging | 114 // Resets the internal state and builds a new login request, acknowledging |
115 // any pending server-to-device messages and rebuilding the send queue | 115 // any pending server-to-device messages and rebuilding the send queue |
116 // from all unacknowledged device-to-server messages. | 116 // from all unacknowledged device-to-server messages. |
117 // Should only be called when the connection has been reset. | 117 // Should only be called when the connection has been reset. |
118 void ResetStateAndBuildLoginRequest(mcs_proto::LoginRequest* request); | 118 void ResetStateAndBuildLoginRequest(mcs_proto::LoginRequest* request); |
119 | 119 |
120 // Send a heartbeat to the MCS server. | 120 // Send a heartbeat to the MCS server. |
121 void SendHeartbeat(); | 121 void SendHeartbeat(); |
122 | 122 |
123 // RMQ Store callback. | 123 // GCM Store callback. |
124 void OnRMQUpdateFinished(bool success); | 124 void OnGCMUpdateFinished(bool success); |
125 | 125 |
126 // Attempt to send a message. | 126 // Attempt to send a message. |
127 void MaybeSendMessage(); | 127 void MaybeSendMessage(); |
128 | 128 |
129 // Helper for sending a protobuf along with any unacknowledged ids to the | 129 // Helper for sending a protobuf along with any unacknowledged ids to the |
130 // wire. | 130 // wire. |
131 void SendPacketToWire(ReliablePacketInfo* packet_info); | 131 void SendPacketToWire(ReliablePacketInfo* packet_info); |
132 | 132 |
133 // Handle a data message sent to the MCS client system from the MCS server. | 133 // Handle a data message sent to the MCS client system from the MCS server. |
134 void HandleMCSDataMesssage( | 134 void HandleMCSDataMesssage( |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 // until the ack message is itself confirmed. The list of all message ids | 199 // until the ack message is itself confirmed. The list of all message ids |
200 // acknowledged are keyed off the device stream id of the message that | 200 // acknowledged are keyed off the device stream id of the message that |
201 // acknowledged them. | 201 // acknowledged them. |
202 std::map<StreamId, PersistentIdList> acked_server_ids_; | 202 std::map<StreamId, PersistentIdList> acked_server_ids_; |
203 | 203 |
204 // Those server messages from a previous connection that were not fully | 204 // Those server messages from a previous connection that were not fully |
205 // acknowledged. They do not have associated stream ids, and will be | 205 // acknowledged. They do not have associated stream ids, and will be |
206 // acknowledged on the next login attempt. | 206 // acknowledged on the next login attempt. |
207 PersistentIdList restored_unackeds_server_ids_; | 207 PersistentIdList restored_unackeds_server_ids_; |
208 | 208 |
209 // The reliable message queue persistent store. Owned by the caller. | 209 // The reliable message queue persistent store. Owned by the caller. |
Nicolas Zea
2013/12/27 20:03:06
nit: update comment.
fgorski
2013/12/27 22:20:39
Done.
| |
210 RMQStore* rmq_store_; | 210 GCMStore* gcm_store_; |
211 | 211 |
212 // ----- Heartbeats ----- | 212 // ----- Heartbeats ----- |
213 // The current heartbeat interval. | 213 // The current heartbeat interval. |
214 base::TimeDelta heartbeat_interval_; | 214 base::TimeDelta heartbeat_interval_; |
215 // Timer for triggering heartbeats. | 215 // Timer for triggering heartbeats. |
216 base::Timer heartbeat_timer_; | 216 base::Timer heartbeat_timer_; |
217 | 217 |
218 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; | 218 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; |
219 | 219 |
220 DISALLOW_COPY_AND_ASSIGN(MCSClient); | 220 DISALLOW_COPY_AND_ASSIGN(MCSClient); |
221 }; | 221 }; |
222 | 222 |
223 } // namespace gcm | 223 } // namespace gcm |
224 | 224 |
225 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 225 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
OLD | NEW |