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 //------------------------------------------------------------------------ 00515 //------------------------------------------------------------------------ 00516 class ListEntry 00517 { 00518 public: 00519 //-------------------------------------------------------------------- 00521 //-------------------------------------------------------------------- 00522 ListEntry( const std::string &hostAddress, 00523 const std::string &name, 00524 StatInfo *statInfo = 0): 00525 pHostAddress( hostAddress ), 00526 pName( name ), 00527 pStatInfo( statInfo ) 00528 {} 00529 00530 //-------------------------------------------------------------------- 00532 //-------------------------------------------------------------------- 00533 ~ListEntry() 00534 { 00535 delete pStatInfo; 00536 } 00537 00538 //-------------------------------------------------------------------- 00540 //-------------------------------------------------------------------- 00541 const std::string &GetHostAddress() const 00542 { 00543 return pHostAddress; 00544 } 00545 00546 //-------------------------------------------------------------------- 00548 //-------------------------------------------------------------------- 00549 const std::string &GetName() const 00550 { 00551 return pName; 00552 } 00553 00554 //-------------------------------------------------------------------- 00556 //-------------------------------------------------------------------- 00557 StatInfo *GetStatInfo() 00558 { 00559 return pStatInfo; 00560 } 00561 00562 //-------------------------------------------------------------------- 00564 //-------------------------------------------------------------------- 00565 const StatInfo *GetStatInfo() const 00566 { 00567 return pStatInfo; 00568 } 00569 00570 //-------------------------------------------------------------------- 00572 //-------------------------------------------------------------------- 00573 void SetStatInfo( StatInfo *info ) 00574 { 00575 pStatInfo = info; 00576 } 00577 00578 private: 00579 std::string pHostAddress; 00580 std::string pName; 00581 StatInfo *pStatInfo; 00582 }; 00583 00584 //------------------------------------------------------------------------ 00586 //------------------------------------------------------------------------ 00587 DirectoryList(); 00588 00589 //------------------------------------------------------------------------ 00591 //------------------------------------------------------------------------ 00592 ~DirectoryList(); 00593 00594 //------------------------------------------------------------------------ 00596 //------------------------------------------------------------------------ 00597 typedef std::vector<ListEntry*> DirList; 00598 00599 //------------------------------------------------------------------------ 00601 //------------------------------------------------------------------------ 00602 typedef DirList::iterator Iterator; 00603 00604 //------------------------------------------------------------------------ 00606 //------------------------------------------------------------------------ 00607 typedef DirList::const_iterator ConstIterator; 00608 00609 //------------------------------------------------------------------------ 00611 //------------------------------------------------------------------------ 00612 void Add( ListEntry *entry ) 00613 { 00614 pDirList.push_back( entry ); 00615 } 00616 00617 //------------------------------------------------------------------------ 00619 //------------------------------------------------------------------------ 00620 ListEntry *At( uint32_t index ) 00621 { 00622 return pDirList[index]; 00623 } 00624 00625 //------------------------------------------------------------------------ 00627 //------------------------------------------------------------------------ 00628 Iterator Begin() 00629 { 00630 return pDirList.begin(); 00631 } 00632 00633 //------------------------------------------------------------------------ 00635 //------------------------------------------------------------------------ 00636 ConstIterator Begin() const 00637 { 00638 return pDirList.begin(); 00639 } 00640 00641 //------------------------------------------------------------------------ 00643 //------------------------------------------------------------------------ 00644 Iterator End() 00645 { 00646 return pDirList.end(); 00647 } 00648 00649 //------------------------------------------------------------------------ 00651 //------------------------------------------------------------------------ 00652 ConstIterator End() const 00653 { 00654 return pDirList.end(); 00655 } 00656 00657 //------------------------------------------------------------------------ 00659 //------------------------------------------------------------------------ 00660 uint32_t GetSize() const 00661 { 00662 return pDirList.size(); 00663 } 00664 00665 //------------------------------------------------------------------------ 00667 //------------------------------------------------------------------------ 00668 const std::string &GetParentName() const 00669 { 00670 return pParent; 00671 } 00672 00673 //------------------------------------------------------------------------ 00675 //------------------------------------------------------------------------ 00676 void SetParentName( const std::string &parent ) 00677 { 00678 size_t pos = parent.find( '?' ); 00679 pParent = pos == std::string::npos ? parent : parent.substr( 0, pos ); 00680 if( !pParent.empty() && pParent[pParent.length()-1] != '/' ) 00681 pParent += "/"; 00682 } 00683 00684 //------------------------------------------------------------------------ 00686 //------------------------------------------------------------------------ 00687 bool ParseServerResponse( const std::string &hostId, 00688 const char *data ); 00689 00690 private: 00691 DirList pDirList; 00692 std::string pParent; 00693 }; 00694 00695 //---------------------------------------------------------------------------- 00697 //---------------------------------------------------------------------------- 00698 class OpenInfo 00699 { 00700 public: 00701 //------------------------------------------------------------------------ 00703 //------------------------------------------------------------------------ 00704 OpenInfo( const uint8_t *fileHandle, 00705 uint64_t sessionId, 00706 StatInfo *statInfo = 0 ): 00707 pSessionId(sessionId), pStatInfo( statInfo ) 00708 { 00709 memcpy( pFileHandle, fileHandle, 4 ); 00710 } 00711 00712 //------------------------------------------------------------------------ 00714 //------------------------------------------------------------------------ 00715 ~OpenInfo() 00716 { 00717 delete pStatInfo; 00718 } 00719 00720 //------------------------------------------------------------------------ 00722 //------------------------------------------------------------------------ 00723 void GetFileHandle( uint8_t *fileHandle ) const 00724 { 00725 memcpy( fileHandle, pFileHandle, 4 ); 00726 } 00727 00728 //------------------------------------------------------------------------ 00730 //------------------------------------------------------------------------ 00731 const StatInfo *GetStatInfo() const 00732 { 00733 return pStatInfo; 00734 } 00735 00736 //------------------------------------------------------------------------ 00737 // Get session ID 00738 //------------------------------------------------------------------------ 00739 uint64_t GetSessionId() const 00740 { 00741 return pSessionId; 00742 } 00743 00744 private: 00745 uint8_t pFileHandle[4]; 00746 uint64_t pSessionId; 00747 StatInfo *pStatInfo; 00748 }; 00749 00750 //---------------------------------------------------------------------------- 00752 //---------------------------------------------------------------------------- 00753 struct ChunkInfo 00754 { 00755 //-------------------------------------------------------------------------- 00757 //-------------------------------------------------------------------------- 00758 ChunkInfo( uint64_t off = 0, uint32_t len = 0, void *buff = 0 ): 00759 offset( off ), length( len ), buffer(buff) {} 00760 00761 uint64_t offset; 00762 uint32_t length; 00763 void *buffer; 00764 }; 00765 00766 //---------------------------------------------------------------------------- 00768 //---------------------------------------------------------------------------- 00769 typedef std::vector<ChunkInfo> ChunkList; 00770 00771 //---------------------------------------------------------------------------- 00773 //---------------------------------------------------------------------------- 00774 class VectorReadInfo 00775 { 00776 public: 00777 //------------------------------------------------------------------------ 00779 //------------------------------------------------------------------------ 00780 VectorReadInfo(): pSize( 0 ) {} 00781 00782 //------------------------------------------------------------------------ 00784 //------------------------------------------------------------------------ 00785 uint32_t GetSize() const 00786 { 00787 return pSize; 00788 } 00789 00790 //------------------------------------------------------------------------ 00792 //------------------------------------------------------------------------ 00793 void SetSize( uint32_t size ) 00794 { 00795 pSize = size; 00796 } 00797 00798 //------------------------------------------------------------------------ 00800 //------------------------------------------------------------------------ 00801 ChunkList &GetChunks() 00802 { 00803 return pChunks; 00804 } 00805 00806 //------------------------------------------------------------------------ 00808 //------------------------------------------------------------------------ 00809 const ChunkList &GetChunks() const 00810 { 00811 return pChunks; 00812 } 00813 00814 private: 00815 ChunkList pChunks; 00816 uint32_t pSize; 00817 }; 00818 00819 //---------------------------------------------------------------------------- 00820 // List of URLs 00821 //---------------------------------------------------------------------------- 00822 struct HostInfo 00823 { 00824 HostInfo(): 00825 flags(0), protocol(0), loadBalancer(false) {} 00826 HostInfo( const URL &u, bool lb = false ): 00827 flags(0), protocol(0), loadBalancer(lb), url(u) {} 00828 uint32_t flags; 00829 uint32_t protocol; 00830 bool loadBalancer; 00831 URL url; 00832 }; 00833 00834 typedef std::vector<HostInfo> HostList; 00835 00836 //---------------------------------------------------------------------------- 00838 //---------------------------------------------------------------------------- 00839 class ResponseHandler 00840 { 00841 public: 00842 virtual ~ResponseHandler() {} 00843 00844 //------------------------------------------------------------------------ 00852 //------------------------------------------------------------------------ 00853 virtual void HandleResponseWithHosts( XRootDStatus *status, 00854 AnyObject *response, 00855 HostList *hostList ) 00856 { 00857 delete hostList; 00858 HandleResponse( status, response ); 00859 } 00860 00861 //------------------------------------------------------------------------ 00868 //------------------------------------------------------------------------ 00869 virtual void HandleResponse( XRootDStatus *status, 00870 AnyObject *response ) 00871 { 00872 (void)status; (void)response; 00873 } 00874 }; 00875 } 00876 00877 #endif // __XRD_CL_XROOTD_RESPONSES_HH__