ATLAS Offline Software
VP1WebWatcher.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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 
14 #include "VP1Gui/VP1WebWatcher.h"
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:
29 static const QString VP1WebWatcher_PREFIX = "vp1webwatcher_";
30 static const QString VP1WebWatcher_Unknown = VP1WebWatcher_PREFIX+"unknown";
31 static const QString VP1WebWatcher_ResultNotReady = VP1WebWatcher_PREFIX+"result_not_ready";
32 static const QString VP1WebWatcher_UrlInvalid = VP1WebWatcher_PREFIX+"url_invalid";
33 static const QString VP1WebWatcher_httpProblems = VP1WebWatcher_PREFIX+"http_problems";
34 static const QString VP1WebWatcher_FileNotExist = VP1WebWatcher_PREFIX+"file_not_existant";
35 
36 //____________________________________________________________________
38 public:
39  class HttpThread : public QThread {
40  public:
41  //________________________________________
42  HttpThread(const QString& url, VP1WebWatcher*/*ww*/ )
43  : QThread(),
44  m_url(url),
45  m_result(VP1WebWatcher_ResultNotReady),
47  // m_http(0)
48  {
49  }
50 
51  //________________________________________
52  ~HttpThread() { /*delete m_http;*/ }
53 
54  //________________________________________
55  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  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;
130  unsigned m_httpStartTime;
131  // QHttp * m_http;
132  };
133 
134  //___________________________________________________________________
135  Imp(VP1WebWatcher *tc,int ri) : theclass(tc), recheckInterval_ms(ri) {}
138 
139  class WatchedUrl {
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 //____________________________________________________________________
177 VP1WebWatcher::VP1WebWatcher(int recheckInterval_ms, QObject * parent)
178  : QObject(parent), m_d(new Imp(this,recheckInterval_ms))
179 {
180  startTimer(recheckInterval_ms);
181 }
182 
183 //____________________________________________________________________
184 VP1WebWatcher::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 //____________________________________________________________________
216 bool 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 //____________________________________________________________________
226 void 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 //____________________________________________________________________
236 void VP1WebWatcher::addUrls(const QStringList&/*l*/)
237 {
238  // foreach (QString u, l)
239  // addUrl(u);
240 }
241 
242 //____________________________________________________________________
243 QStringList 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 //____________________________________________________________________
252 void 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 //____________________________________________________________________
263 void VP1WebWatcher::removeUrls(const QStringList&/*l*/)
264 {
265  // foreach (QString u, l)
266  // removeUrl(u);
267 }
268 
269 //____________________________________________________________________
270 void VP1WebWatcher::timerEvent(QTimerEvent*)
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) {
295  if (wu->lastResult==VP1WebWatcher_UrlInvalid)
296  return INVALID_URL;
297  if (wu->lastResult==VP1WebWatcher_httpProblems)
298  return CONNECTION_PROBLEMS;
299  if (wu->lastResult==VP1WebWatcher_FileNotExist)
300  return NOT_ON_SERVER;
301  if (wu->lastResult==VP1WebWatcher_Unknown)
302  return UNKNOWN;
303  if (wu->lastResult==VP1WebWatcher_ResultNotReady)
304  return UNKNOWN;
305  return EXISTS;
306  }
307  return UNKNOWN;
308 }
309 
310 //____________________________________________________________________
311 QString 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 //____________________________________________________________________
325 QDateTime 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 }
VP1WebWatcher::lastModTime
QDateTime lastModTime(const QString &url)
Definition: VP1WebWatcher.cxx:325
VP1WebWatcher
Definition: VP1WebWatcher.h:29
VP1WebWatcher::CONNECTION_PROBLEMS
@ CONNECTION_PROBLEMS
Definition: VP1WebWatcher.h:51
VP1WebWatcher::Imp::WatchedUrl::lastResult
QString lastResult
Definition: VP1WebWatcher.cxx:145
VP1WebWatcher::timerEvent
void timerEvent(QTimerEvent *)
Definition: VP1WebWatcher.cxx:270
VP1WebWatcher::addUrls
void addUrls(const QStringList &)
Definition: VP1WebWatcher.cxx:236
VP1WebWatcher::m_d
Imp * m_d
Definition: VP1WebWatcher.h:67
VP1WebWatcher::Imp::startDownload
void startDownload(WatchedUrl *)
Definition: VP1WebWatcher.cxx:166
VP1WebWatcher::NOT_ON_SERVER
@ NOT_ON_SERVER
Definition: VP1WebWatcher.h:52
VP1WebWatcher::Imp
Definition: VP1WebWatcher.cxx:37
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
VP1WebWatcher::INVALID_URL
@ INVALID_URL
Definition: VP1WebWatcher.h:50
VP1WebWatcher::removeUrl
void removeUrl(const QString &)
Definition: VP1WebWatcher.cxx:252
VP1WebWatcher::VP1WebWatcher
VP1WebWatcher(int recheckInterval_ms=30000, QObject *parent=0)
Definition: VP1WebWatcher.cxx:177
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
VP1WebWatcher::RESULT
RESULT
Definition: VP1WebWatcher.h:49
VP1WebWatcher.h
VP1WebWatcher::~VP1WebWatcher
virtual ~VP1WebWatcher()
Definition: VP1WebWatcher.cxx:192
VP1WebWatcher::Imp::WatchedUrl::WatchedUrl
WatchedUrl(const QString &u)
Definition: VP1WebWatcher.cxx:141
physics_parameters.url
string url
Definition: physics_parameters.py:27
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
VP1WebWatcher::lastResultToString
QString lastResultToString(const QString &url)
Definition: VP1WebWatcher.cxx:311
VP1WebWatcher::lastResult
RESULT lastResult(const QString &url)
Definition: VP1WebWatcher.cxx:291
VP1WebWatcher::Imp::theclass
VP1WebWatcher * theclass
Definition: VP1WebWatcher.cxx:136
VP1WebWatcher::Imp::HttpThread::m_result
QString m_result
Definition: VP1WebWatcher.cxx:129
VP1WebWatcher::urls
QStringList urls() const
Definition: VP1WebWatcher.cxx:243
VP1WebWatcher::Imp::WatchedUrl::thread
HttpThread * thread
Definition: VP1WebWatcher.cxx:144
CaloNoise_fillDB.dt
dt
Definition: CaloNoise_fillDB.py:58
VP1WebWatcher::addUrl
void addUrl(const QString &)
Definition: VP1WebWatcher.cxx:226
VP1WebWatcher::Imp::HttpThread::HttpThread
HttpThread(const QString &url, VP1WebWatcher *)
Definition: VP1WebWatcher.cxx:42
VP1WebWatcher::UNKNOWN
@ UNKNOWN
Definition: VP1WebWatcher.h:49
VP1WebWatcher::Imp::HttpThread
Definition: VP1WebWatcher.cxx:39
VP1WebWatcher::Imp::HttpThread::result
QString result() const
Definition: VP1WebWatcher.cxx:55
test_pyathena.parent
parent
Definition: test_pyathena.py:15
VP1WebWatcher::removeUrls
void removeUrls(const QStringList &)
Definition: VP1WebWatcher.cxx:263
PixelConditionsData::fromString
T fromString(const std::string &s)
Definition: PixelConditionsDataStringUtils.h:14
VP1WebWatcher::Imp::Imp
Imp(VP1WebWatcher *tc, int ri)
Definition: VP1WebWatcher.cxx:135
VP1WebWatcher::Imp::HttpThread::m_httpStartTime
unsigned m_httpStartTime
Definition: VP1WebWatcher.cxx:130
VP1WebWatcher::Imp::watchedUrls
QList< WatchedUrl * > watchedUrls
Definition: VP1WebWatcher.cxx:147
VP1WebWatcher::urlChanged
void urlChanged(const QString &url)
VP1WebWatcher::Imp::HttpThread::run
void run()
Definition: VP1WebWatcher.cxx:67
VP1WebWatcher::isWatchingUrl
bool isWatchingUrl(const QString &) const
Definition: VP1WebWatcher.cxx:216
VP1WebWatcher::Imp::HttpThread::~HttpThread
~HttpThread()
Definition: VP1WebWatcher.cxx:52
VP1WebWatcher::Imp::WatchedUrl
Definition: VP1WebWatcher.cxx:139
VP1WebWatcher::Imp::WatchedUrl::~WatchedUrl
~WatchedUrl()
Definition: VP1WebWatcher.cxx:142
VP1WebWatcher::Imp::HttpThread::url
QString url() const
Definition: VP1WebWatcher.cxx:61
VP1WebWatcher::EXISTS
@ EXISTS
Definition: VP1WebWatcher.h:53
VP1WebWatcher::httpRequestDone
void httpRequestDone(bool)
Definition: VP1WebWatcher.cxx:200
VP1WebWatcher::Imp::WatchedUrl::url
QString url
Definition: VP1WebWatcher.cxx:143
VP1WebWatcher::Imp::recheckInterval_ms
const int recheckInterval_ms
Definition: VP1WebWatcher.cxx:137
VP1WebWatcher::Imp::HttpThread::httpStartTime
int httpStartTime() const
Definition: VP1WebWatcher.cxx:64
VP1WebWatcher::Imp::HttpThread::handleDone
bool handleDone(bool, QObject *)
Definition: VP1WebWatcher.cxx:92
error
Definition: IImpactPoint3dEstimator.h:70
VP1WebWatcher::Imp::HttpThread::m_url
const QString m_url
Definition: VP1WebWatcher.cxx:128
VP1WebWatcher::Imp::ensureEndThread
static void ensureEndThread(HttpThread *&)
Definition: VP1WebWatcher.cxx:150