1 // Copyright 2019 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 "cast/common/channel/namespace_router.h"
6 
7 #include "cast/common/channel/proto/cast_channel.pb.h"
8 
9 namespace openscreen {
10 namespace cast {
11 
12 NamespaceRouter::NamespaceRouter() = default;
13 NamespaceRouter::~NamespaceRouter() = default;
14 
AddNamespaceHandler(std::string namespace_,CastMessageHandler * handler)15 void NamespaceRouter::AddNamespaceHandler(std::string namespace_,
16                                           CastMessageHandler* handler) {
17   handlers_.emplace(std::move(namespace_), handler);
18 }
19 
RemoveNamespaceHandler(const std::string & namespace_)20 void NamespaceRouter::RemoveNamespaceHandler(const std::string& namespace_) {
21   handlers_.erase(namespace_);
22 }
23 
OnMessage(VirtualConnectionRouter * router,CastSocket * socket,::cast::channel::CastMessage message)24 void NamespaceRouter::OnMessage(VirtualConnectionRouter* router,
25                                 CastSocket* socket,
26                                 ::cast::channel::CastMessage message) {
27   const std::string& ns = message.namespace_();
28   auto it = handlers_.find(ns);
29   if (it != handlers_.end()) {
30     it->second->OnMessage(router, socket, std::move(message));
31   }
32 }
33 
34 }  // namespace cast
35 }  // namespace openscreen
36