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 DEVICE_MANAGER_H 17 #define DEVICE_MANAGER_H 18 19 #include <mutex> 20 #include <set> 21 22 #include "icommunicator.h" 23 24 namespace DistributedDB { 25 class DeviceManager final { 26 public: 27 DeviceManager(); 28 ~DeviceManager(); 29 30 DISABLE_COPY_ASSIGN_MOVE(DeviceManager); 31 32 static int RegisterTransformFunc(); 33 34 // Calculate the length of message. 35 static uint32_t CalculateLen(); 36 37 // Initialize the DeviceManager. 38 int Initialize(ICommunicator *communicator, const std::function<void(std::string)> &onlineCallback, 39 const std::function<void(std::string)> &offlineCallback); 40 41 // Set The Device online Callback. 42 void RegDeviceOnLineCallBack(const std::function<void(std::string)> &callback); 43 44 // Set The Device offline Callback. 45 void RegDeviceOffLineCallBack(const std::function<void(std::string)> &callback); 46 47 // The Device connect message callback, registered to the ICommunicator 48 void OnDeviceConnectCallback(const std::string &targetDev, bool isConnect); 49 50 // Get The online devices list. 51 void GetOnlineDevices(std::vector<std::string> &devices) const; 52 53 #ifndef OMIT_MULTI_VER 54 // Send a BroadCast to all online device. 55 int SendBroadCast(uint32_t msgId); 56 #endif // OMIT_MULTI_VER 57 58 // Determine if the device is online. 59 bool IsDeviceOnline(const std::string &deviceId) const; 60 61 private: 62 63 #ifndef OMIT_MULTI_VER 64 // Send a local data changed broadcast. 65 int SendLocalDataChanged(); 66 #endif // OMIT_MULTI_VER 67 68 std::set<std::string> devices_; 69 std::function<void(std::string)> onlineCallback_; 70 std::function<void(std::string)> offlineCallback_; 71 ICommunicator *communicator_; 72 mutable std::mutex devicesLock_; 73 }; 74 } // namespace DistributedDB 75 76 #endif // DEVICE_MANAGER_H 77