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 ¶mInfo) 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, int softBusErrCode = E_OK); 71 void SimulateTriggerSendableCallback(const std::string &dstTarget, int softBusErrCode = E_OK); 72 73 void SimulateSendPartialLoss(); 74 void SimulateSendPartialLossClear(); 75 76 void SimulateSendTotalLoss(); 77 void SimulateSendTotalLossClear(); 78 79 void SimulateSendBitErrorInMagicField(bool doFlag, uint16_t inMagic); 80 void SimulateSendBitErrorInVersionField(bool doFlag, uint16_t inVersion); 81 void SimulateSendBitErrorInCheckSumField(bool doFlag, uint64_t inCheckSum); 82 void SimulateSendBitErrorInPacketLenField(bool doFlag, uint32_t inPacketLen); 83 void SimulateSendBitErrorInPacketTypeField(bool doFlag, uint8_t inPacketType); 84 void SimulateSendBitErrorInPaddingLenField(bool doFlag, uint8_t inPaddingLen); 85 void SimulateSendBitErrorInMessageIdField(bool doFlag, uint32_t inMessageId); 86 void ForkSendBytes(const OnSendBytes &onSendBytes); 87 private: 88 void Connect(AdapterStub *inStub); 89 void Disconnect(AdapterStub *inStub); 90 void DeliverBytes(const std::string &srcTarget, const uint8_t *bytes, uint32_t length); 91 92 void ApplySendBlock(); 93 bool QuerySendRetry(const std::string &dstTarget); 94 bool QuerySendPartialLoss(); 95 bool QuerySendTotalLoss(); 96 void ApplySendBitError(const uint8_t *bytes, uint32_t length); 97 98 std::string localTarget_; 99 std::map<std::string, AdapterStub *> targetMapAdapter_; 100 101 BytesReceiveCallback onReceiveHandle_; 102 TargetChangeCallback onChangeHandle_; 103 SendableCallback onSendableHandle_; 104 Finalizer onReceiveFinalizer_; 105 Finalizer onChangeFinalizer_; 106 Finalizer onSendableFinalizer_; 107 std::mutex onReceiveMutex_; 108 std::mutex onChangeMutex_; 109 std::mutex onSendableMutex_; 110 111 // Member for simulation 112 std::mutex block_; 113 114 std::mutex retryMutex_; 115 std::set<std::string> targetRetrySet_; 116 117 std::atomic<bool> isPartialLossSimulated_{false}; 118 std::atomic<uint64_t> countForPartialLoss_{0}; 119 120 std::atomic<bool> isTotalLossSimulated_{false}; 121 122 bool doChangeMagicFlag_ = false; 123 bool doChangeVersionFlag_ = false; 124 bool doChangeCheckSumFlag_ = false; 125 bool doChangePacketLenFlag_ = false; 126 bool doChangePacketTypeFlag_ = false; 127 bool doChangePaddingLenFlag_ = false; 128 bool doChangeMessageIdFlag_ = false; 129 uint16_t magicField_ = 0; 130 uint16_t versionField_ = 0; 131 uint64_t checkSumField_ = 0; 132 uint32_t packetLenField_ = 0; 133 uint8_t packetTypeField_ = 0; 134 uint8_t paddingLenField_ = 0; 135 uint32_t messageIdField_ = 0; 136 137 std::mutex sendBytesMutex_; 138 OnSendBytes onSendBytes_; 139 }; 140 } 141 142 #endif // ADAPTER_STUB_H