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> |
(...skipping 19 matching lines...) Expand all Loading... | |
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 CollapseKey; | 38 class CollapseKey; |
39 class ConnectionFactory; | 39 class ConnectionFactory; |
40 class GCMStatsRecorder; | |
40 struct ReliablePacketInfo; | 41 struct ReliablePacketInfo; |
41 | 42 |
42 // An MCS client. This client is in charge of all communications with an | 43 // 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. | 44 // 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 | 45 // NOTE: Not thread safe. This class should live on the same thread as that |
45 // network requests are performed on. | 46 // network requests are performed on. |
46 class GCM_EXPORT MCSClient { | 47 class GCM_EXPORT MCSClient { |
47 public: | 48 public: |
48 // Any change made to this enum should have corresponding change in the | 49 // Any change made to this enum should have corresponding change in the |
49 // GetStateString(...) function. | 50 // GetStateString(...) function. |
50 enum State { | 51 enum State { |
51 UNINITIALIZED, // Uninitialized. | 52 UNINITIALIZED, // Uninitialized. |
52 LOADED, // GCM Load finished, waiting to connect. | 53 LOADED, // GCM Load finished, waiting to connect. |
53 CONNECTING, // Connection in progress. | 54 CONNECTING, // Connection in progress. |
54 CONNECTED, // Connected and running. | 55 CONNECTED, // Connected and running. |
55 }; | 56 }; |
56 | 57 |
58 // Any change made to this enum should have corresponding change in the | |
59 // GetMessageSendStatusString(...) function in mcs_client.cc. | |
57 enum MessageSendStatus { | 60 enum MessageSendStatus { |
58 // Message was queued succcessfully. | 61 // Message was queued succcessfully. |
59 QUEUED, | 62 QUEUED, |
60 // Message was sent to the server and the ACK was received. | 63 // Message was sent to the server and the ACK was received. |
61 SENT, | 64 SENT, |
62 // Message not saved, because total queue size limit reached. | 65 // Message not saved, because total queue size limit reached. |
63 QUEUE_SIZE_LIMIT_REACHED, | 66 QUEUE_SIZE_LIMIT_REACHED, |
64 // Messgae not saved, because app queue size limit reached. | 67 // Message not saved, because app queue size limit reached. |
65 APP_QUEUE_SIZE_LIMIT_REACHED, | 68 APP_QUEUE_SIZE_LIMIT_REACHED, |
66 // Message too large to send. | 69 // Message too large to send. |
67 MESSAGE_TOO_LARGE, | 70 MESSAGE_TOO_LARGE, |
68 // Message not send becuase of TTL = 0 and no working connection. | 71 // Message not send becuase of TTL = 0 and no working connection. |
69 NO_CONNECTION_ON_ZERO_TTL, | 72 NO_CONNECTION_ON_ZERO_TTL, |
70 // Message exceeded TTL. | 73 // Message exceeded TTL. |
71 TTL_EXCEEDED | 74 TTL_EXCEEDED, |
75 | |
76 // NOTE: always keep this entry at the end. Add new status types only | |
77 // immediately above this line. Make sure to update the corresponding | |
78 // histogram enum accordingly. | |
79 SEND_STATUS_COUNT | |
72 }; | 80 }; |
73 | 81 |
74 // Callback for MCSClient's error conditions. | 82 // Callback for MCSClient's error conditions. |
75 // TODO(fgorski): Keeping it as a callback with intention to add meaningful | 83 // TODO(fgorski): Keeping it as a callback with intention to add meaningful |
76 // error information. | 84 // error information. |
77 typedef base::Callback<void()> ErrorCallback; | 85 typedef base::Callback<void()> ErrorCallback; |
78 // Callback when a message is received. | 86 // Callback when a message is received. |
79 typedef base::Callback<void(const MCSMessage& message)> | 87 typedef base::Callback<void(const MCSMessage& message)> |
80 OnMessageReceivedCallback; | 88 OnMessageReceivedCallback; |
81 // Callback when a message is sent (and receipt has been acknowledged by | 89 // Callback when a message is sent (and receipt has been acknowledged by |
82 // the MCS endpoint). | 90 // the MCS endpoint). |
83 typedef base::Callback< | 91 typedef base::Callback< |
84 void(int64 user_serial_number, | 92 void(int64 user_serial_number, |
85 const std::string& app_id, | 93 const std::string& app_id, |
86 const std::string& message_id, | 94 const std::string& message_id, |
87 MessageSendStatus status)> OnMessageSentCallback; | 95 MessageSendStatus status)> OnMessageSentCallback; |
88 | 96 |
89 MCSClient(const std::string& version_string, | 97 MCSClient(const std::string& version_string, |
90 base::Clock* clock, | 98 base::Clock* clock, |
91 ConnectionFactory* connection_factory, | 99 ConnectionFactory* connection_factory, |
92 GCMStore* gcm_store); | 100 GCMStore* gcm_store, |
101 GCMStatsRecorder* recorder); | |
93 virtual ~MCSClient(); | 102 virtual ~MCSClient(); |
94 | 103 |
95 // Initialize the client. Will load any previous id/token information as well | 104 // Initialize the client. Will load any previous id/token information as well |
96 // as unacknowledged message information from the GCM storage, if it exists, | 105 // as unacknowledged message information from the GCM storage, if it exists, |
97 // passing the id/token information back via |initialization_callback| along | 106 // passing the id/token information back via |initialization_callback| along |
98 // with a |success == true| result. If no GCM information is present (and | 107 // 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 | 108 // 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 | 109 // values of 0 will be returned via |initialization_callback| with |
101 // |success == true|. | 110 // |success == true|. |
102 /// If an error loading the GCM store is encountered, | 111 /// 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. | 129 // 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 | 130 // |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 | 131 // 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, | 132 // 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. | 133 // |message_sent_callback_| is invoked with a TTL expiration error. |
125 virtual void SendMessage(const MCSMessage& message); | 134 virtual void SendMessage(const MCSMessage& message); |
126 | 135 |
127 // Returns the current state of the client. | 136 // Returns the current state of the client. |
128 State state() const { return state_; } | 137 State state() const { return state_; } |
129 | 138 |
139 // Returns the size of the send message queue. | |
140 int GetSendQueueSize() const; | |
141 | |
142 // Returns the size of the un-acked messaage queue. | |
143 int GetUnackedQueueSize() const; | |
Nicolas Zea
2014/03/27 18:55:42
Unacked is ambiguous, as incoming messages can be
juyik
2014/03/27 20:05:11
Done.
| |
144 | |
130 // Returns text representation of the state enum. | 145 // Returns text representation of the state enum. |
131 std::string GetStateString() const; | 146 std::string GetStateString() const; |
132 | 147 |
133 private: | 148 private: |
134 typedef uint32 StreamId; | 149 typedef uint32 StreamId; |
135 typedef std::string PersistentId; | 150 typedef std::string PersistentId; |
136 typedef std::vector<StreamId> StreamIdList; | 151 typedef std::vector<StreamId> StreamIdList; |
137 typedef std::vector<PersistentId> PersistentIdList; | 152 typedef std::vector<PersistentId> PersistentIdList; |
138 typedef std::map<StreamId, PersistentId> StreamIdToPersistentIdMap; | 153 typedef std::map<StreamId, PersistentId> StreamIdToPersistentIdMap; |
139 typedef linked_ptr<ReliablePacketInfo> MCSPacketInternal; | 154 typedef linked_ptr<ReliablePacketInfo> MCSPacketInternal; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 // acknowledged. They do not have associated stream ids, and will be | 267 // acknowledged. They do not have associated stream ids, and will be |
253 // acknowledged on the next login attempt. | 268 // acknowledged on the next login attempt. |
254 PersistentIdList restored_unackeds_server_ids_; | 269 PersistentIdList restored_unackeds_server_ids_; |
255 | 270 |
256 // The GCM persistent store. Not owned. | 271 // The GCM persistent store. Not owned. |
257 GCMStore* gcm_store_; | 272 GCMStore* gcm_store_; |
258 | 273 |
259 // Manager to handle triggering/detecting heartbeats. | 274 // Manager to handle triggering/detecting heartbeats. |
260 HeartbeatManager heartbeat_manager_; | 275 HeartbeatManager heartbeat_manager_; |
261 | 276 |
277 // Recorder that records GCM activities for debugging purpose. Not owned. | |
278 GCMStatsRecorder* recorder_; | |
279 | |
262 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; | 280 base::WeakPtrFactory<MCSClient> weak_ptr_factory_; |
263 | 281 |
264 DISALLOW_COPY_AND_ASSIGN(MCSClient); | 282 DISALLOW_COPY_AND_ASSIGN(MCSClient); |
265 }; | 283 }; |
266 | 284 |
267 } // namespace gcm | 285 } // namespace gcm |
268 | 286 |
269 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ | 287 #endif // GOOGLE_APIS_GCM_ENGINE_MCS_CLIENT_H_ |
OLD | NEW |