xrootd
XrdClXRootDResponses.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
3 // Author: Lukasz Janyst <ljanyst@cern.ch>
4 //------------------------------------------------------------------------------
5 // XRootD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // XRootD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
17 //------------------------------------------------------------------------------
18 
19 #ifndef __XRD_CL_XROOTD_RESPONSES_HH__
20 #define __XRD_CL_XROOTD_RESPONSES_HH__
21 
22 #include "XrdCl/XrdClBuffer.hh"
23 #include "XrdCl/XrdClStatus.hh"
24 #include "XrdCl/XrdClURL.hh"
25 #include "XrdCl/XrdClAnyObject.hh"
26 #include "XProtocol/XProtocol.hh"
27 #include <string>
28 #include <vector>
29 #include <list>
30 #include <ctime>
31 
32 namespace XrdCl
33 {
34  //----------------------------------------------------------------------------
36  //----------------------------------------------------------------------------
38  {
39  public:
40  //------------------------------------------------------------------------
42  //------------------------------------------------------------------------
44  {
49  };
50 
51  //------------------------------------------------------------------------
53  //------------------------------------------------------------------------
55  {
56  Read,
58  };
59 
60  //------------------------------------------------------------------------
62  //------------------------------------------------------------------------
63  class Location
64  {
65  public:
66 
67  //--------------------------------------------------------------------
69  //--------------------------------------------------------------------
70  Location( const std::string &address,
71  LocationType type,
72  AccessType access ):
73  pAddress( address ),
74  pType( type ),
75  pAccess( access ) {}
76 
77  //--------------------------------------------------------------------
79  //--------------------------------------------------------------------
80  const std::string &GetAddress() const
81  {
82  return pAddress;
83  }
84 
85  //--------------------------------------------------------------------
87  //--------------------------------------------------------------------
89  {
90  return pType;
91  }
92 
93  //--------------------------------------------------------------------
95  //--------------------------------------------------------------------
97  {
98  return pAccess;
99  }
100 
101  //--------------------------------------------------------------------
103  //--------------------------------------------------------------------
104  bool IsServer() const
105  {
106  return pType == ServerOnline || pType == ServerPending;
107  }
108 
109  //--------------------------------------------------------------------
111  //--------------------------------------------------------------------
112  bool IsManager() const
113  {
114  return pType == ManagerOnline || pType == ManagerPending;
115  }
116 
117  private:
118  std::string pAddress;
121  };
122 
123  //------------------------------------------------------------------------
125  //------------------------------------------------------------------------
126  typedef std::vector<Location> LocationList;
127 
128  //------------------------------------------------------------------------
130  //------------------------------------------------------------------------
131  typedef LocationList::iterator Iterator;
132 
133  //------------------------------------------------------------------------
135  //------------------------------------------------------------------------
136  typedef LocationList::const_iterator ConstIterator;
137 
138  //------------------------------------------------------------------------
140  //------------------------------------------------------------------------
141  LocationInfo();
142 
143  //------------------------------------------------------------------------
145  //------------------------------------------------------------------------
146  uint32_t GetSize() const
147  {
148  return pLocations.size();
149  }
150 
151  //------------------------------------------------------------------------
153  //------------------------------------------------------------------------
154  Location &At( uint32_t index )
155  {
156  return pLocations[index];
157  }
158 
159  //------------------------------------------------------------------------
161  //------------------------------------------------------------------------
163  {
164  return pLocations.begin();
165  }
166 
167  //------------------------------------------------------------------------
169  //------------------------------------------------------------------------
171  {
172  return pLocations.begin();
173  }
174 
175  //------------------------------------------------------------------------
177  //------------------------------------------------------------------------
179  {
180  return pLocations.end();
181  }
182 
183  //------------------------------------------------------------------------
185  //------------------------------------------------------------------------
187  {
188  return pLocations.end();
189  }
190 
191  //------------------------------------------------------------------------
193  //------------------------------------------------------------------------
194  void Add( const Location &location )
195  {
196  pLocations.push_back( location );
197  }
198 
199  //------------------------------------------------------------------------
201  //------------------------------------------------------------------------
202  bool ParseServerResponse( const char *data );
203 
204  private:
205  bool ProcessLocation( std::string &location );
207  };
208 
209  //----------------------------------------------------------------------------
211  //----------------------------------------------------------------------------
212  class XRootDStatus: public Status
213  {
214  public:
215  //------------------------------------------------------------------------
217  //------------------------------------------------------------------------
218  XRootDStatus( uint16_t st = 0,
219  uint16_t code = 0,
220  uint32_t errN = 0,
221  const std::string &message = "" ):
222  Status( st, code, errN ),
223  pMessage( message ) {}
224 
225  //------------------------------------------------------------------------
227  //------------------------------------------------------------------------
228  XRootDStatus( const Status &st,
229  const std::string &message = "" ):
230  Status( st ),
231  pMessage( message ) {}
232 
233  //------------------------------------------------------------------------
235  //------------------------------------------------------------------------
236  const std::string &GetErrorMessage() const
237  {
238  return pMessage;
239  }
240 
241  //------------------------------------------------------------------------
243  //------------------------------------------------------------------------
244  void SetErrorMessage( const std::string &message )
245  {
246  pMessage = message;
247  }
248 
249  //------------------------------------------------------------------------
251  //------------------------------------------------------------------------
252  std::string ToStr() const
253  {
254  if( code == errErrorResponse )
255  {
256  std::ostringstream o;
257  o << "[ERROR] Server responded with an error: [" << errNo << "] ";
258  o << pMessage << std::endl;
259  return o.str();
260  }
261  std::string str = ToString();
262  if( !pMessage.empty() )
263  str += ": " + pMessage;
264  return str;
265  }
266 
267  private:
268  std::string pMessage;
269  };
270 
271  //----------------------------------------------------------------------------
273  //----------------------------------------------------------------------------
275 
276  //----------------------------------------------------------------------------
278  //----------------------------------------------------------------------------
280  {
281  public:
282  //------------------------------------------------------------------------
284  //------------------------------------------------------------------------
286  {
292  };
293 
294  //------------------------------------------------------------------------
296  //------------------------------------------------------------------------
297  ProtocolInfo( uint32_t version, uint32_t hostInfo ):
298  pVersion( version ), pHostInfo( hostInfo ) {}
299 
300  //------------------------------------------------------------------------
302  //------------------------------------------------------------------------
303  uint32_t GetVersion() const
304  {
305  return pVersion;
306  }
307 
308  //------------------------------------------------------------------------
310  //------------------------------------------------------------------------
311  uint32_t GetHostInfo() const
312  {
313  return pHostInfo;
314  }
315 
316  //------------------------------------------------------------------------
318  //------------------------------------------------------------------------
319  bool TestHostInfo( uint32_t flags )
320  {
321  return pHostInfo & flags;
322  }
323 
324  private:
325  uint32_t pVersion;
326  uint32_t pHostInfo;
327  };
328 
329  //----------------------------------------------------------------------------
331  //----------------------------------------------------------------------------
332  class StatInfo
333  {
334  public:
335  //------------------------------------------------------------------------
337  //------------------------------------------------------------------------
338  enum Flags
339  {
345 
349  };
350 
351  //------------------------------------------------------------------------
353  //------------------------------------------------------------------------
354  StatInfo();
355 
356  //------------------------------------------------------------------------
358  //------------------------------------------------------------------------
359  const std::string GetId() const
360  {
361  return pId;
362  }
363 
364  //------------------------------------------------------------------------
366  //------------------------------------------------------------------------
367  uint64_t GetSize() const
368  {
369  return pSize;
370  }
371 
372  //------------------------------------------------------------------------
374  //------------------------------------------------------------------------
375  uint32_t GetFlags() const
376  {
377  return pFlags;
378  }
379 
380  //------------------------------------------------------------------------
382  //------------------------------------------------------------------------
383  bool TestFlags( uint32_t flags ) const
384  {
385  return pFlags & flags;
386  }
387 
388  //------------------------------------------------------------------------
390  //------------------------------------------------------------------------
391  uint64_t GetModTime() const
392  {
393  return pModTime;
394  }
395 
396  //------------------------------------------------------------------------
398  //------------------------------------------------------------------------
399  std::string GetModTimeAsString() const
400  {
401  char ts[256];
402  time_t modTime = pModTime;
403  tm *t = gmtime( &modTime );
404  strftime( ts, 255, "%F %T", t );
405  return ts;
406  }
407 
408  //------------------------------------------------------------------------
410  //------------------------------------------------------------------------
411  bool ParseServerResponse( const char *data );
412 
413  private:
414 
415  //------------------------------------------------------------------------
416  // Normal stat
417  //------------------------------------------------------------------------
418  std::string pId;
419  uint64_t pSize;
420  uint32_t pFlags;
421  uint64_t pModTime;
422  };
423 
424  //----------------------------------------------------------------------------
426  //----------------------------------------------------------------------------
428  {
429  public:
430  //------------------------------------------------------------------------
432  //------------------------------------------------------------------------
433  StatInfoVFS();
434 
435  //------------------------------------------------------------------------
437  //------------------------------------------------------------------------
438  uint64_t GetNodesRW() const
439  {
440  return pNodesRW;
441  }
442 
443  //------------------------------------------------------------------------
445  //------------------------------------------------------------------------
446  uint64_t GetFreeRW() const
447  {
448  return pFreeRW;
449  }
450 
451  //------------------------------------------------------------------------
453  //------------------------------------------------------------------------
454  uint8_t GetUtilizationRW() const
455  {
456  return pUtilizationRW;
457  }
458 
459  //------------------------------------------------------------------------
461  //------------------------------------------------------------------------
462  uint64_t GetNodesStaging() const
463  {
464  return pNodesStaging;
465  }
466 
467  //------------------------------------------------------------------------
469  //------------------------------------------------------------------------
470  uint64_t GetFreeStaging() const
471  {
472  return pFreeStaging;
473  }
474 
475  //------------------------------------------------------------------------
477  //------------------------------------------------------------------------
478  uint8_t GetUtilizationStaging() const
479  {
480  return pUtilizationStaging;
481  }
482 
483  //------------------------------------------------------------------------
485  //------------------------------------------------------------------------
486  bool ParseServerResponse( const char *data );
487 
488  private:
489 
490  //------------------------------------------------------------------------
491  // kXR_vfs stat
492  //------------------------------------------------------------------------
493  uint64_t pNodesRW;
494  uint64_t pFreeRW;
495  uint32_t pUtilizationRW;
496  uint64_t pNodesStaging;
497  uint64_t pFreeStaging;
499  };
500 
501  //----------------------------------------------------------------------------
503  //----------------------------------------------------------------------------
505  {
506  public:
507  //------------------------------------------------------------------------
509  //------------------------------------------------------------------------
510  class ListEntry
511  {
512  public:
513  //--------------------------------------------------------------------
515  //--------------------------------------------------------------------
516  ListEntry( const std::string &hostAddress,
517  const std::string &name,
518  StatInfo *statInfo = 0):
519  pHostAddress( hostAddress ),
520  pName( name ),
521  pStatInfo( statInfo )
522  {}
523 
524  //--------------------------------------------------------------------
526  //--------------------------------------------------------------------
528  {
529  delete pStatInfo;
530  }
531 
532  //--------------------------------------------------------------------
534  //--------------------------------------------------------------------
535  const std::string &GetHostAddress() const
536  {
537  return pHostAddress;
538  }
539 
540  //--------------------------------------------------------------------
542  //--------------------------------------------------------------------
543  const std::string &GetName() const
544  {
545  return pName;
546  }
547 
548  //--------------------------------------------------------------------
550  //--------------------------------------------------------------------
552  {
553  return pStatInfo;
554  }
555 
556  //--------------------------------------------------------------------
558  //--------------------------------------------------------------------
559  const StatInfo *GetStatInfo() const
560  {
561  return pStatInfo;
562  }
563 
564  //--------------------------------------------------------------------
566  //--------------------------------------------------------------------
567  void SetStatInfo( StatInfo *info )
568  {
569  pStatInfo = info;
570  }
571 
572  private:
573  std::string pHostAddress;
574  std::string pName;
576  };
577 
578  //------------------------------------------------------------------------
580  //------------------------------------------------------------------------
581  DirectoryList();
582 
583  //------------------------------------------------------------------------
585  //------------------------------------------------------------------------
586  ~DirectoryList();
587 
588  //------------------------------------------------------------------------
590  //------------------------------------------------------------------------
591  typedef std::vector<ListEntry*> DirList;
592 
593  //------------------------------------------------------------------------
595  //------------------------------------------------------------------------
596  typedef DirList::iterator Iterator;
597 
598  //------------------------------------------------------------------------
600  //------------------------------------------------------------------------
601  typedef DirList::const_iterator ConstIterator;
602 
603  //------------------------------------------------------------------------
605  //------------------------------------------------------------------------
606  void Add( ListEntry *entry )
607  {
608  pDirList.push_back( entry );
609  }
610 
611  //------------------------------------------------------------------------
613  //------------------------------------------------------------------------
614  ListEntry *At( uint32_t index )
615  {
616  return pDirList[index];
617  }
618 
619  //------------------------------------------------------------------------
621  //------------------------------------------------------------------------
623  {
624  return pDirList.begin();
625  }
626 
627  //------------------------------------------------------------------------
629  //------------------------------------------------------------------------
631  {
632  return pDirList.begin();
633  }
634 
635  //------------------------------------------------------------------------
637  //------------------------------------------------------------------------
639  {
640  return pDirList.end();
641  }
642 
643  //------------------------------------------------------------------------
645  //------------------------------------------------------------------------
647  {
648  return pDirList.end();
649  }
650 
651  //------------------------------------------------------------------------
653  //------------------------------------------------------------------------
654  uint32_t GetSize() const
655  {
656  return pDirList.size();
657  }
658 
659  //------------------------------------------------------------------------
661  //------------------------------------------------------------------------
662  const std::string &GetParentName() const
663  {
664  return pParent;
665  }
666 
667  //------------------------------------------------------------------------
669  //------------------------------------------------------------------------
670  void SetParentName( const std::string &parent )
671  {
672  pParent = parent;
673  if( !pParent.empty() && pParent[pParent.length()-1] != '/' )
674  pParent += "/";
675  }
676 
677  //------------------------------------------------------------------------
679  //------------------------------------------------------------------------
680  bool ParseServerResponse( const std::string &hostId,
681  const char *data );
682 
683  private:
685  std::string pParent;
686  };
687 
688  //----------------------------------------------------------------------------
690  //----------------------------------------------------------------------------
691  class OpenInfo
692  {
693  public:
694  //------------------------------------------------------------------------
696  //------------------------------------------------------------------------
697  OpenInfo( const uint8_t *fileHandle,
698  uint64_t sessionId,
699  StatInfo *statInfo = 0 ):
700  pSessionId(sessionId), pStatInfo( statInfo )
701  {
702  memcpy( pFileHandle, fileHandle, 4 );
703  }
704 
705  //------------------------------------------------------------------------
707  //------------------------------------------------------------------------
709  {
710  delete pStatInfo;
711  }
712 
713  //------------------------------------------------------------------------
715  //------------------------------------------------------------------------
716  void GetFileHandle( uint8_t *fileHandle ) const
717  {
718  memcpy( fileHandle, pFileHandle, 4 );
719  }
720 
721  //------------------------------------------------------------------------
723  //------------------------------------------------------------------------
724  const StatInfo *GetStatInfo() const
725  {
726  return pStatInfo;
727  }
728 
729  //------------------------------------------------------------------------
730  // Get session ID
731  //------------------------------------------------------------------------
732  uint64_t GetSessionId() const
733  {
734  return pSessionId;
735  }
736 
737  private:
738  uint8_t pFileHandle[4];
739  uint64_t pSessionId;
741  };
742 
743  //----------------------------------------------------------------------------
745  //----------------------------------------------------------------------------
746  struct ChunkInfo
747  {
748  //--------------------------------------------------------------------------
750  //--------------------------------------------------------------------------
751  ChunkInfo( uint64_t off = 0, uint32_t len = 0, void *buff = 0 ):
752  offset( off ), length( len ), buffer(buff) {}
753 
754  uint64_t offset;
755  uint32_t length;
756  void *buffer;
757  };
758 
759  //----------------------------------------------------------------------------
761  //----------------------------------------------------------------------------
762  typedef std::vector<ChunkInfo> ChunkList;
763 
764  //----------------------------------------------------------------------------
766  //----------------------------------------------------------------------------
768  {
769  public:
770  //------------------------------------------------------------------------
772  //------------------------------------------------------------------------
774 
775  //------------------------------------------------------------------------
777  //------------------------------------------------------------------------
778  uint32_t GetSize() const
779  {
780  return pSize;
781  }
782 
783  //------------------------------------------------------------------------
785  //------------------------------------------------------------------------
786  void SetSize( uint32_t size )
787  {
788  pSize = size;
789  }
790 
791  //------------------------------------------------------------------------
793  //------------------------------------------------------------------------
795  {
796  return pChunks;
797  }
798 
799  //------------------------------------------------------------------------
801  //------------------------------------------------------------------------
802  const ChunkList &GetChunks() const
803  {
804  return pChunks;
805  }
806 
807  private:
809  uint32_t pSize;
810  };
811 
812  //----------------------------------------------------------------------------
813  // List of URLs
814  //----------------------------------------------------------------------------
815  struct HostInfo
816  {
818  flags(0), protocol(0), loadBalancer(false) {}
819  HostInfo( const URL &u, bool lb = false ):
820  flags(0), protocol(0), loadBalancer(lb), url(u) {}
821  uint32_t flags;
822  uint32_t protocol;
825  };
826 
827  typedef std::vector<HostInfo> HostList;
828 
829  //----------------------------------------------------------------------------
831  //----------------------------------------------------------------------------
833  {
834  public:
835  virtual ~ResponseHandler() {}
836 
837  //------------------------------------------------------------------------
845  //------------------------------------------------------------------------
846  virtual void HandleResponseWithHosts( XRootDStatus *status,
847  AnyObject *response,
848  HostList *hostList )
849  {
850  delete hostList;
851  HandleResponse( status, response );
852  }
853 
854  //------------------------------------------------------------------------
861  //------------------------------------------------------------------------
862  virtual void HandleResponse( XRootDStatus *status,
863  AnyObject *response )
864  {
865  (void)status; (void)response;
866  }
867  };
868 }
869 
870 #endif // __XRD_CL_XROOTD_RESPONSES_HH__