XrdMonDecArgParser.cc

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*                                                                           */
00003 /*                           XrdMonDecArgParser.cc                           */
00004 /*                                                                           */
00005 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
00006 /*                            All Rights Reserved                            */
00007 /*       Produced by Jacek Becla for Stanford University under contract      */
00008 /*              DE-AC02-76SF00515 with the Department of Energy              */
00009 /*****************************************************************************/
00010 
00011 // $Id: XrdMonDecArgParser.cc 30949 2009-11-02 16:37:58Z ganis $
00012 
00013 #include "XrdMonDecArgParser.hh"
00014 #include "XrdMon/XrdMonException.hh"
00015 #include "XrdMon/XrdMonErrors.hh"
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include <unistd.h> /* access */
00020 
00021 bool    XrdMonDecArgParser::_forceCloseOnly(false);
00022 kXR_int32  XrdMonDecArgParser::_upToTime(0);
00023 kXR_int32  XrdMonDecArgParser::_ignoreIfBefore(0);
00024 string  XrdMonDecArgParser::_fPath;
00025 kXR_int64 XrdMonDecArgParser::_offset2Dump(0);
00026 string  XrdMonDecArgParser::_hostPort;
00027 string  XrdMonDecArgParser::_baseDir("logs/decoder"); // FIXME configurable
00028 bool    XrdMonDecArgParser::_saveTraces(false);       // FIXME configurable
00029 int     XrdMonDecArgParser::_maxTraceLogSize(2048);   // [MB] FIXME configurable
00030 
00031 void
00032 XrdMonDecArgParser::parseArguments(int argc, char* argv[])
00033 {
00034     if ( argc < 2 ) {
00035         throw XrdMonException(ERR_INVALIDARG, "Expected input file name");
00036     }
00037     bool isActiveFlag = false;
00038     
00039     int nr = 1;
00040     while ( nr < argc ) {
00041         if ( ! (strcmp(argv[nr],  "-isActive")) ) {
00042             if ( argc < nr+8 ) {
00043                 throw XrdMonException(ERR_INVALIDARG, 
00044                         "Expected 7 arguments after -isActive specified");
00045             }
00046             if ( 0 == strchr(argv[nr+1], ':') ) {
00047                 string ss("Expected <host>:<port>, found ");
00048                 ss += argv[nr+1];
00049                 throw XrdMonException(ERR_INVALIDARG, ss);
00050             }
00051             _hostPort = argv[nr+1];
00052             convertTime(nr, argv);            
00053             nr += 7;
00054             isActiveFlag = true;
00055         } else if ( ! strcmp(argv[nr], "-forceCloseOnly") ) {
00056             _forceCloseOnly = true;
00057         } else if ( ! strcmp(argv[nr], "-ignoreIfBefore") ) {
00058             if ( argc < nr+2 ) {
00059                 throw XrdMonException(ERR_INVALIDARG, 
00060                                   "Missing argument after -ignoreIfBefore");
00061             }
00062             sscanf(argv[nr+1], "%d", &_ignoreIfBefore);
00063             ++nr;
00064         } else if ( ! strcmp(argv[nr], "-offset2Dump") ) {
00065             if ( argc < nr+2 ) {
00066                 throw XrdMonException(ERR_INVALIDARG, 
00067                                   "Missing argument after -offset2Dump");
00068             }
00069             sscanf(argv[nr+1], "%lld", &_offset2Dump);
00070             ++nr;
00071         } else if ( _fPath.empty() ) {
00072             if ( 0 != access(argv[nr], F_OK) ) {
00073                 string ss("Invalid argument logFilePath ");
00074                 ss += argv[nr];
00075                 ss += " (file does not exist)";
00076                 throw XrdMonException(ERR_INVALIDARG, ss);
00077             }
00078             _fPath = argv[nr];
00079         } else {
00080             throw XrdMonException(ERR_INVALIDARG, "Invalid argument");
00081         }
00082         ++nr;
00083     }
00084 
00085     if ( isActiveFlag ) {
00086         const char* fName = _fPath.c_str();
00087         int len = strlen(fName);
00088         if ( len < 11 ) {
00089             throw XrdMonException(ERR_INVALIDARG, 
00090                   "Expected active.rcv after -isActive");
00091         }
00092         if ( 0 != strcmp(fName+len-10, "active.rcv") ) {
00093             throw XrdMonException(ERR_INVALIDARG, "Expected active.rcv after -isActive");
00094         }
00095     } else if ( ! _fPath.empty() ) {
00096         _hostPort = parsePath();
00097     }
00098     if ( _hostPort.empty() ) {
00099         throw XrdMonException(ERR_INVALIDARG, "HostPort is invalid");
00100     }
00101 }
00102 
00103 void
00104 XrdMonDecArgParser::convertTime(int nr, char* argv[])
00105 {
00106     struct tm tt;
00107     int x = atoi(argv[nr+2])-1900;
00108     if ( x < 100 ) {
00109         string ss("Invalid arg: year "); ss += argv[nr+2];
00110         throw XrdMonException(ERR_INVALIDARG, ss);
00111     }
00112     tt.tm_year = x;
00113     
00114     x = atoi(argv[nr+3])-1;
00115     if ( x < 0 || x > 11 ) {
00116         string ss("Invalid arg: month "); ss += argv[nr+3];
00117         throw XrdMonException(ERR_INVALIDARG, ss);
00118     }
00119     tt.tm_mon  = x;
00120  
00121     x = atoi(argv[nr+4]);
00122     if ( x < 1 || x > 31 ) {
00123         string ss("Invalid arg: day "); ss += argv[nr+4];
00124         throw XrdMonException(ERR_INVALIDARG, ss);
00125     }
00126     tt.tm_mday = x;
00127  
00128     x = atoi(argv[nr+5]);
00129     if ( x < 0 || x > 23 ) {
00130         string ss("Invalid arg: hour "); ss += argv[nr+5];
00131         throw XrdMonException(ERR_INVALIDARG, ss);
00132     }
00133     tt.tm_hour = x;
00134 
00135     x = atoi(argv[nr+6]);
00136     if ( x < 0 || x > 59 ) {
00137         string ss("Invalid arg: min "); ss += argv[nr+6];
00138         throw XrdMonException(ERR_INVALIDARG, ss);
00139     }
00140     tt.tm_min  = x;
00141 
00142     x = atoi(argv[nr+7]);
00143     if ( x < 0 || x > 59 || (argv[nr+7][0] < '0' || argv[nr+7][0] > '9') ) {
00144         string ss("Invalid arg: sec "); ss += argv[nr+7];
00145         throw XrdMonException(ERR_INVALIDARG, ss);
00146     }
00147     tt.tm_sec  = x;
00148     
00149     _upToTime = mktime(&tt);
00150 }
00151 
00152 // returns <host>:<port>
00153 string
00154 XrdMonDecArgParser::parsePath()
00155 {   
00156     if ( 0 != access(_fPath.c_str(), R_OK) ) {
00157         string se("Cannot open file "); se+=_fPath; se+=" for reading";
00158         throw XrdMonException(ERR_INVALIDARG, se);
00159     }
00160 
00161     // parse input file name, format:
00162     // <path>/YYYYMMDD_HH:MM:SS.MSC_<sender name>:<sender port>.rcv
00163         // if filePath contains '/', remove it
00164     int beg = _fPath.rfind('/', _fPath.size());
00165     if ( beg == -1 ) {
00166         beg = 0; // start from the beginning
00167     } else {
00168         ++beg;   // skip / and all before
00169     }
00170     if ( _fPath[ 8+beg] != '_' || _fPath[11+beg] != ':' ||
00171          _fPath[14+beg] != ':' || _fPath[17+beg] != '.' ||
00172          _fPath[21+beg] != '_' || _fPath.size()-beg < 27   ) {
00173         string se("Incorrect format of log file name, expected: ");
00174         se += "<path>/YYYYMMDD_HH:MM:SS.MSC_<sender name>:<sender port>.rcv";
00175         throw XrdMonException(ERR_INVALIDARG, se);
00176     }
00177     beg += 22; // skip the timestamp
00178     int mid = _fPath.find(':', beg);
00179     if ( mid-beg < 1 ) {
00180         throw XrdMonException(ERR_INVALIDARG, 
00181                 "Log file does not contain ':' after timestamp");
00182     }
00183     int end = _fPath.find(".rcv", beg);
00184     if ( end < 1 ) {
00185         throw XrdMonException(ERR_INVALIDARG, "Log file does not end with \".rcv\"");
00186     }
00187     string strHost(_fPath, beg, mid-beg);
00188     string strPort(_fPath, mid+1, end-(mid+1));
00189 
00190     int portNr = 0;
00191     sscanf(strPort.c_str(), "%d", &portNr);
00192     if ( portNr < 1 ) {
00193         char buf[256];
00194         sprintf(buf, "Decoded port number is invalid: %i", portNr);
00195         throw XrdMonException(ERR_INVALIDARG, buf);
00196     }
00197     strHost += ':';
00198     strHost += portNr;
00199 
00200     return strHost;
00201 }
00202 
00203 

Generated on Tue Jul 5 14:46:42 2011 for ROOT_528-00b_version by  doxygen 1.5.1