| 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" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/time/clock.h" | 13 #include "base/time/clock.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "google_apis/gcm/base/mcs_util.h" | 15 #include "google_apis/gcm/base/mcs_util.h" |
| 16 #include "google_apis/gcm/base/socket_stream.h" | 16 #include "google_apis/gcm/base/socket_stream.h" |
| 17 #include "google_apis/gcm/engine/connection_factory.h" | 17 #include "google_apis/gcm/engine/connection_factory.h" |
| 18 #include "google_apis/gcm/gcm_stats_recorder.h" |
| 18 | 19 |
| 19 using namespace google::protobuf::io; | 20 using namespace google::protobuf::io; |
| 20 | 21 |
| 21 namespace gcm { | 22 namespace gcm { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 typedef scoped_ptr<google::protobuf::MessageLite> MCSProto; | 26 typedef scoped_ptr<google::protobuf::MessageLite> MCSProto; |
| 26 | 27 |
| 27 // The category of messages intended for the GCM client itself from MCS. | 28 // The category of messages intended for the GCM client itself from MCS. |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 122 |
| 122 // The protobuf of the message itself. | 123 // The protobuf of the message itself. |
| 123 MCSProto protobuf; | 124 MCSProto protobuf; |
| 124 }; | 125 }; |
| 125 | 126 |
| 126 ReliablePacketInfo::ReliablePacketInfo() | 127 ReliablePacketInfo::ReliablePacketInfo() |
| 127 : stream_id(0), tag(0) { | 128 : stream_id(0), tag(0) { |
| 128 } | 129 } |
| 129 ReliablePacketInfo::~ReliablePacketInfo() {} | 130 ReliablePacketInfo::~ReliablePacketInfo() {} |
| 130 | 131 |
| 132 int MCSClient::GetSendQueueSize() const { |
| 133 return to_send_.size(); |
| 134 } |
| 135 |
| 136 int MCSClient::GetUnackedQueueSize() const { |
| 137 return to_resend_.size(); |
| 138 } |
| 139 |
| 131 std::string MCSClient::GetStateString() const { | 140 std::string MCSClient::GetStateString() const { |
| 132 switch(state_) { | 141 switch(state_) { |
| 133 case UNINITIALIZED: | 142 case UNINITIALIZED: |
| 134 return "UNINITIALIZED"; | 143 return "UNINITIALIZED"; |
| 135 case LOADED: | 144 case LOADED: |
| 136 return "LOADED"; | 145 return "LOADED"; |
| 137 case CONNECTING: | 146 case CONNECTING: |
| 138 return "CONNECTING"; | 147 return "CONNECTING"; |
| 139 case CONNECTED: | 148 case CONNECTED: |
| 140 return "CONNECTED"; | 149 return "CONNECTED"; |
| 141 default: | 150 default: |
| 142 NOTREACHED(); | 151 NOTREACHED(); |
| 143 return std::string(); | 152 return std::string(); |
| 144 } | 153 } |
| 145 } | 154 } |
| 146 | 155 |
| 147 MCSClient::MCSClient(const std::string& version_string, | 156 MCSClient::MCSClient(const std::string& version_string, |
| 148 base::Clock* clock, | 157 base::Clock* clock, |
| 149 ConnectionFactory* connection_factory, | 158 ConnectionFactory* connection_factory, |
| 150 GCMStore* gcm_store) | 159 GCMStore* gcm_store, |
| 160 GCMStatsRecorder* recorder) |
| 151 : version_string_(version_string), | 161 : version_string_(version_string), |
| 152 clock_(clock), | 162 clock_(clock), |
| 153 state_(UNINITIALIZED), | 163 state_(UNINITIALIZED), |
| 154 android_id_(0), | 164 android_id_(0), |
| 155 security_token_(0), | 165 security_token_(0), |
| 156 connection_factory_(connection_factory), | 166 connection_factory_(connection_factory), |
| 157 connection_handler_(NULL), | 167 connection_handler_(NULL), |
| 158 last_device_to_server_stream_id_received_(0), | 168 last_device_to_server_stream_id_received_(0), |
| 159 last_server_to_device_stream_id_received_(0), | 169 last_server_to_device_stream_id_received_(0), |
| 160 stream_id_out_(0), | 170 stream_id_out_(0), |
| 161 stream_id_in_(0), | 171 stream_id_in_(0), |
| 162 gcm_store_(gcm_store), | 172 gcm_store_(gcm_store), |
| 173 recorder_(recorder), |
| 163 weak_ptr_factory_(this) { | 174 weak_ptr_factory_(this) { |
| 164 } | 175 } |
| 165 | 176 |
| 166 MCSClient::~MCSClient() { | 177 MCSClient::~MCSClient() { |
| 167 } | 178 } |
| 168 | 179 |
| 169 void MCSClient::Initialize( | 180 void MCSClient::Initialize( |
| 170 const ErrorCallback& error_callback, | 181 const ErrorCallback& error_callback, |
| 171 const OnMessageReceivedCallback& message_received_callback, | 182 const OnMessageReceivedCallback& message_received_callback, |
| 172 const OnMessageSentCallback& message_sent_callback, | 183 const OnMessageSentCallback& message_sent_callback, |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 if (packet_info->tag == kDataMessageStanzaTag) { | 499 if (packet_info->tag == kDataMessageStanzaTag) { |
| 489 mcs_proto::DataMessageStanza* data_message = | 500 mcs_proto::DataMessageStanza* data_message = |
| 490 reinterpret_cast<mcs_proto::DataMessageStanza*>( | 501 reinterpret_cast<mcs_proto::DataMessageStanza*>( |
| 491 packet_info->protobuf.get()); | 502 packet_info->protobuf.get()); |
| 492 uint64 sent = data_message->sent(); | 503 uint64 sent = data_message->sent(); |
| 493 DCHECK_GT(sent, 0U); | 504 DCHECK_GT(sent, 0U); |
| 494 int queued = (clock_->Now().ToInternalValue() / | 505 int queued = (clock_->Now().ToInternalValue() / |
| 495 base::Time::kMicrosecondsPerSecond) - sent; | 506 base::Time::kMicrosecondsPerSecond) - sent; |
| 496 DVLOG(1) << "Message was queued for " << queued << " seconds."; | 507 DVLOG(1) << "Message was queued for " << queued << " seconds."; |
| 497 data_message->set_queued(queued); | 508 data_message->set_queued(queued); |
| 509 recorder_->RecordSentToWire( |
| 510 data_message->category(), |
| 511 data_message->to(), |
| 512 data_message->id(), |
| 513 queued); |
| 498 } | 514 } |
| 499 | 515 |
| 500 // Set the proper last received stream id to acknowledge received server | 516 // Set the proper last received stream id to acknowledge received server |
| 501 // packets. | 517 // packets. |
| 502 DVLOG(1) << "Setting last stream id received to " | 518 DVLOG(1) << "Setting last stream id received to " |
| 503 << stream_id_in_; | 519 << stream_id_in_; |
| 504 SetLastStreamIdReceived(stream_id_in_, | 520 SetLastStreamIdReceived(stream_id_in_, |
| 505 packet_info->protobuf.get()); | 521 packet_info->protobuf.get()); |
| 506 if (stream_id_in_ != last_server_to_device_stream_id_received_) { | 522 if (stream_id_in_ != last_server_to_device_stream_id_received_) { |
| 507 last_server_to_device_stream_id_received_ = stream_id_in_; | 523 last_server_to_device_stream_id_received_ = stream_id_in_; |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 } | 871 } |
| 856 | 872 |
| 857 void MCSClient::NotifyMessageSendStatus( | 873 void MCSClient::NotifyMessageSendStatus( |
| 858 const google::protobuf::MessageLite& protobuf, | 874 const google::protobuf::MessageLite& protobuf, |
| 859 MessageSendStatus status) { | 875 MessageSendStatus status) { |
| 860 if (GetMCSProtoTag(protobuf) != kDataMessageStanzaTag) | 876 if (GetMCSProtoTag(protobuf) != kDataMessageStanzaTag) |
| 861 return; | 877 return; |
| 862 | 878 |
| 863 const mcs_proto::DataMessageStanza* data_message_stanza = | 879 const mcs_proto::DataMessageStanza* data_message_stanza = |
| 864 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf); | 880 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf); |
| 881 recorder_->RecordNotifySendStatus( |
| 882 data_message_stanza->category(), |
| 883 data_message_stanza->to(), |
| 884 data_message_stanza->id(), |
| 885 status, |
| 886 protobuf.ByteSize(), |
| 887 data_message_stanza->ttl()); |
| 865 message_sent_callback_.Run( | 888 message_sent_callback_.Run( |
| 866 data_message_stanza->device_user_id(), | 889 data_message_stanza->device_user_id(), |
| 867 data_message_stanza->category(), | 890 data_message_stanza->category(), |
| 868 data_message_stanza->id(), | 891 data_message_stanza->id(), |
| 869 status); | 892 status); |
| 870 } | 893 } |
| 871 | 894 |
| 872 MCSClient::MCSPacketInternal MCSClient::PopMessageForSend() { | 895 MCSClient::MCSPacketInternal MCSClient::PopMessageForSend() { |
| 873 MCSPacketInternal packet = to_send_.front(); | 896 MCSPacketInternal packet = to_send_.front(); |
| 874 to_send_.pop_front(); | 897 to_send_.pop_front(); |
| 875 | 898 |
| 876 if (packet->tag == kDataMessageStanzaTag) { | 899 if (packet->tag == kDataMessageStanzaTag) { |
| 877 mcs_proto::DataMessageStanza* data_message = | 900 mcs_proto::DataMessageStanza* data_message = |
| 878 reinterpret_cast<mcs_proto::DataMessageStanza*>(packet->protobuf.get()); | 901 reinterpret_cast<mcs_proto::DataMessageStanza*>(packet->protobuf.get()); |
| 879 CollapseKey collapse_key(*data_message); | 902 CollapseKey collapse_key(*data_message); |
| 880 if (collapse_key.IsValid()) | 903 if (collapse_key.IsValid()) |
| 881 collapse_key_map_.erase(collapse_key); | 904 collapse_key_map_.erase(collapse_key); |
| 882 } | 905 } |
| 883 | 906 |
| 884 return packet; | 907 return packet; |
| 885 } | 908 } |
| 886 | 909 |
| 887 } // namespace gcm | 910 } // namespace gcm |
| OLD | NEW |