添加广播MDNS服务

This commit is contained in:
2024-08-19 09:39:32 +08:00
parent 1f7bc017ca
commit 0a5b0db9a5
192 changed files with 22181 additions and 616 deletions

View File

@@ -0,0 +1,18 @@
set(SRC
browser.cpp
mainwindow.cpp
servicemodel.cpp
)
add_executable(browser WIN32 ${SRC})
set_target_properties(browser PROPERTIES
CXX_STANDARD 11
)
target_link_libraries(browser qmdnsengine Qt${QT_VERSION_MAJOR}::Widgets)
install(TARGETS browser
RUNTIME DESTINATION "${EXAMPLE_DIR}"
COMPONENT examples
)

View File

@@ -0,0 +1,37 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}

View File

@@ -0,0 +1,145 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <QCheckBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLineEdit>
#include <QListView>
#include <QListWidget>
#include <QPushButton>
#include <QSplitter>
#include <QTableWidget>
#include <QVBoxLayout>
#include <QWidget>
#include <qmdnsengine/mdns.h>
#include <qmdnsengine/service.h>
#include "mainwindow.h"
#include "servicemodel.h"
Q_DECLARE_METATYPE(QMdnsEngine::Service)
MainWindow::MainWindow()
: mServiceModel(nullptr),
mResolver(nullptr)
{
setWindowTitle(tr("mDNS Browser"));
resize(640, 480);
mServiceType = new QLineEdit(tr("_http._tcp.local."));
mStartStop = new QPushButton(tr("Browse"));
mServices = new QListView;
mAddresses = new QListWidget;
mAttributes = new QTableWidget;
mAttributes->setSelectionBehavior(QAbstractItemView::SelectRows);
QVBoxLayout *rootLayout = new QVBoxLayout;
QWidget *widget = new QWidget;
widget->setLayout(rootLayout);
setCentralWidget(widget);
QCheckBox *any = new QCheckBox(tr("Any"));
QHBoxLayout *typeLayout = new QHBoxLayout;
typeLayout->addWidget(mServiceType, 1);
typeLayout->addWidget(any);
typeLayout->addWidget(mStartStop);
rootLayout->addLayout(typeLayout);
QSplitter *vSplitter = new QSplitter;
vSplitter->setOrientation(Qt::Vertical);
vSplitter->addWidget(mAddresses);
vSplitter->addWidget(mAttributes);
QSplitter *hSplitter = new QSplitter;
hSplitter->addWidget(mServices);
hSplitter->addWidget(vSplitter);
QHBoxLayout *servicesLayout = new QHBoxLayout;
servicesLayout->addWidget(hSplitter);
rootLayout->addLayout(servicesLayout);
connect(any, &QCheckBox::toggled, this, &MainWindow::onToggled);
connect(mStartStop, &QPushButton::clicked, this, &MainWindow::onClicked);
}
void MainWindow::onToggled(bool checked)
{
if (checked) {
mServiceType->setText(QMdnsEngine::MdnsBrowseType);
}
mServiceType->setEnabled(!checked);
}
void MainWindow::onClicked()
{
if (mServiceModel) {
mServices->setModel(nullptr);
delete mServiceModel;
mAttributes->clear();
mAttributes->setColumnCount(0);
}
mServiceModel = new ServiceModel(&mServer, mServiceType->text().toUtf8());
mServices->setModel(mServiceModel);
connect(mServices->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MainWindow::onSelectionChanged);
}
void MainWindow::onSelectionChanged(const QItemSelection &selected, const QItemSelection &)
{
mAddresses->clear();
mAttributes->clear();
mAttributes->setColumnCount(0);
if (mResolver) {
delete mResolver;
mResolver = nullptr;
}
if (selected.count()) {
auto service = mServiceModel->data(selected.at(0).topLeft(), Qt::UserRole).value<QMdnsEngine::Service>();
// Show TXT values
auto attributes = service.attributes();
mAttributes->setRowCount(attributes.keys().count());
mAttributes->setColumnCount(2);
mAttributes->setHorizontalHeaderLabels({tr("Key"), tr("Value")});
mAttributes->horizontalHeader()->setStretchLastSection(true);
mAttributes->verticalHeader()->setVisible(false);
int row = 0;
for (auto i = attributes.constBegin(); i != attributes.constEnd(); ++i, ++row) {
mAttributes->setItem(row, 0, new QTableWidgetItem(QString(i.key())));
mAttributes->setItem(row, 1, new QTableWidgetItem(QString(i.value())));
}
// Resolve the address
mResolver = new QMdnsEngine::Resolver(&mServer, service.hostname(), nullptr, this);
connect(mResolver, &QMdnsEngine::Resolver::resolved, [this](const QHostAddress &address) {
mAddresses->addItem(address.toString());
});
}
}

