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 #include "mdns_client.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 30;
32 constexpr uint32_t WAIT_FOR_SERVICE_TIME_S = 1;
Init()33 void MDnsClientResume::Init()
34 {
35     NETMGR_EXT_LOG_I("MDnsClientResume Init");
36     if (initFlag_) {
37         NETMGR_EXT_LOG_I("MDnsClientResume initialization is complete");
38         return;
39     }
40     initFlag_ = true;
41 }
42 
GetInstance()43 MDnsClientResume &MDnsClientResume::GetInstance()
44 {
45     static MDnsClientResume singleInstance_;
46     static std::mutex mutex_;
47     std::unique_lock<std::mutex> lock(mutex_);
48     if (!singleInstance_.initFlag_) {
49         singleInstance_.Init();
50     }
51 
52     return singleInstance_;
53 }
54 
ReRegisterService()55 void MDnsClientResume::ReRegisterService()
56 {
57     std::lock_guard<std::recursive_mutex> guard(registerMutex_);
58     for (const auto& [key, value]: registerMap_) {
59         auto ret = DelayedSingleton<MDnsClient>::GetInstance()->RegisterService(value, key);
60         if (ret != NETMANAGER_EXT_SUCCESS) {
61             NETMGR_EXT_LOG_E("ReRegisterService error, service name is %{public}s", value.name.c_str());
62         }
63     }
64 }
65 
RestartDiscoverService()66 void MDnsClientResume::RestartDiscoverService()
67 {
68     std::lock_guard<std::recursive_mutex> guard(discoveryMutex_);
69     for (const auto& [key, value]: discoveryMap_) {
70         uint32_t count = 0;
71         while (DelayedSingleton<MDnsClient>::GetInstance()->GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
72             std::this_thread::sleep_for(std::chrono::seconds(WAIT_FOR_SERVICE_TIME_S));
73             count++;
74         }
75         auto proxy = DelayedSingleton<MDnsClient>::GetInstance()->GetProxy();
76         NETMGR_EXT_LOG_W("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
77         if (proxy != nullptr) {
78             auto ret = DelayedSingleton<MDnsClient>::GetInstance()->StartDiscoverService(value, key);
79             if (ret != NETMANAGER_EXT_SUCCESS) {
80                 NETMGR_EXT_LOG_E("RestartDiscoverService error, errorCode: %{public}d", ret);
81             }
82         }
83     }
84 }
85 
SaveRegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb)86 int32_t MDnsClientResume::SaveRegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb)
87 {
88     NETMGR_EXT_LOG_D("registerMap_.emplace......");
89     std::lock_guard<std::recursive_mutex> guard(registerMutex_);
90     registerMap_.emplace(cb, serviceInfo);
91     NETMGR_EXT_LOG_D("registerMap_.emplace......[ok]");
92     return 0;
93 }
94 
RemoveRegisterService(const sptr<IRegistrationCallback> &cb)95 int32_t MDnsClientResume::RemoveRegisterService(const sptr<IRegistrationCallback> &cb)
96 {
97     std::lock_guard<std::recursive_mutex> guard(registerMutex_);
98     auto itr = registerMap_.find(cb);
99     if (registerMap_.end() != itr) {
100         NETMGR_EXT_LOG_D("registerMap_.erase......");
101         registerMap_.erase(itr);
102         NETMGR_EXT_LOG_D("registerMap_.erase......[ok]");
103     }
104     return 0;
105 }
106 
SaveStartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb)107 int32_t MDnsClientResume::SaveStartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb)
108 {
109     NETMGR_EXT_LOG_D("discoveryMap_.emplace......");
110     {
111         std::lock_guard<std::recursive_mutex> guard(discoveryMutex_);
112         if (discoveryMap_.find(cb) != discoveryMap_.end()) {
113             NETMGR_EXT_LOG_D("discoveryMap_.emplace......[ok]");
114             return 0;
115         }
116         discoveryMap_.emplace(cb, serviceType);
117     }
118     NETMGR_EXT_LOG_D("discoveryMap_.emplace......[ok]");
119 
120     return 0;
121 }
122 
RemoveStopDiscoverService(const sptr<IDiscoveryCallback> &cb)123 int32_t MDnsClientResume::RemoveStopDiscoverService(const sptr<IDiscoveryCallback> &cb)
124 {
125     NETMGR_EXT_LOG_D("discoveryMap_.erase......");
126 
127     {
128         std::lock_guard<std::recursive_mutex> guard(discoveryMutex_);
129         auto itr = discoveryMap_.find(cb);
130         if (discoveryMap_.end() != itr) {
131             discoveryMap_.erase(itr);
132         }
133     }
134 
135     NETMGR_EXT_LOG_D("discoveryMap_.erase......[ok]");
136 
137     return 0;
138 }
139 } // namespace NetManagerStandard
140 } // namespace OHOS