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_recorder.h" | 5 #include "sync/engine/traffic_recorder.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/stringprintf.h" | |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "sync/protocol/proto_value_conversions.h" | 12 #include "sync/protocol/proto_value_conversions.h" |
| 12 #include "sync/protocol/sync.pb.h" | 13 #include "sync/protocol/sync.pb.h" |
| 13 #include "sync/sessions/sync_session.h" | 14 #include "sync/sessions/sync_session.h" |
| 14 | 15 |
| 15 namespace browser_sync { | 16 namespace browser_sync { |
| 16 | 17 |
| 17 TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message, | 18 TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message, |
| 18 TrafficMessageType message_type, | 19 TrafficMessageType message_type, |
| 19 bool truncated) : | 20 bool truncated) : |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 32 | 33 |
| 33 TrafficRecorder::TrafficRecorder(unsigned int max_messages, | 34 TrafficRecorder::TrafficRecorder(unsigned int max_messages, |
| 34 unsigned int max_message_size) | 35 unsigned int max_message_size) |
| 35 : max_messages_(max_messages), | 36 : max_messages_(max_messages), |
| 36 max_message_size_(max_message_size) { | 37 max_message_size_(max_message_size) { |
| 37 } | 38 } |
| 38 | 39 |
| 39 TrafficRecorder::~TrafficRecorder() { | 40 TrafficRecorder::~TrafficRecorder() { |
| 40 } | 41 } |
| 41 | 42 |
| 43 namespace { | |
| 44 #define ENUM_CASE(x) case TrafficRecorder::##x: return #x; break; | |
|
akalin
2012/03/30 01:45:35
looks like you don't use this macro after all
lipalani1
2012/03/30 20:35:16
Done.
| |
| 45 | |
| 46 const char* GetMessageTypeString(TrafficRecorder::TrafficMessageType type) { | |
| 47 switch(type) { | |
| 48 case TrafficRecorder::CLIENT_TO_SERVER_MESSAGE: | |
| 49 return "Request"; | |
| 50 case TrafficRecorder::CLIENT_TO_SERVER_RESPONSE: | |
| 51 return "Response"; | |
| 52 } | |
| 53 NOTREACHED(); | |
| 54 return ""; | |
| 55 } | |
| 56 | |
| 57 #undef ENUM_CASE | |
| 58 } | |
| 59 | |
| 60 DictionaryValue* TrafficRecorder::TrafficRecord::ToValue() const { | |
| 61 scoped_ptr<DictionaryValue> value; | |
| 62 if (truncated) { | |
| 63 value.reset(new DictionaryValue()); | |
| 64 value->SetString("message_type", | |
| 65 GetMessageTypeString(message_type)); | |
|
akalin
2012/03/30 01:45:35
line up with (
lipalani1
2012/03/30 20:35:16
Done.
| |
| 66 value->SetBoolean("truncated", true); | |
| 67 } else if (message_type == TrafficRecorder::CLIENT_TO_SERVER_MESSAGE) { | |
| 68 sync_pb::ClientToServerMessage message_proto; | |
| 69 if (message_proto.ParseFromString(message)) | |
| 70 value.reset( | |
| 71 ClientToServerMessageToValue(message_proto, | |
| 72 false /* include_specifics */)); | |
| 73 } else if (message_type == TrafficRecorder::CLIENT_TO_SERVER_RESPONSE) { | |
| 74 sync_pb::ClientToServerResponse message_proto; | |
| 75 if (message_proto.ParseFromString(message)) | |
| 76 value.reset( | |
| 77 ClientToServerResponseToValue(message_proto, | |
| 78 false /* include_specifics */)); | |
| 79 } else { | |
| 80 NOTREACHED(); | |
| 81 } | |
| 82 | |
| 83 return value.release(); | |
| 84 } | |
| 85 | |
| 86 | |
| 87 DictionaryValue* TrafficRecorder::ToValue() const { | |
| 88 int message_count = 0; | |
| 89 scoped_ptr<DictionaryValue> dictionary(new DictionaryValue()); | |
| 90 std::deque<TrafficRecord>::const_iterator it; | |
| 91 for (it = records_.begin(); it != records_.end(); ++it) { | |
| 92 const TrafficRecord& record = *it; | |
| 93 std::string message_id = StringPrintf("message%d", message_count); | |
|
akalin
2012/03/30 01:45:35
is it necessary to have a string ID? Why not fill
lipalani1
2012/03/30 20:35:16
Done.
| |
| 94 dictionary->Set(message_id, record.ToValue()); | |
| 95 ++message_count; | |
| 96 } | |
| 97 | |
| 98 return dictionary.release(); | |
| 99 } | |
| 100 | |
| 101 | |
| 42 | 102 |
| 43 void TrafficRecorder::AddTrafficToQueue(TrafficRecord* record) { | 103 void TrafficRecorder::AddTrafficToQueue(TrafficRecord* record) { |
| 44 records_.resize(records_.size() + 1); | 104 records_.resize(records_.size() + 1); |
| 45 std::swap(records_.back(), *record); | 105 std::swap(records_.back(), *record); |
| 46 | 106 |
| 47 // We might have more records than our limit. | 107 // We might have more records than our limit. |
| 48 // Maintain the size invariant by deleting items. | 108 // Maintain the size invariant by deleting items. |
| 49 while (records_.size() > max_messages_) { | 109 while (records_.size() > max_messages_) { |
| 50 records_.pop_front(); | 110 records_.pop_front(); |
| 51 } | 111 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 69 | 129 |
| 70 void TrafficRecorder::RecordClientToServerMessage( | 130 void TrafficRecorder::RecordClientToServerMessage( |
| 71 const sync_pb::ClientToServerMessage& msg) { | 131 const sync_pb::ClientToServerMessage& msg) { |
| 72 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); | 132 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE); |
| 73 } | 133 } |
| 74 | 134 |
| 75 void TrafficRecorder::RecordClientToServerResponse( | 135 void TrafficRecorder::RecordClientToServerResponse( |
| 76 const sync_pb::ClientToServerResponse& response) { | 136 const sync_pb::ClientToServerResponse& response) { |
| 77 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); | 137 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE); |
| 78 } | 138 } |
| 79 | |
| 80 } // namespace browser_sync | 139 } // namespace browser_sync |
| 81 | 140 |
| OLD | NEW |