17 #include <QtCore/QObject> 18 #include <QtCore/QMultiHash> 19 #include <QtCore/QReadWriteLock> 20 #include <QtCore/QQueue> 21 #include <QtNetwork/QHostAddress> 54 QHostAddress
myIp()
const;
60 Q_INVOKABLE
void start();
66 Q_INVOKABLE
void connect(
const QString &ip,
int port);
69 Q_INVOKABLE
void connect(
const QString &ip);
72 Q_INVOKABLE
void send(
int hullNumber,
const QString &message);
75 Q_INVOKABLE
void send(
const QString &message);
85 void newMessage(
int senderHullNumber,
const QString &message);
88 void onNewConnection(
const QHostAddress &ip,
int clientPort,
int serverPort,
int hullNumber);
89 void onConnectionInfo(
const QHostAddress &ip,
int port,
int hullNumber);
90 void onNewData(
const QHostAddress &ip,
int port,
const QByteArray &data);
99 static QHostAddress determineMyIp();
101 Connection *prepareConnection(
const QHostAddress &ip);
106 void forEveryConnection(std::function<
void(
Connection *)> method,
int hullNumber = -1);
110 QHostAddress mSavedIp;
113 QHostAddress mServerIp;
121 inline uint qHash(
const Endpoint &key)
123 return ::qHash(key.ip.toString()) ^ key.port;
126 friend bool operator ==(
const MailboxServer::Endpoint &left,
const MailboxServer::Endpoint &right);
127 friend inline QDebug
operator <<(QDebug dbg,
const Endpoint &endpoint);
129 QMultiHash<int, Endpoint> mKnownRobots;
131 QQueue<QByteArray> mMessagesQueue;
132 QReadWriteLock mMessagesQueueLock;
133 QReadWriteLock mKnownRobotsLock;
135 QReadWriteLock mAuxiliaryInformationLock;
138 inline bool operator ==(
const MailboxServer::Endpoint &left,
const MailboxServer::Endpoint &right)
140 return left.ip == right.ip && left.port == right.port;
143 inline QDebug
operator <<(QDebug dbg,
const MailboxServer::Endpoint &endpoint)
145 dbg.nospace() << endpoint.ip <<
":" << endpoint.port;
void renewIp()
Tries to update our own IP address.
Definition: mailboxServer.cpp:73
QHostAddress myIp() const
Returns our own IP address or empty QHostAddress if we have none.
Definition: mailboxServer.cpp:68
friend QDebug operator<<(QDebug dbg, const Endpoint &endpoint)
Definition: mailboxServer.h:143
static const int port
Definition: trikCommunicatorTest.cpp:24
bool isConnected()
Returns true if at least one opened mailbox connection presents at the moment.
Definition: mailboxServer.cpp:38
friend bool operator==(const MailboxServer::Endpoint &left, const MailboxServer::Endpoint &right)
Definition: mailboxServer.h:138
Worker object for mailbox functionality.
Definition: mailboxServer.h:35
MailboxServer(int port)
Constructor.
Definition: mailboxServer.cpp:27
void newMessage(int senderHullNumber, const QString &message)
Emitted when new message was received from a robot with given hull number.
QHostAddress serverIp()
Returns IP of leader robot to which we connected last.
Definition: mailboxServer.cpp:60
Q_INVOKABLE void setHullNumber(int hullNumber)
Sets hull number for this robot and notifies all known robots about change.
Definition: mailboxServer.cpp:87
Server that can handle multiple clients. Actual work is done in separate threads by Connection object...
Definition: trikServer.h:31
Definition: trikCommunicator.h:31
Q_INVOKABLE void start()
Launches server, supposed to be called when mailbox server is already in a separate thread...
Definition: mailboxServer.cpp:78
int hullNumber() const
Returns hull number of this robot.
Definition: mailboxServer.cpp:55
Q_INVOKABLE void connect(const QString &ip, int port)
Connects to robot by IP and port.
Definition: mailboxServer.cpp:101
Abstract class that serves one client of TrikServer.
Definition: connection.h:50
Connection * connection(const QHostAddress &ip, int port) const
Searches connection to given IP and port in a list of all open connections.
Definition: trikServer.cpp:98
Q_INVOKABLE QString receive()
Returns one incoming message or empty string if there are none.
Definition: mailboxServer.cpp:354
Q_INVOKABLE bool hasMessages()
Returns true if there are incoming messages.
Definition: mailboxServer.cpp:345
Q_INVOKABLE void send(int hullNumber, const QString &message)
Sends message to a robot with given hull number.
Definition: mailboxServer.cpp:278