// @(#)root/net:$Id$
// Author: Fons Rademakers   14/08/97

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TNetFile
#define ROOT_TNetFile


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TNetFile                                                             //
//                                                                      //
// A TNetFile is like a normal TFile except that it reads and writes    //
// its data via a rootd server.                                         //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TFile
#include "TFile.h"
#endif
#ifndef ROOT_TUrl
#include "TUrl.h"
#endif
#ifndef ROOT_TFTP
#include "TFTP.h"
#endif
#ifndef ROOT_TSystem
#include "TSystem.h"
#endif
#ifndef ROOT_MessageTypes
#include "MessageTypes.h"
#endif

class TSocket;


class TNetFile : public TFile {

protected:
   TUrl      fEndpointUrl; //URL of realfile (after possible redirection)
   TString   fUser;        //remote user name
   TSocket  *fSocket;      //connection to rootd server
   Int_t     fProtocol;    //rootd protocol level
   Int_t     fErrorCode;   //error code returned by rootd (matching gRootdErrStr)
   Int_t     fNetopt;      //initial network options (used for ReOpen())

   TNetFile(const TNetFile&);             // NetFile cannot be copied
   TNetFile& operator=(const TNetFile&);  // NetFile cannot be copied

   TNetFile(const char *url, const char *ftitle, Int_t comp, Bool_t);
   virtual void ConnectServer(Int_t *stat, EMessageTypes *kind, Int_t netopt,
                              Int_t tcpwindowsize, Bool_t forceOpen,
                              Bool_t forceRead);
   virtual void Create(const char *url, Option_t *option, Int_t netopt);
   virtual void Create(TSocket *s, Option_t *option, Int_t netopt);
   void         Init(Bool_t create);
   void         Print(Option_t *option) const;
   void         PrintError(const char *where, Int_t err);
   Int_t        Recv(Int_t &status, EMessageTypes &kind);
   Int_t        SysOpen(const char *pathname, Int_t flags, UInt_t mode);
   Int_t        SysClose(Int_t fd);
   Int_t        SysStat(Int_t fd, Long_t *id, Long64_t *size, Long_t *flags, Long_t *modtime);

public:
   TNetFile(const char *url, Option_t *option = "", const char *ftitle = "",
            Int_t compress = 1, Int_t netopt = 0);
   TNetFile() : fEndpointUrl(), fUser(), fSocket(0), fProtocol(0), fErrorCode(0), fNetopt(0) { }
   virtual ~TNetFile();

   void    Close(Option_t *option="");  // *MENU*
   void    Flush();
   Int_t   GetErrorCode() const { return fErrorCode; }
   Bool_t  IsOpen() const;
   Bool_t  Matches(const char *url);
   Int_t   ReOpen(Option_t *mode);
   Bool_t  ReadBuffer(char *buf, Int_t len);
   Bool_t  ReadBuffer(char *buf, Long64_t pos, Int_t len);
   Bool_t  ReadBuffers(char *buf,  Long64_t *pos, Int_t *len, Int_t nbuf);
   Bool_t  WriteBuffer(const char *buf, Int_t len);
   void    Seek(Long64_t offset, ERelativeTo pos = kBeg);

   const TUrl *GetEndpointUrl() const { return &fEndpointUrl; }

   ClassDef(TNetFile,1)  //A ROOT file that reads/writes via a rootd server
};


class TNetSystem : public TSystem {

private:
   Bool_t      fDir;         // true if a directory is open remotely
   void       *fDirp;        // directory handler
   TFTP       *fFTP;         // Connection to rootd
   TString     fHost;        // Remote host
   Bool_t      fFTPOwner;    // True if owner of the FTP instance
   TString     fUser;        // Remote user
   Int_t       fPort;        // Remote port

   TNetSystem(const TNetSystem&);             // not implemented
   TNetSystem& operator=(const TNetSystem&);  // not implemented

   void       *GetDirPtr() const { return fDirp; }

protected:
   Bool_t      fIsLocal;     // TRUE if the path points to this host
   TString     fLocalPrefix; // if fIsLocal, prefix to be prepend locally

   void        Create(const char *url, TSocket *sock = 0);
   void        InitRemoteEntity(const char *url);

public:
   TNetSystem(Bool_t ftpowner = kTRUE);
   TNetSystem(const char *url, Bool_t ftpowner = kTRUE);
   virtual ~TNetSystem();

