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

source code of qtsystems/src/tools/sfwlisten/main.cpp