ATLAS Offline Software
Loading...
Searching...
No Matches
VP1AvailEvtsHttps Class Reference

#include <VP1AvailEvtsHttps.h>

Inheritance diagram for VP1AvailEvtsHttps:
Collaboration diagram for VP1AvailEvtsHttps:

Classes

class  Imp

Public Slots

void start (QNetworkAccessManager *netmanager)

Signals

void allLocalEventsChanged ()
void freshEventsChanged ()
void message (const QString &) const

Public Member Functions

 VP1AvailEvtsHttps (const QString &fileinfoUrl, int updateInterval, int timeCutForNew, const QString &tmpcopydir, int maxLocalFilesToKeep=-1, QObject *parent=0)
virtual ~VP1AvailEvtsHttps ()
virtual void init ()
const QString & fileinfoLocation ()
QList< VP1EventFileallLocalEvents () const
QList< VP1EventFilefreshEvents () const
VP1EventFile newestEvent () const
void setCurrentEvent (int run, int event)
int maxLocalFilesToKeep () const

Protected Member Functions

QList< VP1EventFilefreshEvents (VP1EventFile newestEvt, const QList< VP1EventFile > &) const
void cleanupAndCheckForEventListChanges ()
void invalidateDirCache (const QString &dir)
bool inHistory (int run, int event) const
QList< VP1EventFileallEventFilesInDir (const QString &dir) const
const QString & tmpDir () const
QString tmpLocalFileDir () const
QString tmpActiveRetrievalDir ()
int timeCutForNew () const
bool requireNewestRunNumber () const
bool isConsideredFresh (const VP1EventFile &evt, const VP1EventFile &newestEvt) const
void messageDebug (const QString &) const

Private Slots

void finished ()
void error (QNetworkReply::NetworkError)
void sslErrors (const QList< QSslError > &)
void dataReadProgress (qint64, qint64)
void checkForStall ()
void generateHttpsRequest ()
void actualCleanup ()
void actualCheckForEventListChanges ()

Private Attributes

Impm_d

Detailed Description

Definition at line 27 of file VP1AvailEvtsHttps.h.

Constructor & Destructor Documentation

◆ VP1AvailEvtsHttps()

VP1AvailEvtsHttps::VP1AvailEvtsHttps ( const QString & fileinfoUrl,
int updateInterval,
int timeCutForNew,
const QString & tmpcopydir,
int maxLocalFilesToKeep = -1,
QObject * parent = 0 )

Definition at line 121 of file VP1AvailEvtsHttps.cxx.

128 , m_d(new Imp(this,fileinfoUrl,updateInterval))
129{
130}
VP1AvailEvents(int timeCutForNew, const QString &tmpdir, int maxLocalFilesToKeep=-1, QObject *parent=0)
int timeCutForNew() const
int maxLocalFilesToKeep() const

◆ ~VP1AvailEvtsHttps()

VP1AvailEvtsHttps::~VP1AvailEvtsHttps ( )
virtual

Definition at line 132 of file VP1AvailEvtsHttps.cxx.

133{
134 delete m_d;
135}

Member Function Documentation

◆ actualCheckForEventListChanges

void VP1AvailEvents::actualCheckForEventListChanges ( )
privateslotinherited

Definition at line 161 of file VP1AvailEvents.cxx.

162{
163 QList<VP1EventFile> allLocal = allLocalEvents();
164 QList<VP1EventFile> fresh = freshEvents();
165 if (m_d->lastAllLocal != allLocal) {
166 m_d->lastAllLocal = std::move(allLocal);
168 }
169 if (m_d->lastFresh != fresh) {
170 m_d->lastFresh = std::move(fresh);
172 }
173}
QList< VP1EventFile > allLocalEvents() const
QList< VP1EventFile > freshEvents() const
void allLocalEventsChanged()
void freshEventsChanged()

◆ actualCleanup

void VP1AvailEvents::actualCleanup ( )
privateslotinherited

Definition at line 151 of file VP1AvailEvents.cxx.

