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_RESUME_H
17 #define MDNS_CLIENT_RESUME_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "singleton.h"
23 
24 #include "i_mdns_event.h"
25 #include "mdns_service_info.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 struct MyCompareSmartPointer {
operatorMyCompareSmartPointer30     bool operator()(const sptr<IRemoteBroker> &lhs, const sptr<IRemoteBroker> &rhs) const
31     {
32         if ((lhs == nullptr) || (rhs == nullptr)) {
33             return false;
34         }
35 
36         return lhs->AsObject().GetRefPtr() < rhs->AsObject().GetRefPtr();
37     }
38 };
39 
40 typedef std::map<sptr<IRegistrationCallback>, MDnsServiceInfo, MyCompareSmartPointer> RegisterServiceMap;
41 typedef std::map<sptr<IDiscoveryCallback>, std::string, MyCompareSmartPointer> DiscoverServiceMap;
42 
43 class MDnsClientResume {
44 public:
45     ~MDnsClientResume() = default;
46     void Init();
47 
48     static MDnsClientResume &GetInstance();
49 
50     int32_t SaveRegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb);
51 
52     int32_t RemoveRegisterService(const sptr<IRegistrationCallback> &cb);
53 
54     int32_t SaveStartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb);
55 
56     int32_t RemoveStopDiscoverService(const sptr<IDiscoveryCallback> &cb);
57 
58     RegisterServiceMap *GetRegisterServiceMap();
59     DiscoverServiceMap *GetStartDiscoverServiceMap();
60 
61 private:
62     MDnsClientResume() = default;
63 
64     bool initFlag_ = false;
65 
66     RegisterServiceMap registerMap_;
67     DiscoverServiceMap discoveryMap_;
68 
69     std::recursive_mutex registerMutex_;
70     std::recursive_mutex discoveryMutex_;
71 };
72 } // namespace NetManagerStandard
73 } // namespace OHOS
74 #endif // MDNS_CLIENT_H
75