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 VIRTUAL_TIME_SYNC_COMMUNICATOR_H 17 #define VIRTUAL_TIME_SYNC_COMMUNICATOR_H 18 19 #include <string> 20 #include <chrono> 21 #include <condition_variable> 22 #include <cstdint> 23 #include <functional> 24 #include <mutex> 25 26 #include "db_types.h" 27 #include "communicator_aggregator.h" 28 #include "icommunicator.h" 29 #include "ref_object.h" 30 #include "serial_buffer.h" 31 #include "time_sync.h" 32 33 namespace DistributedDB { 34 class VirtualTimeSyncCommunicator : public ICommunicator { 35 public: 36 VirtualTimeSyncCommunicator(); 37 ~VirtualTimeSyncCommunicator(); 38 39 DISABLE_COPY_ASSIGN_MOVE(VirtualTimeSyncCommunicator); 40 41 int RegOnMessageCallback(const OnMessageCallback &onMessage, const Finalizer &inOper) override; 42 int RegOnConnectCallback(const OnConnectCallback &onConnect, const Finalizer &inOper) override; 43 int RegOnSendableCallback(const std::function<void(void)> &onSendable, const Finalizer &inOper) override; 44 45 void Activate() override; 46 47 // return maximum allowed data size 48 uint32_t GetCommunicatorMtuSize() const override; 49 uint32_t GetCommunicatorMtuSize(const std::string &target) const override; 50 51 // return timeout 52 uint32_t GetTimeout() const override; 53 uint32_t GetTimeout(const std::string &target) const override; 54 55 bool IsDeviceOnline(const std::string &device) const override; 56 57 // Get local target name for identify self 58 int GetLocalIdentity(std::string &outTarget) const override; 59 60 int SendMessage(const std::string &dstTarget, const Message *inMsg, const SendConfig &config) override; 61 int SendMessage(const std::string &dstTarget, const Message *inMsg, const SendConfig &config, 62 const OnSendEnd &onEnd) override; 63 64 int GetRemoteCommunicatorVersion(const std::string &deviceId, uint16_t &version) const override; 65 66 void SetTimeSync(TimeSync *srcTimeSync, TimeSync *dstTimeSync, 67 const std::string &deviceID, SyncTaskContext *syncTaskcontext); 68 69 void GetTimeOffset(TimeOffset &timeOffset) const; 70 71 void Disable(); 72 73 void SetRemoteVersion(uint16_t remoteVersion); 74 75 private: 76 TimeSync *srcTimeSync_; 77 TimeSync *dstTimeSync_; 78 TimeOffset timeOffset_; 79 std::string deviceID_; 80 SyncTaskContext *syncTaskcontext_; 81 bool isEnable_ = true; 82 uint16_t version_ = 0; 83 }; 84 } // namespace DistributedDB 85 86 #endif // VIRTUAL_TIME_SYNC_COMMUNICATOR_H 87