152{
153 //First we cleanup:
154 m_d->cleanupTmpLocalFiles();
155
156 //Then we schedule a check for event list changes:
157 QTimer::singleShot(10, this, SLOT(actualCheckForEventListChanges()));
158}
void actualCheckForEventListChanges()

◆ allEventFilesInDir()

QList< VP1EventFile > VP1AvailEvents::allEventFilesInDir ( const QString & dir) const
protectedinherited

Definition at line 202 of file VP1AvailEvents.cxx.

203{
204 if (dir.isEmpty())
205 return QList<VP1EventFile>();
206
207 QFileInfo fi_dir(dir);
208 if (!fi_dir.exists()||!fi_dir.isDir())
209 return QList<VP1EventFile>();
210
211 QDateTime modtime = fi_dir.lastModified();
212 if (abs(modtime.time().msecsTo(QTime::currentTime()))>50) {
213 std::map<QString,std::pair<QDateTime,QList<VP1EventFile> > >::iterator it = m_d->dircache.find(dir);
214 if (it!=m_d->dircache.end()&&it->second.first==modtime)
215 return it->second.second;
216 }
217
218 QStringList filters;
219 filters << "*_*.pool.root";
220 //fixme
221 QDirIterator itDir(dir,filters,QDir::Files | QDir::NoDotAndDotDot | QDir::Readable | QDir::CaseSensitive);
222
223 QList<VP1EventFile> l;
224 while (itDir.hasNext()) {
225 QString fn = itDir.next();
226 fn.replace("//","/");
227 VP1EventFile evt(fn);
228 if (evt.isValid())
229 l << evt;
230 else
231 message("Could not decode event file name: "+fn);
232 }
233
234 std::sort(l.begin(), l.end());
235
236 m_d->dircache[dir]=std::make_pair(modtime,l);
237 return l;
238}
void message(const QString &) const
l
Printing final latex table to .tex output file.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.

◆ allLocalEvents()

QList< VP1EventFile > VP1AvailEvents::allLocalEvents ( ) const
inherited

Definition at line 241 of file VP1AvailEvents.cxx.

242{
244}
QString tmpLocalFileDir() const
QList< VP1EventFile > allEventFilesInDir(const QString &dir) const

◆ allLocalEventsChanged

void VP1AvailEvents::allLocalEventsChanged ( )
signalinherited

◆ checkForStall

void VP1AvailEvtsHttps::checkForStall ( )
privateslot

Definition at line 389 of file VP1AvailEvtsHttps.cxx.

390{
391 unsigned currentTime = QDateTime::currentDateTime().toTime_t();
392 if (currentTime>m_d->m_lastChangeTime+10) {
393 // Abort current download and go to stage 0
394 if(m_d->m_netreply) {
395 m_d->m_netreply->blockSignals(true);
396 delete m_d->m_netreply;
397 m_d->m_netreply=0;
398 m_d->m_stage = 0;
399 m_d->startTimer(m_d->m_updateInterval);
400 }
401 }
402}

◆ cleanupAndCheckForEventListChanges()

void VP1AvailEvents::cleanupAndCheckForEventListChanges ( )
protectedinherited

Definition at line 176 of file VP1AvailEvents.cxx.

177{
178 //We schedule the cleanup to take place shortly. The check for event
179 //list changes will be scheduled after the cleanup:
180
181 //Then we schedule a check for event list changes:
182 QTimer::singleShot(10, this, SLOT(actualCleanup()));
183
184}

◆ dataReadProgress

void VP1AvailEvtsHttps::dataReadProgress ( qint64 received,
qint64  )
privateslot

Definition at line 377 of file VP1AvailEvtsHttps.cxx.

378{
379 if(m_d->m_stage>0) {
380 if(received>m_d->m_bytesReceived) {
381 m_d->m_lastChangeTime = QDateTime::currentDateTime().toTime_t();
382 m_d->m_bytesReceived = received;
383 }
384 } else {
385 m_d->m_lastChangeTime = QDateTime::currentDateTime().toTime_t();
386 }
387}

◆ error

void VP1AvailEvtsHttps::error ( QNetworkReply::NetworkError err)
privateslot

Definition at line 357 of file VP1AvailEvtsHttps.cxx.

