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 #include "google_apis/gcm/engine/mcs_client.h" | 5 #include "google_apis/gcm/engine/mcs_client.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 return false; | 56 return false; |
57 std::vector<std::string> new_list; | 57 std::vector<std::string> new_list; |
58 for (int i = 0; i < selective_ack.id_size(); ++i) { | 58 for (int i = 0; i < selective_ack.id_size(); ++i) { |
59 DCHECK(!selective_ack.id(i).empty()); | 59 DCHECK(!selective_ack.id(i).empty()); |
60 new_list.push_back(selective_ack.id(i)); | 60 new_list.push_back(selective_ack.id(i)); |
61 } | 61 } |
62 id_list->swap(new_list); | 62 id_list->swap(new_list); |
63 return true; | 63 return true; |
64 } | 64 } |
65 | 65 |
66 // Helper for getting string representation of the MessageSendStatus enum. | |
67 std::string GetMessageSendStatusString( | |
fgorski
2014/03/18 21:28:37
This should happen closer to UI. MCS client should
juyik
2014/03/20 01:09:53
Following my comments about SoC principle in gcm_s
| |
68 gcm::MCSClient::MessageSendStatus status) { | |
69 switch (status) { | |
70 case gcm::MCSClient::QUEUED: | |
71 return "QUEUED"; | |
72 case gcm::MCSClient::SENT: | |
73 return "SENT"; | |
74 case gcm::MCSClient::QUEUE_SIZE_LIMIT_REACHED: | |
75 return "QUEUE_SIZE_LIMIT_REACHED"; | |
76 case gcm::MCSClient::APP_QUEUE_SIZE_LIMIT_REACHED: | |
77 return "APP_QUEUE_SIZE_LIMIT_REACHED"; | |
78 case gcm::MCSClient::MESSAGE_TOO_LARGE: | |
79 return "MESSAGE_TOO_LARGE"; | |
80 case gcm::MCSClient::NO_CONNECTION_ON_ZERO_TTL: | |
81 return "NO_CONNECTION_ON_ZERO_TTL"; | |
82 case gcm::MCSClient::TTL_EXCEEDED: | |
83 return "TTL_EXCEEDED"; | |
84 default: | |
85 NOTREACHED(); | |
86 return "UNKNOWN"; | |
87 } | |
88 } | |
89 | |
66 } // namespace | 90 } // namespace |
67 | 91 |
68 class CollapseKey { | 92 class CollapseKey { |
69 public: | 93 public: |
70 explicit CollapseKey(const mcs_proto::DataMessageStanza& message); | 94 explicit CollapseKey(const mcs_proto::DataMessageStanza& message); |
71 ~CollapseKey(); | 95 ~CollapseKey(); |
72 | 96 |
73 // Comparison operator for use in maps. | 97 // Comparison operator for use in maps. |
74 bool operator<(const CollapseKey& right) const; | 98 bool operator<(const CollapseKey& right) const; |
75 | 99 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 | 145 |
122 // The protobuf of the message itself. | 146 // The protobuf of the message itself. |
123 MCSProto protobuf; | 147 MCSProto protobuf; |
124 }; | 148 }; |
125 | 149 |
126 ReliablePacketInfo::ReliablePacketInfo() | 150 ReliablePacketInfo::ReliablePacketInfo() |
127 : stream_id(0), tag(0) { | 151 : stream_id(0), tag(0) { |
128 } | 152 } |
129 ReliablePacketInfo::~ReliablePacketInfo() {} | 153 ReliablePacketInfo::~ReliablePacketInfo() {} |
130 | 154 |
155 int MCSClient::GetSendQueueSize() const { | |
156 return to_send_.size(); | |
fgorski
2014/03/18 21:28:37
Could we pass these values to the recorder every t
juyik
2014/03/20 01:09:53
It seems that having a snapshot of these items (li
| |
157 } | |
158 | |
159 int MCSClient::GetUnackedQueueSize() const { | |
160 return to_resend_.size(); | |
161 } | |
162 | |
131 std::string MCSClient::GetStateString() const { | 163 std::string MCSClient::GetStateString() const { |
132 switch(state_) { | 164 switch(state_) { |
133 case UNINITIALIZED: | 165 case UNINITIALIZED: |
134 return "UNINITIALIZED"; | 166 return "UNINITIALIZED"; |
135 case LOADED: | 167 case LOADED: |
136 return "LOADED"; | 168 return "LOADED"; |
137 case CONNECTING: | 169 case CONNECTING: |
138 return "CONNECTING"; | 170 return "CONNECTING"; |
139 case CONNECTED: | 171 case CONNECTED: |
140 return "CONNECTED"; | 172 return "CONNECTED"; |
141 default: | 173 default: |
142 NOTREACHED(); | 174 NOTREACHED(); |
143 return std::string(); | 175 return std::string(); |
144 } | 176 } |
145 } | 177 } |
146 | 178 |
147 MCSClient::MCSClient(const std::string& version_string, | 179 MCSClient::MCSClient(const std::string& version_string, |
148 base::Clock* clock, | 180 base::Clock* clock, |
149 ConnectionFactory* connection_factory, | 181 ConnectionFactory* connection_factory, |
150 GCMStore* gcm_store) | 182 GCMStore* gcm_store, |
183 GCMStatsRecorder* recorder) | |
151 : version_string_(version_string), | 184 : version_string_(version_string), |
152 clock_(clock), | 185 clock_(clock), |
153 state_(UNINITIALIZED), | 186 state_(UNINITIALIZED), |
154 android_id_(0), | 187 android_id_(0), |
155 security_token_(0), | 188 security_token_(0), |
156 connection_factory_(connection_factory), | 189 connection_factory_(connection_factory), |
157 connection_handler_(NULL), | 190 connection_handler_(NULL), |
158 last_device_to_server_stream_id_received_(0), | 191 last_device_to_server_stream_id_received_(0), |
159 last_server_to_device_stream_id_received_(0), | 192 last_server_to_device_stream_id_received_(0), |
160 stream_id_out_(0), | 193 stream_id_out_(0), |
161 stream_id_in_(0), | 194 stream_id_in_(0), |
162 gcm_store_(gcm_store), | 195 gcm_store_(gcm_store), |
196 recorder_(recorder), | |
163 weak_ptr_factory_(this) { | 197 weak_ptr_factory_(this) { |
164 } | 198 } |
165 | 199 |
166 MCSClient::~MCSClient() { | 200 MCSClient::~MCSClient() { |
167 } | 201 } |
168 | 202 |
169 void MCSClient::Initialize( | 203 void MCSClient::Initialize( |
170 const ErrorCallback& error_callback, | 204 const ErrorCallback& error_callback, |
171 const OnMessageReceivedCallback& message_received_callback, | 205 const OnMessageReceivedCallback& message_received_callback, |
172 const OnMessageSentCallback& message_sent_callback, | 206 const OnMessageSentCallback& message_sent_callback, |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
488 if (packet_info->tag == kDataMessageStanzaTag) { | 522 if (packet_info->tag == kDataMessageStanzaTag) { |
489 mcs_proto::DataMessageStanza* data_message = | 523 mcs_proto::DataMessageStanza* data_message = |
490 reinterpret_cast<mcs_proto::DataMessageStanza*>( | 524 reinterpret_cast<mcs_proto::DataMessageStanza*>( |
491 packet_info->protobuf.get()); | 525 packet_info->protobuf.get()); |
492 uint64 sent = data_message->sent(); | 526 uint64 sent = data_message->sent(); |
493 DCHECK_GT(sent, 0U); | 527 DCHECK_GT(sent, 0U); |
494 int queued = (clock_->Now().ToInternalValue() / | 528 int queued = (clock_->Now().ToInternalValue() / |
495 base::Time::kMicrosecondsPerSecond) - sent; | 529 base::Time::kMicrosecondsPerSecond) - sent; |
496 DVLOG(1) << "Message was queued for " << queued << " seconds."; | 530 DVLOG(1) << "Message was queued for " << queued << " seconds."; |
497 data_message->set_queued(queued); | 531 data_message->set_queued(queued); |
532 recorder_->RecordSendWithDetails( | |
533 data_message->category(), | |
534 gcm::GCMStatsRecorder::Intermediate, | |
535 data_message->to(), | |
536 data_message->id(), | |
537 packet_info->protobuf->ByteSize(), | |
538 "SENT TO WIRE"); | |
498 } | 539 } |
499 | 540 |
500 // Set the proper last received stream id to acknowledge received server | 541 // Set the proper last received stream id to acknowledge received server |
501 // packets. | 542 // packets. |
502 DVLOG(1) << "Setting last stream id received to " | 543 DVLOG(1) << "Setting last stream id received to " |
503 << stream_id_in_; | 544 << stream_id_in_; |
504 SetLastStreamIdReceived(stream_id_in_, | 545 SetLastStreamIdReceived(stream_id_in_, |
505 packet_info->protobuf.get()); | 546 packet_info->protobuf.get()); |
506 if (stream_id_in_ != last_server_to_device_stream_id_received_) { | 547 if (stream_id_in_ != last_server_to_device_stream_id_received_) { |
507 last_server_to_device_stream_id_received_ = stream_id_in_; | 548 last_server_to_device_stream_id_received_ = stream_id_in_; |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
855 } | 896 } |
856 | 897 |
857 void MCSClient::NotifyMessageSendStatus( | 898 void MCSClient::NotifyMessageSendStatus( |
858 const google::protobuf::MessageLite& protobuf, | 899 const google::protobuf::MessageLite& protobuf, |
859 MessageSendStatus status) { | 900 MessageSendStatus status) { |
860 if (GetMCSProtoTag(protobuf) != kDataMessageStanzaTag) | 901 if (GetMCSProtoTag(protobuf) != kDataMessageStanzaTag) |
861 return; | 902 return; |
862 | 903 |
863 const mcs_proto::DataMessageStanza* data_message_stanza = | 904 const mcs_proto::DataMessageStanza* data_message_stanza = |
864 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf); | 905 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf); |
906 recorder_->RecordSendWithDetails( | |
907 data_message_stanza->category(), | |
908 status == SENT ? gcm::GCMStatsRecorder::Success : | |
909 (status == QUEUED ? gcm::GCMStatsRecorder::Intermediate : | |
910 gcm::GCMStatsRecorder::Failure), | |
911 data_message_stanza->to(), | |
912 data_message_stanza->id(), | |
913 protobuf.ByteSize(), | |
914 GetMessageSendStatusString(status)); | |
865 message_sent_callback_.Run( | 915 message_sent_callback_.Run( |
866 data_message_stanza->device_user_id(), | 916 data_message_stanza->device_user_id(), |
867 data_message_stanza->category(), | 917 data_message_stanza->category(), |
868 data_message_stanza->id(), | 918 data_message_stanza->id(), |
869 status); | 919 status); |
870 } | 920 } |
871 | 921 |
872 void MCSClient::SetGCMStoreForTesting(GCMStore* gcm_store) { | 922 void MCSClient::SetGCMStoreForTesting(GCMStore* gcm_store) { |
873 gcm_store_ = gcm_store; | 923 gcm_store_ = gcm_store; |
874 } | 924 } |
875 | 925 |
876 MCSClient::MCSPacketInternal MCSClient::PopMessageForSend() { | 926 MCSClient::MCSPacketInternal MCSClient::PopMessageForSend() { |
877 MCSPacketInternal packet = to_send_.front(); | 927 MCSPacketInternal packet = to_send_.front(); |
878 to_send_.pop_front(); | 928 to_send_.pop_front(); |
879 | 929 |
880 if (packet->tag == kDataMessageStanzaTag) { | 930 if (packet->tag == kDataMessageStanzaTag) { |
881 mcs_proto::DataMessageStanza* data_message = | 931 mcs_proto::DataMessageStanza* data_message = |
882 reinterpret_cast<mcs_proto::DataMessageStanza*>(packet->protobuf.get()); | 932 reinterpret_cast<mcs_proto::DataMessageStanza*>(packet->protobuf.get()); |
883 CollapseKey collapse_key(*data_message); | 933 CollapseKey collapse_key(*data_message); |
884 if (collapse_key.IsValid()) | 934 if (collapse_key.IsValid()) |
885 collapse_key_map_.erase(collapse_key); | 935 collapse_key_map_.erase(collapse_key); |
886 } | 936 } |
887 | 937 |
888 return packet; | 938 return packet; |
889 } | 939 } |
890 | 940 |
891 } // namespace gcm | 941 } // namespace gcm |
OLD | NEW |