Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: sync/engine/traffic_recorder.cc

Issue 9732008: [Sync] Store the past 10 traffic records in memory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: For review. Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "sync/engine/traffic_recorder.h"
6
7 #include "base/json/json_writer.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/values.h"
11 #include "sync/protocol/proto_value_conversions.h"
12 #include "sync/protocol/sync.pb.h"
13 #include "sync/sessions/sync_session.h"
14
15 namespace browser_sync {
16
17 TrafficRecorder::TrafficRecord::TrafficRecord(const std::string& message,
18 TrafficMessageType message_type,
19 bool truncated) :
20 message(message),
21 message_type(message_type),
22 truncated(truncated) {
23 }
24
25 TrafficRecorder::TrafficRecord::TrafficRecord()
26 : message_type(UNKNOWN_MESSAGE_TYPE),
27 truncated(false) {
28 }
29
30 TrafficRecorder::TrafficRecord::~TrafficRecord() {
31 }
32
33 TrafficRecorder::TrafficRecorder(unsigned int max_messages,
34 unsigned int max_message_size)
35 : max_messages_(max_messages),
36 max_message_size_(max_message_size) {
37 }
38
39 TrafficRecorder::~TrafficRecorder() {
40 }
41
42
43 void TrafficRecorder::AddTrafficToQueue(const TrafficRecord& record) {
44 records_.push_back(record);
45
46 // We might have more records than our limit.
47 // Maintain the size invariant by deleting items.
48 while (records_.size() > max_messages_) {
49 records_.pop_front();
50 }
51 }
52
53 void TrafficRecorder::StoreProtoInQueue(
54 const ::google::protobuf::MessageLite& msg,
55 TrafficMessageType type) {
56 bool truncated = false;
57 std::string message;
58 if ((unsigned int)msg.ByteSize() >= max_message_size_) {
akalin 2012/03/29 18:54:06 static_cast<unsigned int>
59 // TODO(lipalani): Trim the specifics to fit in size.
60 truncated = true;
61 } else {
62 msg.SerializeToString(&message);
63 }
64
65 TrafficRecord record(message, type, truncated);
rlarocque 2012/03/29 17:56:21 Unless the compiler is really smart, this will lik
lipalani1 2012/03/29 18:39:29 this is only one extra copy. We dont copy it durin
akalin 2012/03/29 18:54:06 No, smart pointers isn't the only way. Here's an
rlarocque 2012/03/29 21:38:56 That's a clever solution, but I think it requires
66 AddTrafficToQueue(record);
67 }
68
69 void TrafficRecorder::RecordClientToServerMessage(
70 const sync_pb::ClientToServerMessage& msg) {
71 StoreProtoInQueue(msg, CLIENT_TO_SERVER_MESSAGE);
72 }
73
74 void TrafficRecorder::RecordClientToServerResponse(
75 const sync_pb::ClientToServerResponse& response) {
76 StoreProtoInQueue(response, CLIENT_TO_SERVER_RESPONSE);
77 }
78
79 } // namespace browser_sync
80
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698