358{
359 message("Error processing " + m_d->m_netreply->url().toString()
360 + "\n ===> Error code: " + QString::number((int)err)
361 + "\n Error decoding here: http://doc.trolltech.com/4.4/qnetworkreply.html#NetworkError-enum");
362}

◆ fileinfoLocation()

const QString & VP1AvailEvtsHttps::fileinfoLocation ( )

Definition at line 148 of file VP1AvailEvtsHttps.cxx.

149{
150 return m_d->m_fileInfoUrl;
151}

◆ finished

void VP1AvailEvtsHttps::finished ( )
privateslot

-— For logging std::cout << "VP1AvailEvtsHttps stage -- " << m_d->m_stage << " -- finished with error=" << (int)m_d->m_netreply->error() << ", sc=" << sc << ", LM=" << lastModified.toStdString() << std::endl;

-— For logging std::cout << " Active Ret Dir: " << activeRetrievalDir.toStdString() << std::endl; std::cout << " Target : " << target.toStdString() << std::endl;

-— For logging std::cout << "\t\t" << target.toStdString() << " written and closed" << std::endl;

Definition at line 233 of file VP1AvailEvtsHttps.cxx.

234{
235 VP1Msg::messageDebug("VP1AvailEvtsHttps::finished()");
236 int sc=-9999;
237 QVariant val = m_d->m_netreply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
238 if(val.type()==QVariant::Int)
239 sc = val.value<int>();
240 else if(val.type()==QVariant::Invalid)
241 message("No status code obtained while processing " + m_d->m_netreply->url().toString());
242 else if(!val.canConvert<int>())
243 message("Cannot convert status code to int while processing " + m_d->m_netreply->url().toString());
244 else
245 sc = val.value<int>();
246
247 VP1Msg::messageDebug("sc: " + QString::number(sc));
248
249 QString lastModified;
250 QVariant lastModHeader = m_d->m_netreply->header(QNetworkRequest::LastModifiedHeader);
251 if(lastModHeader.type()!=QVariant::Invalid &&
252 lastModHeader.canConvert<QDateTime>()) {
253 QDateTime lastModTime = lastModHeader.value<QDateTime>();
254 lastModified = lastModTime.toString();
255 }
256
263
264 if(m_d->m_netreply->error()==QNetworkReply::NoError) {
265 if(m_d->m_stage==0) {
266 if(!lastModified.isEmpty() && lastModified!=m_d->m_urlLastMod) {
267 // The file info has been modified, go to the stage 1
268 m_d->m_urlLastMod = std::move(lastModified);
269 m_d->m_stage = 1;
270 } else {
271 // Reuse the already downloaded file info
272 m_d->m_stage = 2;
273 }
274 }else if(m_d->m_stage==1) {
275 // Write out fileinfo to local file for parsing
276 QString activeRetrievalDir = tmpActiveRetrievalDir();
277 QString target = activeRetrievalDir +"downloadedfileinfo.txt";
282
283 // Delete already existing fileinfo
284 if(QFileInfo(target).exists() && !QFile(target).remove()) {
285 message("ERROR: Could not remove the old fileinfo " + target);
286 m_d->m_stage = 0;
287 m_d->startTimer(m_d->m_updateInterval);
288 return;
289 }
290
291 // Create new fileinfo
292 QFile localfileinfo(target);
293 if(!localfileinfo.open(QIODevice::WriteOnly)) {
294 message("ERROR: Unable to open " + target + " for writing");
295 m_d->m_stage = 0;
296 m_d->startTimer(m_d->m_updateInterval);
297 return;
298 }
299
300 // Wrute received fileinfo to local file
301 QByteArray ba = m_d->m_netreply->readAll();
302 localfileinfo.write(ba);
303 localfileinfo.close();
304 m_d->m_stage = 2;
305 }else if(m_d->m_stage==2) {
306 // Open target file for writing
307 QString target = tmpActiveRetrievalDir() + m_d->m_evtToGet;
308 QFile targetFile(target);
309 if(!targetFile.open(QIODevice::WriteOnly)) {
310 message("ERROR: Unable to open " + target + " for writing");
311 m_d->m_stage = 0;
312 m_d->startTimer(m_d->m_updateInterval);
313 return;
314 }
315
316 // Write to file
317 QByteArray ba = m_d->m_netreply->readAll();
318 targetFile.write(ba);
319 targetFile.close();
323
324 // Checksum test
325 bool match = VP1MD5Sum::sumMatches(target,m_d->m_expectedMD5Sum);
326 if(!match) {
327 message("Checksum did not match");
328 QFile::remove(target);
329 m_d->m_stage = 0;
330 m_d->startTimer(m_d->m_updateInterval);
331 return;
332 }
333
334 QString finalTarget = tmpLocalFileDir()+m_d->m_evtToGet;
335 if(!QFile::rename(target,finalTarget)) {
336 message("ERROR: Could not move " + target + " to " + finalTarget);
337 QFile::remove(target);
338 QFile::remove(finalTarget);
339 } else {
341 }
342
343 m_d->m_stage = 0;
344 }
345 }
346 else
347 m_d->m_stage = 0;
348
349 m_d->m_netreply->blockSignals(true);
350 m_d->m_netreply->deleteLater();
351 m_d->m_netreply = 0;
352
353 int interval = m_d->m_stage>0 ? 0 : m_d->m_updateInterval;
354 m_d->startTimer(interval);
355}
static Double_t sc
void cleanupAndCheckForEventListChanges()
QString tmpActiveRetrievalDir()
static bool sumMatches(const QString &filename, const QByteArray &md5sum)
Definition VP1MD5Sum.cxx:38
static void messageDebug(const QString &)
Definition VP1Msg.cxx:39
bool exists(const std::string &filename)
does a file exist
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:357

