| # Gaudi Framework, version v20r4
Generated: 8 Jan 2009

# GFALStageBlock.cpp

Go to the documentation of this file.

    00001 // $Id: GFALStageBlock.cpp,v 1.2 2007/09/28 07:52:33 marcocle Exp $
    00002 // Include files
    00003
    00004 // local
    00005 #include "GFALStageBlock.h"
    00006
    00007 //From Gaudi
    00008 #include "GaudiKernel/GaudiException.h"
    00009 #include "GaudiKernel/System.h"
    00010
    00011 //-----------------------------------------------------------------------------
    00012 // Implementation file for class : GFALStageBlock
    00013 //
    00014 // 2006-11-11 : Andres Felipe OSORIO OLIVEROS
    00015 //              Greig COWAN
    00016 //-----------------------------------------------------------------------------
    00017
    00018 //=============================================================================
    00019 // Standard constructor, initializes variables
    00020 //=============================================================================
    00021 GFALStageBlock::GFALStageBlock(  ) {
    00022
    00023 }
    00024
    00025 GFALStageBlock::GFALStageBlock(size_t nmaxfiles,IMessageSvc* ms)
    00026 {
    00027
    00028   m_MS = ms ;
    00029   m_status = UNKNOWN ;
    00030   m_nmaxfiles = nmaxfiles ;
    00031   m_nfiles = 0 ;
    00032   updateFillStatus();
    00033   m_protocols.push_back(std::string("rfio"));
    00034   m_protocols.push_back(std::string("gsidcap"));
    00035   m_protocols.push_back(std::string("gridftp"));
    00036
    00037 }
    00038
    00039 //=============================================================================
    00040 // Destructor
    00041 //=============================================================================
    00042 GFALStageBlock::~GFALStageBlock() {
    00043
    00044   std::vector<GFALStageFile*>::iterator itr = m_files.begin() ;
    00045   while( (itr != m_files.end()) ) {
    00046     if ( (*itr) ) delete (*itr);
    00047     ++itr;
    00048   }
    00049
    00050 }
    00051
    00052 //=============================================================================
    00053 StatusCode GFALStageBlock::stageInitialize()
    00054 {
    00055
    00056   StatusCode stc = StatusCode::SUCCESS ;
    00057
    00058   MsgStream log( msgSvc(), name() );
    00059   log << MSG::DEBUG << "stageInitialize> preparing files for stage process" << endreq;
    00060
    00061   if (m_nfiles == 0) {
    00062     return StatusCode::FAILURE;
    00063   }
    00064
    00065   //get for each GFALStageFile its surl
    00066   int k=0;
    00067
    00068   m_nmaxfiles = m_files.size();
    00069
    00070   for(std::vector<GFALStageFile*>::iterator itr = m_files.begin();
    00071       itr != m_files.end();
    00072       ++itr)
    00073   {
    00074
    00075     if( (*itr)->isValid() ) {
    00076       std::string fsurl = (*itr)->getSurl();
    00077       m_surls.push_back(fsurl);
    00078       ptr_surls[k] = (char*) fsurl.c_str();
    00079       ++k;
    00080     }
    00081     else {
    00082       //GFALStageFile set to status=unknown
    00083       log << MSG::DEBUG << "stageInitialize> could not get surl from file"  << endreq;
    00084       stc = StatusCode::FAILURE;
    00085     }
    00086   }
    00087
    00088   //protocols
    00089   k = 0;
    00090   for(std::vector<std::string>::iterator itr = m_protocols.begin();
    00091       itr != m_protocols.end();
    00092       ++itr) {
    00093     std::string protocol = (*itr);
    00094     ptr_protocols[k] = (char*) protocol.c_str();
    00095     ++k;
    00096   }
    00097
    00098   m_nprotocols = m_protocols.size();
    00099
    00100   //Not fully implemented (in gfal)
    00101   m_reqid    = 0;
    00102   //m_tokens   = 0;
    00103   m_errbuf   = NULL;
    00104   m_errbufsz = 0;
    00105
    00106   //Timeout default value
    00107   m_timeout  = 100;
    00108
    00109   log << MSG::DEBUG << "stageInitialize> done" << endreq;
    00110
    00111   return stc;
    00112
    00113 }
    00114
    00115
    00116 StatusCode GFALStageBlock::Stage()
    00117 {
    00118
    00119   StatusCode stc = StatusCode::SUCCESS ;
    00120
    00121   MsgStream log( msgSvc(), name() );
    00122
    00123   log << MSG::DEBUG << "Stage> starts here." << endreq;
    00124
    00125   if ( stageInitialize()==StatusCode::FAILURE) {
    00126     stc = StatusCode::FAILURE ;
    00127     return stc ;
    00128   }
    00129
    00130   if ( stageFinalize()==StatusCode::FAILURE) {
    00131     stc = StatusCode::FAILURE ;
    00132   }
    00133
    00134   log << MSG::DEBUG << "invoking srm_getx()" << endreq;
    00135
    00136   int ret = srm_getx( m_nmaxfiles, const_cast<const char **>(ptr_surls),
    00137                       m_nprotocols, ptr_protocols, &m_reqid,
    00138                       &m_filestatuses, m_errbuf, m_errbufsz, m_timeout);
    00139
    00140   log << MSG::DEBUG << "srm_getx() returned: " << ret << endreq;
    00141   log << MSG::DEBUG << "checking now filestatus: " << endreq;
    00142
    00143   if ( ret == -1 ) return StatusCode::FAILURE;
    00144
    00145   struct srm_filestatus *fs = m_filestatuses;
    00146
    00147   std::vector<GFALStageFile*>::iterator itr = m_files.begin();
    00148
    00149   int pos = 0;
    00150   int nstaging = 0;
    00151   int nstaged = 0;
    00152
    00153   for( int i = 0; i < (int)m_nmaxfiles; ++i) {
    00154
    00155     log << MSG::DEBUG << "file status: " << fs->status << endreq;
    00156     (*itr)->setStatus( fs->status );
    00157
    00158     if( fs->status == -1 ) {
    00159       log << MSG::DEBUG << "File is FAILED" << endreq;
    00160       m_failedAt.push_back( pos );
    00161     }
    00162     else if ( fs->status == 0 ) {
    00163       log << MSG::DEBUG << "File is STGING" << endreq;
    00164       (*itr)->setTurl(std::string("PENDING"));
    00165       (*itr)->setRequestID( fs->fileid );
    00166       ++nstaging;
    00167     }
    00168     else if ( fs->status == 1 ) {
    00169       log << MSG::DEBUG << "File is STGED" << endreq;
    00170       (*itr)->setTurl(std::string( fs->turl ));
    00171       (*itr)->setRequestID( fs->fileid );
    00172       log << MSG::DEBUG << "turl: \n"
    00173           << std::string( fs->turl ) << endreq;
    00174       ++nstaged;
    00175     }
    00176     else {
    00177       log << MSG::DEBUG << "status UNKNOWN" << endreq;
    00178     }
    00179
    00180     ++itr;
    00181     fs++;
    00182   }
    00183
    00184   if ( nstaging > 0 ) m_status = STGING;
    00185   else if (nstaged == (int)m_nmaxfiles) {
    00186     log << MSG::DEBUG << "block set to STGED" << endreq;
    00187     m_status = STGED;
    00188   }
    00189   else {
    00190     log << MSG::DEBUG << "block set to FAILED" << endreq;
    00191     m_status = FAILED;
    00192   }
    00193
    00194   log << MSG::DEBUG << " Stage> done" << endreq;
    00195
    00196   return stc ;
    00197
    00198 }
    00199
    00200
    00201 StatusCode GFALStageBlock::stageFinalize()
    00202 {
    00203
    00204   StatusCode stc = StatusCode::SUCCESS ;
    00205   return stc ;
    00206
    00207 }
    00208
    00209
    00210 StatusCode GFALStageBlock::addFile(GFALStageFile * stgfile)
    00211 {
    00212
    00213   MsgStream log( msgSvc(), name() );
    00214
    00215   if (getFillStatus()==FULL) {
    00216     log << MSG::DEBUG << "addFile> Failed. block is full" << endreq;
    00217     return StatusCode::FAILURE;
    00218   }
    00219
    00220   m_files.push_back(stgfile);
    00221   m_nfiles++;
    00222   updateFillStatus();
    00223
    00224   return StatusCode::SUCCESS;
    00225
    00226 }
    00227
    00228 void GFALStageBlock::updateFillStatus()
    00229 {
    00230   if(m_nfiles == 0) {
    00231     m_fillstatus = EMPTY ;
    00232   }
    00233   else if(m_nfiles != 0){
    00234     m_fillstatus = FREE ;
    00235   }
    00236   else if(m_nfiles == (m_nmaxfiles-1)) {
    00237     m_fillstatus = FULL ;
    00238   }
    00239
    00240   else {}
    00241
    00242   return ;
    00243
    00244 }
    00245
    00246 void GFALStageBlock::updateStatus()
    00247 {
    00248
    00249   MsgStream log( msgSvc(), name() );
    00250
    00251   int ret = srm_get( m_nmaxfiles,const_cast<const char**>(ptr_surls),
    00252                      m_nprotocols, ptr_protocols, &m_reqid, m_tokens,
    00253                      &m_filestatuses, m_timeout);
    00254
    00255   log << MSG::DEBUG << "srm_get() returned: " << ret << endreq;
    00256   log << MSG::DEBUG << "checking now filestatus: " << endreq;
    00257
    00258   if ( ret == -1 ) return;
    00259
    00260   struct srm_filestatus *fs = m_filestatuses;
    00261
    00262   std::vector<GFALStageFile*>::iterator itr = m_files.begin();
    00263
    00264   int pos = 0;
    00265   int nstaged = 0;
    00266   int nfailed = 0;
    00267
    00268   for( int i = 0; i < (int)m_nmaxfiles; ++i) {
    00269
    00270     log << MSG::DEBUG << "file status: " << fs->status << endreq;
    00271     (*itr)->setStatus( fs->status );
    00272
    00273     if( fs->status == -1 ) {
    00274       log << MSG::DEBUG << "File is FAILED" << endreq;
    00275       m_failedAt.push_back( pos );
    00276       ++nfailed;
    00277     }
    00278     else if ( fs->status == 0 ) {
    00279       log << MSG::DEBUG << "File is STGING" << endreq;
    00280       (*itr)->setTurl(std::string("PENDING"));
    00281       (*itr)->setRequestID( fs->fileid );
    00282     }
    00283     else if ( fs->status == 1 ) {
    00284       log << MSG::DEBUG << "File is STGED" << endreq;
    00285       log << MSG::DEBUG << "turl:"
    00286           << std::string( fs->turl ) << endreq;
    00287       (*itr)->setTurl(std::string( fs->turl ));
    00288       (*itr)->setRequestID( fs->fileid );
    00289       ++nstaged;
    00290     }
    00291     else {
    00292       log << MSG::DEBUG << "UNKNOWN status" << endreq;
    00293     }
    00294
    00295     ++itr;
    00296     fs++;
    00297     ++pos;
    00298   }
    00299
    00300   if (nstaged == ( (int) m_nmaxfiles - nfailed) && nstaged != 0 ) m_status = STGED ;
    00301   else if ( nfailed == (int) m_nmaxfiles ) m_status = FAILED;
    00302   else  m_status = STGING;
    00303
    00304   return ;
    00305
    00306 }
    00307
    00308 GFALStageBlock::Status GFALStageBlock::getStatus() const
    00309 {
    00310   return m_status ;
    00311 }
    00312
    00313
    00314 GFALStageBlock::FillStatus GFALStageBlock::getFillStatus() const
    00315 {
    00316   return m_fillstatus ;
    00317 }