• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 INCLUDE_DNSRESOLV_CONFIG_H
17 #define INCLUDE_DNSRESOLV_CONFIG_H
18 
19 #include <atomic>
20 #include <list>
21 #include <memory>
22 #include <vector>
23 
24 #include "delayed_queue.h"
25 #include "dns_config_client.h"
26 #include "lru_cache.h"
27 
28 namespace OHOS::nmd {
29 static constexpr size_t DEFAULT_DELAYED_COUNT = 30;
30 
31 class DnsResolvConfig {
32 public:
33     DnsResolvConfig();
34 
35     void SetNetId(uint16_t netId);
36     void SetTimeoutMsec(int32_t baseTimeoutMsec);
37     void SetRetryCount(uint8_t retryCount);
38     void SetServers(const std::vector<std::string> &servers);
39     void SetDomains(const std::vector<std::string> &domains);
40 
41     uint16_t GetNetId() const;
42     uint16_t GetTimeoutMsec() const;
43     std::vector<std::string> GetServers() const;
44     std::vector<std::string> GetDomains() const;
45     uint8_t GetRetryCount() const;
46 
47     NetManagerStandard::LRUCache<AddrInfo> &GetCache();
48 
49     void SetCacheDelayed(const std::string &hostName);
50 
51 private:
52     class DelayedTaskWrapper {
53     public:
54         DelayedTaskWrapper(std::string hostName, NetManagerStandard::LRUCache<AddrInfo> &cache);
55 
56         void Execute() const;
57 
58         bool operator<(const DelayedTaskWrapper &other) const;
59 
60     private:
61         std::string hostName_;
62 
63         NetManagerStandard::LRUCache<AddrInfo> &cache_;
64     };
65 
66     uint16_t netId_;
67     std::atomic_bool netIdIsSet_;
68     int32_t revisionId_;
69     int32_t timeoutMsec_;
70     uint8_t retryCount_;
71     std::list<std::shared_ptr<DelayedTaskWrapper>> delayedTaskWrapperList_;
72     std::vector<std::string> nameServers_;
73     std::vector<std::string> searchDomains_;
74     NetManagerStandard::LRUCache<AddrInfo> cache_;
75     NetManagerStandard::DelayedQueue<DelayedTaskWrapper, NetManagerStandard::DEFAULT_CAPABILITY, DEFAULT_DELAYED_COUNT>
76         delayedQueue_;
77 };
78 } // namespace OHOS::nmd
79 #endif // INCLUDE_DNSRESOLV_CONFIG_H
80