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 #ifndef MDNS_CLIENT_H 17 #define MDNS_CLIENT_H 18 19 #include <map> 20 #include <string> 21 22 #include "parcel.h" 23 #include "singleton.h" 24 #include "system_ability_load_callback_stub.h" 25 26 #include "i_mdns_event.h" 27 #include "i_mdns_service.h" 28 #include "mdns_service_info.h" 29 30 namespace OHOS { 31 namespace NetManagerStandard { 32 class OnDemandLoadCallback : public SystemAbilityLoadCallbackStub { 33 public: 34 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override; 35 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 36 const sptr<IRemoteObject> &GetRemoteObject() const; 37 38 private: 39 sptr<IRemoteObject> remoteObject_ = nullptr; 40 }; 41 42 class MDnsClient { 43 DECLARE_DELAYED_SINGLETON(MDnsClient); 44 45 public: 46 int32_t RegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb); 47 int32_t UnRegisterService(const sptr<IRegistrationCallback> &cb); 48 49 int32_t StartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb); 50 int32_t StopDiscoverService(const sptr<IDiscoveryCallback> &cb); 51 52 int32_t ResolveService(const MDnsServiceInfo &serviceInfo, const sptr<IResolveCallback> &cb); 53 54 private: 55 class MDnsDeathRecipient : public IRemoteObject::DeathRecipient { 56 public: MDnsDeathRecipient(MDnsClient & client)57 explicit MDnsDeathRecipient(MDnsClient &client) : client_(client) {} 58 ~MDnsDeathRecipient() = default; OnRemoteDied(const wptr<IRemoteObject> & remote)59 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 60 { 61 client_.OnRemoteDied(remote); 62 } 63 64 private: 65 MDnsClient &client_; 66 }; 67 68 sptr<IRemoteObject> LoadSaOnDemand(); 69 sptr<IMDnsService> GetProxy(); 70 void OnRemoteDied(const wptr<IRemoteObject> &remote); 71 72 std::mutex mutex_; 73 sptr<IMDnsService> mdnsService_ = nullptr; 74 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 75 sptr<OnDemandLoadCallback> loadCallback_ = nullptr; 76 }; 77 } // namespace NetManagerStandard 78 } // namespace OHOS 79 #endif // MDNS_CLIENT_H 80