From 4f56b885199e43559e1ac436da7137fb24dac5dc Mon Sep 17 00:00:00 2001 From: MinyazevR <89993880+MinyazevR@users.noreply.github.com> Date: Sat, 31 Aug 2024 14:49:19 +0300 Subject: [PATCH] Add support for joinNetwork block (#773) Simple function to join peer network --- .../include/trikNetwork/mailboxInterface.h | 3 +++ trikNetwork/src/mailbox.cpp | 5 +++++ trikNetwork/src/mailbox.h | 2 ++ trikNetwork/src/mailboxServer.cpp | 15 +++++++++++++++ trikNetwork/src/mailboxServer.h | 4 ++++ 5 files changed, 29 insertions(+) diff --git a/trikNetwork/include/trikNetwork/mailboxInterface.h b/trikNetwork/include/trikNetwork/mailboxInterface.h index 7ed62d7c9..fc952a822 100644 --- a/trikNetwork/include/trikNetwork/mailboxInterface.h +++ b/trikNetwork/include/trikNetwork/mailboxInterface.h @@ -54,6 +54,9 @@ class TRIKNETWORK_EXPORT MailboxInterface : public QObject /// Returns true if mailbox is enabled in current configuration. virtual bool isEnabled() = 0; + /// Sets hull number of this robot and connects to robot by IP and port. + virtual void joinNetwork(const QString &ip, int port, int hullNumber) = 0; + public slots: /// Connects to robot by IP and port. virtual void connect(const QString &ip, int port) = 0; diff --git a/trikNetwork/src/mailbox.cpp b/trikNetwork/src/mailbox.cpp index ebd9f2c73..5f3296723 100644 --- a/trikNetwork/src/mailbox.cpp +++ b/trikNetwork/src/mailbox.cpp @@ -46,6 +46,11 @@ Mailbox::~Mailbox() } } +void Mailbox::joinNetwork(const QString &ip, int port, int hullNumber) +{ + QMetaObject::invokeMethod(mWorker.data(), [=](){mWorker->joinNetwork(ip, port, hullNumber);}); +} + bool Mailbox::isConnected() const { bool res; diff --git a/trikNetwork/src/mailbox.h b/trikNetwork/src/mailbox.h index c60464a29..d6dfe250a 100644 --- a/trikNetwork/src/mailbox.h +++ b/trikNetwork/src/mailbox.h @@ -61,6 +61,8 @@ class Mailbox : public MailboxInterface /// True iff the server is running. Q_INVOKABLE bool hasServer() const; + Q_INVOKABLE void joinNetwork(const QString &ip, int port = -1, int hullNumber = -1) override; + public slots: void connect(const QString &ip, int port) override; diff --git a/trikNetwork/src/mailboxServer.cpp b/trikNetwork/src/mailboxServer.cpp index 83ee78481..a38f025a6 100644 --- a/trikNetwork/src/mailboxServer.cpp +++ b/trikNetwork/src/mailboxServer.cpp @@ -36,6 +36,21 @@ MailboxServer::MailboxServer(quint16 port) loadSettings(); } +void MailboxServer::joinNetwork(const QString &ip, int port, int hullNumber) +{ + if (hullNumber != - 1) { + setHullNumber(hullNumber); + } + if (ip == "") { + return; + } + if (port == -1) { + connectTo(ip, mMyPort); + return; + } + connectTo(ip, port); +} + bool MailboxServer::isConnected() { return activeConnections() > 0; diff --git a/trikNetwork/src/mailboxServer.h b/trikNetwork/src/mailboxServer.h index 225588a84..2c1516379 100644 --- a/trikNetwork/src/mailboxServer.h +++ b/trikNetwork/src/mailboxServer.h @@ -84,6 +84,10 @@ class MailboxServer : public TrikServer /// Returns true iff the server was started and is listening. bool hasServer() const; + /// Sets hull number of this robot and connects to robot by IP and port. + Q_INVOKABLE void joinNetwork(const QString &ip = "", int port = -1, int hullNumber = -1); + + signals: /// Emitted when new message was received from a robot with given hull number. void newMessage(int senderHullNumber, const QString &message);