View File

@@ -0,0 +1,70 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <qmdnsengine/server.h>
#include <qmdnsengine/resolver.h>
class QItemSelection;
class QLineEdit;
class QListView;
class QListWidget;
class QPushButton;
class QTableWidget;
class ServiceModel;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
private Q_SLOTS:
void onToggled(bool checked);
void onClicked();
void onSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
private:
QMdnsEngine::Server mServer;
ServiceModel *mServiceModel;
QLineEdit *mServiceType;
QPushButton *mStartStop;
QListView *mServices;
QListWidget *mAddresses;
QTableWidget *mAttributes;
QMdnsEngine::Resolver *mResolver;
};
#endif // MAINWINDOW_H

View File

@@ -0,0 +1,97 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "servicemodel.h"
Q_DECLARE_METATYPE(QMdnsEngine::Service)
ServiceModel::ServiceModel(QMdnsEngine::Server *server, const QByteArray &type)
: mBrowser(server, type, &cache)
{
connect(&mBrowser, &QMdnsEngine::Browser::serviceAdded, this, &ServiceModel::onServiceAdded);
connect(&mBrowser, &QMdnsEngine::Browser::serviceUpdated, this, &ServiceModel::onServiceUpdated);
connect(&mBrowser, &QMdnsEngine::Browser::serviceRemoved, this, &ServiceModel::onServiceRemoved);
}
int ServiceModel::rowCount(const QModelIndex &) const
{
return mServices.count();
}
QVariant ServiceModel::data(const QModelIndex &index, int role) const
{
// Ensure the index points to a valid row
if (!index.isValid() || index.row() < 0 || index.row() >= mServices.count()) {
return QVariant();
}
QMdnsEngine::Service service = mServices.at(index.row());
switch (role) {
case Qt::DisplayRole:
return QString("%1 (%2)")
.arg(QString(service.name()))
.arg(QString(service.type()));
case Qt::UserRole:
return QVariant::fromValue(service);
}
return QVariant();
}
void ServiceModel::onServiceAdded(const QMdnsEngine::Service &service)
{
beginInsertRows(QModelIndex(), mServices.count(), mServices.count());
mServices.append(service);
endInsertRows();
}
void ServiceModel::onServiceUpdated(const QMdnsEngine::Service &service)
{
int i = findService(service.name());
if (i != -1) {
mServices.replace(i, service);
emit dataChanged(index(i), index(i));
}
}
void ServiceModel::onServiceRemoved(const QMdnsEngine::Service &service)
{
int i = findService(service.name());
if (i != -1) {
beginRemoveRows(QModelIndex(), i, i);
mServices.removeAt(i);
endRemoveRows();
}
}
int ServiceModel::findService(const QByteArray &name)
{
for (auto i = mServices.constBegin(); i != mServices.constEnd(); ++i) {
if ((*i).name() == name) {
return i - mServices.constBegin();
}
}
return -1;
}

View File

@@ -0,0 +1,62 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Nathan Osman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef SERVICEMODEL_H
#define SERVICEMODEL_H
#include <QAbstractListModel>
#include <QList>
#include <qmdnsengine/browser.h>
#include <qmdnsengine/cache.h>
#include <qmdnsengine/service.h>
#include <qmdnsengine/server.h>
class ServiceModel : public QAbstractListModel
{
Q_OBJECT
public:
ServiceModel(QMdnsEngine::Server *server, const QByteArray &type);
virtual int rowCount(const QModelIndex &parent) const;
virtual QVariant data(const QModelIndex &index, int role) const;
private Q_SLOTS:
void onServiceAdded(const QMdnsEngine::Service &service);
void onServiceUpdated(const QMdnsEngine::Service &service);
void onServiceRemoved(const QMdnsEngine::Service &service);
private:
int findService(const QByteArray &name);
QMdnsEngine::Cache cache;
QMdnsEngine::Browser mBrowser;
QList<QMdnsEngine::Service> mServices;
};
#endif // SERVICEMODEL_H