• 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 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     int SendBytes(const DeviceInfos &deviceInfos, const uint8_t *bytes, uint32_t length,
50         uint32_t totalLength) override;
51 
52     int RegBytesReceiveCallback(const BytesReceiveCallback &onReceive, const Finalizer &inOper) override;
53     int RegTargetChangeCallback(const TargetChangeCallback &onChange, const Finalizer &inOper) override;
54     int RegSendableCallback(const SendableCallback &onSendable, const Finalizer &inOper) override;
55 
56     bool IsDeviceOnline(const std::string &device) override;
57     std::shared_ptr<ExtendHeaderHandle> GetExtendHeaderHandle(const ExtendInfo &paramInfo) override;
58 
59 private:
60     void OnDataReceiveHandler(const DeviceInfos &srcDevInfo, const uint8_t *data, uint32_t length);
61     void OnDeviceChangeHandler(const DeviceInfos &devInfo, bool isOnline);
62     void OnSendAbleHandler(const DeviceInfos &devInfo);
63 
64     void SearchOnlineRemoteDeviceAtStartup();
65     void CheckDeviceOnlineAfterReception(const DeviceInfos &devInfo);
66     void CheckDeviceOfflineAfterSendFail(const DeviceInfos &devInfo);
67 
68     std::string processLabel_;
69     std::shared_ptr<IProcessCommunicator> processCommunicator_;
70 
71     // For protecting "LocalIdentity" and "MtuSize", these info only need to get from peripheral interface once
72     mutable std::mutex identityMutex_;
73 
74     std::string localIdentity_;
75     mutable std::mutex mtuSizeMutex_;
76     bool isMtuSizeValid_ = false;
77     uint32_t mtuSize_ = 0;
78     std::map<std::string, uint32_t> devMapMtuSize_;
79 
80     mutable std::mutex onlineRemoteDevMutex_;
81     std::set<std::string> onlineRemoteDev_; // Refer to devices that has peer process
82 
83     std::atomic<int> pendingAsyncTaskCount_{0};
84     mutable std::mutex asyncTaskDoneMutex_;
85     std::condition_variable asyncTaskDoneCv_;
86 
87     BytesReceiveCallback onReceiveHandle_;
88     TargetChangeCallback onChangeHandle_;
89     SendableCallback onSendableHandle_;
90     Finalizer onReceiveFinalizer_;
91     Finalizer onChangeFinalizer_;
92     Finalizer onSendableFinalizer_;
93     mutable std::mutex onReceiveMutex_;
94     mutable std::mutex onChangeMutex_;
95     mutable std::mutex onSendableMutex_;
96 };
97 } // namespace DistributedDB
98 
99 #endif