-
Notifications
You must be signed in to change notification settings - Fork 6
/
broadcast.cpp
138 lines (121 loc) · 5.33 KB
/
broadcast.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/***************************************************************************
* Copyright (C) 2007 by Massimiliano Fago *
* *
* 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 2 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, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/*! \file
* \brief Comunicazione UDP ricerca robot
*
* Invia dei messaggi UDP sulla porta 6999 all'indirizzo di broadcast
* della rete. I server OPENSHOWVAR presenti sulla rete rispondono
* all'indirizzo UDP 7000 annunciando chi sono e a che indirizzo si trovano.
*
*/
#include "broadcast.h"
/*! \brief Costruttore
*
* Inizializza il BIND sulla porta UDPBROADCASTPORTCROSS
*
*/
Broadcast::Broadcast(QWidget *parent)
: QUdpSocket()
{
memset(privateBind,0,MAXBROADCASTNETWORK * sizeof(QUdpSocket*));
interfaceCount=initBroadcastNetwork();
}
Broadcast::~Broadcast()
{
for(int i=0;i<MAXBROADCASTNETWORK;i++)
if(privateBind[i]!=NULL){
privateBind[i]->disconnectFromHost();
delete privateBind[i];
}
}
/*! \brief Invio messaggio di broadcas
*
* Invia il messaggio di broadcast sulla porta BROADCASTPORT
*
*/
void Broadcast::send()
{
QByteArray datagram = "WHEREAREYOU?";
for(int i=0;i<interfaceCount;i++)
privateBind[i]->writeDatagram(datagram.data(), datagram.size(),broadcastAddress[i].broadcast(), BROADCASTPORT);
}
/*! \brief Ricezione messaggio di broadcast
*
* Riceve e gestisce il messaggio di broadcast.
* OPENCROSSCOMM risponde con un messaggio tipo il successivo:
*
* KUKA|SERIALNO|ROBOTTYPE
* SERIALNO=$KR_SERIALNO
* ROBOTTYPE=$MODEL_NAME[] --> array 32 elements
* KUKA|"#KR2100P_2 S C2 FLR ZH150/180"|944288
*
*/
//!\todo Trasformare datiRobot in classe
void Broadcast::processPending(int index)
{
/*
#ifdef DEBUGWIN
debugWindow->addDebug(QString("Ricevuta risposta su indice %1").arg(index));
#endif
*/
//qDebug() << "Ricevuto nuovo robot";
QByteArray datagram;
QHostAddress sender;
while (privateBind[index]->hasPendingDatagrams()) {
datagram.resize(privateBind[index]->pendingDatagramSize());
privateBind[index]->readDatagram(datagram.data(), datagram.size(),&sender);
datagram.replace('"',"");
datagram.append('|').append(sender.toString());
QList<QByteArray> datiRobot = datagram.split('|');
if(datiRobot.count()==4)
emit addRobot(datiRobot);
}
}
int Broadcast::initBroadcastNetwork()
{
QList<QNetworkInterface> interface = QNetworkInterface::allInterfaces();
int interfaceCount=0;
for(int i=0;i<interface.count();i++){
if((interface[i].flags() & QNetworkInterface::CanBroadcast) && (interface[i].flags() & QNetworkInterface::IsRunning)){
//qDebug() << "Interface ip: " << interface[i].allAddresses();
//qDebug() << "Interface name: " << interface[i].name();
//qDebug() << "Interface name: " << interface[i].flags();
if((interface[i].flags() & QNetworkInterface::CanBroadcast)){
//QList<QNetworkAddressEntry> address = interface[i].addressEntries();
address = interface[i].addressEntries();
//qDebug() << "Flag " << interface[i].flags() << " nome " << interface[i].name();
//qDebug() << address.count();
if(address.count()>0 && address[0].ip()!=QHostAddress::LocalHost){
//this->bind(address[0].ip(),UDPBROADCASTPORTCROSS);
QSignalMapper* mapper[MAXBROADCASTNETWORK];
privateBind[interfaceCount] = new QUdpSocket(this);
mapper[interfaceCount] = new QSignalMapper(this);
connect(privateBind[interfaceCount], SIGNAL(readyRead()),mapper[interfaceCount], SLOT(map()));
mapper[interfaceCount]->setMapping(privateBind[interfaceCount], interfaceCount);
connect(mapper[interfaceCount], SIGNAL(mapped(int)), this, SLOT(processPending(int)));
broadcastAddress[interfaceCount]=address[0];
privateBind[interfaceCount]->bind(address[0].ip(),UDPBROADCASTPORTCROSS);
interfaceCount++;
}
}
}
}
return interfaceCount;
}