00001 //------------------------------------------------------------------------------ 00002 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN) 00003 // Author: Lukasz Janyst <ljanyst@cern.ch> 00004 //------------------------------------------------------------------------------ 00005 // XRootD is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // XRootD is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with XRootD. If not, see <http://www.gnu.org/licenses/>. 00017 //------------------------------------------------------------------------------ 00018 00019 #ifndef __XRD_CL_XROOTD_RESPONSES_HH__ 00020 #define __XRD_CL_XROOTD_RESPONSES_HH__ 00021 00022 #include "XrdCl/XrdClBuffer.hh" 00023 #include "XrdCl/XrdClStatus.hh" 00024 #include "XrdCl/XrdClURL.hh" 00025 #include "XrdCl/XrdClAnyObject.hh" 00026 #include "XProtocol/XProtocol.hh" 00027 #include <string> 00028 #include <vector> 00029 #include <list> 00030 #include <ctime> 00031 00032 namespace XrdCl 00033 { 00034 //---------------------------------------------------------------------------- 00036 //---------------------------------------------------------------------------- 00037 class LocationInfo 00038 { 00039 public: 00040 //------------------------------------------------------------------------ 00042 //------------------------------------------------------------------------ 00043 enum LocationType 00044 { 00045 ManagerOnline, 00046 ManagerPending, 00047 ServerOnline, 00048 ServerPending 00049 }; 00050 00051 //------------------------------------------------------------------------ 00053 //------------------------------------------------------------------------ 00054 enum AccessType 00055 { 00056 Read, 00057 ReadWrite 00058 }; 00059 00060 //------------------------------------------------------------------------ 00062 //------------------------------------------------------------------------ 00063 class Location 00064 { 00065 public: 00066 00067 //-------------------------------------------------------------------- 00069 //-------------------------------------------------------------------- 00070 Location( const std::string &address, 00071 LocationType type, 00072 AccessType access ): 00073 pAddress( address ), 00074 pType( type ), 00075 pAccess( access ) {} 00076 00077 //-------------------------------------------------------------------- 00079 //-------------------------------------------------------------------- 00080 const std::string &GetAddress() const 00081 { 00082 return pAddress; 00083 } 00084 00085 //-------------------------------------------------------------------- 00087 //-------------------------------------------------------------------- 00088 LocationType GetType() const 00089 { 00090 return pType; 00091 } 00092 00093 //-------------------------------------------------------------------- 00095 //-------------------------------------------------------------------- 00096 AccessType GetAccessType() const 00097 { 00098 return pAccess; 00099 } 00100 00101 //-------------------------------------------------------------------- 00103 //-------------------------------------------------------------------- 00104 bool IsServer() const 00105 { 00106 return pType == ServerOnline || pType == ServerPending; 00107 } 00108 00109 //-------------------------------------------------------------------- 00111 //-------------------------------------------------------------------- 00112 bool IsManager() const 00113 { 00114 return pType == ManagerOnline || pType == ManagerPending; 00115 } 00116 00117 private: 00118 std::string pAddress; 00119 LocationType pType; 00120 AccessType pAccess; 00121 }; 00122 00123 //------------------------------------------------------------------------ 00125 //------------------------------------------------------------------------ 00126 typedef std::vector<Location> LocationList; 00127 00128 //------------------------------------------------------------------------ 00130 //------------------------------------------------------------------------ 00131 typedef LocationList::iterator Iterator; 00132 00133 //------------------------------------------------------------------------ 00135 //------------------------------------------------------------------------ 00136 typedef LocationList::const_iterator ConstIterator; 00137 00138 //------------------------------------------------------------------------ 00140 //------------------------------------------------------------------------ 00141 LocationInfo(); 00142 00143 //------------------------------------------------------------------------ 00145 //------------------------------------------------------------------------ 00146 uint32_t GetSize() const 00147 { 00148 return pLocations.size(); 00149 } 00150 00151 //------------------------------------------------------------------------ 00153 //------------------------------------------------------------------------ 00154 Location &At( uint32_t index ) 00155 { 00156 return pLocations[index]; 00157 } 00158 00159 //------------------------------------------------------------------------ 00161 //------------------------------------------------------------------------ 00162 Iterator Begin() 00163 { 00164 return pLocations.begin(); 00165 } 00166 00167 //------------------------------------------------------------------------ 00169 //------------------------------------------------------------------------ 00170 ConstIterator Begin() const 00171 { 00172 return pLocations.begin(); 00173 } 00174 00175 //------------------------------------------------------------------------ 00177 //------------------------------------------------------------------------ 00178 Iterator End() 00179 { 00180 return pLocations.end(); 00181 } 00182 00183 //------------------------------------------------------------------------ 00185 //------------------------------------------------------------------------ 00186 ConstIterator End() const 00187 { 00188 return pLocations.end(); 00189 } 00190 00191 //------------------------------------------------------------------------ 00193 //------------------------------------------------------------------------ 00194 void Add( const Location &location ) 00195 { 00196 pLocations.push_back( location ); 00197 } 00198 00199 //------------------------------------------------------------------------ 00201 //------------------------------------------------------------------------ 00202 bool ParseServerResponse( const char *data ); 00203 00204 private: 00205 bool ProcessLocation( std::string &location ); 00206 LocationList pLocations; 00207 }; 00208 00209 //---------------------------------------------------------------------------- 00211 //---------------------------------------------------------------------------- 00212 class XRootDStatus: public Status 00213 { 00214 public: 00215 //------------------------------------------------------------------------ 00217 //------------------------------------------------------------------------ 00218 XRootDStatus( uint16_t st = 0, 00219 uint16_t code = 0, 00220 uint32_t errN = 0, 00221 const std::string &message = "" ): 00222 Status( st, code, errN ), 00223 pMessage( message ) {} 00224 00225 //------------------------------------------------------------------------ 00227 //------------------------------------------------------------------------ 00228 XRootDStatus( const Status &st, 00229 const std::string &message = "" ): 00230 Status( st ), 00231 pMessage( message ) {} 00232 00233 //------------------------------------------------------------------------ 00235 //------------------------------------------------------------------------ 00236 const std::string &GetErrorMessage() const 00237 { 00238 return pMessage; 00239 } 00240 00241 //------------------------------------------------------------------------ 00243 //------------------------------------------------------------------------ 00244 void SetErrorMessage( const std::string &message ) 00245 { 00246 pMessage = message; 00247 } 00248 00249 //------------------------------------------------------------------------ 00251 //------------------------------------------------------------------------ 00252 std::string ToStr() const 00253 { 00254 if( code == errErrorResponse ) 00255 { 00256 std::ostringstream o; 00257 o << "[ERROR] Server responded with an error: [" << errNo << "] "; 00258 o << pMessage << std::endl; 00259 return o.str(); 00260 } 00261 std::string str = ToString(); 00262 if( !pMessage.empty() ) 00263 str += ": " + pMessage; 00264 return str; 00265 } 00266 00267 private: 00268 std::string pMessage; 00269 }; 00270 00271 //---------------------------------------------------------------------------- 00273 //---------------------------------------------------------------------------- 00274 typedef Buffer BinaryDataInfo; 00275 00276 //---------------------------------------------------------------------------- 00278 //---------------------------------------------------------------------------- 00279 class ProtocolInfo 00280 { 00281 public: 00282 //------------------------------------------------------------------------ 00284 //------------------------------------------------------------------------ 00285 enum HostTypes 00286 { 00287 IsManager = kXR_isManager, 00288 IsServer = kXR_isServer, 00289 AttrMeta = kXR_attrMeta, 00290 AttrProxy = kXR_attrProxy, 00291 AttrSuper = kXR_attrSuper 00292 }; 00293 00294 //------------------------------------------------------------------------ 00296 //------------------------------------------------------------------------ 00297 ProtocolInfo( uint32_t version, uint32_t hostInfo ): 00298 pVersion( version ), pHostInfo( hostInfo ) {} 00299 00300 //------------------------------------------------------------------------ 00302 //------------------------------------------------------------------------ 00303 uint32_t GetVersion() const 00304 { 00305 return pVersion; 00306 } 00307 00308 //------------------------------------------------------------------------ 00310 //------------------------------------------------------------------------ 00311 uint32_t GetHostInfo() const 00312 { 00313 return pHostInfo; 00314 } 00315 00316 //------------------------------------------------------------------------ 00318 //------------------------------------------------------------------------ 00319 bool TestHostInfo( uint32_t flags ) 00320 { 00321 return pHostInfo & flags; 00322 } 00323 00324 private: 00325 uint32_t pVersion; 00326 uint32_t pHostInfo; 00327 }; 00328 00329 //---------------------------------------------------------------------------- 00331 //---------------------------------------------------------------------------- 00332 class StatInfo 00333 { 00334 public: 00335 //------------------------------------------------------------------------ 00337 //------------------------------------------------------------------------ 00338 enum Flags 00339 { 00340 XBitSet = kXR_xset, 00341 IsDir = kXR_isDir, 00342 Other = kXR_other, 00343 Offline = kXR_offline, 00344 POSCPending = kXR_poscpend, 00345 00346 IsReadable = kXR_readable, 00347 IsWritable = kXR_writable, 00348 BackUpExists = kXR_bkpexist 00349 }; 00350 00351 //------------------------------------------------------------------------ 00353 //------------------------------------------------------------------------ 00354 StatInfo(); 00355 00356 //------------------------------------------------------------------------ 00358 //------------------------------------------------------------------------ 00359 StatInfo( const std::string &id, uint64_t size, uint32_t flags, 00360 uint64_t modTime); 00361 00362 //------------------------------------------------------------------------ 00364 //------------------------------------------------------------------------ 00365 const std::string GetId() const 00366 { 00367 return pId; 00368 } 00369 00370 //------------------------------------------------------------------------ 00372 //------------------------------------------------------------------------ 00373 uint64_t GetSize() const 00374 { 00375 return pSize; 00376 } 00377 00378 //------------------------------------------------------------------------ 00380 //------------------------------------------------------------------------ 00381 uint32_t GetFlags() const 00382 { 00383 return pFlags; 00384 } 00385 00386 //------------------------------------------------------------------------ 00388 //------------------------------------------------------------------------ 00389 bool TestFlags( uint32_t flags ) const 00390 { 00391 return pFlags & flags; 00392 } 00393 00394 //------------------------------------------------------------------------ 00396 //------------------------------------------------------------------------ 00397 uint64_t GetModTime() const 00398 { 00399 return pModTime; 00400 } 00401 00402 //------------------------------------------------------------------------ 00404 //------------------------------------------------------------------------ 00405 std::string GetModTimeAsString() const 00406 { 00407 char ts[256]; 00408 time_t modTime = pModTime; 00409 tm *t = gmtime( &modTime ); 00410 strftime( ts, 255, "%F %T", t ); 00411 return ts; 00412 } 00413 00414 //------------------------------------------------------------------------ 00416 //------------------------------------------------------------------------ 00417 bool ParseServerResponse( const char *data ); 00418 00419 private: 00420 00421 //------------------------------------------------------------------------ 00422 // Normal stat 00423 //------------------------------------------------------------------------ 00424 std::string pId; 00425 uint64_t pSize; 00426 uint32_t pFlags; 00427 uint64_t pModTime; 00428 }; 00429 00430 //---------------------------------------------------------------------------- 00432 //---------------------------------------------------------------------------- 00433 class StatInfoVFS 00434 { 00435 public: 00436 //------------------------------------------------------------------------ 00438 //------------------------------------------------------------------------ 00439 StatInfoVFS(); 00440 00441 //------------------------------------------------------------------------ 00443 //------------------------------------------------------------------------ 00444 uint64_t GetNodesRW() const 00445 { 00446 return pNodesRW; 00447 } 00448 00449 //------------------------------------------------------------------------ 00451 //------------------------------------------------------------------------ 00452 uint64_t GetFreeRW() const 00453 { 00454 return pFreeRW; 00455 } 00456 00457 //------------------------------------------------------------------------ 00459 //------------------------------------------------------------------------ 00460 uint8_t GetUtilizationRW() const 00461 { 00462 return pUtilizationRW; 00463 } 00464 00465 //------------------------------------------------------------------------ 00467 //------------------------------------------------------------------------ 00468 uint64_t GetNodesStaging() const 00469 { 00470 return pNodesStaging; 00471 } 00472 00473 //------------------------------------------------------------------------ 00475 //------------------------------------------------------------------------ 00476 uint64_t GetFreeStaging() const 00477 { 00478 return pFreeStaging; 00479 } 00480 00481 //------------------------------------------------------------------------ 00483 //------------------------------------------------------------------------ 00484 uint8_t GetUtilizationStaging() const 00485 { 00486 return pUtilizationStaging; 00487 } 00488 00489 //------------------------------------------------------------------------ 00491 //------------------------------------------------------------------------ 00492 bool ParseServerResponse( const char *data ); 00493 00494 private: 00495 00496 //------------------------------------------------------------------------ 00497 // kXR_vfs stat 00498 //------------------------------------------------------------------------ 00499 uint64_t pNodesRW; 00500 uint64_t pFreeRW; 00501 uint32_t pUtilizationRW; 00502 uint64_t pNodesStaging; 00503 uint64_t pFreeStaging; 00504 uint32_t pUtilizationStaging; 00505 }; 00506 00507 //---------------------------------------------------------------------------- 00509 //---------------------------------------------------------------------------- 00510 class DirectoryList 00511 { 00512 public: 00513 00514 //------------------------------------------------------------------------ 00516 //------------------------------------------------------------------------ 00517 class ListEntry 00518 { 00519 public: 00520 //-------------------------------------------------------------------- 00522 //-------------------------------------------------------------------- 00523 ListEntry( const std::string &hostAddress, 00524 const std::string &name, 00525 StatInfo *statInfo = 0): 00526 pHostAddress( hostAddress ), 00527 pName( name ), 00528 pStatInfo( statInfo ) 00529 {} 00530 00531 //-------------------------------------------------------------------- 00533 //-------------------------------------------------------------------- 00534 ~ListEntry() 00535 { 00536 delete pStatInfo; 00537 } 00538 00539 //-------------------------------------------------------------------- 00541 //-------------------------------------------------------------------- 00542 const std::string &GetHostAddress() const 00543 { 00544 return pHostAddress; 00545 } 00546 00547 //-------------------------------------------------------------------- 00549 //-------------------------------------------------------------------- 00550 const std::string &GetName() const 00551 { 00552 return pName; 00553 } 00554 00555 //-------------------------------------------------------------------- 00557 //-------------------------------------------------------------------- 00558 StatInfo *GetStatInfo() 00559 { 00560 return pStatInfo; 00561 } 00562 00563 //-------------------------------------------------------------------- 00565 //-------------------------------------------------------------------- 00566 const StatInfo *GetStatInfo() const 00567 { 00568 return pStatInfo; 00569 } 00570 00571 //-------------------------------------------------------------------- 00573 //-------------------------------------------------------------------- 00574 void SetStatInfo( StatInfo *info ) 00575 { 00576 pStatInfo = info; 00577 } 00578 00579 private: 00580 std::string pHostAddress; 00581 std::string pName; 00582 StatInfo *pStatInfo; 00583 }; 00584 00585 //------------------------------------------------------------------------ 00587 //------------------------------------------------------------------------ 00588 DirectoryList(); 00589 00590 //------------------------------------------------------------------------ 00592 //------------------------------------------------------------------------ 00593 ~DirectoryList(); 00594 00595 //------------------------------------------------------------------------ 00597 //------------------------------------------------------------------------ 00598 typedef std::vector<ListEntry*> DirList; 00599 00600 //------------------------------------------------------------------------ 00602 //------------------------------------------------------------------------ 00603 typedef DirList::iterator Iterator; 00604 00605 //------------------------------------------------------------------------ 00607 //------------------------------------------------------------------------ 00608 typedef DirList::const_iterator ConstIterator; 00609 00610 //------------------------------------------------------------------------ 00612 //------------------------------------------------------------------------ 00613 void Add( ListEntry *entry ) 00614 { 00615 pDirList.push_back( entry ); 00616 } 00617 00618 //------------------------------------------------------------------------ 00620 //------------------------------------------------------------------------ 00621 ListEntry *At( uint32_t index ) 00622 { 00623 return pDirList[index]; 00624 } 00625 00626 //------------------------------------------------------------------------ 00628 //------------------------------------------------------------------------ 00629 Iterator Begin() 00630 { 00631 return pDirList.begin(); 00632 } 00633 00634 //------------------------------------------------------------------------ 00636 //------------------------------------------------------------------------ 00637 ConstIterator Begin() const 00638 { 00639 return pDirList.begin(); 00640 } 00641 00642 //------------------------------------------------------------------------ 00644 //------------------------------------------------------------------------ 00645 Iterator End() 00646 { 00647 return pDirList.end(); 00648 } 00649 00650 //------------------------------------------------------------------------ 00652 //------------------------------------------------------------------------ 00653 ConstIterator End() const 00654 { 00655 return pDirList.end(); 00656 } 00657 00658 //------------------------------------------------------------------------ 00660 //------------------------------------------------------------------------ 00661 uint32_t GetSize() const 00662 { 00663 return pDirList.size(); 00664 } 00665 00666 //------------------------------------------------------------------------ 00668 //------------------------------------------------------------------------ 00669 const std::string &GetParentName() const 00670 { 00671 return pParent; 00672 } 00673 00674 //------------------------------------------------------------------------ 00676 //------------------------------------------------------------------------ 00677 void SetParentName( const std::string &parent ) 00678 { 00679 size_t pos = parent.find( '?' ); 00680 pParent = pos == std::string::npos ? parent : parent.substr( 0, pos ); 00681 if( !pParent.empty() && pParent[pParent.length()-1] != '/' ) 00682 pParent += "/"; 00683 } 00684 00685 //------------------------------------------------------------------------ 00687 //------------------------------------------------------------------------ 00688 bool ParseServerResponse( const std::string &hostId, 00689 const char *data ); 00690 00691 //------------------------------------------------------------------------ 00693 //------------------------------------------------------------------------ 00694 bool ParseServerResponse( const std::string &hostId, 00695 const char *data, 00696 bool isDStat ); 00697 00698 //------------------------------------------------------------------------ 00700 //------------------------------------------------------------------------ 00701 static bool HasStatInfo( const char *data ); 00702 00703 private: 00704 DirList pDirList; 00705 std::string pParent; 00706 00707 static const std::string dStatPrefix; 00708 }; 00709 00710 //---------------------------------------------------------------------------- 00712 //---------------------------------------------------------------------------- 00713 class OpenInfo 00714 { 00715 public: 00716 //------------------------------------------------------------------------ 00718 //------------------------------------------------------------------------ 00719 OpenInfo( const uint8_t *fileHandle, 00720 uint64_t sessionId, 00721 StatInfo *statInfo = 0 ): 00722 pSessionId(sessionId), pStatInfo( statInfo ) 00723 { 00724 memcpy( pFileHandle, fileHandle, 4 ); 00725 } 00726 00727 //------------------------------------------------------------------------ 00729 //------------------------------------------------------------------------ 00730 ~OpenInfo() 00731 { 00732 delete pStatInfo; 00733 } 00734 00735 //------------------------------------------------------------------------ 00737 //------------------------------------------------------------------------ 00738 void GetFileHandle( uint8_t *fileHandle ) const 00739 { 00740 memcpy( fileHandle, pFileHandle, 4 ); 00741 } 00742 00743 //------------------------------------------------------------------------ 00745 //------------------------------------------------------------------------ 00746 const StatInfo *GetStatInfo() const 00747 { 00748 return pStatInfo; 00749 } 00750 00751 //------------------------------------------------------------------------ 00752 // Get session ID 00753 //------------------------------------------------------------------------ 00754 uint64_t GetSessionId() const 00755 { 00756 return pSessionId; 00757 } 00758 00759 private: 00760 uint8_t pFileHandle[4]; 00761 uint64_t pSessionId; 00762 StatInfo *pStatInfo; 00763 }; 00764 00765 //---------------------------------------------------------------------------- 00767 //---------------------------------------------------------------------------- 00768 struct ChunkInfo 00769 { 00770 //-------------------------------------------------------------------------- 00772 //-------------------------------------------------------------------------- 00773 ChunkInfo( uint64_t off = 0, uint32_t len = 0, void *buff = 0 ): 00774 offset( off ), length( len ), buffer(buff) {} 00775 00776 uint64_t offset; 00777 uint32_t length; 00778 void *buffer; 00779 }; 00780 00781 //---------------------------------------------------------------------------- 00783 //---------------------------------------------------------------------------- 00784 typedef std::vector<ChunkInfo> ChunkList; 00785 00786 //---------------------------------------------------------------------------- 00788 //---------------------------------------------------------------------------- 00789 class VectorReadInfo 00790 { 00791 public: 00792 //------------------------------------------------------------------------ 00794 //------------------------------------------------------------------------ 00795 VectorReadInfo(): pSize( 0 ) {} 00796 00797 //------------------------------------------------------------------------ 00799 //------------------------------------------------------------------------ 00800 uint32_t GetSize() const 00801 { 00802 return pSize; 00803 } 00804 00805 //------------------------------------------------------------------------ 00807 //------------------------------------------------------------------------ 00808 void SetSize( uint32_t size ) 00809 { 00810 pSize = size; 00811 } 00812 00813 //------------------------------------------------------------------------ 00815 //------------------------------------------------------------------------ 00816 ChunkList &GetChunks() 00817 { 00818 return pChunks; 00819 } 00820 00821 //------------------------------------------------------------------------ 00823 //------------------------------------------------------------------------ 00824 const ChunkList &GetChunks() const 00825 { 00826 return pChunks; 00827 } 00828 00829 private: 00830 ChunkList pChunks; 00831 uint32_t pSize; 00832 }; 00833 00834 //---------------------------------------------------------------------------- 00835 // List of URLs 00836 //---------------------------------------------------------------------------- 00837 struct HostInfo 00838 { 00839 HostInfo(): 00840 flags(0), protocol(0), loadBalancer(false) {} 00841 HostInfo( const URL &u, bool lb = false ): 00842 flags(0), protocol(0), loadBalancer(lb), url(u) {} 00843 uint32_t flags; 00844 uint32_t protocol; 00845 bool loadBalancer; 00846 URL url; 00847 }; 00848 00849 typedef std::vector<HostInfo> HostList; 00850 00851 //---------------------------------------------------------------------------- 00853 //---------------------------------------------------------------------------- 00854 class ResponseHandler 00855 { 00856 public: 00857 virtual ~ResponseHandler() {} 00858 00859 //------------------------------------------------------------------------ 00867 //------------------------------------------------------------------------ 00868 virtual void HandleResponseWithHosts( XRootDStatus *status, 00869 AnyObject *response, 00870 HostList *hostList ) 00871 { 00872 delete hostList; 00873 HandleResponse( status, response ); 00874 } 00875 00876 //------------------------------------------------------------------------ 00883 //------------------------------------------------------------------------ 00884 virtual void HandleResponse( XRootDStatus *status, 00885 AnyObject *response ) 00886 { 00887 (void)status; (void)response; 00888 } 00889 }; 00890 } 00891 00892 #endif // __XRD_CL_XROOTD_RESPONSES_HH__