forked from magnocube/POV-Globe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myudp.cpp
99 lines (80 loc) · 2.33 KB
/
myudp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "myudp.h"
MyUDP::MyUDP(QObject *parent, QString ip) : QObject(parent)
{
qDebug() << "udp setup"<<endl;
QNetworkInterface hotspot;
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
{
if((interface.flags().testFlag(QNetworkInterface::IsUp))&&interface.flags().testFlag(QNetworkInterface::IsRunning))
{
qDebug()<<interface.humanReadableName();
if(interface.humanReadableName().indexOf("Local")!=-1)
{
hotspot=interface;
qDebug()<<interface.humanReadableName()<<"choosen";
}
}
}
QHostAddress hotspotAddress;
foreach(QNetworkAddressEntry address, hotspot.addressEntries())
{
if(address.ip().protocol()== QAbstractSocket::IPv4Protocol)
{
hotspotAddress=address.ip();
}
}
ipAdress = ip;
socket = new QUdpSocket(this);
//qDebug()<<hotspot.addressEntries();
qDebug()<<hotspotAddress;
qDebug()<<
socket->bind(hotspotAddress,42069);
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
}
void MyUDP::SayHello()
{
QByteArray Data;
Data.append("testing with sending some random data");
sendData(Data);
}
void MyUDP::StartGlobe()
{
QByteArray Data;
Data.append("1");
sendData(Data);
}
void MyUDP::StopGlobe()
{
QByteArray Data;
Data.append("0");
sendData(Data);
}
void MyUDP::ChangeValue(int value)
{
QByteArray Data;
Data.append("2");
Data.append(QString::number(value));
sendData(Data);
}
void MyUDP::sendToLeds(QByteArray byteArray,int port)
{
//a lot of work to do
// qDebug() << byteArray.toLower() << ipAdress;
socket->writeDatagram(byteArray,QHostAddress(ipAdress),port);
}
void MyUDP::readyRead() //will be called when data is received on the udp interface
{
QByteArray Buffer;
Buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
//qDebug() << "Message From: " << sender.toString();
//qDebug() << "Message Port: " << senderPort;
//qDebug() << "Message: " << Buffer;
emit newData(Buffer); //callback to mainwindow
}
void MyUDP::sendData(QByteArray message)
{
socket->writeDatagram(message,QHostAddress(ipAdress),42069);
}