ATLAS Offline Software
VP1AvailEvtsHttp.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 VP1AvailEvtsHttp //
9 // //
10 // Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11 // Initial version: May 2008 //
12 // //
14 
16 #include "VP1Gui/VP1HttpGetFile.h"
18 #include "VP1Gui/VP1WebWatcher.h"
19 #include <QTimer>
20 #include <QFileInfo>
21 #include <QDir>
22 #include <QFile>
23 #include <QUrl>
24 
25 //____________________________________________________________________
27 public:
29  QString fileinfoUrl;
30  QString baseUrl;
38  examineEvtsOnServerTimer = new QTimer(theclass);
40  }
41  examineEvtsOnServerTimer->start(3000);
42  }
44  QTimer::singleShot(10000, theclass, SLOT(fileInfoChanged()));
45  }
46  static unsigned ntmpdlcount;
47 };
48 
50 
51 //____________________________________________________________________
53  int updateInterval,
54  int timeCutForNew,
55  QString tmpcopydir,
56  int maxLocalFilesToKeep,
57  QObject * parent )
58  : VP1AvailEvents(timeCutForNew,tmpcopydir,maxLocalFilesToKeep,parent), m_d(new Imp)
59 {
60  m_d->theclass = this;
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.
96 }
97 
98 //____________________________________________________________________
100 {
101  delete m_d->evtsOnServer;
102  delete m_d;
103 }
104 
105 //____________________________________________________________________
107 {
109  message("Problems investigating "+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  }
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 //____________________________________________________________________
133 void 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.
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 
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:
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 //____________________________________________________________________
219 void 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 //____________________________________________________________________
228 void 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);
237  m_d->getFileInfoLater();//Avoid stalling forever in case this is temporary.
238  return;
239  }
241 }
242 
243 //____________________________________________________________________
244 void 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 }
VP1AvailEvtsHttp::Imp::examineEvtsOnServerTimer
QTimer * examineEvtsOnServerTimer
Definition: VP1AvailEvtsHttp.cxx:35
VP1WebWatcher
Definition: VP1WebWatcher.h:29
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
VP1AvailEvents::tmpLocalFileDir
QString tmpLocalFileDir() const
Definition: VP1AvailEvents.cxx:283
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
VP1AvailEvtsHttp::Imp::theclass
VP1AvailEvtsHttp * theclass
Definition: VP1AvailEvtsHttp.cxx:28
VP1AvailEvtsHttp::Imp::evtsOnServer
VP1EvtsOnServerInfo * evtsOnServer
Definition: VP1AvailEvtsHttp.cxx:34
VP1EvtsOnServerInfo::error
QString error() const
Definition: VP1EvtsOnServerInfo.cxx:59
VP1AvailEvtsHttp::init
virtual void init()
Definition: VP1AvailEvtsHttp.cxx:92
VP1EventFile::md5Sum
QString md5Sum() const
Definition: VP1EventFile.cxx:170
VP1HttpGetFile
Definition: VP1HttpGetFile.h:25
VP1AvailEvtsHttp::~VP1AvailEvtsHttp
virtual ~VP1AvailEvtsHttp()
Definition: VP1AvailEvtsHttp.cxx:99
VP1AvailEvtsHttp::fileInfoDownloadSuccessful
void fileInfoDownloadSuccessful(const QString &urltofile, const QString &localtargetfile)
Definition: VP1AvailEvtsHttp.cxx:133
VP1HttpGetFile::numberOfPendingDownloads
unsigned numberOfPendingDownloads() const
Definition: VP1HttpGetFile.cxx:67
VP1AvailEvtsHttp::Imp::ntmpdlcount
static unsigned ntmpdlcount
Definition: VP1AvailEvtsHttp.cxx:46
fillPileUpNoiseLumi.connect
string connect
Definition: fillPileUpNoiseLumi.py:70
VP1AvailEvtsHttp::Imp::baseUrl
QString baseUrl
Definition: VP1AvailEvtsHttp.cxx:30
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
VP1AvailEvtsHttp::Imp::httpgetfile_fileinfo
VP1HttpGetFile httpgetfile_fileinfo
Definition: VP1AvailEvtsHttp.cxx:32
VP1AvailEvents::cleanupAndCheckForEventListChanges
void cleanupAndCheckForEventListChanges()
Definition: VP1AvailEvents.cxx:176
VP1AvailEvents::tmpActiveRetrievalDir
QString tmpActiveRetrievalDir()
Definition: VP1AvailEvents.cxx:277
VP1AvailEvtsHttp::eventFileDownloadSuccessful
void eventFileDownloadSuccessful(const QString &urltofile, const QString &localtargetfile, const QString &data)
Definition: VP1AvailEvtsHttp.cxx:228
VP1EventFile::fileName
QString fileName() const
Definition: VP1EventFile.cxx:164
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
VP1EvtsOnServerInfo.h
VP1EvtsOnServerInfo
Definition: VP1EvtsOnServerInfo.h:26
VP1AvailEvtsHttp::Imp::httpgetfile_events
VP1HttpGetFile httpgetfile_events
Definition: VP1AvailEvtsHttp.cxx:31
VP1WebWatcher.h
physics_parameters.url
string url
Definition: physics_parameters.py:27
VP1AvailEvents::message
void message(const QString &) const
VP1HttpGetFile::startDownload
QString startDownload(const QString &urltofile, const QString &localtargetfile, const QString &md5sum="", const QString &data="")
Definition: VP1HttpGetFile.cxx:73
VP1WebWatcher::lastResultToString
QString lastResultToString(const QString &url)
Definition: VP1WebWatcher.cxx:311
VP1AvailEvents::inHistory
bool inHistory(int run, int event) const
Definition: VP1AvailEvents.cxx:187
VP1WebWatcher::lastResult
RESULT lastResult(const QString &url)
Definition: VP1WebWatcher.cxx:291
VP1AvailEvents::freshEvents
QList< VP1EventFile > freshEvents() const
Definition: VP1AvailEvents.cxx:129
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
VP1AvailEvtsHttp.h
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:182
VP1WebWatcher::addUrl
void addUrl(const QString &)
Definition: VP1WebWatcher.cxx:226
test_pyathena.parent
parent
Definition: test_pyathena.py:15
VP1AvailEvents::timeCutForNew
int timeCutForNew() const
Definition: VP1AvailEvents.cxx:92
VP1HttpGetFile::isDownloading
bool isDownloading(const QString &urltofile) const
Definition: VP1HttpGetFile.cxx:137
VP1AvailEvtsHttp::fileInfoChanged
void fileInfoChanged()
Definition: VP1AvailEvtsHttp.cxx:106
VP1EventFile::isValid
bool isValid() const
Definition: VP1EventFile.cxx:158
VP1AvailEvents::requireNewestRunNumber
bool requireNewestRunNumber() const
Definition: VP1AvailEvents.h:62
VP1AvailEvtsHttp::fileInfoDownloadFailed
void fileInfoDownloadFailed(const QString &error, const QString &urltofile, const QString &localtargetfile)
Definition: VP1AvailEvtsHttp.cxx:219
python.selection.number
number
Definition: selection.py:20
VP1HttpGetFile.h
VP1AvailEvtsHttp::examineEvtsOnServer
void examineEvtsOnServer()
Definition: VP1AvailEvtsHttp.cxx:152
VP1EvtsOnServerInfo::isValid
bool isValid() const
Definition: VP1EvtsOnServerInfo.cxx:53
VP1AvailEvtsHttp::Imp::fileinfoUrl
QString fileinfoUrl
Definition: VP1AvailEvtsHttp.cxx:29
VP1AvailEvtsHttp::Imp::getFileInfoLater
void getFileInfoLater()
Definition: VP1AvailEvtsHttp.cxx:43
VP1AvailEvtsHttp::Imp::restartExamineEvtsOnServerTimer
void restartExamineEvtsOnServerTimer()
Definition: VP1AvailEvtsHttp.cxx:36
VP1AvailEvtsHttp::eventFileDownloadFailed
void eventFileDownloadFailed(const QString &error, const QString &urltofile, const QString &localtargetfile)
Definition: VP1AvailEvtsHttp.cxx:244
VP1AvailEvtsHttp::m_d
Imp * m_d
Definition: VP1AvailEvtsHttp.h:50
VP1EventFile
Definition: VP1EventFile.h:23
VP1AvailEvtsHttp::Imp::webwatcher_fileinfo
VP1WebWatcher * webwatcher_fileinfo
Definition: VP1AvailEvtsHttp.cxx:33
VP1WebWatcher::EXISTS
@ EXISTS
Definition: VP1WebWatcher.h:53
copySelective.target
string target
Definition: copySelective.py:37
VP1AvailEvtsHttp::VP1AvailEvtsHttp
VP1AvailEvtsHttp(QString fileinfoUrl, int updateInterval, int timeCutForNew, QString tmpcopydir, int maxLocalFilesToKeep=-1, QObject *parent=0)
Definition: VP1AvailEvtsHttp.cxx:52
VP1AvailEvents
Definition: VP1AvailEvents.h:25
VP1AvailEvtsHttp::Imp
Definition: VP1AvailEvtsHttp.cxx:26
python.dummyaccess.exists
def exists(filename)
Definition: dummyaccess.py:9
VP1HttpGetFile::isDownloadingTo
bool isDownloadingTo(const QString &localtargetfile) const
Definition: VP1HttpGetFile.cxx:148
VP1AvailEvtsHttp
Definition: VP1AvailEvtsHttp.h:24
error
Definition: IImpactPoint3dEstimator.h:70
python.TrigInDetArtSteps.timeout
timeout
Definition: TrigInDetArtSteps.py:35
VP1EvtsOnServerInfo::events
QList< VP1EventFile > events(int timecut=-1, bool requireNewestRunNumber=false) const
Definition: VP1EvtsOnServerInfo.cxx:241
VP1AvailEvents::invalidateDirCache
void invalidateDirCache(const QString &dir)
Definition: VP1AvailEvents.cxx:194