Rework network client callback pipeline - route callbacks from clients through ResourceManager so that device list callbacks get called. Fixes remote devices not showing up in plugins.

This commit is contained in:
Adam Honse
2021-05-14 23:28:55 -05:00
parent 94370c24a8
commit d88f9979a9
8 changed files with 81 additions and 56 deletions

View File

@@ -45,6 +45,12 @@ NetworkClient::~NetworkClient()
StopClient();
}
void NetworkClient::ClearCallbacks()
{
ClientInfoChangeCallbacks.clear();
ClientInfoChangeCallbackArgs.clear();
}
void NetworkClient::ClientInfoChanged()
{
ClientInfoChangeMutex.lock();

View File

@@ -31,6 +31,7 @@ public:
unsigned int GetProtocolVersion();
bool GetOnline();
void ClearCallbacks();
void RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg);
void SetIP(const char *new_ip);

View File

@@ -88,10 +88,10 @@ void ResourceManager::RegisterRGBController(RGBController *rgb_controller)
LOG_NOTICE("Registering RGB controller: %s", rgb_controller->name.c_str());
rgb_controllers_hw.push_back(rgb_controller);
DeviceListChanged();
UpdateDeviceList();
}
void ResourceManager::UnregisterRGBController(RGBController *rgb_controller)
void ResourceManager::UnregisterRGBController(RGBController* rgb_controller)
{
LOG_NOTICE("Unregistering RGB controller: %s", rgb_controller->name.c_str());
@@ -120,7 +120,7 @@ void ResourceManager::UnregisterRGBController(RGBController *rgb_controller)
rgb_controllers.erase(rgb_it);
}
DeviceListChanged();
UpdateDeviceList();
}
std::vector<RGBController*> & ResourceManager::GetRGBControllers()
@@ -190,7 +190,7 @@ void ResourceManager::RegisterDetectionProgressCallback(DetectionProgressCallbac
DetectionProgressCallbackArgs.push_back(new_callback_arg);
}
void ResourceManager::DeviceListChanged()
void ResourceManager::UpdateDeviceList()
{
DeviceListChangeMutex.lock();
@@ -239,10 +239,7 @@ void ResourceManager::DeviceListChanged()
/*-------------------------------------------------*\
| Device list has changed, call the callbacks |
\*-------------------------------------------------*/
for(unsigned int callback_idx = 0; callback_idx < DeviceListChangeCallbacks.size(); callback_idx++)
{
DeviceListChangeCallbacks[callback_idx](DeviceListChangeCallbackArgs[callback_idx]);
}
DeviceListChanged();
/*-------------------------------------------------*\
| Device list has changed, inform all clients |
@@ -253,6 +250,17 @@ void ResourceManager::DeviceListChanged()
DeviceListChangeMutex.unlock();
}
void ResourceManager::DeviceListChanged()
{
/*-------------------------------------------------*\
| Device list has changed, call the callbacks |
\*-------------------------------------------------*/
for(unsigned int callback_idx = 0; callback_idx < DeviceListChangeCallbacks.size(); callback_idx++)
{
ResourceManager::DeviceListChangeCallbacks[callback_idx](DeviceListChangeCallbackArgs[callback_idx]);
}
}
void ResourceManager::DetectionProgressChanged()
{
DetectionProgressMutex.lock();
@@ -340,6 +348,50 @@ NetworkServer* ResourceManager::GetServer()
return(server);
}
static void NetworkClientInfoChangeCallback(void* this_ptr)
{
ResourceManager* this_obj = (ResourceManager*)this_ptr;
this_obj->DeviceListChanged();
}
void ResourceManager::RegisterNetworkClient(NetworkClient* new_client)
{
new_client->RegisterClientInfoChangeCallback(NetworkClientInfoChangeCallback, this);
clients.push_back(new_client);
}
void ResourceManager::UnregisterNetworkClient(NetworkClient* network_client)
{
/*-------------------------------------------------------------------------*\
| Stop the disconnecting client |
\*-------------------------------------------------------------------------*/
network_client->StopClient();
/*-------------------------------------------------------------------------*\
| Clear callbacks from the client before removal |
\*-------------------------------------------------------------------------*/
network_client->ClearCallbacks();
/*-------------------------------------------------------------------------*\
| Find the client to remove and remove it from the clients list |
\*-------------------------------------------------------------------------*/
std::vector<NetworkClient*>::iterator client_it = std::find(clients.begin(), clients.end(), network_client);
if(client_it != clients.end())
{
clients.erase(client_it);
}
/*-------------------------------------------------------------------------*\
| Delete the client |
\*-------------------------------------------------------------------------*/
delete network_client;
UpdateDeviceList();
}
std::vector<NetworkClient*>& ResourceManager::GetClients()
{
return(clients);
@@ -451,7 +503,7 @@ void ResourceManager::DetectDevices()
Cleanup();
DeviceListChanged();
UpdateDeviceList();
/*-------------------------------------------------*\
| Start the device detection thread |
@@ -603,7 +655,7 @@ void ResourceManager::DetectDevicesThreadFunction()
profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false);
}
DeviceListChanged();
UpdateDeviceList();
}
prev_count = rgb_controllers_hw.size();
@@ -690,7 +742,7 @@ void ResourceManager::DetectDevicesThreadFunction()
profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false);
}
DeviceListChanged();
UpdateDeviceList();
}
prev_count = rgb_controllers_hw.size();
}
@@ -827,7 +879,7 @@ void ResourceManager::DetectDevicesThreadFunction()
profile_manager->LoadDeviceFromListWithOptions(rgb_controllers_sizes, size_used, rgb_controllers_hw[controller_size_idx], true, false);
}
DeviceListChanged();
UpdateDeviceList();
}
prev_count = rgb_controllers_hw.size();

View File

@@ -76,7 +76,7 @@ public:
virtual ProfileManager* GetProfileManager() = 0;
virtual SettingsManager* GetSettingsManager() = 0;
virtual void DeviceListChanged() = 0;
virtual void UpdateDeviceList() = 0;
virtual void WaitForDeviceDetection() = 0;
protected:
@@ -90,7 +90,7 @@ public:
ResourceManager();
~ResourceManager();
void RegisterI2CBus(i2c_smbus_interface *);
std::vector<i2c_smbus_interface*> & GetI2CBusses();
@@ -120,6 +120,9 @@ public:
std::string GetConfigurationDirectory();
void RegisterNetworkClient(NetworkClient* new_client);
void UnregisterNetworkClient(NetworkClient* network_client);
std::vector<NetworkClient*>& GetClients();
NetworkServer* GetServer();
@@ -128,6 +131,7 @@ public:
void SetConfigurationDirectory(std::string directory);
void UpdateDeviceList();
void DeviceListChanged();
void DetectionProgressChanged();
void I2CBusListChanged();

View File

@@ -161,11 +161,6 @@ void OpenRGBClientInfoPage::UpdateInfo()
}
}
}
/*-----------------------------------------------------*\
| Emit client information updated signal |
\*-----------------------------------------------------*/
emit ClientListUpdated();
}
void Ui::OpenRGBClientInfoPage::on_ClientConnectButton_clicked()
@@ -193,7 +188,7 @@ void Ui::OpenRGBClientInfoPage::on_ClientConnectButton_clicked()
/*-----------------------------------------------------*\
| Add new client to list and register update callback |
\*-----------------------------------------------------*/
ResourceManager::get()->GetClients().push_back(rgb_client);
ResourceManager::get()->RegisterNetworkClient(rgb_client);
rgb_client->RegisterClientInfoChangeCallback(UpdateInfoCallback, this);
}
@@ -206,24 +201,8 @@ void Ui::OpenRGBClientInfoPage::onClientDisconnectButton_clicked(QObject * arg)
NetworkClient * disconnect_client = ((NetworkClientPointer *)arg)->net_client;
/*-----------------------------------------------------*\
| Stop the disconnecting client |
| Remove the client from the resource manager, which |
| deletes the client |
\*-----------------------------------------------------*/
disconnect_client->StopClient();
/*-----------------------------------------------------*\
| Remove disconnecting client from list |
\*-----------------------------------------------------*/
for(unsigned int client_idx = 0; client_idx < ResourceManager::get()->GetClients().size(); client_idx++)
{
if(disconnect_client == ResourceManager::get()->GetClients()[client_idx])
{
ResourceManager::get()->GetClients().erase(ResourceManager::get()->GetClients().begin() + client_idx);
break;
}
}
/*-----------------------------------------------------*\
| Delete the disconnecting client |
\*-----------------------------------------------------*/
delete disconnect_client;
ResourceManager::get()->UnregisterNetworkClient(disconnect_client);
}

