• 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 #ifndef OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_DEV_MANAGER_H
16 #define OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_DEV_MANAGER_H
17 #include <string>
18 #include "concurrent_map.h"
19 #include "types.h"
20 #include "lru_bucket.h"
21 namespace OHOS::DistributedKv {
22 class DevManager {
23 public:
24     static constexpr size_t MAX_ID_LEN = 64;
25     struct DetailInfo {
26         std::string uuid;
27         std::string networkId;
28         std::string deviceName;
29         std::string deviceType;
30     };
31     static DevManager &GetInstance();
32     std::string ToUUID(const std::string &networkId);
33     std::string ToNetworkId(const std::string &uuid);
34     const DetailInfo &GetLocalDevice();
35     std::vector<DetailInfo> GetRemoteDevices();
36     class Observer {
37     public:
38         Observer() = default;
~Observer()39         virtual ~Observer() {}
40         virtual void Online(const std::string &networkId) = 0;
41         virtual void Offline(const std::string &networkId) = 0;
42     };
43 
44     void Register(Observer *observer);
45     void Unregister(Observer *observer);
46 
47 private:
48     friend class DMStateCallback;
49     friend class DmDeathCallback;
50     DevManager(const std::string &pkgName);
51     ~DevManager() = default;
52     void Online(const std::string &networkId);
53     void Offline(const std::string &networkId);
54     void OnChanged(const std::string &networkId);
55     void OnReady(const std::string &networkId);
56     void RegisterDevCallback();
57     void UpdateBucket();
58     DetailInfo GetDvInfoFromBucket(const std::string &id);
59 
60     int32_t Init();
61     std::function<void()> Retry();
62     const std::string PKG_NAME;
63     const DetailInfo invalidDetail_ {};
64     DetailInfo localInfo_ {};
65     mutable std::mutex mutex_ {};
66     mutable LRUBucket<std::string, DetailInfo> deviceInfos_ {64};
67     ConcurrentMap<Observer *, Observer *> observers_;
68 };
69 } // namespace OHOS::DistributedKv
70 #endif // OHOS_DISTRIBUTED_DATA_FRAMEWORKS_KVDB_DEV_MANAGER_H
71