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_OBJECTSTORE_H
17 #define DISTRIBUTED_OBJECTSTORE_H
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include <functional>
22 
23 #include "distributed_object.h"
24 
25 namespace OHOS::ObjectStore {
26 class StatusNotifier {
27 public:
28     virtual void OnChanged(
29         const std::string &sessionId, const std::string &networkId, const std::string &onlineStatus) = 0;
30 };
31 class DistributedObjectStore {
32 public:
~DistributedObjectStore()33     virtual ~DistributedObjectStore(){};
34 
35     /**
36      * @brief Get the instance to handle the object, such as create the object.
37      *
38      * @param bundleName Indicates the bundleName.
39      *
40      * @return Returns the pointer to the DistributedObjectStore class.
41      */
42     static DistributedObjectStore *GetInstance(const std::string &bundleName = "");
43 
44     /**
45      * @brief Create a object according to the sessionId.
46      *
47      * @param sessionId Indicates the sessionId.
48      *
49      * @return Returns the pointer to the DistributedObject class.
50      */
51     virtual DistributedObject *CreateObject(const std::string &sessionId) = 0;
52 
53     /**
54      * @brief Create a object according to the sessionId.
55      *
56      * @param sessionId Indicates the sessionId.
57      * @param status Indicates whether the distributed object is created successfully,
58      * 0 means success, other means fail.
59      *
60      * @return Returns the pointer to the DistributedObject class.
61      */
62     virtual DistributedObject *CreateObject(const std::string &sessionId, uint32_t &status) = 0;
63 
64     /**
65      * @brief Get the double pointer to the object.
66      *
67      * @param sessionId Indicates the sessionId.
68      * @param object Indicates the double pointer to the object.
69      *
70      * @return Returns 0 for success, others for failure.
71      */
72     virtual uint32_t Get(const std::string &sessionId, DistributedObject **object) = 0;
73 
74     /**
75      * @brief Delete the object according to the sessionId.
76      *
77      * @param sessionId Indicates the sessionId.
78      *
79      * @return Returns 0 for success, others for failure.
80      */
81     virtual uint32_t DeleteObject(const std::string &sessionId) = 0;
82 
83     /**
84      * @brief Set listening for data changes.
85      *
86      * @param object Indicates the pointer to the DistributedObject class.
87      * @param objectWatcher Indicates callback function for data changes.
88      *
89      * @return Returns 0 for success, others for failure.
90      */
91     virtual uint32_t Watch(DistributedObject *object, std::shared_ptr<ObjectWatcher> objectWatcher) = 0;
92 
93     /**
94      * @brief Undo listening for data changes.
95      *
96      * @param object Indicates the pointer to the DistributedObject class.
97      *
98      * @return Returns the pointer to the DistributedObject class.
99      */
100     virtual uint32_t UnWatch(DistributedObject *object) = 0;
101 
102     /**
103      * @brief Set listening for device online and offline .
104      *
105      * @param notifier Indicates callback function for device online ond offline.
106      *
107      * @return Returns 0 for success, others for failure.
108      */
109     virtual uint32_t SetStatusNotifier(std::shared_ptr<StatusNotifier> notifier) = 0;
110 
111     /**
112      * @brief Notify the status of the local device from the cached callback function according to the sessionId.
113      *
114      * @param sessionId Indicates the sessionId.
115      *
116      */
117     virtual void NotifyCachedStatus(const std::string &sessionId) = 0;
118 };
119 } // namespace OHOS::ObjectStore
120 
121 #endif // DISTRIBUTED_OBJECTSTORE_H
122