Debian Codegraph @

src/tools/sfwlisten/main.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include <QUdpSocket>
41 #include <QHostAddress>
42 #include <QCoreApplication>
43 #include <QBuffer>
44 #include <QDataStream>
45 #include <QTime>
46 #include <QTimer>
47 #include <QNetworkInterface>
48 #include <cstdio>
49
50 /* keep this in sync with qtsystems/src/serviceframework/qservicedebuglog_p.h */
51 enum DataType {
52 Int32Type = 1,
53 FloatType = 2,
54 StringType = 3
55 };
56
57 class SFWReceiver : public QObject
58 {
59 Q_OBJECT
60 public:
61 SFWReceiver(const QString &intfFilter);
62
63 public slots:
64 void socketReadyRead();
65 void socketVerify();
66 void makeSockets();
67
68 private:
69 QString intfFilter;
70 QTimer socketCheck;
71 QList<QUdpSocket *> sockets;
72 };
73
74 SFWReceiver::SFWReceiver(const QString &intfFilter) : intfFilter(intfFilter)
75 {
76 connect(&socketCheck, SIGNAL(timeout()), this, SLOT(socketVerify()));
77 makeSockets();
78 }
79
80 void SFWReceiver::makeSockets()
81 {
82 QList<QNetworkInterface> intfs = QNetworkInterface::allInterfaces();
83
84 bool gotone = false;
85 foreach (const QNetworkInterface &intf, intfs) {
86 if (!intfFilter.isEmpty() && !intf.name().startsWith(intfFilter))
87 continue;
88
89 if (intf.name().startsWith("wifi")
90 || intf.name().startsWith("wl") || intf.name().startsWith("ppp")
91 || intf.name().startsWith("tun") || intf.name().startsWith("tap"))
92 continue;
93
94 QUdpSocket *socket = new QUdpSocket(this);
95 printf("Trying interface %s ...", qPrintable(intf.name()));
96 if (!socket->bind(QHostAddress::AnyIPv4, 10520, QUdpSocket::ShareAddress)) {
97 printf("Couldn't bind: %s\n", qPrintable(socket->errorString()));
98 delete socket;
99 continue;
100 }
101 socket->setMulticastInterface(intf);
102 if (!socket->joinMulticastGroup(QHostAddress("224.0.105.201"), intf)) {
103 printf("Couldn't join group: %s\n", qPrintable(socket->errorString()));
104 delete socket;
105 continue;
106 }
107
108 printf("ok\n");
109 sockets.append(socket);
110 if (!gotone) {
111 connect(socket, SIGNAL(readyRead()),
112 this, SLOT(socketReadyRead()));
113 gotone = true;
114 }
115 }
116
117 if (!gotone) {
118 QTimer::singleShot(200, this, SLOT(makeSockets()));
119 socketCheck.stop();
120 } else {
121 socketCheck.setInterval(1000);
122 socketCheck.start();
123 }
124 }
125
126 void SFWReceiver::socketVerify()
127 {
128 QList<QNetworkInterface> intfs = QNetworkInterface::allInterfaces();
129 bool gotone = false;
130
131 foreach (QUdpSocket *socket, sockets) {
132 QString name = socket->multicastInterface().name();
133
134 foreach (const QNetworkInterface &intf, intfs) {
135 if (intf.name() == name) {
136 gotone = true;
137 break;
138 }
139 }
140
141 if (!gotone) {
142 printf("Interface down...\n");
143 delete socket;
144 sockets.removeAll(socket);
145 makeSockets();
146 }
147 }
148 }
149
150 void SFWReceiver::socketReadyRead()
151 {
152 foreach (QUdpSocket *socket, sockets) {
153 QString intf = socket->multicastInterface().name();
154
155 static QByteArray last;
156
157 while (socket->hasPendingDatagrams()) {
158 QByteArray dgram;
159 dgram.resize(socket->pendingDatagramSize());
160 socket->readDatagram(dgram.data(), dgram.size());
161
162 if (dgram == last)
163 continue;
164
165 QBuffer buff(&dgram);
166 buff.open(QIODevice::ReadOnly);
167 QDataStream ds(&buff);
168
169 quint8 hour,minute,second;
170 quint16 msec;
171 ds >> hour;
172 ds >> minute;
173 ds >> second;
174 ds >> msec;
175
176 qint32 pid;
177 ds >> pid;
178
179 char *str;
180 uint len;
181
182 ds.readBytes(str, len);
183 QByteArray appName(str, len);
184 delete[] str;
185
186 printf("{%4s} %2d:%02d:%02d.%03d ", qPrintable(intf), hour, minute,
187 second, msec);
188 printf("[%5d/%10s] ", pid, appName.constData());
189 while (!ds.atEnd()) {
190 ds.readBytes(str, len);
191 QByteArray termName(str, len);
192 delete[] str;
193
194 printf("{%s, ", termName.constData());
195
196 qint8 type;
197 ds >> type;
198 DataType dt = static_cast<DataType>(type);
199 switch (dt) {
200 case Int32Type:
201 {
202 qint32 data;
203 ds >> data;
204 printf("%d} ", data);
205 }
206 break;
207 case FloatType:
208 {
209 float data;
210 ds >> data;
211 printf("%.4f} ", data);
212 }
213 break;
214 case StringType:
215 {
216 ds.readBytes(str, len);
217 QByteArray ba(str, len);
218 ba.truncate(35);
219 printf("'%s'} ", ba.constData());
220 }
221 break;
222 }
223 }
224
225 printf("\n");
226 last = dgram;
227 }
228 }
229 }
230
231 int main(int argc, char *argv[])
232 {
233 QCoreApplication app(argc, argv);
234 QString filter;
235 if (argc > 1)
236 filter = QString::fromLatin1(argv[1]);
237 SFWReceiver recv(filter);
238 return app.exec();
239 }
240
241 #include "main.moc"