trikRuntime
mailboxServer.h
Go to the documentation of this file.
1 /* Copyright 2014 - 2015 CyberTech Labs Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14 
15 #pragma once
16 
17 #include <QtCore/QObject>
18 #include <QtCore/QMultiHash>
19 #include <QtCore/QReadWriteLock>
20 #include <QtCore/QQueue>
21 #include <QtNetwork/QHostAddress>
22 
23 #include "trikServer.h"
24 
25 namespace trikNetwork {
26 
35 class MailboxServer : public TrikServer
36 {
37  Q_OBJECT
38 
39 public:
42  MailboxServer(int port);
43 
45  bool isConnected();
46 
48  int hullNumber() const;
49 
51  QHostAddress serverIp();
52 
54  QHostAddress myIp() const;
55 
57  void renewIp();
58 
60  Q_INVOKABLE void start();
61 
63  Q_INVOKABLE void setHullNumber(int hullNumber);
64 
66  Q_INVOKABLE void connect(const QString &ip, int port);
67 
69  Q_INVOKABLE void connect(const QString &ip);
70 
72  Q_INVOKABLE void send(int hullNumber, const QString &message);
73 
75  Q_INVOKABLE void send(const QString &message);
76 
78  Q_INVOKABLE bool hasMessages();
79 
81  Q_INVOKABLE QString receive();
82 
83 signals:
85  void newMessage(int senderHullNumber, const QString &message);
86 
87 private slots:
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);
91 
92 private:
93  Connection *connect(const QHostAddress &ip, int port);
94 
95  Connection *connectionFactory();
96 
97  void connectConnection(Connection * connection);
98 
99  static QHostAddress determineMyIp();
100 
101  Connection *prepareConnection(const QHostAddress &ip);
102 
103  void loadSettings();
104  void saveSettings();
105 
106  void forEveryConnection(std::function<void(Connection *)> method, int hullNumber = -1);
107 
108  int mHullNumber;
109  QHostAddress mMyIp;
110  QHostAddress mSavedIp;
111  const int mMyPort;
113  QHostAddress mServerIp;
114  int mServerPort{};
115 
116  struct Endpoint {
117  QHostAddress ip;
118  int port;
119  };
120 
121  inline uint qHash(const Endpoint &key)
122  {
123  return ::qHash(key.ip.toString()) ^ key.port;
124  }
125 
126  friend bool operator ==(const MailboxServer::Endpoint &left, const MailboxServer::Endpoint &right);
127  friend inline QDebug operator <<(QDebug dbg, const Endpoint &endpoint);
128 
129  QMultiHash<int, Endpoint> mKnownRobots;
130 
131  QQueue<QByteArray> mMessagesQueue;
132  QReadWriteLock mMessagesQueueLock;
133  QReadWriteLock mKnownRobotsLock;
134 
135  QReadWriteLock mAuxiliaryInformationLock;
136 };
137 
138 inline bool operator ==(const MailboxServer::Endpoint &left, const MailboxServer::Endpoint &right)
139 {
140  return left.ip == right.ip && left.port == right.port;
141 }
142 
143 inline QDebug operator <<(QDebug dbg, const MailboxServer::Endpoint &endpoint)
144 {
145  dbg.nospace() << endpoint.ip << ":" << endpoint.port;
146  return dbg.space();
147 }
148 
149 }
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