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/gcm_store.h" |
20 #include "google_apis/gcm/engine/heartbeat_manager.h" | 20 #include "google_apis/gcm/engine/heartbeat_manager.h" |
21 #include "google_apis/gcm/gcm_stats_recorder.h" | |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 class Clock; | 24 class Clock; |
24 } // namespace base | 25 } // namespace base |
25 | 26 |
26 namespace google { | 27 namespace google { |
27 namespace protobuf { | 28 namespace protobuf { |
28 class MessageLite; | 29 class MessageLite; |
29 } // namespace protobuf | 30 } // namespace protobuf |
30 } // namespace google | 31 } // namespace google |
31 | 32 |
32 namespace mcs_proto { | 33 namespace mcs_proto { |
33 class LoginRequest; | 34 class LoginRequest; |
34 } | 35 } |
35 | 36 |
36 namespace gcm { | 37 namespace gcm { |
37 | 38 |
38 class CollapseKey; | 39 class CollapseKey; |
39 class ConnectionFactory; | 40 class ConnectionFactory; |
41 class GCMStatsRecorder; | |
40 struct ReliablePacketInfo; | 42 struct ReliablePacketInfo; |
41 | 43 |
42 // An MCS client. This client is in charge of all communications with an | 44 // An MCS client. This client is in charge of all communications with an |
43 // MCS endpoint, and is capable of reliably sending/receiving GCM messages. | 45 // MCS endpoint, and is capable of reliably sending/receiving GCM messages. |
44 // NOTE: Not thread safe. This class should live on the same thread as that | 46 // NOTE: Not thread safe. This class should live on the same thread as that |
45 // network requests are performed on. | 47 // network requests are performed on. |
46 class GCM_EXPORT MCSClient { | 48 class GCM_EXPORT MCSClient { |
47 public: | 49 public: |
48 // Any change made to this enum should have corresponding change in the | 50 // Any change made to this enum should have corresponding change in the |
49 // GetStateString(...) function. | 51 // GetStateString(...) function. |
50 enum State { | 52 enum State { |
51 UNINITIALIZED, // Uninitialized. | 53 UNINITIALIZED, // Uninitialized. |
52 LOADED, // GCM Load finished, waiting to connect. | 54 LOADED, // GCM Load finished, waiting to connect. |
53 CONNECTING, // Connection in progress. | 55 CONNECTING, // Connection in progress. |
54 CONNECTED, // Connected and running. | 56 CONNECTED, // Connected and running. |
55 }; | 57 }; |
56 | 58 |
59 // Any change made to this enum should have corresponding change in the | |
60 // GetMessageSendStatusString(...) function in mcs_client.cc. | |
57 enum MessageSendStatus { | 61 enum MessageSendStatus { |
58 // Message was queued succcessfully. | 62 // Message was queued succcessfully. |
59 QUEUED, | 63 QUEUED, |
60 // Message was sent to the server and the ACK was received. | 64 // Message was sent to the server and the ACK was received. |
61 SENT, | 65 SENT, |
62 // Message not saved, because total queue size limit reached. | 66 // Message not saved, because total queue size limit reached. |
63 QUEUE_SIZE_LIMIT_REACHED, | 67 QUEUE_SIZE_LIMIT_REACHED, |
64 // Messgae not saved, because app queue size limit reached. | 68 // Message not saved, because app queue size limit reached. |
65 APP_QUEUE_SIZE_LIMIT_REACHED, | 69 APP_QUEUE_SIZE_LIMIT_REACHED, |
66 // Message too large to send. | 70 // Message too large to send. |
67 MESSAGE_TOO_LARGE, | 71 MESSAGE_TOO_LARGE, |
68 // Message not send becuase of TTL = 0 and no working connection. | 72 // Message not send becuase of TTL = 0 and no working connection. |
69 NO_CONNECTION_ON_ZERO_TTL, | 73 NO_CONNECTION_ON_ZERO_TTL, |
70 // Message exceeded TTL. | 74 // Message exceeded TTL. |
71 TTL_EXCEEDED | 75 TTL_EXCEEDED, |
72 }; | 76 }; |
73 | 77 |
74 // Callback for MCSClient's error conditions. | 78 // Callback for MCSClient's error conditions. |
75 // TODO(fgorski): Keeping it as a callback with intention to add meaningful | 79 // TODO(fgorski): Keeping it as a callback with intention to add meaningful |
76 // error information. | 80 // error information. |
77 typedef base::Callback<void()> ErrorCallback; | 81 typedef base::Callback<void()> ErrorCallback; |
78 // Callback when a message is received. | 82 // Callback when a message is received. |
79 typedef base::Callback<void(const MCSMessage& message)> | 83 typedef base::Callback<void(const MCSMessage& message)> |
80 OnMessageReceivedCallback; | 84 OnMessageReceivedCallback; |
81 // Callback when a message is sent (and receipt has been acknowledged by | 85 // Callback when a message is sent (and receipt has been acknowledged by |
82 // the MCS endpoint). | 86 // the MCS endpoint). |
83 typedef base::Callback< | 87 typedef base::Callback< |
84 void(int64 user_serial_number, | 88 void(int64 user_serial_number, |
85 const std::string& app_id, | 89 const std::string& app_id, |
86 const std::string& message_id, | 90 const std::string& message_id, |
87 MessageSendStatus status)> OnMessageSentCallback; | 91 MessageSendStatus status)> OnMessageSentCallback; |
88 | 92 |
89 MCSClient(const std::string& version_string, | 93 MCSClient(const std::string& version_string, |
90 base::Clock* clock, | 94 base::Clock* clock, |
91 ConnectionFactory* connection_factory, | 95 ConnectionFactory* connection_factory, |
92 GCMStore* gcm_store); | 96 GCMStore* gcm_store, |
97 GCMStatsRecorder* recorder); | |
93 virtual ~MCSClient(); | 98 virtual ~MCSClient(); |
94 | 99 |
95 // Initialize the client. Will load any previous id/token information as well | 100 // Initialize the client. Will load any previous id/token information as well |
96 // as unacknowledged message information from the GCM storage, if it exists, | 101 // as unacknowledged message information from the GCM storage, if it exists, |
97 // passing the id/token information back via |initialization_callback| along | 102 // passing the id/token information back via |initialization_callback| along |
98 // with a |success == true| result. If no GCM information is present (and | 103 // with a |success == true| result. If no GCM information is present (and |
99 // this is therefore a fresh client), a clean GCM store will be created and | 104 // this is therefore a fresh client), a clean GCM store will be created and |
100 // values of 0 will be returned via |initialization_callback| with | 105 // values of 0 will be returned via |initialization_callback| with |
101 // |success == true|. | 106 // |success == true|. |
102 /// If an error loading the GCM store is encountered, | 107 /// If an error loading the GCM store is encountered, |
(...skipping 17 matching lines...) Expand all Loading... | |
120 // Whether to use RMQ depends on whether the protobuf has |ttl| set or not. | 125 // Whether to use RMQ depends on whether the protobuf has |ttl| set or not. |
121 // |ttl == 0| denotes the message should only be sent if the connection is | 126 // |ttl == 0| denotes the message should only be sent if the connection is |
122 // open. |ttl > 0| will keep the message saved for |ttl| seconds, after which | 127 // open. |ttl > 0| will keep the message saved for |ttl| seconds, after which |
123 // it will be dropped if it was unable to be sent. When a message is dropped, | 128 // it will be dropped if it was unable to be sent. When a message is dropped, |
124 // |message_sent_callback_| is invoked with a TTL expiration error. | 129 // |message_sent_callback_| is invoked with a TTL expiration error. |
125 virtual void SendMessage(const MCSMessage& message); | 130 virtual void SendMessage(const MCSMessage& message); |
126 | 131 |
127 // Returns the current state of the client. | 132 // Returns the current state of the client. |
128 State state() const { return state_; } | 133 State state() const { return state_; } |
129 | 134 |
135 // Returns the size of the send message queue. | |
136 int GetSendQueueSize() const ; | |
jianli
2014/03/18 23:53:23
nit: space betwee ';'.
juyik
2014/03/20 01:09:53
Done.
| |
137 | |
138 // Returns the size of the un-acked messaage queue. | |
139 int GetUnackedQueueSize() const ; | |
jianli
2014/03/18 23:53:23
ditto
juyik
2014/03/20 01:09:53
Done.
| |
140 | |
130 // Returns text representation of the state enum. | 141 // Returns text representation of the state enum. |
131 std::string GetStateString() const; | 142 std::string GetStateString() const; |
132 | 143 |
133 protected: | 144 protected: |
134 // Sets a |gcm_store| for testing. Does not take ownership. | 145 // Sets a |gcm_store| for testing. Does not take ownership. |
135 // TODO(fgorski): Remove this method. Create GCMEngineFactory that will create | 146 // TODO(fgorski): Remove this method. Create GCMEngineFactory that will create |
136 // components of the engine. | 147 // components of the engine. |
137 void SetGCMStoreForTesting(GCMStore* gcm_store); | 148 void SetGCMStoreForTesting(GCMStore* gcm_store); |
138 | 149 |
139 private: | 150 private: |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 // acknowledged. They do not have associated stream ids, and will be | 269 // acknowledged. They do not have associated stream ids, and will be |
259 // acknowledged on the next login attempt. | 270 // acknowledged on the next login attempt. |
260 PersistentIdList restored_unackeds_server_ids_; | 271 PersistentIdList restored_unackeds_server_ids_; |
261 | 272 |
262 // The GCM persistent store. Not owned. | 273 // The GCM persistent store. Not owned. |
263 GCMStore* gcm_store_; | 274 GCMStore* gcm_store_; |
264 | 275 |
265 // Manager to handle triggering/detecting heartbeats. | 276 // Manager to handle triggering/detecting heartbeats. |
266 HeartbeatManager heartbeat_manager_; | 277 HeartbeatManager heartbeat_manager_; |
267 | 278 |
279 // Recorder that records GCM activities for debugging purpose. Not owned. | |
280 GCMStatsRecorder* recorder_; | |
281 | |
268 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; | 282 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; |
269 | 283 |
270 DISALLOW_COPY_AND_ASSIGN(MCSClient); | 284 DISALLOW_COPY_AND_ASSIGN(MCSClient); |
271 }; | 285 }; |
272 | 286 |
273 } // namespace gcm | 287 } // namespace gcm |
274 | 288 |
275 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 289 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
OLD | NEW |