mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-21 14:03:11 -07:00
Finish Discovery Service
This commit is contained in:
parent
fe83c6790a
commit
70b2b3f009
9 changed files with 157 additions and 18 deletions
|
@ -19,20 +19,42 @@
|
|||
#define CHIAKI_DISCOVERYMANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QList>
|
||||
|
||||
#include <chiaki/discoveryservice.h>
|
||||
|
||||
struct DiscoveryHost
|
||||
{
|
||||
ChiakiDiscoveryHostState state;
|
||||
uint16_t host_request_port;
|
||||
#define STRING_MEMBER(name) QString name;
|
||||
CHIAKI_DISCOVERY_HOST_STRING_FOREACH(STRING_MEMBER)
|
||||
#undef STRING_MEMBER
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(DiscoveryHost)
|
||||
|
||||
class DiscoveryManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
friend class DiscoveryManagerPrivate;
|
||||
|
||||
private:
|
||||
ChiakiDiscoveryService service;
|
||||
QList<DiscoveryHost> hosts;
|
||||
|
||||
private slots:
|
||||
void DiscoveryServiceHosts(QList<DiscoveryHost> hosts);
|
||||
|
||||
public:
|
||||
explicit DiscoveryManager(QObject *parent = nullptr);
|
||||
~DiscoveryManager();
|
||||
|
||||
const QList<DiscoveryHost> GetHosts() const { return hosts; }
|
||||
|
||||
signals:
|
||||
void HostsUpdated();
|
||||
};
|
||||
|
||||
#endif //CHIAKI_DISCOVERYMANAGER_H
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
#include <QMainWindow>
|
||||
|
||||
#include "discoverymanager.h"
|
||||
|
||||
class DynamicGridWidget;
|
||||
class ServerItemWidget;
|
||||
|
||||
|
@ -31,6 +33,8 @@ class MainWindow : public QMainWindow
|
|||
DynamicGridWidget *grid_widget;
|
||||
QList<ServerItemWidget *> server_item_widgets;
|
||||
|
||||
DiscoveryManager discovery_manager;
|
||||
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
|
@ -40,6 +44,8 @@ class MainWindow : public QMainWindow
|
|||
|
||||
void RunDiscovery();
|
||||
void ShowSettings();
|
||||
|
||||
void DiscoveryHostsUpdated();
|
||||
};
|
||||
|
||||
#endif //CHIAKI_MAINWINDOW_H
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#include <utility>
|
||||
|
||||
/*
|
||||
* This file is part of Chiaki.
|
||||
*
|
||||
|
@ -17,14 +19,29 @@
|
|||
|
||||
#include <discoverymanager.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
|
||||
#define PING_MS 500
|
||||
#define HOSTS_MAX 16
|
||||
#define DROP_PINGS 3
|
||||
|
||||
static void DiscoveryServiceHostsCallback(ChiakiDiscoveryHost *hosts, size_t hosts_count, void *user);
|
||||
|
||||
DiscoveryManager::DiscoveryManager(QObject *parent) : QObject(parent)
|
||||
{
|
||||
ChiakiDiscoveryServiceOptions options;
|
||||
options.ping_ms = 500;
|
||||
options.hosts_max = 16;
|
||||
options.ping_ms = PING_MS;
|
||||
options.hosts_max = HOSTS_MAX;
|
||||
options.host_drop_pings = DROP_PINGS;
|
||||
options.cb = DiscoveryServiceHostsCallback;
|
||||
options.cb_user = this;
|
||||
|
||||
options.send_addr = nullptr; // TODO
|
||||
options.send_addr_size = 0; // TODO
|
||||
sockaddr_in addr = {};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(CHIAKI_DISCOVERY_PORT);
|
||||
addr.sin_addr.s_addr = 0xffffffff; // 255.255.255.255
|
||||
options.send_addr = reinterpret_cast<sockaddr *>(&addr);
|
||||
options.send_addr_size = sizeof(addr);
|
||||
|
||||
ChiakiErrorCode err = chiaki_discovery_service_init(&service, &options, nullptr /* TODO */);
|
||||
if(err != CHIAKI_ERR_SUCCESS)
|
||||
|
@ -35,3 +52,40 @@ DiscoveryManager::~DiscoveryManager()
|
|||
{
|
||||
chiaki_discovery_service_fini(&service);
|
||||
}
|
||||
|
||||
void DiscoveryManager::DiscoveryServiceHosts(QList<DiscoveryHost> hosts)
|
||||
{
|
||||
this->hosts = std::move(hosts);
|
||||
emit HostsUpdated();
|
||||
}
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
class DiscoveryManagerPrivate
|
||||
{
|
||||
public:
|
||||
static void DiscoveryServiceHosts(DiscoveryManager *discovery_manager, const QList<DiscoveryHost> &hosts)
|
||||
{
|
||||
QMetaObject::invokeMethod(discovery_manager, "DiscoveryServiceHosts", Qt::ConnectionType::QueuedConnection, Q_ARG(QList<DiscoveryHost>, hosts));
|
||||
}
|
||||
};
|
||||
|
||||
static void DiscoveryServiceHostsCallback(ChiakiDiscoveryHost *hosts, size_t hosts_count, void *user)
|
||||
{
|
||||
QList<DiscoveryHost> hosts_list;
|
||||
hosts_list.reserve(hosts_count);
|
||||
|
||||
for(size_t i=0; i<hosts_count; i++)
|
||||
{
|
||||
ChiakiDiscoveryHost *h = hosts + i;
|
||||
DiscoveryHost o = {};
|
||||
o.state = h->state;
|
||||
o.host_request_port = o.host_request_port;
|
||||
#define CONVERT_STRING(name) if(h->name) { o.name = QString::fromLocal8Bit(h->name); }
|
||||
CHIAKI_DISCOVERY_HOST_STRING_FOREACH(CONVERT_STRING)
|
||||
#undef CONVERT_STRING
|
||||
hosts_list.append(o);
|
||||
}
|
||||
|
||||
DiscoveryManagerPrivate::DiscoveryServiceHosts(reinterpret_cast<DiscoveryManager *>(user), hosts_list);
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ int RunMain(QApplication &app);
|
|||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
qRegisterMetaType<DiscoveryHost>();
|
||||
qRegisterMetaType<ChiakiQuitReason>();
|
||||
|
||||
ChiakiErrorCode err = chiaki_lib_init();
|
||||
|
|
|
@ -74,6 +74,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
|
|||
}
|
||||
|
||||
resize(800, 600);
|
||||
|
||||
connect(&discovery_manager, &DiscoveryManager::HostsUpdated, this, &MainWindow::DiscoveryHostsUpdated);
|
||||
}
|
||||
|
||||
void MainWindow::ServerItemWidgetSelected()
|
||||
|
@ -108,3 +110,8 @@ void MainWindow::ShowSettings()
|
|||
{
|
||||
qDebug() << "TODO: ShowSettings()";
|
||||
}
|
||||
|
||||
void MainWindow::DiscoveryHostsUpdated()
|
||||
{
|
||||
qDebug() << "updated hosts" << discovery_manager.GetHosts().count();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue