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( const char *data = 0 ); 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 private: 00200 void ParseServerResponse( const char *data ); 00201 void ProcessLocation( std::string &location ); 00202 LocationList pLocations; 00203 }; 00204 00205 //---------------------------------------------------------------------------- 00207 //---------------------------------------------------------------------------- 00208 class XRootDStatus: public Status 00209 { 00210 public: 00211 //------------------------------------------------------------------------ 00213 //------------------------------------------------------------------------ 00214 XRootDStatus( uint16_t st = 0, 00215 uint16_t code = 0, 00216 uint32_t errN = 0, 00217 const std::string &message = "" ): 00218 Status( st, code, errN ), 00219 pMessage( message ) {} 00220 00221 //------------------------------------------------------------------------ 00223 //------------------------------------------------------------------------ 00224 XRootDStatus( const Status &st, 00225 const std::string &message = "" ): 00226 Status( st ), 00227 pMessage( message ) {} 00228 00229 //------------------------------------------------------------------------ 00231 //------------------------------------------------------------------------ 00232 const std::string &GetErrorMessage() const 00233 { 00234 return pMessage; 00235 } 00236 00237 //------------------------------------------------------------------------ 00239 //------------------------------------------------------------------------ 00240 void SetErrorMessage( const std::string &message ) 00241 { 00242 pMessage = message; 00243 } 00244 00245 //------------------------------------------------------------------------ 00247 //------------------------------------------------------------------------ 00248 std::string ToStr() const 00249 { 00250 00251 if( code == errErrorResponse ) 00252 { 00253 std::ostringstream o; 00254 o << "[ERROR] Server responded with an error: [" << errNo << "] "; 00255 o << pMessage << std::endl; 00256 return o.str(); 00257 } 00258 return ToString(); 00259 } 00260 00261 private: 00262 std::string pMessage; 00263 }; 00264 00265 //---------------------------------------------------------------------------- 00267 //---------------------------------------------------------------------------- 00268 typedef Buffer BinaryDataInfo; 00269 00270 //---------------------------------------------------------------------------- 00272 //---------------------------------------------------------------------------- 00273 class ProtocolInfo 00274 { 00275 public: 00276 //------------------------------------------------------------------------ 00278 //------------------------------------------------------------------------ 00279 enum HostTypes 00280 { 00281 IsManager = kXR_isManager, 00282 IsServer = kXR_isServer, 00283 AttrMeta = kXR_attrMeta, 00284 AttrProxy = kXR_attrProxy, 00285 AttrSuper = kXR_attrSuper 00286 }; 00287 00288 //------------------------------------------------------------------------ 00290 //------------------------------------------------------------------------ 00291 ProtocolInfo( uint32_t version, uint32_t hostInfo ): 00292 pVersion( version ), pHostInfo( hostInfo ) {} 00293 00294 //------------------------------------------------------------------------ 00296 //------------------------------------------------------------------------ 00297 uint32_t GetVersion() const 00298 { 00299 return pVersion; 00300 } 00301 00302 //------------------------------------------------------------------------ 00304 //------------------------------------------------------------------------ 00305 uint32_t GetHostInfo() const 00306 { 00307 return pHostInfo; 00308 } 00309 00310 //------------------------------------------------------------------------ 00312 //------------------------------------------------------------------------ 00313 bool TestHostInfo( uint32_t flags ) 00314 { 00315 return pHostInfo & flags; 00316 } 00317 00318 private: 00319 uint32_t pVersion; 00320 uint32_t pHostInfo; 00321 }; 00322 00323 //---------------------------------------------------------------------------- 00325 //---------------------------------------------------------------------------- 00326 class StatInfo 00327 { 00328 public: 00329 //------------------------------------------------------------------------ 00331 //------------------------------------------------------------------------ 00332 enum Flags 00333 { 00334 XBitSet = kXR_xset, 00335 IsDir = kXR_isDir, 00336 Other = kXR_other, 00337 Offline = kXR_offline, 00338 POSCPending = kXR_poscpend, 00339 00340 IsReadable = kXR_readable, 00341 IsWritable = kXR_writable 00342 }; 00343 00344 //------------------------------------------------------------------------ 00346 //------------------------------------------------------------------------ 00347 StatInfo( const char *data ); 00348 00349 //------------------------------------------------------------------------ 00351 //------------------------------------------------------------------------ 00352 const std::string GetId() const 00353 { 00354 return pId; 00355 } 00356 00357 //------------------------------------------------------------------------ 00359 //------------------------------------------------------------------------ 00360 uint64_t GetSize() const 00361 { 00362 return pSize; 00363 } 00364 00365 //------------------------------------------------------------------------ 00367 //------------------------------------------------------------------------ 00368 uint32_t GetFlags() const 00369 { 00370 return pFlags; 00371 } 00372 00373 //------------------------------------------------------------------------ 00375 //------------------------------------------------------------------------ 00376 bool TestFlags( uint32_t flags ) const 00377 { 00378 return pFlags & flags; 00379 } 00380 00381 //------------------------------------------------------------------------ 00383 //------------------------------------------------------------------------ 00384 uint64_t GetModTime() const 00385 { 00386 return pModTime; 00387 } 00388 00389 //------------------------------------------------------------------------ 00391 //------------------------------------------------------------------------ 00392 std::string GetModTimeAsString() const 00393 { 00394 char ts[256]; 00395 time_t modTime = pModTime; 00396 tm *t = gmtime( &modTime ); 00397 strftime( ts, 255, "%F %T", t ); 00398 return ts; 00399 } 00400 00401 00402 private: 00403 00404 //------------------------------------------------------------------------ 00405 // Parse the stat info returned by the server 00406 //------------------------------------------------------------------------ 00407 void ParseServerResponse( const char *data ); 00408 00409 //------------------------------------------------------------------------ 00410 // Normal stat 00411 //------------------------------------------------------------------------ 00412 std::string pId; 00413 uint64_t pSize; 00414 uint32_t pFlags; 00415 uint64_t pModTime; 00416 }; 00417 00418 //---------------------------------------------------------------------------- 00420 //---------------------------------------------------------------------------- 00421 class StatInfoVFS 00422 { 00423 public: 00424 //------------------------------------------------------------------------ 00426 //------------------------------------------------------------------------ 00427 StatInfoVFS( const char *data ); 00428 00429 //------------------------------------------------------------------------ 00431 //------------------------------------------------------------------------ 00432 uint64_t GetNodesRW() const 00433 { 00434 return pNodesRW; 00435 } 00436 00437 //------------------------------------------------------------------------ 00439 //------------------------------------------------------------------------ 00440 uint64_t GetFreeRW() const 00441 { 00442 return pFreeRW; 00443 } 00444 00445 //------------------------------------------------------------------------ 00447 //------------------------------------------------------------------------ 00448 uint8_t GetUtilizationRW() const 00449 { 00450 return pUtilizationRW; 00451 } 00452 00453 //------------------------------------------------------------------------ 00455 //------------------------------------------------------------------------ 00456 uint64_t GetNodesStaging() const 00457 { 00458 return pNodesStaging; 00459 } 00460 00461 //------------------------------------------------------------------------ 00463 //------------------------------------------------------------------------ 00464 uint64_t GetFreeStaging() const 00465 { 00466 return pFreeStaging; 00467 } 00468 00469 //------------------------------------------------------------------------ 00471 //------------------------------------------------------------------------ 00472 uint8_t GetUtilizationStaging() const 00473 { 00474 return pUtilizationStaging; 00475 } 00476 00477 private: 00478 00479 //------------------------------------------------------------------------ 00480 // Parse the stat info returned by the server 00481 //------------------------------------------------------------------------ 00482 void ParseServerResponse( const char *data ); 00483 00484 //------------------------------------------------------------------------ 00485 // kXR_vfs stat 00486 //------------------------------------------------------------------------ 00487 uint64_t pNodesRW; 00488 uint64_t pFreeRW; 00489 uint32_t pUtilizationRW; 00490 uint64_t pNodesStaging; 00491 uint64_t pFreeStaging; 00492 uint32_t pUtilizationStaging; 00493 }; 00494 00495 //---------------------------------------------------------------------------- 00497 //---------------------------------------------------------------------------- 00498 class DirectoryList 00499 { 00500 public: 00501 //------------------------------------------------------------------------ 00503 //------------------------------------------------------------------------ 00504 class ListEntry 00505 { 00506 public: 00507 //-------------------------------------------------------------------- 00509 //-------------------------------------------------------------------- 00510 ListEntry( const std::string &hostAddress, 00511 const std::string &name, 00512 StatInfo *statInfo = 0): 00513 pHostAddress( hostAddress ), 00514 pName( name ), 00515 pStatInfo( statInfo ) 00516 {} 00517 00518 //-------------------------------------------------------------------- 00520 //-------------------------------------------------------------------- 00521 ~ListEntry() 00522 { 00523 delete pStatInfo; 00524 } 00525 00526 //-------------------------------------------------------------------- 00528 //-------------------------------------------------------------------- 00529 const std::string &GetHostAddress() const 00530 { 00531 return pHostAddress; 00532 } 00533 00534 //-------------------------------------------------------------------- 00536 //-------------------------------------------------------------------- 00537 const std::string &GetName() const 00538 { 00539 return pName; 00540 } 00541 00542 //-------------------------------------------------------------------- 00544 //-------------------------------------------------------------------- 00545 StatInfo *GetStatInfo() 00546 { 00547 return pStatInfo; 00548 } 00549 00550 //-------------------------------------------------------------------- 00552 //-------------------------------------------------------------------- 00553 const StatInfo *GetStatInfo() const 00554 { 00555 return pStatInfo; 00556 } 00557 00558 //-------------------------------------------------------------------- 00560 //-------------------------------------------------------------------- 00561 void SetStatInfo( StatInfo *info ) 00562 { 00563 pStatInfo = info; 00564 } 00565 00566 private: 00567 std::string pHostAddress; 00568 std::string pName; 00569 StatInfo *pStatInfo; 00570 }; 00571 00572 //------------------------------------------------------------------------ 00574 //------------------------------------------------------------------------ 00575 DirectoryList( const std::string &hostID, 00576 const std::string &parent, 00577 const char *data ); 00578 00579 //------------------------------------------------------------------------ 00581 //------------------------------------------------------------------------ 00582 ~DirectoryList(); 00583 00584 //------------------------------------------------------------------------ 00586 //------------------------------------------------------------------------ 00587 typedef std::vector<ListEntry*> DirList; 00588 00589 //------------------------------------------------------------------------ 00591 //------------------------------------------------------------------------ 00592 typedef DirList::iterator Iterator; 00593 00594 //------------------------------------------------------------------------ 00596 //------------------------------------------------------------------------ 00597 typedef DirList::const_iterator ConstIterator; 00598 00599 //------------------------------------------------------------------------ 00601 //------------------------------------------------------------------------ 00602 void Add( ListEntry *entry ) 00603 { 00604 pDirList.push_back( entry ); 00605 } 00606 00607 //------------------------------------------------------------------------ 00609 //------------------------------------------------------------------------ 00610 ListEntry *At( uint32_t index ) 00611 { 00612 return pDirList[index]; 00613 } 00614 00615 //------------------------------------------------------------------------ 00617 //------------------------------------------------------------------------ 00618 Iterator Begin() 00619 { 00620 return pDirList.begin(); 00621 } 00622 00623 //------------------------------------------------------------------------ 00625 //------------------------------------------------------------------------ 00626 ConstIterator Begin() const 00627 { 00628 return pDirList.begin(); 00629 } 00630 00631 //------------------------------------------------------------------------ 00633 //------------------------------------------------------------------------ 00634 Iterator End() 00635 { 00636 return pDirList.end(); 00637 } 00638 00639 //------------------------------------------------------------------------ 00641 //------------------------------------------------------------------------ 00642 ConstIterator End() const 00643 { 00644 return pDirList.end(); 00645 } 00646 00647 //------------------------------------------------------------------------ 00649 //------------------------------------------------------------------------ 00650 uint32_t GetSize() const 00651 { 00652 return pDirList.size(); 00653 } 00654 00655 //------------------------------------------------------------------------ 00657 //------------------------------------------------------------------------ 00658 const std::string &GetParentName() const 00659 { 00660 return pParent; 00661 } 00662 00663 private: 00664 void ParseServerResponse( const std::string &hostId, const char *data ); 00665 DirList pDirList; 00666 std::string pParent; 00667 }; 00668 00669 //---------------------------------------------------------------------------- 00671 //---------------------------------------------------------------------------- 00672 class OpenInfo 00673 { 00674 public: 00675 //------------------------------------------------------------------------ 00677 //------------------------------------------------------------------------ 00678 OpenInfo( const uint8_t *fileHandle, 00679 uint64_t sessionId, 00680 StatInfo *statInfo = 0 ): 00681 pSessionId(sessionId), pStatInfo( statInfo ) 00682 { 00683 memcpy( pFileHandle, fileHandle, 4 ); 00684 } 00685 00686 //------------------------------------------------------------------------ 00688 //------------------------------------------------------------------------ 00689 ~OpenInfo() 00690 { 00691 delete pStatInfo; 00692 } 00693 00694 //------------------------------------------------------------------------ 00696 //------------------------------------------------------------------------ 00697 void GetFileHandle( uint8_t *fileHandle ) const 00698 { 00699 memcpy( fileHandle, pFileHandle, 4 ); 00700 } 00701 00702 //------------------------------------------------------------------------ 00704 //------------------------------------------------------------------------ 00705 const StatInfo *GetStatInfo() const 00706 { 00707 return pStatInfo; 00708 } 00709 00710 //------------------------------------------------------------------------ 00711 // Get session ID 00712 //------------------------------------------------------------------------ 00713 uint64_t GetSessionId() const 00714 { 00715 return pSessionId; 00716 } 00717 00718 private: 00719 uint8_t pFileHandle[4]; 00720 uint64_t pSessionId; 00721 StatInfo *pStatInfo; 00722 }; 00723 00724 //---------------------------------------------------------------------------- 00726 //---------------------------------------------------------------------------- 00727 struct ChunkInfo 00728 { 00729 //-------------------------------------------------------------------------- 00731 //-------------------------------------------------------------------------- 00732 ChunkInfo( uint64_t off = 0, uint32_t len = 0, void *buff = 0 ): 00733 offset( off ), length( len ), buffer(buff) {} 00734 00735 uint64_t offset; 00736 uint32_t length; 00737 void *buffer; 00738 }; 00739 00740 //---------------------------------------------------------------------------- 00742 //---------------------------------------------------------------------------- 00743 typedef std::vector<ChunkInfo> ChunkList; 00744 00745 //---------------------------------------------------------------------------- 00747 //---------------------------------------------------------------------------- 00748 class VectorReadInfo 00749 { 00750 public: 00751 //------------------------------------------------------------------------ 00753 //------------------------------------------------------------------------ 00754 VectorReadInfo(): pSize( 0 ) {} 00755 00756 //------------------------------------------------------------------------ 00758 //------------------------------------------------------------------------ 00759 uint32_t GetSize() const 00760 { 00761 return pSize; 00762 } 00763 00764 //------------------------------------------------------------------------ 00766 //------------------------------------------------------------------------ 00767 void SetSize( uint32_t size ) 00768 { 00769 pSize = size; 00770 } 00771 00772 //------------------------------------------------------------------------ 00774 //------------------------------------------------------------------------ 00775 ChunkList &GetChunks() 00776 { 00777 return pChunks; 00778 } 00779 00780 //------------------------------------------------------------------------ 00782 //------------------------------------------------------------------------ 00783 const ChunkList &GetChunks() const 00784 { 00785 return pChunks; 00786 } 00787 00788 private: 00789 ChunkList pChunks; 00790 uint32_t pSize; 00791 }; 00792 00793 //---------------------------------------------------------------------------- 00794 // List of URLs 00795 //---------------------------------------------------------------------------- 00796 struct HostInfo 00797 { 00798 HostInfo(): 00799 flags(0), protocol(0), loadBalancer(false) {} 00800 HostInfo( const URL &u, bool lb = false ): 00801 flags(0), protocol(0), loadBalancer(lb), url(u) {} 00802 uint32_t flags; 00803 uint32_t protocol; 00804 bool loadBalancer; 00805 URL url; 00806 }; 00807 00808 typedef std::vector<HostInfo> HostList; 00809 00810 //---------------------------------------------------------------------------- 00812 //---------------------------------------------------------------------------- 00813 struct RedirectInfo 00814 { 00815 //-------------------------------------------------------------------------- 00817 //-------------------------------------------------------------------------- 00818 RedirectInfo( const std::string &h, 00819 uint16_t p, 00820 const std::string &c ): 00821 protocol("xroot"), host(h), port(p), path(""), cgi(c), token("") {}; 00822 std::string protocol; 00823 std::string host; 00824 uint16_t port; 00825 std::string path; 00826 std::string cgi; 00827 std::string token; 00828 }; 00829 00830 //---------------------------------------------------------------------------- 00832 //---------------------------------------------------------------------------- 00833 class ResponseHandler 00834 { 00835 public: 00836 virtual ~ResponseHandler() {} 00837 00838 //------------------------------------------------------------------------ 00846 //------------------------------------------------------------------------ 00847 virtual void HandleResponseWithHosts( XRootDStatus *status, 00848 AnyObject *response, 00849 HostList *hostList ) 00850 { 00851 delete hostList; 00852 HandleResponse( status, response ); 00853 } 00854 00855 //------------------------------------------------------------------------ 00862 //------------------------------------------------------------------------ 00863 virtual void HandleResponse( XRootDStatus *status, 00864 AnyObject *response ) {} 00865 }; 00866 } 00867 00868 #endif // __XRD_CL_XROOTD_RESPONSES_HH__