ATLAS Offline Software
Loading...
Searching...
No Matches
VP1WebWatcher.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6// //
7// Implementation of class VP1WebWatcher //
8// //
9// Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
10// Initial version: June 2008 //
11// //
13
15
16#include <QUrl>
17#include <QThread>
18#include <QDateTime>
19
20#include <iostream>
21
22
23// TODO: replace QHttp with QNetworkManager!! See: http://stackoverflow.com/a/26182590
24// #include <QHttp>
25// #include <QHttpRequestHeader>
26// #include <QHttpResponseHeader>
27
28//Special values for result strings:
29static const QString VP1WebWatcher_PREFIX = "vp1webwatcher_";
30static const QString VP1WebWatcher_Unknown = VP1WebWatcher_PREFIX+"unknown";
31static const QString VP1WebWatcher_ResultNotReady = VP1WebWatcher_PREFIX+"result_not_ready";
32static const QString VP1WebWatcher_UrlInvalid = VP1WebWatcher_PREFIX+"url_invalid";
33static const QString VP1WebWatcher_httpProblems = VP1WebWatcher_PREFIX+"http_problems";
34static const QString VP1WebWatcher_FileNotExist = VP1WebWatcher_PREFIX+"file_not_existant";
35
36//____________________________________________________________________
38public:
39 class HttpThread : public QThread {
40 public:
41 //________________________________________
42 HttpThread(const QString& url, VP1WebWatcher*/*ww*/ )
43 : QThread(),
44 m_url(url),
47 // m_http(0)
48 {
49 }
50
51 //________________________________________
52 ~HttpThread() { /*delete m_http;*/ }
53
54 //________________________________________
55 const QString& result() const { return m_result; }
56 //For special values, see static variables. All other values will
57 //be the value of the "last-modified" value of the http response
58 //header.
59
60 //________________________________________
61 const QString& url() const { return m_url; }
62
63 //________________________________________
64 int httpStartTime() const { return m_httpStartTime; }
65
66 //________________________________________
67 void run()
68 {
69 // TODO: replace with QNetworkManager!!
70
71 std::cout << "WARNING!! - VP1WebWatcher::run() needs to be ported to QNetworkManager. Returning..." << std::endl;
72
73 // QUrl qurl(m_url);
74 // if (!qurl.isValid()) {
75 // m_result = VP1WebWatcher_UrlInvalid;
76 // return;
77 // }
78 // m_http = new QHttp;
79 // QHttpRequestHeader header("HEAD", m_url);
80 // header.setValue("Host", qurl.host());
81 // header.setValue("User-Agent", "ATLASVP1");
82 // m_http->setHost(qurl.host());
83 // m_httpStartTime = QDateTime::currentDateTime().toTime_t();
84 // m_http->request(header);
85 // connect(m_http,SIGNAL(done(bool)),m_watcher,SLOT(httpRequestDone(bool)),Qt::QueuedConnection);
86 // exec();
87 // m_http->deleteLater();
88 // m_http = 0;
89 }
90
91 //________________________________________
92 bool handleDone(bool /*error*/, QObject * /*sender*/) {
93
94 // TODO: replace with QNetworkManager!!
95 std::cout << "WARNING!! - VP1WebWatcher::handleDone() needs to be ported to QNetworkManager. Returning..." << std::endl;
96
97 // if (sender!=m_http)
98 // return false;
99 // if (!m_http) {
100 // quit();
101 // return false;
102 // }
103 // if (error) {
104 // m_result = VP1WebWatcher_httpProblems;
105 // quit();
106 // return true;
107 // }
108 // QHttpResponseHeader response = m_http->lastResponse();
109 // if (!response.isValid()||!response.hasKey("last-modified")) {
110 // int sc = response.statusCode();
111 // m_result = (sc==404||sc==410)? VP1WebWatcher_FileNotExist : VP1WebWatcher_httpProblems;
112 // quit();
113 // return true;
114 // }
115 // m_result = response.value("last-modified");
116 // quit();
117 // return true;
118 return true;
119
120 }
121
122 // TODO: replace with QNetworkManager!!
123 // QHttp * http() const {
124 // return m_http;
125 // }
126
127 private:
128 const QString m_url;
129 QString m_result;
131 // QHttp * m_http;
132 };
133
134 //___________________________________________________________________
138
140 public:
141 WatchedUrl(const QString& u) : url(u), thread(0), lastResult(VP1WebWatcher_Unknown) {}
143 QString url;
145 QString lastResult;
146 };
147 QList<WatchedUrl*> watchedUrls;
148
149
150 static void ensureEndThread(HttpThread*& /*thread*/) {
151
152 // if (!thread)
153 // return;
154 // thread->blockSignals(true);
155 // if (thread->http())
156 // thread->http()->blockSignals(true);
157 // thread->quit();
158 // if (!thread->wait(50)) {//Put 50 -> 0 to test the terminate fallback.
159 // thread->terminate();
160 // thread->wait();//Ensure synchronisation (so we can safely delete)
161 // }
162 // thread->deleteLater();
163 // thread=0;
164 }
165
166 void startDownload(WatchedUrl* /*wu*/) {
167
168 // ensureEndThread(wu->thread);
169 // wu->thread = new Imp::HttpThread(wu->url,theclass);
170 // wu->thread->start();
171 }
172
173};
174
175
176//____________________________________________________________________
177VP1WebWatcher::VP1WebWatcher(int recheckInterval_ms, QObject * parent)
178 : QObject(parent), m_d(new Imp(this,recheckInterval_ms))
179{
180 startTimer(recheckInterval_ms);
181}
182
183//____________________________________________________________________
184VP1WebWatcher::VP1WebWatcher(const QStringList& urls, int recheckInterval_ms, QObject * parent)
185 : QObject(parent), m_d(new Imp(this,recheckInterval_ms))
186{
187 addUrls(urls);
188 startTimer(recheckInterval_ms);
189}
190
191//____________________________________________________________________
193{
194 while(!m_d->watchedUrls.isEmpty())
195 removeUrl(m_d->watchedUrls.front()->url);
196 delete m_d;
197}
198
199//____________________________________________________________________
201{
202 for(Imp::WatchedUrl*wu : m_d->watchedUrls)
203 if (wu->thread && wu->thread->handleDone(error, sender())) {
204 bool changed = wu->lastResult != wu->thread->result();
205 wu->lastResult = wu->thread->result();
206 m_d->ensureEndThread(wu->thread);
207 if (changed)
208 emit urlChanged(wu->url);
209 return;
210 }
211 //We could not find the thread with that http instance. We must
212 //assume it was terminated.
213}
214
215//____________________________________________________________________
216bool VP1WebWatcher::isWatchingUrl(const QString&u) const
217{
218 for (Imp::WatchedUrl*wu : m_d->watchedUrls) {
219 if (wu->url == u)
220 return true;
221 }
222 return false;
223}
224
225//____________________________________________________________________
226void VP1WebWatcher::addUrl(const QString&/*u*/)
227{
228 // if (isWatchingUrl(u))
229 // return;
230 // Imp::WatchedUrl * wu = new Imp::WatchedUrl(u);
231 // m_d->watchedUrls << wu;
232 // m_d->startDownload(wu);
233}
234
235//____________________________________________________________________
236void VP1WebWatcher::addUrls(const QStringList&/*l*/)
237{
238 // foreach (QString u, l)
239 // addUrl(u);
240}
241
242//____________________________________________________________________
243QStringList VP1WebWatcher::urls() const
244{
245 QStringList l;
246 // foreach(Imp::WatchedUrl*wu,m_d->watchedUrls)
247 // l << wu->url;
248 return l;
249}
250
251//____________________________________________________________________
252void VP1WebWatcher::removeUrl(const QString&/*u*/)
253{
254 // foreach(Imp::WatchedUrl*wu,m_d->watchedUrls)
255 // if (wu->url == u) {
256 // m_d->watchedUrls.removeAll(wu);
257 // delete wu;
258 // return;
259 // }
260}
261
262//____________________________________________________________________
263void VP1WebWatcher::removeUrls(const QStringList&/*l*/)
264{
265 // foreach (QString u, l)
266 // removeUrl(u);
267}
268
269//____________________________________________________________________
271{
272 // const unsigned currentTime = QDateTime::currentDateTime().toTime_t();
273 // foreach(Imp::WatchedUrl*wu,m_d->watchedUrls) {
274 // if (!wu->thread) {
275 // m_d->startDownload(wu);
276 // } else {
277 // //Thread is running. Check that it didn't run for too long.
278 // //
279 // //(No matter what, we never restart running thread that have been
280 // //running for less than a second, or no less than twice the
281 // //recheckInterval!)
282 // if (wu->thread->httpStartTime()>0 && currentTime - wu->thread->httpStartTime() > (unsigned(m_d->recheckInterval_ms) * 1000 * 2 + 1000)) {
283 // m_d->ensureEndThread(wu->thread);
284 // m_d->startDownload(wu);
285 // }
286 // }
287 // }
288}
289
290//____________________________________________________________________
292{
293 for(Imp::WatchedUrl*wu : m_d->watchedUrls)
294 if (wu->url==url) {
296 return INVALID_URL;
298 return CONNECTION_PROBLEMS;
300 return NOT_ON_SERVER;
302 return UNKNOWN;
304 return UNKNOWN;
305 return EXISTS;
306 }
307 return UNKNOWN;
308}
309
310//____________________________________________________________________
311QString VP1WebWatcher::lastResultToString(const QString& url)
312{
313 switch (lastResult(url)) {
314 case UNKNOWN: return "UNKNOWN";
315 case INVALID_URL: return "INVALID_URL";
316 case CONNECTION_PROBLEMS: return "CONNECTION_PROBLEMS";
317 case NOT_ON_SERVER: return "NOT_ON_SERVER";
318 case EXISTS: return "EXISTS";
319 default:
320 return "UNKNOWN";
321 }
322}
323
324//____________________________________________________________________
325QDateTime VP1WebWatcher::lastModTime(const QString& url)
326{
327 for(Imp::WatchedUrl*wu : m_d->watchedUrls)
328 if (wu->url==url) {
329 if (wu->lastResult.startsWith(VP1WebWatcher_PREFIX))
330 return QDateTime();//invalid
331 QString lastmod = wu->lastResult;
332 lastmod = lastmod.simplified();
333 lastmod.chop(4);
334 QDateTime dt(QDateTime::fromString(lastmod,"ddd, dd MMM yyyy hh:mm:ss"));
335 dt.setTimeSpec(Qt::UTC);
336 return dt;
337 }
338 return QDateTime();//invalid
339}
static Double_t tc
static const QString VP1WebWatcher_FileNotExist
static const QString VP1WebWatcher_PREFIX
static const QString VP1WebWatcher_ResultNotReady
static const QString VP1WebWatcher_httpProblems
static const QString VP1WebWatcher_UrlInvalid
static const QString VP1WebWatcher_Unknown
const QString & result() const
bool handleDone(bool, QObject *)
const QString & url() const
HttpThread(const QString &url, VP1WebWatcher *)
Imp(VP1WebWatcher *tc, int ri)
QList< WatchedUrl * > watchedUrls
void startDownload(WatchedUrl *)
VP1WebWatcher * theclass
static void ensureEndThread(HttpThread *&)
void addUrls(const QStringList &)
QString lastResultToString(const QString &url)
virtual ~VP1WebWatcher()
bool isWatchingUrl(const QString &) const
RESULT lastResult(const QString &url)
void addUrl(const QString &)
void removeUrl(const QString &)
void removeUrls(const QStringList &)
QDateTime lastModTime(const QString &url)
void timerEvent(QTimerEvent *)
VP1WebWatcher(int recheckInterval_ms=30000, QObject *parent=0)
void httpRequestDone(bool)
QStringList urls() const
void urlChanged(const QString &url)