1 /*
2  * Copyright (c) 2021 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 DISTRIBUTEDDATA_COMMUNICATION_PROVIDER_H
17 #define DISTRIBUTEDDATA_COMMUNICATION_PROVIDER_H
18 
19 #include <functional>
20 #include <memory>
21 #include <vector>
22 
23 #include "app_data_change_listener.h"
24 #include "app_device_change_listener.h"
25 #include "idevice_query.h"
26 namespace OHOS {
27 namespace AppDistributedKv {
28 class CommunicationProvider {
29 public:
30     // constructor
CommunicationProvider()31     API_EXPORT CommunicationProvider() {}
32 
33     // destructor
~CommunicationProvider()34     API_EXPORT virtual ~CommunicationProvider() {}
35 
36     // user should use this method to get instance of CommunicationProvider;
37     API_EXPORT static CommunicationProvider &GetInstance();
38 
39     API_EXPORT static std::shared_ptr<CommunicationProvider> MakeCommunicationProvider();
40 
41     // add DataChangeListener to watch data change
42     virtual Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo) = 0;
43 
44     // stop DataChangeListener to watch data change
45     virtual Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo) = 0;
46 
47     // Send data to other device, function will be called back after sent to notify send result
48     virtual std::pair<Status, int32_t> SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId,
49         const DataInfo &dataInfo, uint32_t totalLength,
50         const MessageInfo &info = { MessageType::DEFAULT }) = 0;
51 
52     // start one server to listen data from other devices;
53     virtual Status Start(const PipeInfo &pipeInfo) = 0;
54 
55     // stop server
56     virtual Status Stop(const PipeInfo &pipeInfo) = 0;
57 
58     // check peer device pipeInfo Process
59     virtual bool IsSameStartedOnPeer(const PipeInfo &pipeInfo, const DeviceId &peer) const = 0;
60 
61     virtual void SetDeviceQuery(std::shared_ptr<IDeviceQuery> deviceQuery) = 0;
62 
63     virtual void SetMessageTransFlag(const PipeInfo &pipeInfo, bool flag) = 0;
64 
65     virtual Status Broadcast(const PipeInfo &pipeInfo, const LevelInfo &levelInfo) = 0;
66     virtual int32_t ListenBroadcastMsg(const PipeInfo &pipeInfo,
67         std::function<void(const std::string &, const LevelInfo &)> listener) = 0;
68 };
69 } // namespace AppDistributedKv
70 } // namespace OHOS
71 #endif // DISTRIBUTEDDATA_COMMUNICATION_PROVIDER_H
72