Refactor Socket class to store information about individual sockets.

This commit is contained in:
Andrius Štikonas 2015-01-14 00:50:55 +00:00
parent 1f74a6f1c6
commit dc98486dc7
4 changed files with 100 additions and 56 deletions

View File

@ -1,17 +1,6 @@
cmake_minimum_required(VERSION 2.8)
project(s20)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
# set(CMAKE_AUTOMOC OFF)
SET(CMAKE_CXX_FLAGS "-std=gnu++11")
# Find the QtWidgets library
find_package(Qt5 REQUIRED Core Network)
# Tell CMake to create the helloworld executable
add_executable(s20 s20.cpp)
# Use the Widgets module from Qt 5.
add_executable(s20 main.cpp socket.cpp)
target_link_libraries(s20 Qt5::Network)

View File

@ -15,50 +15,31 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/
#include <QCoreApplication>
#include <iostream>
#include <set>
#include <QByteArray>
#include <QHostAddress>
#include <QCoreApplication>
#include <QUdpSocket>
class Socket : public QObject
#include "socket.h"
void listSockets(std::vector<Socket> const &sockets);
QByteArray discover = QByteArray::fromHex("68 64 00 06 71 61");
int main(int argc, char *argv[])
{
public:
Socket();
QCoreApplication s20(argc, argv);
private:
void readPendingDatagrams();
QByteArray* makeNewDatagram();
QUdpSocket *udpSocketSend = new QUdpSocket();
QUdpSocket *udpSocketGet = new QUdpSocket();
QUdpSocket *udpSocketSend;
QUdpSocket *udpSocketGet;
QHostAddress socketIPAddress;
enum {Discover, Subscribe, PowerOff, PowerOn};
QByteArray datagram[4] = {
QByteArray::fromHex("68 64 00 06 71 61"), // global discovery
QByteArray::fromHex("68 64 00 1e 63 6c ac cf 23 35 f5 8c 20 20 20 20 20 20 8c f5 35 23 cf ac 20 20 20 20 20 20"), // subscribe
QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 00"), // power off
QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 01") // power on
};
};
Socket::Socket() {
udpSocketSend = new QUdpSocket();
udpSocketGet = new QUdpSocket();
// QHostAddress *host = new QHostAddress("192.168.1.212");
QHostAddress *bcast = new QHostAddress("192.168.1.255");
udpSocketSend->connectToHost(*bcast, 10000);
udpSocketSend->connectToHost(QHostAddress::Broadcast, 10000);
udpSocketGet->bind(QHostAddress::Any, 10000);
// QObject::connect(udpSocketGet, &QIODevice::readyRead, this, &Socket::readPendingDatagrams);
udpSocketSend->write(datagram[Discover]);
readPendingDatagrams();
}
void Socket::readPendingDatagrams()
{
udpSocketSend->write(discover);
std::vector<Socket> sockets;
while (udpSocketGet->waitForReadyRead(1000)) // 1s
{
while (udpSocketGet->hasPendingDatagrams())
@ -70,18 +51,31 @@ void Socket::readPendingDatagrams()
udpSocketGet->readDatagram(datagramGet.data(), datagramGet.size(), &sender, &senderPort);
if (datagramGet != datagram[Discover])
if (datagramGet != discover)
{
socketIPAddress = sender;
bool duplicate = false;
for(std::vector<Socket>::const_iterator i = sockets.begin() ; i != sockets.end(); ++i)
{
if (i->ip == sender)
duplicate = true;
}
if(!duplicate)
{
Socket socket(sender, datagramGet);
sockets.push_back(socket);
}
}
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication s20(argc, argv);
Socket socket;
listSockets(sockets);
return 0;
}
void listSockets(std::vector<Socket> const &sockets)
{
for (std::vector<Socket>::const_iterator i = sockets.begin() ; i != sockets.end(); ++i)
{
std::cout << "IP Address: " << i->ip.toString().toStdString() << "\t MAC Address: " << i->mac.toHex().toStdString() << "\t Powered: " << i->powered << std::endl;
}
}

25
socket.cpp Normal file
View File

@ -0,0 +1,25 @@
/*************************************************************************
* 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 "socket.h"
Socket::Socket(QHostAddress IPaddress, QByteArray reply)
{
ip = IPaddress;
mac = reply.mid(7, 6);
powered = reply.right(1) == QByteArray::fromHex("01");
}

36
socket.h Normal file
View File

@ -0,0 +1,36 @@
/*************************************************************************
* 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 <QByteArray>
#include <QHostAddress>
class Socket
{
public:
Socket(QHostAddress, QByteArray);
QHostAddress ip;
QByteArray mac;
bool powered;
enum {Subscribe, PowerOff, PowerOn};
private:
QByteArray datagram[3];
// QByteArray::fromHex("68 64 00 1e 63 6c ac cf 23 35 f5 8c 20 20 20 20 20 20 8c f5 35 23 cf ac 20 20 20 20 20 20"), // subscribe
// QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 00"), // power off
// QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 01") // power on
};