Index: blimp/common/net/message_dispatcher.h |
diff --git a/blimp/common/net/message_dispatcher.h b/blimp/common/net/message_dispatcher.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6f11a6f8092dd643b9fc518dc17e7ace0d1d128f |
--- /dev/null |
+++ b/blimp/common/net/message_dispatcher.h |
@@ -0,0 +1,56 @@ |
+// 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. |
+ |
+#ifndef BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_ |
+#define BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_ |
+ |
+#include <map> |
+ |
+#include "base/threading/thread_checker.h" |
+#include "blimp/common/blimp_common_export.h" |
+#include "blimp/common/proto/blimp_message.pb.h" |
+ |
+namespace blimp { |
+ |
+// Routes BlimpMessages to feature-specific handlers according to the messages' |
+// types. |
+class BLIMP_COMMON_EXPORT MessageDispatcher { |
+ public: |
+ class Handler { |
+ public: |
+ // Returns true if |this| is capable of handling |message|. |
+ virtual bool Validate(const BlimpMessage& message) const = 0; |
+ |
+ // Receives |message| for processing. |
+ // Validate() is guaranteed to be called prior to this call to OnMessage(). |
+ virtual void OnMessage(const BlimpMessage& message) = 0; |
+ }; |
+ |
+ MessageDispatcher(); |
+ ~MessageDispatcher(); |
+ |
+ // Registers a message dispatcher handler which will process all messages |
+ // with the type |type|. Only one handler may be added per type. |
+ // |
+ // The caller must ensure that the lifetime of |handler| exceeds the lifetime |
+ // of |this|. |
+ void AddHandler(BlimpMessage::Type type, Handler* handler); |
+ |
+ // Takes a BlimpMessage and dispatches it to the appropriate feature-specific |
+ // handler, based on the value of |message.type|. |
+ // |
+ // Logs an error if there is no Handler registered for |message.type|, or if |
+ // the Handler failed to validate the message. |
+ void Dispatch(const BlimpMessage& message) const; |
+ |
+ private: |
+ std::map<BlimpMessage::Type, Handler*> feature_handler_map_; |
+ base::ThreadChecker thread_checker_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MessageDispatcher); |
+}; |
+ |
+} // namespace blimp |
+ |
+#endif // BLIMP_COMMON_NET_MESSAGE_DISPATCHER_H_ |