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 NETWORK_ADAPTER_H 17 #define NETWORK_ADAPTER_H 18 19 #include <set> 20 #include <map> 21 #include <mutex> 22 #include <atomic> 23 #include <memory> 24 #include <condition_variable> 25 #include "iadapter.h" 26 #include "iprocess_communicator.h" 27 28 namespace DistributedDB { 29 class NetworkAdapter : public IAdapter { 30 public: 31 NetworkAdapter(); 32 explicit NetworkAdapter(const std::string &inProcessLabel); 33 NetworkAdapter(const std::string &inProcessLabel, const std::shared_ptr<IProcessCommunicator> &inCommunicator); 34 35 ~NetworkAdapter() override; 36 37 int StartAdapter() override; 38 void StopAdapter() override; 39 40 uint32_t GetMtuSize() override; 41 uint32_t GetMtuSize(const std::string &target) override; 42 43 uint32_t GetTimeout() override; 44 uint32_t GetTimeout(const std::string &target) override; 45 int GetLocalIdentity(std::string &outTarget) override; 46 47 int SendBytes(const std::string &dstTarget, const uint8_t *bytes, uint32_t length, 48 uint32_t totalLength) override; 49 50 int RegBytesReceiveCallback(const BytesReceiveCallback &onReceive, const Finalizer &inOper) override; 51 int RegTargetChangeCallback(const TargetChangeCallback &onChange, const Finalizer &inOper) override; 52 int RegSendableCallback(const SendableCallback &onSendable, const Finalizer &inOper) override; 53 54 bool IsDeviceOnline(const std::string &device) override; 55 std::shared_ptr<ExtendHeaderHandle> GetExtendHeaderHandle(const ExtendInfo ¶mInfo) override; 56 57 private: 58 void OnDataReceiveHandler(const DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length); 59 void OnDeviceChangeHandler(const DeviceInfos &devInfo, bool isOnline); 60 void OnSendAbleHandler(const DeviceInfos &devInfo); 61 62 void SearchOnlineRemoteDeviceAtStartup(); 63 void CheckDeviceOnlineAfterReception(const DeviceInfos &devInfo); 64 void CheckDeviceOfflineAfterSendFail(const DeviceInfos &devInfo); 65 66 std::string processLabel_; 67 std::shared_ptr<IProcessCommunicator> processCommunicator_; 68 69 // For protecting "LocalIdentity" and "MtuSize", these info only need to get from peripheral interface once 70 mutable std::mutex identityMutex_; 71 72 std::string localIdentity_; 73 mutable std::mutex mtuSizeMutex_; 74 bool isMtuSizeValid_ = false; 75 uint32_t mtuSize_ = 0; 76 std::map<std::string, uint32_t> devMapMtuSize_; 77 78 mutable std::mutex onlineRemoteDevMutex_; 79 std::set<std::string> onlineRemoteDev_; // Refer to devices that has peer process 80 81 std::atomic<int> pendingAsyncTaskCount_{0}; 82 mutable std::mutex asyncTaskDoneMutex_; 83 std::condition_variable asyncTaskDoneCv_; 84 85 BytesReceiveCallback onReceiveHandle_; 86 TargetChangeCallback onChangeHandle_; 87 SendableCallback onSendableHandle_; 88 Finalizer onReceiveFinalizer_; 89 Finalizer onChangeFinalizer_; 90 Finalizer onSendableFinalizer_; 91 mutable std::mutex onReceiveMutex_; 92 mutable std::mutex onChangeMutex_; 93 mutable std::mutex onSendableMutex_; 94 }; 95 } // namespace DistributedDB 96 97 #endif