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