2013-11-08 22:18:10 +01:00
|
|
|
// system includes
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
// project includes
|
|
|
|
#include <boblightserver/BoblightServer.h>
|
|
|
|
#include "BoblightClientConnection.h"
|
|
|
|
|
2016-03-08 17:31:56 +01:00
|
|
|
BoblightServer::BoblightServer(Hyperion *hyperion, const int priority,uint16_t port) :
|
2013-11-08 22:18:10 +01:00
|
|
|
QObject(),
|
|
|
|
_hyperion(hyperion),
|
|
|
|
_server(),
|
2016-03-08 17:31:56 +01:00
|
|
|
_openConnections(),
|
|
|
|
_priority(priority)
|
2013-11-08 22:18:10 +01:00
|
|
|
{
|
|
|
|
if (!_server.listen(QHostAddress::Any, port))
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
throw std::runtime_error("BOBLIGHT ERROR: server could not bind to port");
|
2013-11-08 22:18:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set trigger for incoming connections
|
|
|
|
connect(&_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
|
|
|
|
}
|
|
|
|
|
|
|
|
BoblightServer::~BoblightServer()
|
|
|
|
{
|
|
|
|
foreach (BoblightClientConnection * connection, _openConnections) {
|
|
|
|
delete connection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t BoblightServer::getPort() const
|
|
|
|
{
|
|
|
|
return _server.serverPort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoblightServer::newConnection()
|
|
|
|
{
|
|
|
|
QTcpSocket * socket = _server.nextPendingConnection();
|
|
|
|
|
|
|
|
if (socket != nullptr)
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "BOBLIGHT INFO: new connection" << std::endl;
|
2016-03-08 17:31:56 +01:00
|
|
|
BoblightClientConnection * connection = new BoblightClientConnection(socket, _priority, _hyperion);
|
2013-11-08 22:18:10 +01:00
|
|
|
_openConnections.insert(connection);
|
|
|
|
|
|
|
|
// register slot for cleaning up after the connection closed
|
|
|
|
connect(connection, SIGNAL(connectionClosed(BoblightClientConnection*)), this, SLOT(closedConnection(BoblightClientConnection*)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoblightServer::closedConnection(BoblightClientConnection *connection)
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "BOBLIGHT INFO: connection closed" << std::endl;
|
2013-11-08 22:18:10 +01:00
|
|
|
_openConnections.remove(connection);
|
|
|
|
|
|
|
|
// schedule to delete the connection object
|
|
|
|
connection->deleteLater();
|
|
|
|
}
|