1 /*
2 * Copyright (C) 2023 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 "mdns_client_resume.h"
17
18 #include <condition_variable>
19 #include <mutex>
20 #include <unistd.h>
21 #include <thread>
22
23 #include "iservice_registry.h"
24 #include "netmgr_ext_log_wrapper.h"
25
26 #include "mdns_common.h"
27
28 namespace OHOS {
29 namespace NetManagerStandard {
Init()30 void MDnsClientResume::Init()
31 {
32 NETMGR_EXT_LOG_I("MDnsClientResume Init");
33 if (initFlag_) {
34 NETMGR_EXT_LOG_I("MDnsClientResume initialization is complete");
35 return;
36 }
37 initFlag_ = true;
38 }
39
GetInstance()40 MDnsClientResume &MDnsClientResume::GetInstance()
41 {
42 static MDnsClientResume singleInstance_;
43 static std::mutex mutex_;
44 std::unique_lock<std::mutex> lock(mutex_);
45 if (!singleInstance_.initFlag_) {
46 singleInstance_.Init();
47 }
48
49 return singleInstance_;
50 }
51
GetRegisterServiceMap()52 RegisterServiceMap *MDnsClientResume::GetRegisterServiceMap()
53 {
54 std::lock_guard<std::recursive_mutex> guard(registerMutex_);
55 return ®isterMap_;
56 }
57
GetStartDiscoverServiceMap()58 DiscoverServiceMap *MDnsClientResume::GetStartDiscoverServiceMap()
59 {
60 std::lock_guard<std::recursive_mutex> guard(discoveryMutex_);
61 return &discoveryMap_;
62 }
63
SaveRegisterService(const MDnsServiceInfo & serviceInfo,const sptr<IRegistrationCallback> & cb)64 int32_t MDnsClientResume::SaveRegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb)
65 {
66 NETMGR_EXT_LOG_D("registerMap_.emplace......");
67 std::lock_guard<std::recursive_mutex> guard(registerMutex_);
68 registerMap_.emplace(cb, serviceInfo);
69 NETMGR_EXT_LOG_D("registerMap_.emplace......[ok]");
70 return 0;
71 }
72
RemoveRegisterService(const sptr<IRegistrationCallback> & cb)73 int32_t MDnsClientResume::RemoveRegisterService(const sptr<IRegistrationCallback> &cb)
74 {
75 std::lock_guard<std::recursive_mutex> guard(registerMutex_);
76 auto itr = registerMap_.find(cb);
77 if (registerMap_.end() != itr) {
78 NETMGR_EXT_LOG_D("registerMap_.erase......");
79 registerMap_.erase(itr);
80 NETMGR_EXT_LOG_D("registerMap_.erase......[ok]");
81 }
82 return 0;
83 }
84
SaveStartDiscoverService(const std::string & serviceType,const sptr<IDiscoveryCallback> & cb)85 int32_t MDnsClientResume::SaveStartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb)
86 {
87 NETMGR_EXT_LOG_D("discoveryMap_.emplace......");
88 {
89 std::lock_guard<std::recursive_mutex> guard(discoveryMutex_);
90 if (discoveryMap_.find(cb) != discoveryMap_.end()) {
91 NETMGR_EXT_LOG_D("discoveryMap_.emplace......[ok]");
92 return 0;
93 }
94 discoveryMap_.emplace(cb, serviceType);
95 }
96 NETMGR_EXT_LOG_D("discoveryMap_.emplace......[ok]");
97
98 return 0;
99 }
100
RemoveStopDiscoverService(const sptr<IDiscoveryCallback> & cb)101 int32_t MDnsClientResume::RemoveStopDiscoverService(const sptr<IDiscoveryCallback> &cb)
102 {
103 NETMGR_EXT_LOG_D("discoveryMap_.erase......");
104
105 {
106 std::lock_guard<std::recursive_mutex> guard(discoveryMutex_);
107 auto itr = discoveryMap_.find(cb);
108 if (discoveryMap_.end() != itr) {
109 discoveryMap_.erase(itr);
110 }
111 }
112
113 NETMGR_EXT_LOG_D("discoveryMap_.erase......[ok]");
114
115 return 0;
116 }
117 } // namespace NetManagerStandard
118 } // namespace OHOS