   Bool_t      ConsistentWith(const char *path, void *dirptr);
   Int_t       MakeDirectory(const char *name);
   void       *OpenDirectory(const char *name);
   void        FreeDirectory(void *dirp = 0);
   const char *GetDirEntry(void *dirp = 0);
   Int_t       GetPathInfo(const char *path, FileStat_t &buf);
   Bool_t      AccessPathName(const char *path, EAccessMode mode);
   int         Unlink(const char *path);

   ClassDef(TNetSystem,0)  // Directory handler for NetSystem
};

#endif
 TNetFile.h:1
 TNetFile.h:2
 TNetFile.h:3
 TNetFile.h:4
 TNetFile.h:5
 TNetFile.h:6
 TNetFile.h:7
 TNetFile.h:8
 TNetFile.h:9
 TNetFile.h:10
 TNetFile.h:11
 TNetFile.h:12
 TNetFile.h:13
 TNetFile.h:14
 TNetFile.h:15
 TNetFile.h:16
 TNetFile.h:17
 TNetFile.h:18
 TNetFile.h:19
 TNetFile.h:20
 TNetFile.h:21
 TNetFile.h:22
 TNetFile.h:23
 TNetFile.h:24
 TNetFile.h:25
 TNetFile.h:26
 TNetFile.h:27
 TNetFile.h:28
 TNetFile.h:29
 TNetFile.h:30
 TNetFile.h:31
 TNetFile.h:32
 TNetFile.h:33
 TNetFile.h:34
 TNetFile.h:35
 TNetFile.h:36
 TNetFile.h:37
 TNetFile.h:38
 TNetFile.h:39
 TNetFile.h:40
 TNetFile.h:41
 TNetFile.h:42
 TNetFile.h:43
 TNetFile.h:44
 TNetFile.h:45
 TNetFile.h:46
 TNetFile.h:47
 TNetFile.h:48
 TNetFile.h:49
 TNetFile.h:50
 TNetFile.h:51
 TNetFile.h:52
 TNetFile.h:53
 TNetFile.h:54
 TNetFile.h:55
 TNetFile.h:56
 TNetFile.h:57
 TNetFile.h:58
 TNetFile.h:59
 TNetFile.h:60
 TNetFile.h:61
 TNetFile.h:62
 TNetFile.h:63
 TNetFile.h:64
 TNetFile.h:65
 TNetFile.h:66
 TNetFile.h:67
 TNetFile.h:68
 TNetFile.h:69
 TNetFile.h:70
 TNetFile.h:71
 TNetFile.h:72
 TNetFile.h:73
 TNetFile.h:74
 TNetFile.h:75
 TNetFile.h:76
 TNetFile.h:77
 TNetFile.h:78
 TNetFile.h:79
 TNetFile.h:80
 TNetFile.h:81
 TNetFile.h:82
 TNetFile.h:83
 TNetFile.h:84
 TNetFile.h:85
 TNetFile.h:86
 TNetFile.h:87
 TNetFile.h:88
 TNetFile.h:89
 TNetFile.h:90
 TNetFile.h:91
 TNetFile.h:92
 TNetFile.h:93
 TNetFile.h:94
 TNetFile.h:95
 TNetFile.h:96
 TNetFile.h:97
 TNetFile.h:98
 TNetFile.h:99
 TNetFile.h:100
 TNetFile.h:101
 TNetFile.h:102
 TNetFile.h:103
 TNetFile.h:104
 TNetFile.h:105
 TNetFile.h:106
 TNetFile.h:107
 TNetFile.h:108
 TNetFile.h:109
 TNetFile.h:110
 TNetFile.h:111
 TNetFile.h:112
 TNetFile.h:113
 TNetFile.h:114
 TNetFile.h:115
 TNetFile.h:116
 TNetFile.h:117
 TNetFile.h:118
 TNetFile.h:119
 TNetFile.h:120
 TNetFile.h:121
 TNetFile.h:122
 TNetFile.h:123
 TNetFile.h:124
 TNetFile.h:125
 TNetFile.h:126
 TNetFile.h:127
 TNetFile.h:128
 TNetFile.h:129
 TNetFile.h:130
 TNetFile.h:131
 TNetFile.h:132
 TNetFile.h:133
 TNetFile.h:134
 TNetFile.h:135