Skip to content

Commit

Permalink
Add support for joinNetwork block (#773)
Browse files Browse the repository at this point in the history
Simple function to join peer network
  • Loading branch information
MinyazevR authored Aug 31, 2024
1 parent eaf52e7 commit 4f56b88
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions trikNetwork/include/trikNetwork/mailboxInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions trikNetwork/src/mailbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions trikNetwork/src/mailbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 15 additions & 0 deletions trikNetwork/src/mailboxServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions trikNetwork/src/mailboxServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4f56b88

Please sign in to comment.