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