◆ freshEvents() [1/2]

QList< VP1EventFile > VP1AvailEvents::freshEvents ( ) const
inherited

Definition at line 129 of file VP1AvailEvents.cxx.

130{
132}
VP1EventFile newestEvent() const

◆ freshEvents() [2/2]

QList< VP1EventFile > VP1AvailEvents::freshEvents ( VP1EventFile newestEvt,
const QList< VP1EventFile > & inputEventList ) const
protectedinherited

Definition at line 110 of file VP1AvailEvents.cxx.

111{
112 QList<VP1EventFile> l;
113 if (!newestEvt.isValid())
114 return l;
115 std::set<Imp::EventID>::const_iterator histIt, histItE(m_d->historySorted.end());
116 if (m_d->timeCutForNew==0) {
117 if (m_d->historySorted.find(Imp::evtToID(newestEvt))==histItE)
118 l << newestEvt;
119 return l;
120 }
121 for(VP1EventFile evt : inputEventList)
122 if (m_d->historySorted.find(Imp::evtToID(evt))==histItE && isConsideredFresh(evt,newestEvt))
123 l << evt;
124 return l;
125}
static EventID evtToID(const VP1EventFile &evt)
bool isConsideredFresh(const VP1EventFile &evt, const VP1EventFile &newestEvt) const
bool isValid() const

◆ freshEventsChanged

void VP1AvailEvents::freshEventsChanged ( )
signalinherited

◆ generateHttpsRequest

void VP1AvailEvtsHttps::generateHttpsRequest ( )
privateslot

-— For logging std::cout << "VP1AvailEvtsHttps getHead stage -- " << m_d->m_stage << " --" << std::endl;

-— For logging std::cout << "VP1AvailEvtsHttps get stage -- " << m_d->m_stage << " --" << std::endl;

-— For logging std::cout << "VP1AvailEvtsHttps get stage -- " << m_d->m_stage << " --" << std::endl;

-— For logging std::cout << " Event : " << url.toString().toStdString() << std::endl;

Definition at line 153 of file VP1AvailEvtsHttps.cxx.

