1 /* 2 * Copyright (c) 2022 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 DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_COMMUNICATION_STRATEGY_H 17 #define DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_COMMUNICATION_STRATEGY_H 18 #include <memory> 19 #include <mutex> 20 21 #include "concurrent_map.h" 22 #include "visibility.h" 23 namespace OHOS::AppDistributedKv { 24 class API_EXPORT CommunicationStrategy { 25 public: 26 enum class Strategy : int32_t { 27 // If AP is available, the AP is preferred. When AP is not available, only BR can be used; p2p is not support 28 DEFAULT, 29 // If AP is available, the AP is preferred. When AP is not available, BR is used for a small amount of data 30 // and P2P is used for a large amount of data; The strategy takes effect only at the device online stage; 31 ON_LINE_SELECT_CHANNEL, 32 BUTT 33 }; 34 static CommunicationStrategy &GetInstance(); 35 using Strategy = CommunicationStrategy::Strategy; 36 void RegGetSyncDataSize(const std::string &type, const std::function<size_t(const std::string &)> &getDataSize); 37 CommunicationStrategy::Strategy GetStrategy(const std::string &deviceId); 38 void SetStrategy(const std::string &deviceId, Strategy strategy, 39 const std::function<void(const std::string &, Strategy)> &action); 40 private: 41 CommunicationStrategy() = default; 42 ~CommunicationStrategy() = default; 43 CommunicationStrategy(CommunicationStrategy const &) = delete; 44 void operator=(CommunicationStrategy const &) = delete; 45 CommunicationStrategy(CommunicationStrategy &&) = delete; 46 CommunicationStrategy &operator=(CommunicationStrategy &&) = delete; 47 48 size_t CalcSyncDataSize(const std::string &deviceId); 49 50 static constexpr uint32_t SWITCH_CONNECTION_THRESHOLD = 75 * 1024u; 51 ConcurrentMap<std::string, std::function<uint32_t(const std::string &)>> calcDataSizes_; 52 ConcurrentMap<std::string, Strategy> strategys_; 53 }; 54 } 55 56 #endif // DISTRIBUTEDDATAMGR_DATAMGR_SERVICE_COMMUNICATION_STRATEGY_H 57