• 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 
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 &paramInfo) 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 
61     void SearchOnlineRemoteDeviceAtStartup();
62     void CheckDeviceOnlineAfterReception(const DeviceInfos &devInfo);
63     void CheckDeviceOfflineAfterSendFail(const DeviceInfos &devInfo);
64 
65     std::string processLabel_;
66     std::shared_ptr<IProcessCommunicator> processCommunicator_;
67 
68     // For protecting "LocalIdentity" and "MtuSize", these info only need to get from peripheral interface once
69     mutable std::mutex identityMutex_;
70 
71     std::string localIdentity_;
72     mutable std::mutex mtuSizeMutex_;
73     bool isMtuSizeValid_ = false;
74     uint32_t mtuSize_ = 0;
75     std::map<std::string, uint32_t> devMapMtuSize_;
76 
77     mutable std::mutex onlineRemoteDevMutex_;
78     std::set<std::string> onlineRemoteDev_; // Refer to devices that has peer process
79 
80     std::atomic<int> pendingAsyncTaskCount_{0};
81     mutable std::mutex asyncTaskDoneMutex_;
82     std::condition_variable asyncTaskDoneCv_;
83 
84     BytesReceiveCallback onReceiveHandle_;
85     TargetChangeCallback onChangeHandle_;
86     SendableCallback onSendableHandle_;
87     Finalizer onReceiveFinalizer_;
88     Finalizer onChangeFinalizer_;
89     Finalizer onSendableFinalizer_;
90     mutable std::mutex onReceiveMutex_;
91     mutable std::mutex onChangeMutex_;
92     mutable std::mutex onSendableMutex_;
93 };
94 } // namespace DistributedDB
95 
96 #endif