View File

@@ -29,9 +29,6 @@ private slots:
private:
Ui::OpenRGBClientInfoPageUi *ui;
signals:
void ClientListUpdated();
};
#endif // OPENRGBCLIENTINFOPAGE_H

View File

@@ -667,14 +667,6 @@ void OpenRGBDialog2::AddClientTab()
{
ClientInfoPage = new OpenRGBClientInfoPage();
ui->MainTabBar->insertTab(2, ClientInfoPage, "SDK Client");
/*-----------------------------------------------------*\
| Connect the page's Set All button to the Set All slot |
\*-----------------------------------------------------*/
connect(ClientInfoPage,
SIGNAL(ClientListUpdated()),
this,
SLOT(on_ClientListUpdated()));
}
}
@@ -1002,11 +994,6 @@ void OpenRGBDialog2::on_QuickWhite()
on_SetAllDevices(0xFF, 0xFF, 0xFF);
}
void OpenRGBDialog2::on_ClientListUpdated()
{
UpdateDevicesList();
}
void OpenRGBDialog2::onDeviceListUpdated()
{
UpdateDevicesList();

View File

@@ -95,7 +95,6 @@ private slots:
void on_QuickBlue();
void on_QuickMagenta();
void on_QuickWhite();
void on_ClientListUpdated();
void onDeviceListUpdated();
void onDetectionProgressUpdated();
void on_SetAllDevices(unsigned char red, unsigned char green, unsigned char blue);