Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sync/engine/traffic_logger.h" | 5 #include "sync/engine/traffic_logger.h" |
| 6 | 6 |
| 7 #include <queue> | |
| 7 #include <string> | 8 #include <string> |
| 8 | 9 |
| 9 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/values.h" | 13 #include "base/values.h" |
| 13 #include "sync/protocol/proto_value_conversions.h" | 14 #include "sync/protocol/proto_value_conversions.h" |
| 14 #include "sync/protocol/sync.pb.h" | 15 #include "sync/protocol/sync.pb.h" |
| 16 #include "sync/sessions/sync_session.h" | |
| 15 | 17 |
| 16 namespace browser_sync { | 18 namespace browser_sync { |
| 17 | 19 |
| 20 using sessions::SyncSession; | |
| 21 | |
| 22 TrafficRecord::TrafficRecord(const std::string& message, | |
| 23 TrafficMessageType message_type, | |
| 24 bool truncated) : | |
| 25 message(message), | |
| 26 message_type(message_type), | |
| 27 truncated(truncated) { | |
| 28 } | |
| 29 | |
| 30 TrafficRecord::TrafficRecord() | |
| 31 : message_type(UNKNOWN_MESSAGE_TYPE), | |
| 32 truncated(false) { | |
| 33 } | |
| 34 | |
| 35 TrafficRecord::~TrafficRecord() { | |
| 36 } | |
| 37 | |
| 38 static const unsigned int kMaxMessages = 10; | |
| 39 static const unsigned int kMaxMessageSize = 5 * 1024; | |
| 18 namespace { | 40 namespace { |
| 19 template <class T> | 41 template <class T> |
| 20 void LogData(const T& data, | 42 void LogData(const T& data, |
| 21 DictionaryValue* (*to_dictionary_value)(const T&, bool), | 43 DictionaryValue* (*to_dictionary_value)(const T&, bool), |
| 22 const std::string& description) { | 44 const std::string& description) { |
| 23 if (::logging::DEBUG_MODE && VLOG_IS_ON(1)) { | 45 if (::logging::DEBUG_MODE && VLOG_IS_ON(1)) { |
| 24 scoped_ptr<DictionaryValue> value( | 46 scoped_ptr<DictionaryValue> value( |
| 25 (*to_dictionary_value)(data, true /* include_specifics */)); | 47 (*to_dictionary_value)(data, true /* include_specifics */)); |
| 26 std::string message; | 48 std::string message; |
| 27 base::JSONWriter::WriteWithOptions(value.get(), | 49 base::JSONWriter::WriteWithOptions(value.get(), |
| 28 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 50 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 29 &message); | 51 &message); |
| 30 DVLOG(1) << "\n" << description << "\n" << message << "\n"; | 52 DVLOG(1) << "\n" << description << "\n" << message << "\n"; |
| 31 } | 53 } |
| 32 } | 54 } |
| 55 | |
| 56 void AddTrafficToQueue(std::queue<TrafficRecord>* traffic_recorder, | |
| 57 const TrafficRecord& record) { | |
| 58 while (traffic_recorder->size() > kMaxMessages) { | |
|
akalin
2012/03/23 00:58:50
actually, I think >= was correct. It's just confu
lipalani1
2012/03/26 21:25:21
Done.
| |
| 59 traffic_recorder->pop(); | |
| 60 } | |
| 61 | |
| 62 traffic_recorder->push(record); | |
| 63 } | |
| 64 | |
| 65 void StoreProtoInQueue(const ::google::protobuf::MessageLite& msg, | |
| 66 TrafficMessageType type, | |
| 67 SyncSession* session) { | |
| 68 bool truncated = false; | |
| 69 std::string message; | |
| 70 if (msg.ByteSize() >= kMaxMessageSize) { | |
| 71 // TODO(lipalani): Trim the specifics to fit in size. | |
| 72 truncated = true; | |
| 73 } else { | |
| 74 msg.SerializeToString(&message); | |
| 75 } | |
| 76 | |
| 77 TrafficRecord record(message, type, truncated); | |
| 78 AddTrafficToQueue(session->context()->traffic_recorder(), record); | |
| 79 } | |
| 80 | |
| 33 } // namespace | 81 } // namespace |
| 34 | 82 |
| 35 void LogClientToServerMessage(const sync_pb::ClientToServerMessage& msg) { | 83 void LogClientToServerMessage(const sync_pb::ClientToServerMessage& msg, |
| 84 SyncSession* session) { | |
| 36 LogData(msg, &ClientToServerMessageToValue, | 85 LogData(msg, &ClientToServerMessageToValue, |
| 37 "******Client To Server Message******"); | 86 "******Client To Server Message******"); |
| 38 // TODO(lipalani) : Store the data (minus specifics) | 87 |
| 39 // in a circular buffer in memory. | 88 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE, session); |
| 40 } | 89 } |
| 41 | 90 |
| 42 void LogClientToServerResponse( | 91 void LogClientToServerResponse( |
| 43 const sync_pb::ClientToServerResponse& response) { | 92 const sync_pb::ClientToServerResponse& response, |
| 93 SyncSession* session) { | |
| 44 LogData(response, &ClientToServerResponseToValue, | 94 LogData(response, &ClientToServerResponseToValue, |
| 45 "******Server Response******"); | 95 "******Server Response******"); |
| 46 // TODO(lipalani) : Store the data (minus specifics) | 96 |
| 47 // in a circular buffer in memory. | 97 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE, session); |
| 48 } | 98 } |
| 49 | 99 |
| 50 } // namespace browser_sync | 100 } // namespace browser_sync |
| OLD | NEW |