• 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_SERVICE_MATRIX_DEVICE_MATRIX_H
16 #define OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_DEVICE_MATRIX_H
17 #include <map>
18 #include <mutex>
19 #include "eventcenter/event.h"
20 #include "lru_bucket.h"
21 #include "metadata/matrix_meta_data.h"
22 #include "metadata/store_meta_data.h"
23 #include "utils/ref_count.h"
24 #include "visibility.h"
25 namespace OHOS::DistributedData {
26 class API_EXPORT DeviceMatrix {
27 public:
28     using TimePoint = std::chrono::steady_clock::time_point;
29     static constexpr uint16_t META_STORE_MASK = 0x0001;
30     enum : int32_t {
31         MATRIX_ONLINE = Event::EVT_CUSTOM,
32         MATRIX_META_FINISHED,
33         MATRIX_BROADCAST,
34         MATRIX_BUTT
35     };
36     static DeviceMatrix &GetInstance();
37     bool Initialize(uint32_t token, std::string storeId);
38     void Online(const std::string &device, RefCount refCount = {});
39     void Offline(const std::string &device);
40     uint16_t OnBroadcast(const std::string &device, uint16_t code);
41     void OnChanged(uint16_t code);
42     void OnChanged(const StoreMetaData &metaData);
43     void OnExchanged(const std::string &device, uint16_t code, bool isRemote = false);
44     bool IsChangedInTerm(const StoreMetaData &metaData, uint64_t term);
45     uint16_t GetCode(const StoreMetaData &metaData);
46     std::pair<bool, uint16_t> GetMask(const std::string &device);
47     std::pair<bool, uint16_t> GetRemoteMask(const std::string &device);
48     MatrixMetaData GetMatrixMeta(const std::string &device);
49     void Clear();
50 
51 private:
52     static constexpr uint32_t CURRENT_VERSION = 2;
53     static constexpr uint32_t CURRENT_MASK = 0x000E;
54     static constexpr size_t MAX_DEVICES = 16;
55 
56     DeviceMatrix();
57     ~DeviceMatrix();
58     DeviceMatrix(const DeviceMatrix &) = delete;
59     DeviceMatrix(DeviceMatrix &&) noexcept = delete;
60     DeviceMatrix &operator=(const DeviceMatrix &) = delete;
61     DeviceMatrix &operator=(DeviceMatrix &&) noexcept = delete;
SetMask(size_t index)62     static inline uint16_t SetMask(size_t index)
63     {
64         return 0x1 << (index + 1);
65     }
66 
67     uint16_t ConvertMask(const std::string &device, uint16_t code);
68 
69     struct Mask {
70         uint16_t bitset = META_STORE_MASK | CURRENT_MASK;
71     };
72 
73     uint32_t tokenId_ = 0;
74     std::string storeId_;
75     std::mutex mutex_;
76     std::map<std::string, Mask> onLines_;
77     std::map<std::string, Mask> offLines_;
78     std::map<std::string, Mask> remotes_;
79     std::vector<std::string> maskApps_ = { "distributed_device_profile_service", "bundle_manager_service",
80         "dtbhardware_manager_service" };
81     LRUBucket<std::string, MatrixMetaData> versions_{ MAX_DEVICES };
82     LRUBucket<uint32_t, TimePoint> changeTime_ { 64 };
83 };
84 } // namespace OHOS::DistributedData
85 #endif // OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_DEVICE_MATRIX_H
86