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