#include #include "livoxProto1Protocol.h" #include "livoxProto1Core.h" namespace livoxProto1 { struct ProtoState { bool isInitialized = false; std::shared_ptr componentThread; std::unique_ptr deviceManager; }; static ProtoState protoState = { .isInitialized = false, .componentThread = nullptr, .deviceManager = nullptr }; DeviceManager::DeviceManager() : broadcastListener(protoState.componentThread) { broadcastListener.setDeviceGoneAwayCb(deviceGoneAwayInd); } void DeviceManager::deviceGoneAwayInd(const comms::DiscoveredDevice &device) { std::cout << "Device gone away: " << device.stringify() << std::endl; auto it = std::find_if( protoState.deviceManager->devices.begin(), protoState.deviceManager->devices.end(), [&device](const Device &d) { return d.discoveredDevice == device; } ); if (it != protoState.deviceManager->devices.end()) { protoState.deviceManager->devices.erase(it); } } void main(const std::shared_ptr &componentThread) { if (protoState.isInitialized) { return; } protoState.isInitialized = true; protoState.componentThread = componentThread; protoState.deviceManager = std::make_unique(); protoState.deviceManager->broadcastListener.start(); } void exit(void) { if (!protoState.isInitialized) { return; } protoState.deviceManager->broadcastListener.stop(); protoState.deviceManager.reset(); protoState.isInitialized = false; protoState.componentThread = nullptr; } } // namespace livoxProto1