Move input handling to another thread and enable event loop.
This commit is contained in:
parent
06aeb187d5
commit
748ad8cb91
|
@ -4,5 +4,5 @@ project(s20)
|
||||||
add_definitions(-std=gnu++11)
|
add_definitions(-std=gnu++11)
|
||||||
|
|
||||||
find_package(Qt5 REQUIRED Core Network)
|
find_package(Qt5 REQUIRED Core Network)
|
||||||
add_executable(s20 main.cpp socket.cpp)
|
add_executable(s20 main.cpp socket.cpp consolereader.cpp)
|
||||||
target_link_libraries(s20 Qt5::Core Qt5::Network)
|
target_link_libraries(s20 Qt5::Core Qt5::Network)
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* 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 <iostream>
|
||||||
|
|
||||||
|
#include "consolereader.h"
|
||||||
|
|
||||||
|
ConsoleReader::ConsoleReader(std::vector<Socket> *sockets_vector)
|
||||||
|
{
|
||||||
|
sockets = sockets_vector;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConsoleReader::~ConsoleReader()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConsoleReader::run()
|
||||||
|
{
|
||||||
|
char command;
|
||||||
|
unsigned int number = 0;
|
||||||
|
bool cont = true;
|
||||||
|
|
||||||
|
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();
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
// std::cout << "Enter new name (max 16 characters)" << std::endl;
|
||||||
|
// string name;
|
||||||
|
// std::cin >> name;
|
||||||
|
(*sockets)[number].changeSocketName();
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
(*sockets)[number].toggle();
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
cont = false;
|
||||||
|
emit(QCoreApplication::quit());
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
std::cin >> number;
|
||||||
|
--number; // count from 0
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cout << "Invalid command" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConsoleReader::listSockets()
|
||||||
|
{
|
||||||
|
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 << "___________________________________________________________________________\n" << std::endl;
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*************************************************************************
|
||||||
|
* 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/>.*
|
||||||
|
*************************************************************************/
|
||||||
|
|
||||||
|
#ifndef CONSOLEREADER_H
|
||||||
|
#define CONSOLEREADER_H
|
||||||
|
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QThread>
|
||||||
|
|
||||||
|
#include "socket.h"
|
||||||
|
|
||||||
|
class ConsoleReader : public QThread
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ConsoleReader(std::vector<Socket> *sockets_vector);
|
||||||
|
~ConsoleReader();
|
||||||
|
void run ();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void listSockets();
|
||||||
|
std::vector<Socket> *sockets;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* CONSOLEREADER_H */
|
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
QByteArray discover = QByteArray::fromHex("68 64 00 06 71 61");
|
QByteArray discover = QByteArray::fromHex("68 64 00 06 71 61");
|
||||||
|
|
||||||
void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector<Socket> &sockets)
|
void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector<Socket> *sockets)
|
||||||
{
|
{
|
||||||
while (udpSocketGet->waitForReadyRead(500)) // 500ms
|
while (udpSocketGet->waitForReadyRead(500)) // 500ms
|
||||||
{
|
{
|
||||||
|
@ -35,7 +35,7 @@ void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector<Socket> &socket
|
||||||
if (datagramGet != discover && datagramGet.left(2) == QByteArray::fromHex("68 64"))
|
if (datagramGet != discover && datagramGet.left(2) == QByteArray::fromHex("68 64"))
|
||||||
{
|
{
|
||||||
bool duplicate = false;
|
bool duplicate = false;
|
||||||
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)
|
||||||
{
|
{
|
||||||
if (i->ip == sender)
|
if (i->ip == sender)
|
||||||
duplicate = true;
|
duplicate = true;
|
||||||
|
@ -43,7 +43,7 @@ void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector<Socket> &socket
|
||||||
if(!duplicate)
|
if(!duplicate)
|
||||||
{
|
{
|
||||||
const Socket socket(sender, datagramGet);
|
const Socket socket(sender, datagramGet);
|
||||||
sockets.push_back(socket);
|
sockets->push_back(socket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
55
main.cpp
55
main.cpp
|
@ -18,13 +18,15 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "consolereader.h"
|
||||||
#include "discover.h"
|
#include "discover.h"
|
||||||
|
|
||||||
void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector<Socket> &sockets);
|
|
||||||
void listSockets(std::vector<Socket> const &sockets);
|
void listSockets(std::vector<Socket> const &sockets);
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
QCoreApplication app(argc, argv);
|
||||||
|
|
||||||
QUdpSocket *udpSocketSend = new QUdpSocket();
|
QUdpSocket *udpSocketSend = new QUdpSocket();
|
||||||
QUdpSocket *udpSocketGet = new QUdpSocket();
|
QUdpSocket *udpSocketGet = new QUdpSocket();
|
||||||
|
|
||||||
|
@ -34,54 +36,15 @@ int main(int argc, char *argv[])
|
||||||
udpSocketSend->write(discover);
|
udpSocketSend->write(discover);
|
||||||
udpSocketSend->disconnectFromHost();
|
udpSocketSend->disconnectFromHost();
|
||||||
delete udpSocketSend;
|
delete udpSocketSend;
|
||||||
std::vector<Socket> sockets;
|
std::vector<Socket> *sockets = new std::vector<Socket>;
|
||||||
|
|
||||||
readDiscoverDatagrams(udpSocketGet, sockets);
|
readDiscoverDatagrams(udpSocketGet, sockets);
|
||||||
delete udpSocketGet;
|
delete udpSocketGet;
|
||||||
|
|
||||||
char command;
|
ConsoleReader reader(sockets);
|
||||||
unsigned int number = 0;
|
reader.start();
|
||||||
bool cont=true;
|
|
||||||
while (cont)
|
|
||||||
{
|
|
||||||
listSockets(sockets);
|
|
||||||
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();
|
|
||||||
break;
|
|
||||||
case 'n':
|
|
||||||
// std::cout << "Enter new name (max 16 characters)" << std::endl;
|
|
||||||
// string name;
|
|
||||||
// std::cin >> name;
|
|
||||||
sockets[number].changeSocketName();
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
sockets[number].toggle();
|
|
||||||
break;
|
|
||||||
case 'q':
|
|
||||||
cont = false;
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
std::cin >> number;
|
|
||||||
--number; // count from 0
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
std::cout << "Invalid command" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void listSockets(std::vector<Socket> const &sockets)
|
int exitCode = app.exec();
|
||||||
{
|
reader.wait();
|
||||||
for (std::vector<Socket>::const_iterator i = sockets.begin() ; i != sockets.end(); ++i)
|
return exitCode;
|
||||||
{
|
|
||||||
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 << "___________________________________________________________________________\n" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because one or more lines are too long
5
socket.h
5
socket.h
|
@ -15,6 +15,9 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
|
#ifndef SOCKET_H
|
||||||
|
#define SOCKET_H
|
||||||
|
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
#include <QHostAddress>
|
#include <QHostAddress>
|
||||||
#include <QUdpSocket>
|
#include <QUdpSocket>
|
||||||
|
@ -53,3 +56,5 @@ private:
|
||||||
|
|
||||||
QUdpSocket *udpSocketSend, *udpSocketGet;
|
QUdpSocket *udpSocketSend, *udpSocketGet;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif /* SOCKET_H */
|
||||||
|
|
Loading…
Reference in New Issue