Make UDP datagrams asynchronous.
This commit is contained in:
parent
748ad8cb91
commit
5e035a4448
|
@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 2.8)
|
|||
project(s20)
|
||||
|
||||
add_definitions(-std=gnu++11)
|
||||
set(CMAKE_AUTOMOC TRUE)
|
||||
|
||||
find_package(Qt5 REQUIRED Core Network)
|
||||
add_executable(s20 main.cpp socket.cpp consolereader.cpp)
|
||||
add_executable(s20 main.cpp socket.cpp consolereader.cpp server.cpp)
|
||||
target_link_libraries(s20 Qt5::Core Qt5::Network)
|
||||
|
|
|
@ -19,14 +19,10 @@
|
|||
|
||||
#include "consolereader.h"
|
||||
|
||||
ConsoleReader::ConsoleReader(std::vector<Socket> *sockets_vector)
|
||||
ConsoleReader::ConsoleReader ( std::vector<Socket*> *sockets_vector )
|
||||
{
|
||||
sockets = sockets_vector;
|
||||
}
|
||||
|
||||
ConsoleReader::~ConsoleReader()
|
||||
{
|
||||
//
|
||||
start();
|
||||
}
|
||||
|
||||
void ConsoleReader::run()
|
||||
|
@ -37,22 +33,20 @@ void ConsoleReader::run()
|
|||
|
||||
while ( cont )
|
||||
{
|
||||
listSockets();
|
||||
std::cout << "d - update table data\nn - change socket name\ns - pick another socket (default is 1)\np - toggle power state\nq - quit" << std::endl;
|
||||
std::cin >> command;
|
||||
switch ( command )
|
||||
{
|
||||
case 'd':
|
||||
(*sockets)[number].tableData();
|
||||
( *sockets ) [number]->tableData();
|
||||
break;
|
||||
case 'n':
|
||||
// std::cout << "Enter new name (max 16 characters)" << std::endl;
|
||||
// string name;
|
||||
// std::cin >> name;
|
||||
(*sockets)[number].changeSocketName();
|
||||
( *sockets ) [number]->changeSocketName();
|
||||
break;
|
||||
case 'p':
|
||||
(*sockets)[number].toggle();
|
||||
( *sockets ) [number]->toggle();
|
||||
break;
|
||||
case 'q':
|
||||
cont = false;
|
||||
|
@ -61,20 +55,22 @@ void ConsoleReader::run()
|
|||
case 's':
|
||||
std::cin >> number;
|
||||
--number; // count from 0
|
||||
listSockets();
|
||||
break;
|
||||
default:
|
||||
std::cout << "Invalid command" << std::endl;
|
||||
std::cout << "Invalid command: try again" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleReader::listSockets()
|
||||
{
|
||||
for (std::vector<Socket>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i)
|
||||
for ( std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i )
|
||||
{
|
||||
std::cout << "___________________________________________________________________________\n" << std::endl;
|
||||
std::cout << "IP Address: " << i->ip.toString().toStdString() << "\t MAC Address: " << i->mac.toHex().toStdString() << "\t Power: " << (i->powered ? "On" : "Off") << std::endl;
|
||||
std::cout << "Socket Name: " << i->name.toStdString() << "\t Remote Password: " << i->remotePassword.toStdString() << std::endl;
|
||||
std::cout << "IP Address: " << (*i)->ip.toString().toStdString() << "\t MAC Address: " << (*i)->mac.toHex().toStdString() << "\t Power: " << ( (*i)->powered ? "On" : "Off" ) << std::endl;
|
||||
std::cout << "Socket Name: " << (*i)->name.toStdString() << "\t Remote Password: " << (*i)->remotePassword.toStdString() << std::endl;
|
||||
}
|
||||
std::cout << "___________________________________________________________________________\n" << std::endl;
|
||||
std::cout << "d - update table data\nn - change socket name\ns - pick another socket (default is 1)\np - toggle power state\nq - quit" << std::endl;
|
||||
}
|
||||
|
|
|
@ -26,13 +26,12 @@
|
|||
class ConsoleReader : public QThread
|
||||
{
|
||||
public:
|
||||
ConsoleReader(std::vector<Socket> *sockets_vector);
|
||||
~ConsoleReader();
|
||||
ConsoleReader ( std::vector<Socket*> *sockets_vector );
|
||||
void run ();
|
||||
void listSockets();
|
||||
|
||||
private:
|
||||
void listSockets();
|
||||
std::vector<Socket> *sockets;
|
||||
std::vector<Socket*> *sockets;
|
||||
};
|
||||
|
||||
#endif /* CONSOLEREADER_H */
|
||||
|
|
34
main.cpp
34
main.cpp
|
@ -19,32 +19,22 @@
|
|||
#include <vector>
|
||||
|
||||
#include "consolereader.h"
|
||||
#include "discover.h"
|
||||
|
||||
void listSockets(std::vector<Socket> const &sockets);
|
||||
#include "server.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
QUdpSocket *udpSocketSend = new QUdpSocket();
|
||||
QUdpSocket *udpSocketGet = new QUdpSocket();
|
||||
std::vector<Socket*> *sockets = new std::vector<Socket*>;
|
||||
Server server(sockets);
|
||||
ConsoleReader *reader = new ConsoleReader(sockets);
|
||||
QThread::sleep(2);
|
||||
server.readPendingDatagrams();
|
||||
|
||||
udpSocketSend->connectToHost(QHostAddress::Broadcast, 10000);
|
||||
udpSocketGet->bind(QHostAddress::Any, 10000, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
|
||||
|
||||
udpSocketSend->write(discover);
|
||||
udpSocketSend->disconnectFromHost();
|
||||
delete udpSocketSend;
|
||||
std::vector<Socket> *sockets = new std::vector<Socket>;
|
||||
|
||||
readDiscoverDatagrams(udpSocketGet, sockets);
|
||||
delete udpSocketGet;
|
||||
|
||||
ConsoleReader reader(sockets);
|
||||
reader.start();
|
||||
|
||||
int exitCode = app.exec();
|
||||
reader.wait();
|
||||
return exitCode;
|
||||
for ( unsigned i = 0; i < sockets->size(); ++i )
|
||||
{
|
||||
QObject::connect((*sockets)[i], &Socket::stateChanged, reader, &ConsoleReader::listSockets);
|
||||
}
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
/*************************************************************************
|
||||
* Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation; either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
||||
*************************************************************************/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "consolereader.h"
|
||||
#include "server.h"
|
||||
|
||||
Server::Server ( std::vector<Socket*> *sockets_vector )
|
||||
{
|
||||
sockets = sockets_vector;
|
||||
QUdpSocket *udpSocketSend = new QUdpSocket();
|
||||
udpSocketGet = new QUdpSocket();
|
||||
|
||||
udpSocketSend->connectToHost ( QHostAddress::Broadcast, 10000 );
|
||||
udpSocketGet->bind ( QHostAddress::Any, 10000);
|
||||
|
||||
udpSocketSend->write ( discover );
|
||||
udpSocketSend->disconnectFromHost();
|
||||
delete udpSocketSend;
|
||||
|
||||
connect ( udpSocketGet, &QUdpSocket::readyRead, this, &Server::readPendingDatagrams );
|
||||
}
|
||||
|
||||
Server::~Server()
|
||||
{
|
||||
delete udpSocketGet;
|
||||
}
|
||||
|
||||
void Server::readPendingDatagrams()
|
||||
{
|
||||
while ( udpSocketGet->hasPendingDatagrams() )
|
||||
{
|
||||
QByteArray reply;
|
||||
reply.resize ( udpSocketGet->pendingDatagramSize() );
|
||||
QHostAddress sender;
|
||||
quint16 senderPort;
|
||||
|
||||
udpSocketGet->readDatagram ( reply.data(), reply.size(), &sender, &senderPort );
|
||||
|
||||
if ( reply != discover && reply.left ( 2 ) == magicKey ) // check for Magic Key
|
||||
{
|
||||
if ( reply.mid ( 4, 2 ) == QByteArray::fromHex ( "71 61" ) ) // Reply to discover packet
|
||||
{
|
||||
bool duplicate = false;
|
||||
for ( std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i )
|
||||
{
|
||||
if ( (*i)->ip == sender )
|
||||
{
|
||||
duplicate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( !duplicate )
|
||||
{
|
||||
Socket *socket = new Socket ( sender, reply );
|
||||
sockets->push_back ( socket );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
QByteArray mac = reply.mid(6,6);
|
||||
for ( std::vector<Socket*>::iterator i = sockets->begin() ; i != sockets->end(); ++i )
|
||||
{
|
||||
if ( (*i)->mac == mac )
|
||||
{
|
||||
(*i)->parseReply(reply);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,37 +15,24 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
||||
*************************************************************************/
|
||||
|
||||
#ifndef SERVER_H
|
||||
#define SERVER_H
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
class Server : public QObject
|
||||
{
|
||||
public:
|
||||
Server ( std::vector<Socket*> *sockets_vector );
|
||||
~Server();
|
||||
|
||||
void discoverSockets ();
|
||||
void readPendingDatagrams();
|
||||
|
||||
private:
|
||||
QByteArray discover = QByteArray::fromHex ( "68 64 00 06 71 61" );
|
||||
QUdpSocket *udpSocketGet;
|
||||
std::vector<Socket*> *sockets;
|
||||
};
|
||||
|
||||
void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector<Socket> *sockets)
|
||||
{
|
||||
while (udpSocketGet->waitForReadyRead(500)) // 500ms
|
||||
{
|
||||
while (udpSocketGet->hasPendingDatagrams())
|
||||
{
|
||||
QByteArray datagramGet;
|
||||
datagramGet.resize(udpSocketGet->pendingDatagramSize());
|
||||
QHostAddress sender;
|
||||
quint16 senderPort;
|
||||
|
||||
udpSocketGet->readDatagram(datagramGet.data(), datagramGet.size(), &sender, &senderPort);
|
||||
|
||||
if (datagramGet != discover && datagramGet.left(2) == QByteArray::fromHex("68 64"))
|
||||
{
|
||||
bool duplicate = false;
|
||||
for(std::vector<Socket>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i)
|
||||
{
|
||||
if (i->ip == sender)
|
||||
duplicate = true;
|
||||
}
|
||||
if(!duplicate)
|
||||
{
|
||||
const Socket socket(sender, datagramGet);
|
||||
sockets->push_back(socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* SERVER_H */
|
65
socket.cpp
65
socket.cpp
File diff suppressed because one or more lines are too long
19
socket.h
19
socket.h
|
@ -20,12 +20,21 @@
|
|||
|
||||
#include <QByteArray>
|
||||
#include <QHostAddress>
|
||||
#include <QTimer>
|
||||
#include <QUdpSocket>
|
||||
|
||||
class Socket
|
||||
const QByteArray magicKey = QByteArray::fromHex ( "68 64" ); // recognize datagrams from the socket
|
||||
|
||||
class Socket : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_SIGNALS:
|
||||
void stateChanged();
|
||||
|
||||
public:
|
||||
Socket ( QHostAddress, QByteArray );
|
||||
~Socket();
|
||||
void toggle();
|
||||
void tableData();
|
||||
void changeSocketName ( /*QString name*/ );
|
||||
|
@ -40,21 +49,23 @@ private:
|
|||
enum Datagram {Subscribe, PowerOff, PowerOn, TableData, SocketData, TimingData, WriteSocketData, MaxCommands};
|
||||
|
||||
void sendDatagram ( Datagram );
|
||||
void readDatagrams(QUdpSocket *udpSocketGet);
|
||||
QByteArray fromIP ( unsigned char, unsigned char, unsigned char, unsigned char );
|
||||
void subscribe();
|
||||
|
||||
QByteArray commandID[MaxCommands];
|
||||
QByteArray datagram[MaxCommands];
|
||||
QByteArray rmac; // Reveresed mac
|
||||
QByteArray socketTableNumber, socketTableVersion, timingTableNumber, timingTableVersion;
|
||||
|
||||
const QByteArray magicKey = QByteArray::fromHex("68 64"); // recognize datagrams from the socket
|
||||
const QByteArray twenties = QByteArray::fromHex ( "20 20 20 20 20 20" ); // mac address padding
|
||||
const QByteArray zeros = QByteArray::fromHex ( "00 00 00 00" );
|
||||
const QByteArray zero = QByteArray::fromHex ( "00" );
|
||||
const QByteArray one = QByteArray::fromHex ( "01" );
|
||||
|
||||
QUdpSocket *udpSocketSend, *udpSocketGet;
|
||||
QUdpSocket *udpSocket;
|
||||
|
||||
QTimer *subscribeTimer;
|
||||
|
||||
};
|
||||
|
||||
#endif /* SOCKET_H */
|
||||
|
|
Loading…
Reference in New Issue