OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "blimp/common/net/message_dispatcher.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/strings/stringprintf.h" | |
10 | |
11 namespace blimp { | |
12 namespace { | |
13 | |
14 std::string BlimpMessageToDebugString(const BlimpMessage& message) { | |
15 return base::StringPrintf("<message type=%d>", message.type()); | |
16 } | |
17 | |
18 } // namespace | |
19 | |
20 MessageDispatcher::MessageDispatcher() {} | |
21 | |
22 MessageDispatcher::~MessageDispatcher() { | |
23 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!
| |
24 } | |
25 | |
26 void MessageDispatcher::AddHandler(BlimpMessage::Type type, Handler* handler) { | |
27 DCHECK(thread_checker_.CalledOnValidThread()); | |
28 if (feature_handler_map_.find(type) == feature_handler_map_.end()) { | |
29 feature_handler_map_.insert(std::make_pair(type, handler)); | |
30 } else { | |
31 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.
| |
32 } | |
33 } | |
34 | |
35 void MessageDispatcher::Dispatch(const BlimpMessage& message) const { | |
36 DCHECK(thread_checker_.CalledOnValidThread()); | |
37 | |
38 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
| |
39 if (handler_iter == feature_handler_map_.end()) { | |
40 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
| |
41 << BlimpMessageToDebugString(message) << "."; | |
42 return; | |
43 } | |
44 | |
45 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
| |
46 if (!handler_iter->second->Validate(message)) { | |
47 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.
| |
48 << " rejected by handler."; | |
49 return; | |
50 } | |
51 | |
52 handler_iter->second->OnMessage(message); | |
53 } | |
54 | |
55 } // namespace blimp | |
OLD | NEW |