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

Unified Diff: blimp/common/net/message_dispatcher.cc

Issue 1324263003: Blimp: create MessageDispatcher class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blimp-protos
Patch Set: Fix tryserver entries. Created 5 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: blimp/common/net/message_dispatcher.cc
diff --git a/blimp/common/net/message_dispatcher.cc b/blimp/common/net/message_dispatcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..14115736a2214c326f3b0f232dfab51d736ebd76
--- /dev/null
+++ b/blimp/common/net/message_dispatcher.cc
@@ -0,0 +1,55 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "blimp/common/net/message_dispatcher.h"
+
+#include <string>
+
+#include "base/strings/stringprintf.h"
+
+namespace blimp {
+namespace {
+
+std::string BlimpMessageToDebugString(const BlimpMessage& message) {
+ return base::StringPrintf("<message type=%d>", message.type());
+}
+
+} // namespace
+
+MessageDispatcher::MessageDispatcher() {}
+
+MessageDispatcher::~MessageDispatcher() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+}
+
+void MessageDispatcher::AddHandler(BlimpMessage::Type type, Handler* handler) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ if (feature_handler_map_.find(type) == feature_handler_map_.end()) {
+ feature_handler_map_.insert(std::make_pair(type, handler));
+ } else {
+ LOG(ERROR) << "Handler already registered for type=" << type << ".";
+ }
+}
+
+void MessageDispatcher::Dispatch(const BlimpMessage& message) const {
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ auto handler_iter = feature_handler_map_.find(message.type());
+ if (handler_iter == feature_handler_map_.end()) {
+ LOG(ERROR) << "No registered handler for "
+ << BlimpMessageToDebugString(message) << ".";
+ return;
+ }
+
+ DCHECK(handler_iter->second);
+ if (!handler_iter->second->Validate(message)) {
+ LOG(WARNING) << BlimpMessageToDebugString(message)
+ << " rejected by handler.";
+ return;
+ }
+
+ handler_iter->second->OnMessage(message);
+}
+
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698