-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatachannel.cpp
130 lines (97 loc) · 3.57 KB
/
datachannel.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
#include "datachannel.h"
#include "notsuitablewifiadapterfoundveniceexception.h"
DataChannel::DataChannel(QNetworkInterface::InterfaceType networkInterfaceType)
{
this->networkInterfaceType = networkInterfaceType;
this->networkInterfaceName = DataChannel::getNetworkInterfaceNameByType(this->networkInterfaceType);
}
DataChannel::~DataChannel()
{
}
QHostAddress DataChannel::getIpAddress(QNetworkInterface::InterfaceType networkInterfaceType)
{
QHostAddress ip;
QList<QNetworkInterface> currentInterfaces = QNetworkInterface::allInterfaces();
for(QNetworkInterface currentInterface: currentInterfaces)
{
if(currentInterface.type()== networkInterfaceType)
{
QList<QNetworkAddressEntry> addresses = currentInterface.addressEntries();
if(!addresses.isEmpty())
// Here we assume that there is only one IP
ip = addresses.first().ip();
else
{
qDebug() << "There is not IP address related to the type ";
qDebug() << DataChannel::networkInterfaceTypeToString(networkInterfaceType);
}
break;
}
}
return ip;
}
QString DataChannel::getNetworkInterfaceNameByType(QNetworkInterface::InterfaceType networkInterfaceType)
{
QString interfaceName;
QList<QNetworkInterface> currentInterfaces = QNetworkInterface::allInterfaces();
for(QNetworkInterface currentInterface: currentInterfaces)
{
if(currentInterface.type()== networkInterfaceType)
{
interfaceName = currentInterface.name();
qDebug() << "Network interface Name " << interfaceName;
break;
}
}
return interfaceName;
}
QNetworkInterface DataChannel::searchNetworkInterfaceByType(QNetworkInterface::InterfaceType networkInterfaceType) throw()
{
QNetworkInterface networkInterface;
QList<QNetworkInterface> currentInterfaces = QNetworkInterface::allInterfaces();
for(QNetworkInterface currentInterface: currentInterfaces)
{
if(currentInterface.type()== networkInterfaceType)
{
networkInterface = currentInterface;
qDebug() << "Selected Network interface Name " << networkInterface.name();
qDebug() << "Adresses " << networkInterface.allAddresses().size();
qDebug() << "Adresses Entries " << networkInterface.addressEntries().size();
return networkInterface;
}
}
throw NotSuitableWifiAdapterFoundVeniceException();
}
const char* DataChannel::networkInterfaceTypeToString(QNetworkInterface::InterfaceType networkInterfaceType)
{
switch (networkInterfaceType) {
case QNetworkInterface::Wifi:
return WIFI;
case QNetworkInterface::Ethernet:
return ETHERNET;
case QNetworkInterface::Ppp:
return PPP;
case QNetworkInterface::Fddi:
return FDDI;
case QNetworkInterface::Slip:
return SLIP;
case QNetworkInterface::Loopback:
return LOOPBACK;
case QNetworkInterface::Virtual:
return VIRTUAL;
case QNetworkInterface::CanBus:
return CANBUS;
case QNetworkInterface::Phonet:
return PHONET;
case QNetworkInterface::SixLoWPAN:
return SIXLOWPAN;
case QNetworkInterface::Ieee802154:
return IEEE802154;
case QNetworkInterface::Ieee80216:
return IEEE80216;
case QNetworkInterface::Ieee1394:
return IEEE1394;
default:
return UNKNOWN;
}
}