ATLAS Offline Software
VP1HttpGetFile.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 // //
8 // Implementation of class VP1HttpGetFile //
9 // //
10 // Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11 // Initial version: May 2008 //
12 // //
14 
15 #include "VP1Gui/VP1HttpGetFile.h"
16 #include <QTimer>
17 
18 #define VP1HttpGetFile_cxx
20 #undef VP1HttpGetFile_cxx
21 
22 //____________________________________________________________________
24 public:
25  Imp() : maxDownloads(8), dead(false) {}
26  unsigned maxDownloads;
27  bool dead;
28  QList<VP1HttpGetFile_DownloadThread *> activeDownloadThreads;
29 };
30 
31 //____________________________________________________________________
33  : QObject(parent), m_d(new Imp)
34 {
35 }
36 
37 //____________________________________________________________________
39 {
40  m_d->dead = true;
42  thread->blockSignals(true);
43  QString urltofile = thread->urltofile();
44  QString localtargetfile = thread->localtargetfile();
45  QString data = thread->data();
46  emit downloadFailed( "VP1HttpGetFile object deleted before download finished", urltofile, localtargetfile, data );
47  thread->terminate();
48  thread->wait();
49  thread->deleteLater();
50  }
51  delete m_d;
52 }
53 
54 //____________________________________________________________________
56 {
57  m_d->maxDownloads = n;
58 }
59 
60 //____________________________________________________________________
62 {
63  return m_d->maxDownloads;
64 }
65 
66 //____________________________________________________________________
68 {
69  return m_d->activeDownloadThreads.size();
70 }
71 
72 //____________________________________________________________________
73 QString VP1HttpGetFile::startDownload( const QString& urltofile,
74  const QString& localtargetfile,
75  const QString& md5sum,
76  const QString& data )
77 {
78  if (m_d->maxDownloads>0 && unsigned(m_d->activeDownloadThreads.count()) >= m_d->maxDownloads)
79  return "Too many simultaneous downloads requested";
80 
81  if (isDownloadingTo(localtargetfile))
82  return "Already downloading file to location: "+localtargetfile;
83 
84  VP1HttpGetFile_DownloadThread * thread = new VP1HttpGetFile_DownloadThread( urltofile, localtargetfile, md5sum, data);
85  connect(thread,SIGNAL(finished()),this,SLOT(downloadThreadFinished()));
86  connect(thread,SIGNAL(terminated()),this,SLOT(downloadThreadTerminated()));
87  m_d->activeDownloadThreads << thread;
88  thread->start();
89 
90  return "";//Download started succesfully.
91 }
92 
93 //____________________________________________________________________
95 {
96  if (m_d->dead)
97  return;
98  VP1HttpGetFile_DownloadThread * thread = dynamic_cast<VP1HttpGetFile_DownloadThread *>(sender());
99  if (!thread)
100  return;
101  m_d->activeDownloadThreads.removeAll(thread);
102  const QString urltofile = thread->urltofile();
103  const QString localtargetfile = thread->localtargetfile();
104  const QString data = thread->data();
105  const bool errors = thread->errors();
106  const QString errorString = thread->errorString();
107  thread->wait();
108  thread->deleteLater();
109  if (errors)
110  emit downloadFailed( errorString, urltofile, localtargetfile, data );
111  else
112  emit downloadSuccessful( urltofile, localtargetfile, data );
113 }
114 
115 //____________________________________________________________________
117 {
118  if (m_d->dead)
119  return;
120  VP1HttpGetFile_DownloadThread * thread = dynamic_cast<VP1HttpGetFile_DownloadThread *>(sender());
121  if (!thread)
122  return;
123  m_d->activeDownloadThreads.removeAll(thread);
124  const QString urltofile = thread->urltofile();
125  const QString localtargetfile = thread->localtargetfile();
126  const QString data = thread->data();
127  thread->wait();
128  thread->deleteLater();
129  emit downloadFailed( "Download thread terminated", urltofile, localtargetfile, data );
130 
131  //NB: As always when threads are terminated, we might get memory
132  //leaks, but it usually only happens if the VP1HttpGetFile instance
133  //is deleted prematurely (e.g. at program shutdown).
134 }
135 
136 //____________________________________________________________________
137 bool VP1HttpGetFile::isDownloading(const QString& urltofile) const
138 {
140  if (urltofile==thread->urltofile()) {
141  QTimer::singleShot(0, thread, SLOT(checkForStall()));
142  return true;
143  }
144  return false;
145 }
146 
147 //____________________________________________________________________
148 bool VP1HttpGetFile::isDownloadingTo(const QString& localtargetfile) const
149 {
151  if (localtargetfile==thread->localtargetfile()) {
152  QTimer::singleShot(0, thread, SLOT(checkForStall()));
153  return true;
154  }
155  return false;
156 }
VP1HttpGetFile::m_d
Imp * m_d
Definition: VP1HttpGetFile.h:60
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
VP1HttpGetFile::downloadThreadTerminated
void downloadThreadTerminated()
Definition: VP1HttpGetFile.cxx:116
VP1HttpGetFile::Imp::activeDownloadThreads
QList< VP1HttpGetFile_DownloadThread * > activeDownloadThreads
Definition: VP1HttpGetFile.cxx:28
VP1HttpGetFile_DownloadThread::data
QString data() const
Definition: VP1HttpGetFile_Private.h:49
VP1HttpGetFile::maxNumberOfPendingDownloads
unsigned maxNumberOfPendingDownloads() const
Definition: VP1HttpGetFile.cxx:61
VP1HttpGetFile::downloadThreadFinished
void downloadThreadFinished()
Definition: VP1HttpGetFile.cxx:94
VP1HttpGetFile::numberOfPendingDownloads
unsigned numberOfPendingDownloads() const
Definition: VP1HttpGetFile.cxx:67
fillPileUpNoiseLumi.connect
string connect
Definition: fillPileUpNoiseLumi.py:70
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
VP1HttpGetFile::startDownload
QString startDownload(const QString &urltofile, const QString &localtargetfile, const QString &md5sum="", const QString &data="")
Definition: VP1HttpGetFile.cxx:73
VP1HttpGetFile::downloadSuccessful
void downloadSuccessful(const QString &urltofile, const QString &localtargetfile, const QString &data)
VP1HttpGetFile_DownloadThread::errors
bool errors() const
Definition: VP1HttpGetFile_Private.h:43
VP1HttpGetFile_DownloadThread::errorString
QString errorString() const
Definition: VP1HttpGetFile_Private.h:44
VP1HttpGetFile::VP1HttpGetFile
VP1HttpGetFile(QObject *parent=0)
Definition: VP1HttpGetFile.cxx:32
beamspotman.n
n
Definition: beamspotman.py:731
test_pyathena.parent
parent
Definition: test_pyathena.py:15
VP1HttpGetFile::isDownloading
bool isDownloading(const QString &urltofile) const
Definition: VP1HttpGetFile.cxx:137
VP1HttpGetFile::Imp
Definition: VP1HttpGetFile.cxx:23
VP1HttpGetFile_DownloadThread::localtargetfile
QString localtargetfile() const
Definition: VP1HttpGetFile_Private.h:47
mergePhysValFiles.errors
list errors
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:43
VP1HttpGetFile::Imp::Imp
Imp()
Definition: VP1HttpGetFile.cxx:25
VP1HttpGetFile::setMaxNumberOfPendingDownloads
void setMaxNumberOfPendingDownloads(unsigned)
Definition: VP1HttpGetFile.cxx:55
VP1HttpGetFile.h
VP1HttpGetFile_DownloadThread::urltofile
QString urltofile() const
Definition: VP1HttpGetFile_Private.h:46
VP1HttpGetFile::downloadFailed
void downloadFailed(const QString &error, const QString &urltofile, const QString &localtargetfile, const QString &data)
VP1HttpGetFile::Imp::dead
bool dead
Definition: VP1HttpGetFile.cxx:27
VP1HttpGetFile_DownloadThread
Definition: VP1HttpGetFile_Private.h:29
VP1HttpGetFile_Private.h
VP1HttpGetFile::isDownloadingTo
bool isDownloadingTo(const QString &localtargetfile) const
Definition: VP1HttpGetFile.cxx:148
VP1HttpGetFile::Imp::maxDownloads
unsigned maxDownloads
Definition: VP1HttpGetFile.cxx:26
VP1HttpGetFile::~VP1HttpGetFile
virtual ~VP1HttpGetFile()
Definition: VP1HttpGetFile.cxx:38