• 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 #define LOG_TAG "ProcessCommunicationImpl"
17 
18 #include "process_communication_impl.h"
19 #include "log_print.h"
20 
21 namespace OHOS {
22 namespace DistributedKv {
23 using namespace DistributedDB;
ProcessCommunicationImpl(std::shared_ptr<Endpoint> endpoint)24 ProcessCommunicationImpl::ProcessCommunicationImpl(std::shared_ptr<Endpoint> endpoint)
25     : endpoint_(endpoint)
26 {
27 }
28 
~ProcessCommunicationImpl()29 ProcessCommunicationImpl::~ProcessCommunicationImpl()
30 {
31 }
32 
Start(const std::string & processLabel)33 DBStatus ProcessCommunicationImpl::Start(const std::string &processLabel)
34 {
35     Status errCode = endpoint_->Start();
36     if (errCode != Status::SUCCESS) {
37         ZLOGE("endpoint Start Fail: %{public}d", errCode);
38         return DBStatus::DB_ERROR;
39     }
40     isCreateSessionServer_ = true;
41     return DBStatus::OK;
42 }
43 
Stop()44 DBStatus ProcessCommunicationImpl::Stop()
45 {
46     Status errCode = endpoint_->Stop();
47     if (errCode != Status::SUCCESS) {
48         ZLOGE("endpoint Stop Fail: %{public}d", errCode);
49         return DBStatus::DB_ERROR;
50     }
51     isCreateSessionServer_ = false;
52     return DBStatus::OK;
53 }
54 
RegOnDeviceChange(const OnDeviceChange & callback)55 DBStatus ProcessCommunicationImpl::RegOnDeviceChange(const OnDeviceChange &callback)
56 {
57     return DBStatus::OK;
58 }
59 
RegOnDataReceive(const OnDataReceive & callback)60 DBStatus ProcessCommunicationImpl::RegOnDataReceive(const OnDataReceive &callback)
61 {
62     auto dataReciveCallback = [callback](const std::string &identifier, const uint8_t *data, uint32_t length) {
63         DistributedDB::DeviceInfos devInfo = {
64             identifier
65         };
66         callback(devInfo, data, length);
67     };
68 
69     Status errCode = endpoint_->RegOnDataReceive(dataReciveCallback);
70     if (errCode != Status::SUCCESS) {
71         ZLOGE("RegOnDataReceive Fail.");
72         return DBStatus::DB_ERROR;
73     }
74 
75     return DBStatus::OK;
76 }
77 
SendData(const DistributedDB::DeviceInfos & dstDevInfo,const uint8_t * data,uint32_t length)78 DBStatus ProcessCommunicationImpl::SendData(const DistributedDB::DeviceInfos &dstDevInfo, const uint8_t *data,
79     uint32_t length)
80 {
81     std::string dtsIdentifier = dstDevInfo.identifier;
82     Status errCode = endpoint_->SendData(dtsIdentifier, data, length);
83     if (errCode != Status::SUCCESS) {
84         ZLOGE("SendData Fail.");
85         return DBStatus::DB_ERROR;
86     }
87 
88     return DBStatus::OK;
89 }
90 
GetMtuSize()91 uint32_t ProcessCommunicationImpl::GetMtuSize()
92 {
93     return endpoint_->GetMtuSize({""});
94 }
95 
GetMtuSize(const DistributedDB::DeviceInfos & devInfo)96 uint32_t ProcessCommunicationImpl::GetMtuSize(const DistributedDB::DeviceInfos &devInfo)
97 {
98     std::string identifier = devInfo.identifier;
99     return endpoint_->GetMtuSize(identifier);
100 }
101 
GetLocalDeviceInfos()102 DistributedDB::DeviceInfos ProcessCommunicationImpl::GetLocalDeviceInfos()
103 {
104     std::string identifier = endpoint_->GetLocalDeviceInfos();
105     DistributedDB::DeviceInfos devInfos = {
106         identifier
107     };
108     return devInfos;
109 }
110 
GetRemoteOnlineDeviceInfosList()111 std::vector<DistributedDB::DeviceInfos> ProcessCommunicationImpl::GetRemoteOnlineDeviceInfosList()
112 {
113     return {};
114 }
115 
IsSameProcessLabelStartedOnPeerDevice(const DistributedDB::DeviceInfos & peerDevInfo)116 bool ProcessCommunicationImpl::IsSameProcessLabelStartedOnPeerDevice(__attribute__((unused))
117     const DistributedDB::DeviceInfos &peerDevInfo)
118 {
119     return isCreateSessionServer_;
120 }
121 
GetExtendHeaderHandle(const DistributedDB::ExtendInfo & paramInfo)122 std::shared_ptr<DistributedDB::ExtendHeaderHandle> ProcessCommunicationImpl::GetExtendHeaderHandle(
123     __attribute__((unused)) const DistributedDB::ExtendInfo &paramInfo)
124 {
125     return std::make_shared<DistributedDB::ExtendHeaderHandle>();
126 }
127 } // namespace AppDistributedKv
128 } // namespace OHOS
129 
130