• 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 DISTRIBUTED_RDB_SERVICE_PROXY_H
17 #define DISTRIBUTED_RDB_SERVICE_PROXY_H
18 
19 #include <atomic>
20 #include <list>
21 #include <iremote_proxy.h>
22 #include "irdb_service.h"
23 #include "rdb_notifier.h"
24 #include "concurrent_map.h"
25 
26 namespace OHOS::DistributedRdb {
27 class RdbServiceProxy : public IRemoteProxy<IRdbService> {
28 public:
29     using ObserverMapValue = std::pair<std::list<RdbStoreObserver*>, RdbSyncerParam>;
30     using ObserverMap = ConcurrentMap<std::string, ObserverMapValue>;
31 
32     explicit RdbServiceProxy(const sptr<IRemoteObject>& object);
33 
34     std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override;
35 
36     int32_t InitNotifier(const RdbSyncerParam& param);
37     int32_t InitNotifier(const RdbSyncerParam& param, const sptr<IRemoteObject> notifier) override;
38 
39     int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector<std::string>& tables) override;
40 
41     int32_t Sync(const RdbSyncerParam& param, const SyncOption& option,
42                  const RdbPredicates& predicates, const SyncCallback& callback) override;
43 
44     int32_t Subscribe(const RdbSyncerParam& param, const SubscribeOption& option,
45                       RdbStoreObserver *observer) override;
46 
47     int32_t UnSubscribe(const RdbSyncerParam& param, const SubscribeOption& option,
48                         RdbStoreObserver *observer) override;
49 
50     ObserverMap ExportObservers();
51 
52     void ImportObservers(ObserverMap& observers);
53 
54 protected:
55     int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option,
56                    const RdbPredicates& predicates, SyncResult& result) override;
57 
58     int32_t DoAsync(const RdbSyncerParam& param, uint32_t seqNum, const SyncOption& option,
59                     const RdbPredicates& predicates) override;
60 
61     int32_t DoSubscribe(const RdbSyncerParam& param) override;
62 
63     int32_t DoUnSubscribe(const RdbSyncerParam& param) override;
64 
65 private:
66     uint32_t GetSeqNum();
67 
68     int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option,
69                    const RdbPredicates& predicates, const SyncCallback& callback);
70 
71     int32_t DoAsync(const RdbSyncerParam& param, const SyncOption& option,
72                     const RdbPredicates& predicates, const SyncCallback& callback);
73 
74     void OnSyncComplete(uint32_t seqNum, const SyncResult& result);
75 
76     void OnDataChange(const std::string& storeName, const std::vector<std::string>& devices);
77 
78     std::atomic<uint32_t> seqNum_ {};
79 
80     ConcurrentMap<uint32_t, SyncCallback> syncCallbacks_;
81     ObserverMap observers_;
82     sptr<RdbNotifierStub> notifier_;
83 
84     static inline BrokerDelegator<RdbServiceProxy> delegator_;
85 };
86 }
87 #endif
88