• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
17 
18 #include "net_manager_constants.h"
19 #include "netmanager_ext_log.h"
20 
21 #include "constant.h"
22 #include "mdns_callback_observer.h"
23 #include "mdns_instances.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 
ErrorCodeTrans(int32_t retCode)28 static int32_t ErrorCodeTrans(int32_t retCode)
29 {
30     NETMANAGER_EXT_LOGE("ErrorCodeTrans init value %{public}d", retCode);
31     switch (retCode) {
32         case NET_MDNS_ERR_CALLBACK_DUPLICATED:
33             [[fallthrough]];
34         case NET_MDNS_ERR_CALLBACK_NOT_FOUND:
35             return static_cast<int32_t>(MDnsErr::ALREADY_ACTIVE);
36         default:
37             return static_cast<int32_t>(MDnsErr::INTERNAL_ERROR);
38     }
39 }
40 
HandleRegister(const MDnsServiceInfo & serviceInfo,int32_t retCode)41 int32_t MDnsRegistrationObserver::HandleRegister(const MDnsServiceInfo &serviceInfo, int32_t retCode)
42 {
43     return NETMANAGER_EXT_SUCCESS;
44 }
45 
HandleUnRegister(const MDnsServiceInfo & serviceInfo,int32_t retCode)46 int32_t MDnsRegistrationObserver::HandleUnRegister(const MDnsServiceInfo &serviceInfo, int32_t retCode)
47 {
48     return NETMANAGER_EXT_SUCCESS;
49 }
50 
HandleRegisterResult(const MDnsServiceInfo & serviceInfo,int32_t retCode)51 int32_t MDnsRegistrationObserver::HandleRegisterResult(const MDnsServiceInfo &serviceInfo, int32_t retCode)
52 {
53     return NETMANAGER_EXT_SUCCESS;
54 }
55 
HandleStartDiscover(const MDnsServiceInfo & serviceInfo,int32_t retCode)56 int32_t MDnsDiscoveryObserver::HandleStartDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode)
57 {
58     return NETMANAGER_EXT_SUCCESS;
59 }
60 
HandleStopDiscover(const MDnsServiceInfo & serviceInfo,int32_t retCode)61 int32_t MDnsDiscoveryObserver::HandleStopDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode)
62 {
63     return NETMANAGER_EXT_SUCCESS;
64 }
65 
EmitStartDiscover(const MDnsServiceInfo & serviceInfo,int32_t retCode)66 void MDnsDiscoveryObserver::EmitStartDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode)
67 {
68     MDnsDiscoveryInstance *mdnsDisdicover = MDnsDiscoveryInstance::discoverInstanceMap_[this];
69     if (mdnsDisdicover == nullptr || mdnsDisdicover->GetEventManager() == nullptr) {
70         NETMANAGER_EXT_LOGE("can not find MDnsDiscoveryInstance handle");
71         return;
72     }
73 
74     if (!mdnsDisdicover->GetEventManager()->HasEventListener(EVENT_SERVICESTART)) {
75         NETMANAGER_EXT_LOGE("no event listener find %{public}s", EVENT_SERVICESTART);
76         return;
77     }
78     if (retCode == 0) {
79         mdnsDisdicover->GetEventManager()->EmitByUv(EVENT_SERVICESTART, new MDnsServiceInfo(serviceInfo),
80                                                     ServiceCallback);
81         return;
82     }
83 
84     auto pair = new std::pair<int32_t, MDnsServiceInfo>(ErrorCodeTrans(retCode), serviceInfo);
85     mdnsDisdicover->GetEventManager()->EmitByUv(EVENT_SERVICESTART, pair, ServiceCallbackWithError);
86 }
87 
EmitStopDiscover(const MDnsServiceInfo & serviceInfo,int32_t retCode)88 void MDnsDiscoveryObserver::EmitStopDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode)
89 {
90     MDnsDiscoveryInstance *mdnsDisdicover = MDnsDiscoveryInstance::discoverInstanceMap_[this];
91     if (mdnsDisdicover == nullptr || mdnsDisdicover->GetEventManager() == nullptr) {
92         NETMANAGER_EXT_LOGE("can not find MDnsDiscoveryInstance handle");
93         return;
94     }
95 
96     if (!mdnsDisdicover->GetEventManager()->HasEventListener(EVENT_SERVICESTOP)) {
97         NETMANAGER_EXT_LOGE("no event listener find %{public}s", EVENT_SERVICESTOP);
98         return;
99     }
100     if (retCode == 0) {
101         mdnsDisdicover->GetEventManager()->EmitByUv(EVENT_SERVICESTOP, new MDnsServiceInfo(serviceInfo),
102                                                     ServiceCallback);
103         return;
104     }
105     auto pair = new std::pair<int32_t, MDnsServiceInfo>(ErrorCodeTrans(retCode), serviceInfo);
106     mdnsDisdicover->GetEventManager()->EmitByUv(EVENT_SERVICESTOP, pair, ServiceCallbackWithError);
107 }
108 
HandleServiceFound(const MDnsServiceInfo & serviceInfo,int32_t retCode)109 int32_t MDnsDiscoveryObserver::HandleServiceFound(const MDnsServiceInfo &serviceInfo, int32_t retCode)
110 {
111     MDnsDiscoveryInstance *mdnsDisdicover = MDnsDiscoveryInstance::discoverInstanceMap_[this];
112     if (mdnsDisdicover == nullptr || mdnsDisdicover->GetEventManager() == nullptr) {
113         NETMANAGER_EXT_LOGE("can not find MDnsDiscoveryInstance handle");
114         return NETMANAGER_EXT_ERR_INTERNAL;
115     }
116 
117     if (!mdnsDisdicover->GetEventManager()->HasEventListener(EVENT_SERVICEFOUND)) {
118         NETMANAGER_EXT_LOGE("no event listener find %{public}s", EVENT_SERVICEFOUND);
119         return NETMANAGER_EXT_ERR_INTERNAL;
120     }
121 
122     mdnsDisdicover->GetEventManager()->EmitByUv(EVENT_SERVICEFOUND, new MDnsServiceInfo(serviceInfo),
123                                                 ServiceCallback);
124     return NETMANAGER_EXT_SUCCESS;
125 }
126 
HandleServiceLost(const MDnsServiceInfo & serviceInfo,int32_t retCode)127 int32_t MDnsDiscoveryObserver::HandleServiceLost(const MDnsServiceInfo &serviceInfo, int32_t retCode)
128 {
129     MDnsDiscoveryInstance *mdnsDisdicover = MDnsDiscoveryInstance::discoverInstanceMap_[this];
130     if (mdnsDisdicover == nullptr || mdnsDisdicover->GetEventManager() == nullptr) {
131         NETMANAGER_EXT_LOGE("can not find MDnsDiscoveryInstance handle");
132         return NETMANAGER_EXT_ERR_INTERNAL;
133     }
134 
135     if (!mdnsDisdicover->GetEventManager()->HasEventListener(EVENT_SERVICELOST)) {
136         NETMANAGER_EXT_LOGE("no event listener find %{public}s", EVENT_SERVICELOST);
137         return NETMANAGER_EXT_ERR_INTERNAL;
138     }
139 
140     mdnsDisdicover->GetEventManager()->EmitByUv(EVENT_SERVICELOST, new MDnsServiceInfo(serviceInfo),
141                                                 ServiceCallback);
142     return NETMANAGER_EXT_SUCCESS;
143 }
144 
CreateCallbackParam(const MDnsServiceInfo & serviceInfo,napi_env env)145 napi_value CreateCallbackParam(const MDnsServiceInfo &serviceInfo, napi_env env)
146 {
147     napi_value object = NapiUtils::CreateObject(env);
148     NapiUtils::SetStringPropertyUtf8(env, object, SERVICEINFO_TYPE, serviceInfo.type);
149     NapiUtils::SetStringPropertyUtf8(env, object, SERVICEINFO_NAME, serviceInfo.name);
150     NapiUtils::SetInt32Property(env, object, SERVICEINFO_PORT, serviceInfo.port);
151 
152     napi_value eleObj = NapiUtils::CreateObject(env);
153     NapiUtils::SetStringPropertyUtf8(env, eleObj, SERVICEINFO_ADDRESS, serviceInfo.addr);
154     NapiUtils::SetInt32Property(env, eleObj, SERVICEINFO_PORT, serviceInfo.port);
155     int32_t family = serviceInfo.family == MDnsServiceInfo::IPV6 ? 0 : 1;
156     NapiUtils::SetInt32Property(env, eleObj, SERVICEINFO_FAMILY, family);
157 
158     NapiUtils::SetNamedProperty(env, object, SERVICEINFO_HOST, eleObj);
159     return object;
160 }
161 
CreateServiceWithError(napi_env env,void * data)162 napi_value MDnsDiscoveryObserver::CreateServiceWithError(napi_env env, void *data)
163 {
164     auto pair = static_cast<std::pair<int32_t, MDnsServiceInfo> *>(data);
165     napi_value obj = NapiUtils::CreateObject(env);
166     NapiUtils::SetUint32Property(env, obj, ERRCODE, pair->first);
167     napi_value infoObj = CreateCallbackParam(pair->second, env);
168     NapiUtils::SetNamedProperty(env, obj, SERVICEINFO, infoObj);
169     delete pair;
170     return obj;
171 }
172 
ServiceCallbackWithError(uv_work_t * work,int32_t status)173 void MDnsDiscoveryObserver::ServiceCallbackWithError(uv_work_t *work, int32_t status)
174 {
175     CallbackTemplate<CreateServiceWithError>(work, status);
176 }
177 
CreateService(napi_env env,void * data)178 napi_value MDnsDiscoveryObserver::CreateService(napi_env env, void *data)
179 {
180     auto serviceInfo = static_cast<MDnsServiceInfo *>(data);
181     napi_value obj = CreateCallbackParam(*serviceInfo, env);
182     napi_value infoObj = CreateCallbackParam(*serviceInfo, env);
183     NapiUtils::SetNamedProperty(env, obj, SERVICEINFO, infoObj);
184     delete serviceInfo;
185     return obj;
186 }
187 
ServiceCallback(uv_work_t * work,int32_t status)188 void MDnsDiscoveryObserver::ServiceCallback(uv_work_t *work, int32_t status)
189 {
190     CallbackTemplate<CreateService>(work, status);
191 }
192 
HandleResolveResult(const MDnsServiceInfo & serviceInfo,int32_t retCode)193 int32_t MDnsResolveObserver::HandleResolveResult(const MDnsServiceInfo &serviceInfo, int32_t retCode)
194 {
195     NETMANAGER_EXT_LOGI("HandleResolveResult [%{public}s][%{public}s][%{public}d]", serviceInfo.name.c_str(),
196                         serviceInfo.type.c_str(), serviceInfo.port);
197     mutex_.lock();
198     retCode_ = retCode;
199     serviceInfo_ = serviceInfo;
200     resolved_ = true;
201     mutex_.unlock();
202     cv_.notify_one();
203     return NETMANAGER_EXT_SUCCESS;
204 }
205 } // namespace NetManagerStandard
206 } // namespace OHOS
207