154{
155 QUrl fileInfoUrl(m_d->m_fileInfoUrl);
156 QNetworkRequest netrequest(fileInfoUrl);
157 if(m_d->m_stage==0) {
161 m_d->m_netreply = m_d->m_netmanager->head(netrequest);
162 } else if(m_d->m_stage==1) {
166 m_d->m_netreply = m_d->m_netmanager->get(netrequest);
167 } else {
168 // stage 2
172
173 QString activeRetrievalDir = tmpActiveRetrievalDir();
174 QString target = activeRetrievalDir +"downloadedfileinfo.txt";
175 VP1EvtsOnServerInfo newEvtsOnServerInfo(target);
176
177 if (!newEvtsOnServerInfo.isValid()) {
178 message("Problems decoding info in file downloaded from " + m_d->m_fileInfoUrl);
179 QFile::remove(target);
181 m_d->m_stage = 0;
182 m_d->startTimer(m_d->m_updateInterval);
183 return;
184 }
185
186 VP1EventFile evtToGet;
187 QString localfiledir = tmpLocalFileDir();
188
189 for (VP1EventFile evt : newEvtsOnServerInfo.events(timeCutForNew(),requireNewestRunNumber())) {
190 //We are looking for an event which was never seen before and not available locally:
191 if (!evt.isValid()||inHistory(evt.runNumber(),evt.eventNumber()))
192 continue;
193 if (!QFile::exists(localfiledir+evt.fileName())){
194 evtToGet=evt;
195 break;
196 }
197 }
198
199 if(evtToGet.isValid()) {
200 //Before we get it, let us check that we don't already have 3
201 //fresh events locally which are newer than the one we are trying
202 //to download:
203 QList<VP1EventFile> freshLocalEvents = freshEvents();
204 unsigned nNewer(0);
205 for(VP1EventFile evt : freshLocalEvents) {
206 if (evt < evtToGet) {
207 ++nNewer;
208 if (nNewer>=3) {
209 m_d->m_stage = 0;
210 m_d->startTimer(m_d->m_updateInterval);
211 return;
212 }
213 }
214 }
215
216 QUrl url(m_d->m_baseUrl + evtToGet.fileName());
217 m_d->m_expectedMD5Sum = evtToGet.md5Sum();
221 QNetworkRequest netrequestEvt(url);
222 m_d->m_evtToGet = evtToGet.fileName();
223 m_d->m_netreply = m_d->m_netmanager->get(netrequestEvt);
224 } else {
225 m_d->m_stage = 0;
226 m_d->startTimer(m_d->m_updateInterval);
227 return;
228 }
229 }
230 m_d->connectNetworkSignalsToSlots();
231}
bool requireNewestRunNumber() const
bool inHistory(int run, int event) const
void invalidateDirCache(const QString &dir)
const QString & fileName() const
const QString & md5Sum() const

◆ inHistory()

bool VP1AvailEvents::inHistory ( int run,
int event ) const
protectedinherited

Definition at line 187 of file VP1AvailEvents.cxx.

188{
189 return m_d->historySorted.find(Imp::EventID(run,event))!=m_d->historySorted.end();
190}

◆ init()

void VP1AvailEvtsHttps::init ( )
virtual

Implements VP1AvailEvents.

Definition at line 137 of file VP1AvailEvtsHttps.cxx.

138{
139 // Dummy for now
140}

◆ invalidateDirCache()

void VP1AvailEvents::invalidateDirCache ( const QString & dir)
protectedinherited

Definition at line 194 of file VP1AvailEvents.cxx.

195{
196 std::map<QString,std::pair<QDateTime,QList<VP1EventFile> > >::iterator it = m_d->dircache.find(dir);
197 if (it!=m_d->dircache.end())
198 m_d->dircache.erase(it);
199}

◆ isConsideredFresh()

bool VP1AvailEvents::isConsideredFresh ( const VP1EventFile & evt,
const VP1EventFile & newestEvt ) const
protectedinherited

Definition at line 336 of file VP1AvailEvents.cxx.

337{
338 //Notice: Logic here must be similar to logic in VP1EvtsOnServerInfo::events
339
340 if (m_d->timeCutForNew==0) {
341 //Special case where only the newest event is fresh
342 return evt.rawTime()==newestEvt.rawTime() && evt.runNumber()==newestEvt.runNumber();
343 }
344 if (requireNewestRunNumber()&&evt.runNumber()!=newestEvt.runNumber())
345 return false;
346 const unsigned oldest_time = (m_d->timeCutForNew<0||unsigned(m_d->timeCutForNew)>newestEvt.rawTime()) ? 0 : newestEvt.rawTime() - m_d->timeCutForNew;
347 return evt.rawTime() >= oldest_time;
348}
int runNumber() const
unsigned rawTime() const

