diff --git a/main.cpp b/main.cpp index 9306133..6c36a24 100644 --- a/main.cpp +++ b/main.cpp @@ -28,6 +28,5 @@ int main(int argc, char *argv[]) Server server(sockets); ConsoleReader *reader = new ConsoleReader(sockets); - qWarning() << "event loop"; return app.exec(); } diff --git a/server.cpp b/server.cpp index 0e03c96..1f70a8a 100644 --- a/server.cpp +++ b/server.cpp @@ -29,12 +29,13 @@ Server::Server ( std::vector *sockets_vector ) udpSocketSend->connectToHost ( QHostAddress::Broadcast, 10000 ); udpSocketGet->bind ( QHostAddress::Any, 10000); + connect ( udpSocketGet, &QUdpSocket::readyRead, this, &Server::readPendingDatagrams); + + udpSocketSend->write ( discover ); udpSocketSend->write ( discover ); udpSocketSend->disconnectFromHost(); delete udpSocketSend; - connect ( udpSocketGet, &QUdpSocket::readyRead, this, &Server::readPendingDatagrams); - qWarning() << "starting server"; start(); } @@ -64,6 +65,7 @@ void Server::readPendingDatagrams() { if ( reply.mid ( 4, 2 ) == QByteArray::fromHex ( "71 61" ) ) // Reply to discover packet { + qWarning() << "Discover"; bool duplicate = false; for ( std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) { diff --git a/socket.cpp b/socket.cpp index 4420bfd..28cf720 100644 --- a/socket.cpp +++ b/socket.cpp @@ -50,12 +50,15 @@ Socket::Socket ( QHostAddress IPaddress, QByteArray reply ) udpSocket = new QUdpSocket(); udpSocket->connectToHost ( ip, 10000 ); + connect (this, &Socket::datagramQueued, this, &Socket::listen); subscribed = false; subscribeTimer = new QTimer(this); - subscribeTimer->setInterval(200); // increase later, debug + subscribeTimer->setInterval(2*60*1000); // increase later, debug subscribeTimer->setSingleShot(false); connect(subscribeTimer, &QTimer::timeout, this, &Socket::subscribe); subscribeTimer->start(); + subscribe(); + tableData(); } Socket::~Socket() @@ -64,9 +67,24 @@ Socket::~Socket() delete udpSocket; } +void Socket::sendDatagram ( Datagram d ) +{ + commands.enqueue(d); + Q_EMIT datagramQueued(); +} + +void Socket::run() +{ + while ( commands.size() > 0 ) + { + qWarning() << "sending datagram" << commands.head() << " subscribed = " << subscribed; + udpSocket->write ( datagram[commands.head()] ); + QThread::msleep(100); + } +} + void Socket::subscribe() { - qWarning() << "subscribing"; sendDatagram ( Subscribe ); } @@ -93,12 +111,6 @@ void Socket::tableData() sendDatagram ( TimingData ); } -void Socket::sendDatagram ( Datagram d ) -{ - qWarning() << "sending datagram" << d << " subscribed = " << subscribed; - udpSocket->write ( datagram[d] ); -} - bool Socket::parseReply ( QByteArray reply ) { qWarning() << "parsing datagram"; @@ -144,6 +156,10 @@ bool Socket::parseReply ( QByteArray reply ) powered = reply.right ( 1 ) == one; if (powered != poweredOld) Q_EMIT stateChanged(); + if ( datagram == PowerOff && powered == true ) + { + datagram = PowerOn; + } break; } case TableData: @@ -169,6 +185,15 @@ bool Socket::parseReply ( QByteArray reply ) default: return false; } + + if (commands.size() > 0) + { + if ( datagram == commands.head() ) + { + commands.dequeue(); + } + } + return true; } diff --git a/socket.h b/socket.h index 00a9c1a..64cb114 100644 --- a/socket.h +++ b/socket.h @@ -20,18 +20,21 @@ #include #include +#include #include +#include class QUdpSocket; const QByteArray magicKey = QByteArray::fromHex ( "68 64" ); // recognize datagrams from the socket -class Socket : public QObject +class Socket : public QThread { Q_OBJECT Q_SIGNALS: void stateChanged(); + void datagramQueued(); public: Socket ( QHostAddress, QByteArray ); @@ -52,12 +55,14 @@ private: void sendDatagram ( Datagram ); QByteArray fromIP ( unsigned char, unsigned char, unsigned char, unsigned char ); void subscribe(); + void listen() { start(); } + void run(); QByteArray commandID[MaxCommands]; QByteArray datagram[MaxCommands]; QByteArray rmac; // Reveresed mac QByteArray versionID; - QByteArray socketTableNumber, socketTableVersion, timingTableNumber, timingTableVersion; + QByteArray socketTableNumber, socketTableVersion, timingTableNumber, timingTableVersion; // FIXME: not used yet const QByteArray twenties = QByteArray::fromHex ( "20 20 20 20 20 20" ); // mac address padding const QByteArray zeros = QByteArray::fromHex ( "00 00 00 00" ); @@ -67,6 +72,7 @@ private: QUdpSocket *udpSocket; QTimer *subscribeTimer; bool subscribed; + QQueue commands; };