ATLAS Offline Software
Loading...
Searching...
No Matches
VP1AvailEvtsHttp.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 VP1AvailEvtsHttp //
9// //
10// Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11// Initial version: May 2008 //
12// //
14
19#include <QTimer>
20#include <QFileInfo>
21#include <QDir>
22#include <QFile>
23#include <QUrl>
24
25//____________________________________________________________________
48
50
51//____________________________________________________________________
52VP1AvailEvtsHttp::VP1AvailEvtsHttp( const QString & fileinfoUrl,
53 int updateInterval,
54 int timeCutForNew,
55 const QString& tmpcopydir,
57 QObject * parent )
58 : VP1AvailEvents(timeCutForNew,tmpcopydir,maxLocalFilesToKeep,parent), m_d(new Imp)
59{
60 m_d->theclass = this;
61 m_d->examineEvtsOnServerTimer = 0;
62 m_d->fileinfoUrl = fileinfoUrl;
63 QUrl url(fileinfoUrl);
64 if (url.isValid()) {
65 QString path = url.path();
66 QString infofilebasename = QFileInfo(path).fileName();
67 if (!infofilebasename.isEmpty()&&path.endsWith(infofilebasename)) {
68 m_d->baseUrl=fileinfoUrl;
69 m_d->baseUrl.chop(infofilebasename.count());
70 if (!QUrl(m_d->baseUrl).isValid())
71 m_d->baseUrl="";
72 else if (!m_d->baseUrl.endsWith("/"))
73 m_d->baseUrl += "/";
74 }
75 }
76
77 m_d->evtsOnServer = 0;
78 connect(&(m_d->httpgetfile_fileinfo),SIGNAL(downloadSuccessful(const QString&,const QString&,const QString&)),
79 this,SLOT(fileInfoDownloadSuccessful(const QString&,const QString&)));
80 connect(&(m_d->httpgetfile_fileinfo),SIGNAL(downloadFailed(const QString&,const QString&,const QString&,const QString&)),
81 this,SLOT(fileInfoDownloadFailed(const QString&,const QString&,const QString&)));
82 connect(&(m_d->httpgetfile_events),SIGNAL(downloadSuccessful(const QString&,const QString&,const QString&)),
83 this,SLOT(eventFileDownloadSuccessful(const QString&,const QString&,const QString&)));
84 connect(&(m_d->httpgetfile_events),SIGNAL(downloadFailed(const QString&,const QString&,const QString&,const QString&)),
85 this,SLOT(eventFileDownloadFailed(const QString&,const QString&,const QString&)));
86
87 m_d->webwatcher_fileinfo = new VP1WebWatcher(updateInterval*1000,this);
88 connect(m_d->webwatcher_fileinfo,SIGNAL(urlChanged(const QString&)),this,SLOT(fileInfoChanged()));
89}
90
91//____________________________________________________________________
93{
94 m_d->webwatcher_fileinfo->addUrl(m_d->fileinfoUrl);//A signal will be triggered the first time the webwatcher knows anything about the url.
95 m_d->restartExamineEvtsOnServerTimer();
96}
97
98//____________________________________________________________________
100{
101 delete m_d->evtsOnServer;
102 delete m_d;
103}
104
105//____________________________________________________________________
107{
108 if (m_d->webwatcher_fileinfo->lastResult(m_d->fileinfoUrl)!=VP1WebWatcher::EXISTS) {
109 message("Problems investigating "+m_d->fileinfoUrl);
110 message(" ===> "+m_d->webwatcher_fileinfo->lastResultToString(m_d->fileinfoUrl));
111 return;
112 }
113 QString ad = tmpActiveRetrievalDir();
114 if (ad.isEmpty()) {
115 message("Error: No temporary retrieval directory set!");
116 m_d->getFileInfoLater();//Avoid stalling forever (however this is an unlikely error).
117 return;
118 }
119 QString target = ad+"downloadedfileinfo.txt";
120 if ( QFileInfo(target).exists() && !QFile(target).remove() ) {
121 message("ERROR: Could not remove old "+target);
122 m_d->getFileInfoLater();//Avoid stalling forever in case user fixes error (i.e. fixes permissions).
123 return;
124 }
125 QString err = m_d->httpgetfile_fileinfo.startDownload( m_d->fileinfoUrl,target);
126 if (!err.isEmpty()) {
127 message("Problems starting download to get file-info file: "+err);
128 m_d->getFileInfoLater();//Avoid stalling forever in case user fixes error (i.e. fixes net connection).
129 }
130}
131
132//____________________________________________________________________
133void VP1AvailEvtsHttp::fileInfoDownloadSuccessful( const QString& urltofile, const QString& localtargetfile )
134{
135 VP1EvtsOnServerInfo * newEvtsOnServerInfo = new VP1EvtsOnServerInfo(localtargetfile);
136 if (!newEvtsOnServerInfo->isValid()) {
137 message("Problems decoding info in file downloaded from "+urltofile);
138 message("Error: "+newEvtsOnServerInfo->error());
139 delete newEvtsOnServerInfo;
140 QFile::remove(localtargetfile);
141 m_d->getFileInfoLater();//Avoid stalling forever in case this is temporary.
143 return;
144 }
145 delete m_d->evtsOnServer;
146 m_d->evtsOnServer = newEvtsOnServerInfo;
147
149}
150
151//____________________________________________________________________
153{
154 m_d->restartExamineEvtsOnServerTimer();//To ensure we don't get events too closely spaced here.
155 if (m_d->httpgetfile_events.numberOfPendingDownloads()>3)
156 return;
157 if (!m_d->evtsOnServer)
158 return;
159
160 //Start download of one of the files from the server?
161 VP1EventFile evtToGet;
162 QString localfiledir = tmpLocalFileDir();
163 if (localfiledir.isEmpty()) {
164 message("Problems with temporary local event file directory.");
165 return;
166 }
167
168 for (VP1EventFile evt : m_d->evtsOnServer->events(timeCutForNew(), requireNewestRunNumber() )) {
169 //We are looking for an event which was never seen before and not available locally:
170 if (!evt.isValid()||inHistory(evt.runNumber(),evt.eventNumber()))
171 continue;
172 if ( !QFile::exists ( localfiledir+evt.fileName() ) ) {
173 evtToGet=evt;
174 break;
175 }
176 }
177
178 if (!evtToGet.isValid()&&m_d->examineEvtsOnServerTimer)
179 m_d->examineEvtsOnServerTimer->stop();//No need to check back regularly until we get new information.
180
181 if (evtToGet.isValid()) {
182 //Before we get it, let us check that we don't already have 3
183 //fresh events locally which are newer than the one we are trying
184 //to download:
185 QList<VP1EventFile> freshLocalEvents = freshEvents();
186 unsigned nNewer(0);
187 for(VP1EventFile evt : freshLocalEvents) {
188 if (evt < evtToGet) {
189 ++nNewer;
190 if (nNewer>=3) {
191 return;
192 }
193 }
194 }
195 QString ad = tmpActiveRetrievalDir();
196 if (!ad.isEmpty()) {
197 QString url = m_d->baseUrl+evtToGet.fileName();
198 QString target = ad+evtToGet.fileName()+"_"+QString::number(Imp::ntmpdlcount++);
199 //A quick check that we are not already downloading that file currently:
200 if (!m_d->httpgetfile_events.isDownloading(url)&&!m_d->httpgetfile_events.isDownloadingTo(target)) {
201 QString err = m_d->httpgetfile_events.startDownload( url,target,evtToGet.md5Sum(),localfiledir+evtToGet.fileName());
202 if (!err.isEmpty()) {
203 message("Problems starting download of :" +url);
204 message(" => "+err);
205 }
206 } else {
207 //We are already downloading url - abort silently.
208 return;
209 }
210 } else {
211 message("Problems with temporary local download directory.");
212 }
213 }
214 invalidateDirCache(tmpActiveRetrievalDir());//Fixme: remove from here???
215}
216
217
218//____________________________________________________________________
219void VP1AvailEvtsHttp::fileInfoDownloadFailed( const QString& error, const QString& urltofile,const QString& localtargetfile )
220{
221 message("Problems downloading "+urltofile+": "+error);
222 QFile::remove(localtargetfile);
224 m_d->getFileInfoLater();//Avoid stalling forever in case this is temporary.
225}
226
227//____________________________________________________________________
228void VP1AvailEvtsHttp::eventFileDownloadSuccessful( const QString&, const QString& localtargetfile, const QString& data )
229{
232
233 if (!QFile::rename(localtargetfile,data)) {
234 message("Error: Could not move "+localtargetfile+" to "+data);
235 QFile::remove(localtargetfile);
236 QFile::remove(data);
237 m_d->getFileInfoLater();//Avoid stalling forever in case this is temporary.
238 return;
239 }
241}
242
243//____________________________________________________________________
244void VP1AvailEvtsHttp::eventFileDownloadFailed( const QString& error, const QString& urltofile,const QString& localtargetfile )
245{
246 message("Problems downloading "+urltofile+": "+error);
247 QFile::remove(localtargetfile);
248 m_d->getFileInfoLater();//Avoid stalling forever in case this is temporary.
250}
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
QList< VP1EventFile > freshEvents() const
QString tmpLocalFileDir() const
void message(const QString &) const
VP1AvailEvents(int timeCutForNew, const QString &tmpdir, int maxLocalFilesToKeep=-1, QObject *parent=0)
bool requireNewestRunNumber() const
int timeCutForNew() const
bool inHistory(int run, int event) const
int maxLocalFilesToKeep() const
void invalidateDirCache(const QString &dir)
void cleanupAndCheckForEventListChanges()
QString tmpActiveRetrievalDir()
VP1HttpGetFile httpgetfile_events
VP1HttpGetFile httpgetfile_fileinfo
VP1EvtsOnServerInfo * evtsOnServer
static unsigned ntmpdlcount
VP1WebWatcher * webwatcher_fileinfo
VP1AvailEvtsHttp * theclass
void eventFileDownloadSuccessful(const QString &urltofile, const QString &localtargetfile, const QString &data)
VP1AvailEvtsHttp(const QString &fileinfoUrl, int updateInterval, int timeCutForNew, const QString &tmpcopydir, int maxLocalFilesToKeep=-1, QObject *parent=0)
virtual void init()
void fileInfoDownloadSuccessful(const QString &urltofile, const QString &localtargetfile)
void fileInfoDownloadFailed(const QString &error, const QString &urltofile, const QString &localtargetfile)
void eventFileDownloadFailed(const QString &error, const QString &urltofile, const QString &localtargetfile)
const QString & fileName() const
bool isValid() const
const QString & md5Sum() const
const QString & error() const
bool exists(const std::string &filename)
does a file exist