Chromium Code Reviews| 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()); |
|
Wez
2015/10/02 01:10:29
nit: Doesn't thread-checker do this for you, and b
Kevin M
2015/10/02 22:02:00
I guess it would, neato!
|
| +} |
| + |
| +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 << "."; |
|
Wez
2015/10/02 01:10:29
Replace this with a DCHECK; it's a coding-time err
Kevin M
2015/10/02 22:02:00
Done.
|
| + } |
| +} |
| + |
| +void MessageDispatcher::Dispatch(const BlimpMessage& message) const { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + |
| + auto handler_iter = feature_handler_map_.find(message.type()); |
|
Wez
2015/10/02 01:12:09
Note that for the small numbers of message-types w
Kevin M
2015/10/02 22:02:00
How about this, SmallMap? Linear comparisons acros
|
| + if (handler_iter == feature_handler_map_.end()) { |
| + LOG(ERROR) << "No registered handler for " |
|
Wez
2015/10/02 01:10:29
There's no real point logging this - who will see
Kevin M
2015/10/02 22:02:00
OK. Added a bool return value so the session/chann
|
| + << BlimpMessageToDebugString(message) << "."; |
| + return; |
| + } |
| + |
| + DCHECK(handler_iter->second); |
|
Wez
2015/10/02 01:10:29
This DCHECK should be on |handler| when it's passe
Kevin M
2015/10/02 22:02:00
Done
|
| + if (!handler_iter->second->Validate(message)) { |
| + LOG(WARNING) << BlimpMessageToDebugString(message) |
|
Wez
2015/10/02 01:10:29
See above re logging vs DCHECK & disconnect.
Kevin M
2015/10/02 22:02:00
Done.
|
| + << " rejected by handler."; |
| + return; |
| + } |
| + |
| + handler_iter->second->OnMessage(message); |
| +} |
| + |
| +} // namespace blimp |