ATLAS Offline Software
Loading...
Searching...
No Matches
VP1HttpGetFile.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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
16#include <QTimer>
17
18#define VP1HttpGetFile_cxx
20#undef VP1HttpGetFile_cxx
21
22//____________________________________________________________________
24public:
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;
41 for( VP1HttpGetFile_DownloadThread * thread : m_d->activeDownloadThreads) {
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//____________________________________________________________________
73QString 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//____________________________________________________________________
137bool VP1HttpGetFile::isDownloading(const QString& urltofile) const
138{
139 for( VP1HttpGetFile_DownloadThread * thread : m_d->activeDownloadThreads)
140 if (urltofile==thread->urltofile()) {
141 QTimer::singleShot(0, thread, SLOT(checkForStall()));
142 return true;
143 }
144 return false;
145}
146
147//____________________________________________________________________
148bool VP1HttpGetFile::isDownloadingTo(const QString& localtargetfile) const
149{
150 for( VP1HttpGetFile_DownloadThread * thread : m_d->activeDownloadThreads)
151 if (localtargetfile==thread->localtargetfile()) {
152 QTimer::singleShot(0, thread, SLOT(checkForStall()));
153 return true;
154 }
155 return false;
156}
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
QList< VP1HttpGetFile_DownloadThread * > activeDownloadThreads
const QString & localtargetfile() const
void downloadSuccessful(const QString &urltofile, const QString &localtargetfile, const QString &data)
void setMaxNumberOfPendingDownloads(unsigned)
void downloadThreadTerminated()
void downloadThreadFinished()
VP1HttpGetFile(QObject *parent=0)
void downloadFailed(const QString &error, const QString &urltofile, const QString &localtargetfile, const QString &data)
unsigned maxNumberOfPendingDownloads() const
bool isDownloadingTo(const QString &localtargetfile) const
virtual ~VP1HttpGetFile()
unsigned numberOfPendingDownloads() const
QString startDownload(const QString &urltofile, const QString &localtargetfile, const QString &md5sum="", const QString &data="")
bool isDownloading(const QString &urltofile) const