1 /*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "publisher.h"
17
18 #include "distributed_hardware_log.h"
19
20 namespace OHOS {
21 namespace DistributedHardware {
22 IMPLEMENT_SINGLE_INSTANCE(Publisher);
Publisher()23 Publisher::Publisher() : publisherItems_({
24 { DHTopic::TOPIC_START_DSCREEN, std::make_shared<PublisherItem>(DHTopic::TOPIC_START_DSCREEN) },
25 { DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO,
26 std::make_shared<PublisherItem>(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO) },
27 { DHTopic::TOPIC_STOP_DSCREEN, std::make_shared<PublisherItem>(DHTopic::TOPIC_STOP_DSCREEN) },
28 { DHTopic::TOPIC_DEV_OFFLINE, std::make_shared<PublisherItem>(DHTopic::TOPIC_DEV_OFFLINE) },
29 { DHTopic::TOPIC_LOW_LATENCY, std::make_shared<PublisherItem>(DHTopic::TOPIC_LOW_LATENCY) },
30 { DHTopic::TOPIC_INIT_DHMS_READY, std::make_shared<PublisherItem>(DHTopic::TOPIC_INIT_DHMS_READY) },
31 { DHTopic::TOPIC_PHY_DEV_PLUGIN, std::make_shared<PublisherItem>(DHTopic::TOPIC_PHY_DEV_PLUGIN) },
32 { DHTopic::TOPIC_ISOMERISM, std::make_shared<PublisherItem>(DHTopic::TOPIC_ISOMERISM) }
33 })
34 {
35 }
36
~Publisher()37 Publisher::~Publisher()
38 {
39 }
40
RegisterListener(const DHTopic topic,const sptr<IPublisherListener> listener)41 void Publisher::RegisterListener(const DHTopic topic, const sptr<IPublisherListener> listener)
42 {
43 if (!IsTopicExist(topic)) {
44 return;
45 }
46 publisherItems_[topic]->AddListener(listener);
47 }
48
UnregisterListener(const DHTopic topic,const sptr<IPublisherListener> listener)49 void Publisher::UnregisterListener(const DHTopic topic, const sptr<IPublisherListener> listener)
50 {
51 if (!IsTopicExist(topic)) {
52 return;
53 }
54 publisherItems_[topic]->RemoveListener(listener);
55 }
56
PublishMessage(const DHTopic topic,const std::string & message)57 void Publisher::PublishMessage(const DHTopic topic, const std::string &message)
58 {
59 if (!IsTopicExist(topic)) {
60 return;
61 }
62 publisherItems_[topic]->PublishMessage(message);
63 }
64
IsTopicExist(const DHTopic topic)65 bool Publisher::IsTopicExist(const DHTopic topic)
66 {
67 if (publisherItems_.find(topic) == publisherItems_.end()) {
68 DHLOGE("The topic: %{public}u is not exist.", static_cast<uint32_t>(topic));
69 return false;
70 }
71 return true;
72 }
73 } // namespace DistributedHardware
74 } // namespace OHOS