• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IPROCESSCOMMUNICATOR_H
17 #define IPROCESSCOMMUNICATOR_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 #include "store_types.h"
25 
26 namespace DistributedDB {
27 struct AccessInfos {
28     std::string appId;
29     std::string storeId;
30     std::string userId;
31     std::string subUserId;
32 };
33 
34 // The DeviceInfos may contain other fields(Can only be auxiliary information) besides identifier field in the future.
35 struct DeviceInfos {
36     std::string identifier; // An unique and fixed identifier representing a device, such as UUID.
37     AccessInfos callee; // A message frame's access info.
38     bool isRetryTask = true;
39 };
40 
41 struct ExtendInfo {
42     std::string appId;
43     std::string storeId;
44     std::string userId;
45     std::string dstTarget;
46     std::string subUserId;
47 };
48 
49 struct DataHeadInfo {
50     const uint8_t *data = nullptr;
51     uint32_t totalLen;
52     std::string device;
53 };
54 
55 struct DataUserInfo {
56     const uint8_t *data = nullptr;
57     uint32_t totalLen;
58     const std::string label;
59     const std::string device;
60 };
61 
62 struct UserInfo {
63     std::string receiveUser;
64     std::string sendUser;
65 };
66 
67 class ExtendHeaderHandle {
68 public:
ExtendHeaderHandle()69     ExtendHeaderHandle() {};
~ExtendHeaderHandle()70     virtual ~ExtendHeaderHandle() {};
71     // headSize should be 8 byte align
72     // return OK and headSize = 0 if no need to fill Head Data
73     // return OK and headSize > 0 if permit sync and will call FillHeadData
74     // return NO_PERMISSION if not permit sync
GetHeadDataSize(uint32_t & headSize)75     virtual DBStatus GetHeadDataSize(uint32_t &headSize)
76     {
77         headSize = 0;
78         return OK;
79     };
80     // return OK if fill data ok
81     // return not OK if fill data failed
FillHeadData(uint8_t * data,uint32_t headSize,uint32_t totalLen)82     virtual DBStatus FillHeadData(uint8_t *data, uint32_t headSize, uint32_t totalLen)
83     {
84         return OK;
85     };
86 
GetTargetUserId()87     virtual std::string GetTargetUserId()
88     {
89         return "";
90     }
91 };
92 
93 // In OnDeviceChange, all field of devInfo should be valid, isOnline true for online and false for offline.
94 // The concept of online or offline:
95 // 1: Can be at the physical device level, which means the remote device can be visible and communicable by local device
96 // 2: Can also be at the process level, which means the same ProcessCommunicator(with same processLabel) had been
97 //    started on the remote device and thus visible and communicable by this local ProcessCommunicator.
98 using OnDeviceChange = std::function<void(const DeviceInfos &devInfo, bool isOnline)>;
99 
100 // In OnDataReceive, all field of srcDevInfo should be valid
101 using OnDataReceive = std::function<void(const DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length)>;
102 
103 using OnSendAble = std::function<void(const DeviceInfos &deviceInfo, int deviceCommErrCode)>;
104 
105 // For all functions with returnType DBStatus:
106 // return DBStatus::OK if successful, otherwise DBStatus::DB_ERROR if anything wrong.
107 // Additional information of reason why failed can be present in the log by the implementation.
108 // For "Get" or "Is" functions, implementation should notice that concurrent call is possible.
109 class IProcessCommunicator {
110 public:
111     // The distributeddb in one process can only use one ProcessCommunicator at the same time
112     // The ProcessCommunicator can only Start one processLabel at the same time
113     // The ProcessCommunicator can Start again after stop
114     // The processLabel should not be an empty string
115     virtual DBStatus Start(const std::string &processLabel) = 0;
116 
117     // The Stop should only be called after Start successfully
118     virtual DBStatus Stop() = 0;
119 
120     // The register function can be called anytime regardless of whether started or stopped.
121     // There will only be one callback at the same time for each function
122     // If register again, the latter callback replace the former callback.
123     // Register nullptr as callback to do unregister semantic.
124     // For concurrency security of implementation, there should be lock between register_operation and callback_event.
125     virtual DBStatus RegOnDeviceChange(const OnDeviceChange &callback) = 0;
126     virtual DBStatus RegOnDataReceive(const OnDataReceive &callback) = 0;
127 
128     // The SendData function should only be called after Start successfully
129     // Only the identifier field of dstDevInfo must be valid, no requirement for other field.
130     virtual DBStatus SendData(const DeviceInfos &dstDevInfo, const uint8_t *data, uint32_t length) = 0;
131 
SendData(const DeviceInfos & dstDevInfo,const uint8_t * data,uint32_t length,uint32_t totalLength)132     virtual DBStatus SendData(const DeviceInfos &dstDevInfo, const uint8_t *data, uint32_t length, uint32_t totalLength)
133     {
134         (void)totalLength;
135         return SendData(dstDevInfo, data, length);
136     }
137 
138     // The GetMtuSize function can be called anytime regardless of whether started or stopped.
139     // The mtuSize should not less than 1K otherwise it will be regard as 1K.
140     // For run on OHOS, there is agreement that the mtuSize should be nearly 5M.
141     virtual uint32_t GetMtuSize() = 0;
142 
143     // The GetLocalDeviceInfos function should only be called after Start successfully
144     // All field of returned DeviceInfos must be valid, the identifier must not be empty and changed between time.
145     virtual DeviceInfos GetLocalDeviceInfos() = 0;
146 
147     // The GetRemoteOnlineDeviceInfosList function should only be called after Start successfully
148     // All field of returned DeviceInfos must be valid, should not contain duplicate device or local device
149     virtual std::vector<DeviceInfos> GetRemoteOnlineDeviceInfosList() = 0;
150 
151     // The IsSameProcessLabelStartedOnPeerDevice function should only be called after Start successfully
152     // Only the identifier field of peerDevInfo must be valid, no requirement for other field.
153     // If the peer device is offline, then return false.
154     // If the peer device is online but no ProcessCommunicator with same processLabel had started on it, return false.
155     // If the peer device is online and ProcessCommunicator with same processLabel had started on it, return true.
156     virtual bool IsSameProcessLabelStartedOnPeerDevice(const DeviceInfos &peerDevInfo) = 0;
157 
~IProcessCommunicator()158     virtual ~IProcessCommunicator() {};
159 
160     // For ABI compatibility reason, temporarily place this method at last and offer a fake implementation.
161     // The valid mtuSize range from 1K to 5M, value beyond this range will be set to the upper or lower limit.
GetMtuSize(const DeviceInfos & devInfo)162     virtual uint32_t GetMtuSize(const DeviceInfos &devInfo)
163     {
164         if (devInfo.identifier.empty()) {
165             // Error case(would never happen actually) to avoid "unused-parameter" warning.
166             return 0;
167         }
168         return GetMtuSize();
169     }
170 
171     // The valid timeout range from 5s to 60s, value beyond this range will be set to the upper or lower limit.
GetTimeout()172     virtual uint32_t GetTimeout()
173     {
174         return 5 * 1000; // 5 * 1000ms
175     };
176 
177     // The valid timeout range from 5s to 60s, value beyond this range will be set to the upper or lower limit.
GetTimeout(const DeviceInfos & devInfo)178     virtual uint32_t GetTimeout(const DeviceInfos &devInfo)
179     {
180         if (devInfo.identifier.empty()) {
181             // Error case(would never happen actually) to avoid "unused-parameter" warning.
182             return 5 * 1000; // 5 * 1000ms
183         }
184         return GetTimeout();
185     }
186 
GetExtendHeaderHandle(const ExtendInfo & paramInfo)187     virtual std::shared_ptr<ExtendHeaderHandle> GetExtendHeaderHandle(const ExtendInfo &paramInfo)
188     {
189         return nullptr;
190     }
191     // called after OnDataReceive
192     // return INVALID_FORMAT and headLength = 0 if data service can not deSerialize the buff
193     // return OK if deSerialize ok and get HeadLength/localUserId successfully
CheckAndGetDataHeadInfo(const uint8_t * data,uint32_t totalLen,uint32_t & headLength,std::vector<std::string> & userId)194     virtual DBStatus CheckAndGetDataHeadInfo(const uint8_t *data, uint32_t totalLen, uint32_t &headLength,
195         std::vector<std::string> &userId)
196     {
197         headLength = 0;
198         return OK;
199     }
GetDataHeadInfo(DataHeadInfo dataHeadInfo,uint32_t & headLength)200     virtual DBStatus GetDataHeadInfo(DataHeadInfo dataHeadInfo, uint32_t &headLength)
201     {
202         headLength = 0;
203         return OK;
204     }
205     // return NO_PERMISSION while no need to handle the dataBuff if remote device userId is not mate with local userId
GetDataUserInfo(DataUserInfo dataUserInfo,std::vector<UserInfo> & userInfos)206     virtual DBStatus GetDataUserInfo(DataUserInfo dataUserInfo, std::vector<UserInfo> &userInfos)
207     {
208         return OK;
209     }
210 
RegOnSendAble(const OnSendAble & sendAbleCallback)211     virtual void RegOnSendAble([[gnu::unused]] const OnSendAble &sendAbleCallback)
212     {
213     }
214 };
215 } // namespace DistributedDB
216 
217 #endif // IPROCESSCOMMUNICATOR_H
218