• 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     void Clear();
47 
48 private:
49     static constexpr uint32_t CURRENT_VERSION = 1;
50     static constexpr uint32_t CURRENT_MASK = 0x0002;
51     static constexpr size_t MAX_DEVICES = 16;
52 
53     DeviceMatrix();
54     ~DeviceMatrix();
55     DeviceMatrix(const DeviceMatrix &) = delete;
56     DeviceMatrix(DeviceMatrix &&) noexcept = delete;
57     DeviceMatrix &operator=(const DeviceMatrix &) = delete;
58     DeviceMatrix &operator=(DeviceMatrix &&) noexcept = delete;
SetMask(size_t index)59     static inline uint16_t SetMask(size_t index)
60     {
61         return 0x1 << (index + 1);
62     }
63 
64     uint16_t ConvertMask(const std::string &device, uint16_t code);
65     MatrixMetaData GetMatrixMeta(const std::string &device);
66 
67     struct Mask {
68         uint16_t bitset = META_STORE_MASK | CURRENT_MASK;
69     };
70 
71     uint32_t tokenId_ = 0;
72     std::string storeId_;
73     std::mutex mutex_;
74     std::map<std::string, Mask> onLines_;
75     std::map<std::string, Mask> offLines_;
76     std::map<std::string, Mask> remotes_;
77     std::vector<std::string> maskApps_ = { "distributed_device_profile_service" };
78     LRUBucket<std::string, MatrixMetaData> versions_{ MAX_DEVICES };
79     LRUBucket<uint32_t, TimePoint> changeTime_ { 64 };
80 };
81 } // namespace OHOS::DistributedData
82 #endif // OHOS_DISTRIBUTED_DATA_SERVICE_MATRIX_DEVICE_MATRIX_H
83