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 return ®isterMap_;
55 }
56
GetStartDiscoverServiceMap()57 DiscoverServiceMap *MDnsClientResume::GetStartDiscoverServiceMap()
58 {
59 return &discoveryMap_;
60 }
61
SaveRegisterService(const MDnsServiceInfo & serviceInfo,const sptr<IRegistrationCallback> & cb)62 int32_t MDnsClientResume::SaveRegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb)
63 {
64 NETMGR_EXT_LOG_D("registerMap_.emplace......");
65 std::lock_guard<std::recursive_mutex> guard(registerMutex_);
66 registerMap_.emplace(cb, serviceInfo);
67 NETMGR_EXT_LOG_D("registerMap_.emplace......[ok]");
68 return 0;
69 }
70
RemoveRegisterService(const sptr<IRegistrationCallback> & cb)71 int32_t MDnsClientResume::RemoveRegisterService(const sptr<IRegistrationCallback> &cb)
72 {
73 std::lock_guard<std::recursive_mutex> guard(registerMutex_);
74 auto itr = registerMap_.find(cb);
75 if (registerMap_.end() != itr) {
76 NETMGR_EXT_LOG_D("registerMap_.erase......");
77 registerMap_.erase(itr);
78 NETMGR_EXT_LOG_D("registerMap_.erase......[ok]");
79 }
80 return 0;
81 }
82
SaveStartDiscoverService(const std::string & serviceType,const sptr<IDiscoveryCallback> & cb)83 int32_t MDnsClientResume::SaveStartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb)
84 {
85 NETMGR_EXT_LOG_D("discoveryMap_.emplace......");
86 {
87 std::lock_guard<std::recursive_mutex> guard(discoveryMutex_);
88 if (discoveryMap_.find(cb) != discoveryMap_.end()) {
89 NETMGR_EXT_LOG_D("discoveryMap_.emplace......[ok]");
90 return 0;
91 }
92 discoveryMap_.emplace(cb, serviceType);
93 }
94 NETMGR_EXT_LOG_D("discoveryMap_.emplace......[ok]");
95
96 return 0;
97 }
98
RemoveStopDiscoverService(const sptr<IDiscoveryCallback> & cb)99 int32_t MDnsClientResume::RemoveStopDiscoverService(const sptr<IDiscoveryCallback> &cb)
100 {
101 NETMGR_EXT_LOG_D("discoveryMap_.erase......");
102
103 {
104 std::lock_guard<std::recursive_mutex> guard(discoveryMutex_);
105 auto itr = discoveryMap_.find(cb);
106 if (discoveryMap_.end() != itr) {
107 discoveryMap_.erase(itr);
108 }
109 }
110
111 NETMGR_EXT_LOG_D("discoveryMap_.erase......[ok]");
112
113 return 0;
114 }
115 } // namespace NetManagerStandard
116 } // namespace OHOS