1 /* Copyright (C) 2019 Casper Meijn <casper@meijn.net>
2  * SPDX-License-Identifier: GPL-3.0-or-later
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "wsdiscoveryprobejob.h"
18 
19 #include "loggingcategory.h"
20 #include "wsdiscoveryclient.h"
21 #include "wsdiscoverytargetservice.h"
22 #include <QDebug>
23 #include <QSharedPointer>
24 
WSDiscoveryProbeJob(WSDiscoveryClient * parent)25 WSDiscoveryProbeJob::WSDiscoveryProbeJob(WSDiscoveryClient *parent) :
26     QObject(parent),
27     m_client(parent)
28 {
29     connect(m_client, &WSDiscoveryClient::probeMatchReceived, this, &WSDiscoveryProbeJob::probeMatchReceived);
30 
31     m_timer.setInterval(30000);
32     connect(&m_timer, &QTimer::timeout, this, &WSDiscoveryProbeJob::timeout);
33 }
34 
typeList() const35 QList<KDQName> WSDiscoveryProbeJob::typeList() const
36 {
37     return m_typeList;
38 }
39 
setTypeList(const QList<KDQName> & typeList)40 void WSDiscoveryProbeJob::setTypeList(const QList<KDQName> &typeList)
41 {
42     m_typeList = typeList;
43 }
44 
addType(const KDQName & type)45 void WSDiscoveryProbeJob::addType(const KDQName &type)
46 {
47     m_typeList.append(type);
48 }
49 
scopeList() const50 QList<QUrl> WSDiscoveryProbeJob::scopeList() const
51 {
52     return m_scopeList;
53 }
54 
setScopeList(const QList<QUrl> & scopeList)55 void WSDiscoveryProbeJob::setScopeList(const QList<QUrl> &scopeList)
56 {
57     m_scopeList = scopeList;
58 }
59 
addScope(const QUrl & scope)60 void WSDiscoveryProbeJob::addScope(const QUrl &scope)
61 {
62     m_scopeList.append(scope);
63 }
64 
interval() const65 int WSDiscoveryProbeJob::interval() const
66 {
67     return m_timer.interval();
68 }
69 
setInterval(int interval)70 void WSDiscoveryProbeJob::setInterval(int interval)
71 {
72     m_timer.setInterval(interval);
73 }
74 
start()75 void WSDiscoveryProbeJob::start()
76 {
77     timeout();
78     m_timer.start();
79 }
80 
stop()81 void WSDiscoveryProbeJob::stop()
82 {
83     m_timer.stop();
84 }
85 
timeout()86 void WSDiscoveryProbeJob::timeout()
87 {
88     m_client->sendProbe(m_typeList, m_scopeList);
89 }
90 
probeMatchReceived(const WSDiscoveryTargetService & probeMatchService)91 void WSDiscoveryProbeJob::probeMatchReceived(const WSDiscoveryTargetService &probeMatchService)
92 {
93     bool isMatch = true;
94     for(const KDQName& type : qAsConst(m_typeList)) {
95         isMatch = probeMatchService.isMatchingType(type) && isMatch;
96     }
97     for(const QUrl& scope : qAsConst(m_scopeList)) {
98         isMatch = probeMatchService.isMatchingScope(scope) && isMatch;
99     }
100     if(isMatch) {
101         emit matchReceived(probeMatchService);
102     } else {
103         qCDebug(KDSoapWSDiscoveryClient) << "Received probe match that didn't match the probe job";
104     }
105 }
106