• 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 ADAPTER_STUB_H
17 #define ADAPTER_STUB_H
18 
19 #include <set>
20 #include <map>
21 #include <mutex>
22 #include <atomic>
23 #include <string>
24 #include <cstdint>
25 #include "iadapter.h"
26 
27 namespace DistributedDB {
28 using OnSendBytes = std::function<int (void)>;
29 class AdapterStub : public IAdapter {
30 public:
31     /*
32      * Override Part
33      */
34     ~AdapterStub() override;
35 
36     int StartAdapter() override;
37     void StopAdapter() override;
38     uint32_t GetMtuSize() override;
39     uint32_t GetMtuSize(const std::string &target) override;
40     uint32_t GetTimeout() override;
41     uint32_t GetTimeout(const std::string &target) override;
42     int GetLocalIdentity(std::string &outTarget) override;
43 
44     int SendBytes(const std::string &dstTarget, const uint8_t *bytes, uint32_t length, uint32_t totalLength) override;
45 
46     int RegBytesReceiveCallback(const BytesReceiveCallback &onReceive, const Finalizer &inOper) override;
47     int RegTargetChangeCallback(const TargetChangeCallback &onChange, const Finalizer &inOper) override;
48     int RegSendableCallback(const SendableCallback &onSendable, const Finalizer &inOper) override;
49 
50     bool IsDeviceOnline(const std::string &device) override;
51 
52     std::shared_ptr<ExtendHeaderHandle> GetExtendHeaderHandle(const ExtendInfo &paramInfo) override;
53 
54     void CheckAndGetDataHeadInfo(const uint8_t *data, uint32_t totalLen, uint32_t &headLength,
55         std::string &userId);
56 
57     /*
58      * Extended Part
59      */
60     static void ConnectAdapterStub(AdapterStub *thisStub, AdapterStub *thatStub);
61     static void DisconnectAdapterStub(AdapterStub *thisStub, AdapterStub *thatStub);
62 
63     explicit AdapterStub(const std::string &inLocalTarget);
64     const std::string &GetLocalTarget();
65 
66     void SimulateSendBlock();
67     void SimulateSendBlockClear();
68 
69     void SimulateSendRetry(const std::string &dstTarget);
70     void SimulateSendRetryClear(const std::string &dstTarget);
71 
72     void SimulateSendPartialLoss();
73     void SimulateSendPartialLossClear();
74 
75     void SimulateSendTotalLoss();
76     void SimulateSendTotalLossClear();
77 
78     void SimulateSendBitErrorInMagicField(bool doFlag, uint16_t inMagic);
79     void SimulateSendBitErrorInVersionField(bool doFlag, uint16_t inVersion);
80     void SimulateSendBitErrorInCheckSumField(bool doFlag, uint64_t inCheckSum);
81     void SimulateSendBitErrorInPacketLenField(bool doFlag, uint32_t inPacketLen);
82     void SimulateSendBitErrorInPacketTypeField(bool doFlag, uint8_t inPacketType);
83     void SimulateSendBitErrorInPaddingLenField(bool doFlag, uint8_t inPaddingLen);
84     void SimulateSendBitErrorInMessageIdField(bool doFlag, uint32_t inMessageId);
85     void ForkSendBytes(const OnSendBytes &onSendBytes);
86 private:
87     void Connect(AdapterStub *inStub);
88     void Disconnect(AdapterStub *inStub);
89     void DeliverBytes(const std::string &srcTarget, const uint8_t *bytes, uint32_t length);
90 
91     void ApplySendBlock();
92     bool QuerySendRetry(const std::string &dstTarget);
93     bool QuerySendPartialLoss();
94     bool QuerySendTotalLoss();
95     void ApplySendBitError(const uint8_t *bytes, uint32_t length);
96 
97     std::string localTarget_;
98     std::map<std::string, AdapterStub *> targetMapAdapter_;
99 
100     BytesReceiveCallback onReceiveHandle_;
101     TargetChangeCallback onChangeHandle_;
102     SendableCallback onSendableHandle_;
103     Finalizer onReceiveFinalizer_;
104     Finalizer onChangeFinalizer_;
105     Finalizer onSendableFinalizer_;
106     std::mutex onReceiveMutex_;
107     std::mutex onChangeMutex_;
108     std::mutex onSendableMutex_;
109 
110     // Member for simulation
111     std::mutex block_;
112 
113     std::mutex retryMutex_;
114     std::set<std::string> targetRetrySet_;
115 
116     std::atomic<bool> isPartialLossSimulated_{false};
117     std::atomic<uint64_t> countForPartialLoss_{0};
118 
119     std::atomic<bool> isTotalLossSimulated_{false};
120 
121     bool doChangeMagicFlag_ = false;
122     bool doChangeVersionFlag_ = false;
123     bool doChangeCheckSumFlag_ = false;
124     bool doChangePacketLenFlag_ = false;
125     bool doChangePacketTypeFlag_ = false;
126     bool doChangePaddingLenFlag_ = false;
127     bool doChangeMessageIdFlag_ = false;
128     uint16_t magicField_ = 0;
129     uint16_t versionField_ = 0;
130     uint64_t checkSumField_ = 0;
131     uint32_t packetLenField_ = 0;
132     uint8_t packetTypeField_ = 0;
133     uint8_t paddingLenField_ = 0;
134     uint32_t messageIdField_ = 0;
135 
136     std::mutex sendBytesMutex_;
137     OnSendBytes onSendBytes_;
138 };
139 }
140 
141 #endif // ADAPTER_STUB_H