◆ maxLocalFilesToKeep()

int VP1AvailEvents::maxLocalFilesToKeep ( ) const
inherited

Definition at line 98 of file VP1AvailEvents.cxx.

99{
100 return m_d->maxLocalFilesToKeep;
101}

◆ message

void VP1AvailEvents::message ( const QString & ) const
signalinherited

◆ messageDebug()

void VP1AvailEvents::messageDebug ( const QString & s) const
protectedinherited

Definition at line 351 of file VP1AvailEvents.cxx.

352{
353 std::cout<<VP1Msg::prefix_verbose()<<": "<<s.toStdString()<<std::endl;
354}
static const char * prefix_verbose()
Definition VP1Msg.h:59

◆ newestEvent()

VP1EventFile VP1AvailEvents::newestEvent ( ) const
inherited

Definition at line 135 of file VP1AvailEvents.cxx.

136{
137 QList<VP1EventFile> evts(allLocalEvents());
138 if (evts.isEmpty())
139 return VP1EventFile();//invalid
140 return evts.front();
141}

◆ requireNewestRunNumber()

bool VP1AvailEvents::requireNewestRunNumber ( ) const
inlineprotectedinherited

Definition at line 62 of file VP1AvailEvents.h.

62{ return false; /*fixme*/ }

◆ setCurrentEvent()

void VP1AvailEvents::setCurrentEvent ( int run,
int event )
inherited

Definition at line 144 of file VP1AvailEvents.cxx.

145{
146 m_d->historyOrdered << Imp::EventID(run,event);
147 m_d->historySorted.insert(Imp::EventID(run,event));
148}

◆ sslErrors

void VP1AvailEvtsHttps::sslErrors ( const QList< QSslError > & )
privateslot

-— For logging std::cout << "VP1AvailEvtsHttps SSL errors" << std::endl; for(int ii=0; ii<errlist.size(); ++ii) std::cout << " SSL * " << (int)errlist.at(ii).error() << ", " << errlist.at(ii).errorString().toStdString() << std::endl;

Definition at line 365 of file VP1AvailEvtsHttps.cxx.

366{
373 m_d->m_netreply->ignoreSslErrors();
374}

◆ start

void VP1AvailEvtsHttps::start ( QNetworkAccessManager * netmanager)
slot

Definition at line 142 of file VP1AvailEvtsHttps.cxx.

143{
144 m_d->m_netmanager = netmanager;
145 m_d->startTimer();
146}

◆ timeCutForNew()

int VP1AvailEvents::timeCutForNew ( ) const
protectedinherited

Definition at line 92 of file VP1AvailEvents.cxx.

93{
94 return m_d->timeCutForNew;
95}

◆ tmpActiveRetrievalDir()

QString VP1AvailEvents::tmpActiveRetrievalDir ( )
protectedinherited

Definition at line 277 of file VP1AvailEvents.cxx.

278{
279 return m_d->attemptGenerationOfTmpSubdir("activeretrievals",m_d->tmpActiveRetrievalDir);
280}

◆ tmpDir()

const QString & VP1AvailEvents::tmpDir ( ) const
protectedinherited

Definition at line 104 of file VP1AvailEvents.cxx.

105{
106 return m_d->tmpDir;
107}

◆ tmpLocalFileDir()

QString VP1AvailEvents::tmpLocalFileDir ( ) const
protectedinherited

Definition at line 283 of file VP1AvailEvents.cxx.

284{
285 return m_d->attemptGenerationOfTmpSubdir("eventfiles",m_d->tmpLocalFileDir);
286}

Member Data Documentation

◆ m_d

Imp* VP1AvailEvtsHttps::m_d
private

Definition at line 64 of file VP1AvailEvtsHttps.h.


The documentation for this class was generated from the following files: