// @(#)root/proof:$Id$
// Author: G. Ganis, Nov 2005

/*************************************************************************
 * Copyright (C) 1995-2005, 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_TProofMgr
#define ROOT_TProofMgr


//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TProofMgr                                                            //
//                                                                      //
// The PROOF manager interacts with the PROOF server coordinator to     //
// create or destroy a PROOF session, attach to or detach from          //
// existing one, and to monitor any client activity on the cluster.     //
// At most one manager instance per server is allowed.                  //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TList
#include "TList.h"
#endif
#ifndef ROOT_TNamed
#include "TNamed.h"
#endif
#ifndef ROOT_TUrl
#include "TUrl.h"
#endif
#ifndef ROOT_TString
#include "TString.h"
#endif
#ifndef ROOT_TSystem
#include "TSystem.h"
#endif

class TFileCollection;
class TObjString;
class TProof;
class TProofDesc;
class TProofLog;
class TProofMgr;
class TSignalHandler;

typedef TProofMgr *(*TProofMgr_t)(const char *, Int_t, const char *);

class TProofMgr : public TNamed {

public:
   enum EServType { kProofd = 0, kXProofd = 1, kProofLite = 2 };

private:
   TProofMgr(const TProofMgr&); // Not implemented
   TProofMgr& operator=(const TProofMgr&); // Not implemented

   static TProofMgr_t fgTXProofMgrHook; // Constructor hooks for TXProofMgr
   static TProofMgr_t GetXProofMgrHook();

   static void ReplaceSubdirs(const char *fn, TString &fdst, TList &dirph);

protected:
   TString        fMssUrl;         // URL for the remote data pool, if any
   Int_t          fRemoteProtocol; // Protocol number run by the daemon server
   EServType      fServType;       // Type of server: old-proofd, XrdProofd
   TList         *fSessions;       // PROOF session managed by this server
   TUrl           fUrl;            // Server URL

   TSignalHandler *fIntHandler;    // Interrupt signal handler (ctrl-c)

   static TList   fgListOfManagers; // Sub-list of TROOT::ListOfProofs for managers

   TProofMgr() : fRemoteProtocol(-1),
                        fServType(kXProofd), fSessions(0), fUrl(), fIntHandler(0) { }

public:
   TProofMgr(const char *url, Int_t loglevel = -1, const char *alias = "");
   virtual ~TProofMgr();

   virtual Bool_t      IsLite() const { return (fServType == kProofLite); }
   virtual Bool_t      IsProofd() const { return (fServType == kProofd); }
   virtual Bool_t      IsValid() const { return kTRUE; }
   virtual void        SetInvalid() { }
           void        Close() { SetInvalid(); }

   virtual TProof     *AttachSession(Int_t, Bool_t = kFALSE);
   virtual TProof     *AttachSession(TProofDesc *, Bool_t = kFALSE);
   virtual TProof     *CreateSession(const char * = 0, const char * = 0, Int_t = -1);
   virtual void        DetachSession(Int_t, Option_t * = "");
   virtual void        DetachSession(TProof *, Option_t * = "");
   virtual void        DiscardSession(TProof *p);
   virtual const char *GetMssUrl(Bool_t = kFALSE) { return fMssUrl.Data(); }
   virtual TProofDesc *GetProofDesc(Int_t id);
   virtual TProofDesc *GetProofDesc(TProof *p);
   virtual Int_t       GetRemoteProtocol() const { return fRemoteProtocol; }
   virtual TProofLog  *GetSessionLogs(Int_t = 0, const char * = 0,
                                      const char * = "-v \"| SvcMsg\"", Bool_t = kFALSE)
                                      { return (TProofLog *)0; }
   virtual const char *GetUrl() { return fUrl.GetUrl(); }
   virtual Bool_t      MatchUrl(const char *url);
   virtual TList      *QuerySessions(Option_t *opt = "S");
   virtual TObjString *ReadBuffer(const char *, Long64_t, Int_t)
                                        { return (TObjString *)0; }
   virtual TObjString *ReadBuffer(const char *, const char *)
                                        { return (TObjString *)0; }
   virtual Int_t       Reset(Bool_t hard = kFALSE, const char *usr = 0);
   virtual void        ShowWorkers();
   virtual Int_t       SendMsgToUsers(const char *, const char * = 0);
   virtual void        SetAlias(const char *alias="") { TNamed::SetTitle(alias); }
   virtual void        SetMssUrl(const char *mss) { fMssUrl = mss; }
   virtual Int_t       SetROOTVersion(const char *) { return 0; }
   virtual void        ShowROOTVersions() { }
   virtual void        ShutdownSession(Int_t id) { DetachSession(id,"S"); }
   virtual void        ShutdownSession(TProof *p) { DetachSession(p,"S"); }

   // Remote file system actions
   virtual Int_t       Cp(const char *, const char * = 0, const char * = 0) { return -1; }
   virtual void        Find(const char * = "~/", const char * = 0, const char * = 0) { }
   virtual void        Grep(const char *, const char * = 0, const char * = 0) { }
   virtual void        Ls(const char * = "~/", const char * = 0, const char * = 0) { }
   virtual void        More(const char *, const char * = 0, const char * = 0) { }
   virtual Int_t       Rm(const char *, const char * = 0, const char * = 0);
   virtual void        Tail(const char *, const char * = 0, const char * = 0) { }
   virtual Int_t       Md5sum(const char *, TString &, const char * = 0) { return -1; }
   virtual Int_t       Stat(const char *, FileStat_t &, const char * = 0) { return -1; }

   virtual Int_t       GetFile(const char *, const char *, const char * = 0) { return -1; }
   virtual Int_t       PutFile(const char *, const char *, const char * = 0) { return -1; }

   static TList       *GetListOfManagers();

   static void         SetTXProofMgrHook(TProofMgr_t pmh);

   static TProofMgr   *Create(const char *url, Int_t loglevel = -1,
                              const char *alias = 0, Bool_t xpd = kTRUE);
   static Int_t        Ping(const char *url, Bool_t checkxrd = kFALSE);

   static TFileCollection *UploadFiles(TList *src, const char *mss, const char *dest = 0); 
   static TFileCollection *UploadFiles(const char *txtfile, const char *mss, const char *dest = 0); 

   ClassDef(TProofMgr,0)  // Abstract PROOF manager interface
};

//
// Metaclass describing the essentials of a PROOF session
//
class TProofDesc : public TNamed {
public:
   enum EStatus { kUnknown = -1, kIdle = 0, kRunning =1, kShutdown = 2};

private:
   TProofDesc(const TProofDesc&); // Not implemented
   TProofDesc& operator=(const TProofDesc&); // Not implemented

   Int_t          fLocalId;  // ID in the local list
   Int_t          fStatus;   // Session status (see EStatus)
   TProof        *fProof;    // Related instance of TProof
   Int_t          fRemoteId; // Remote ID assigned by the coordinator to the proofserv
   TString        fUrl;      // Url of the connection

public:
   TProofDesc(const char *tag = 0, const char *alias = 0, const char *url = 0,
                     Int_t id = -1, Int_t remid = -1, Int_t status = kIdle, TProof *p = 0)
                    : TNamed(tag, alias),
     fLocalId(id), fStatus(0), fProof(p), fRemoteId(remid), fUrl(url) { SetStatus(status); }
   virtual ~TProofDesc() { }

   Int_t          GetLocalId() const { return fLocalId; }
   TProof        *GetProof() const { return fProof; }
   Int_t          GetRemoteId() const { return fRemoteId; }
   Int_t          GetStatus() const { return fStatus; }
   const char    *GetUrl() const { return fUrl; }

   Bool_t         IsIdle() const { return (fStatus == kIdle) ? kTRUE : kFALSE; }
   Bool_t         IsRunning() const { return (fStatus == kRunning) ? kTRUE : kFALSE; }
   Bool_t         IsShuttingDown() const { return (fStatus == kShutdown) ? kTRUE : kFALSE; }

   Bool_t         MatchId(Int_t id) const { return (fLocalId == id); }

   void           Print(Option_t *opt = "") const;

   void           SetStatus(Int_t st) { fStatus = (st < kIdle || st > kShutdown) ? -1 : st; }

   void           SetProof(TProof *p) { fProof = p; }
   void           SetRemoteId(Int_t id) { fRemoteId = id; }

   ClassDef(TProofDesc,1)  // Small class describing a proof session
};

#endif
 TProofMgr.h:1
 TProofMgr.h:2
 TProofMgr.h:3
 TProofMgr.h:4
 TProofMgr.h:5
 TProofMgr.h:6
 TProofMgr.h:7
 TProofMgr.h:8
 TProofMgr.h:9
 TProofMgr.h:10
 TProofMgr.h:11
 TProofMgr.h:12
 TProofMgr.h:13
 TProofMgr.h:14
 TProofMgr.h:15
 TProofMgr.h:16
 TProofMgr.h:17
 TProofMgr.h:18
 TProofMgr.h:19
 TProofMgr.h:20
 TProofMgr.h:21
 TProofMgr.h:22
 TProofMgr.h:23
 TProofMgr.h:24
 TProofMgr.h:25
 TProofMgr.h:26
 TProofMgr.h:27
 TProofMgr.h:28
 TProofMgr.h:29
 TProofMgr.h:30
 TProofMgr.h:31
 TProofMgr.h:32
 TProofMgr.h:33
 TProofMgr.h:34
 TProofMgr.h:35
 TProofMgr.h:36
 TProofMgr.h:37
 TProofMgr.h:38
 TProofMgr.h:39
 TProofMgr.h:40
 TProofMgr.h:41
 TProofMgr.h:42
 TProofMgr.h:43
 TProofMgr.h:44
 TProofMgr.h:45
 TProofMgr.h:46
 TProofMgr.h:47
 TProofMgr.h:48
 TProofMgr.h:49
 TProofMgr.h:50
 TProofMgr.h:51
 TProofMgr.h:52
 TProofMgr.h:53
 TProofMgr.h:54
 TProofMgr.h:55
 TProofMgr.h:56
 TProofMgr.h:57
 TProofMgr.h:58
 TProofMgr.h:59
 TProofMgr.h:60
 TProofMgr.h:61
 TProofMgr.h:62
 TProofMgr.h:63
 TProofMgr.h:64
 TProofMgr.h:65
 TProofMgr.h:66
 TProofMgr.h:67
 TProofMgr.h:68
 TProofMgr.h:69
 TProofMgr.h:70
 TProofMgr.h:71
 TProofMgr.h:72
 TProofMgr.h:73
 TProofMgr.h:74
 TProofMgr.h:75
 TProofMgr.h:76
 TProofMgr.h:77
 TProofMgr.h:78
 TProofMgr.h:79
 TProofMgr.h:80
 TProofMgr.h:81
 TProofMgr.h:82
 TProofMgr.h:83
 TProofMgr.h:84
 TProofMgr.h:85
 TProofMgr.h:86
 TProofMgr.h:87
 TProofMgr.h:88
 TProofMgr.h:89
 TProofMgr.h:90
 TProofMgr.h:91
 TProofMgr.h:92
 TProofMgr.h:93
 TProofMgr.h:94
 TProofMgr.h:95
 TProofMgr.h:96
 TProofMgr.h:97
 TProofMgr.h:98
 TProofMgr.h:99
 TProofMgr.h:100
 TProofMgr.h:101
 TProofMgr.h:102
 TProofMgr.h:103
 TProofMgr.h:104
 TProofMgr.h:105
 TProofMgr.h:106
 TProofMgr.h:107
 TProofMgr.h:108
 TProofMgr.h:109
 TProofMgr.h:110
 TProofMgr.h:111
 TProofMgr.h:112
 TProofMgr.h:113
 TProofMgr.h:114
 TProofMgr.h:115
 TProofMgr.h:116
 TProofMgr.h:117
 TProofMgr.h:118
 TProofMgr.h:119
 TProofMgr.h:120
 TProofMgr.h:121
 TProofMgr.h:122
 TProofMgr.h:123
 TProofMgr.h:124
 TProofMgr.h:125
 TProofMgr.h:126
 TProofMgr.h:127
 TProofMgr.h:128
 TProofMgr.h:129
 TProofMgr.h:130
 TProofMgr.h:131
 TProofMgr.h:132
 TProofMgr.h:133
 TProofMgr.h:134
 TProofMgr.h:135
 TProofMgr.h:136
 TProofMgr.h:137
 TProofMgr.h:138
 TProofMgr.h:139
 TProofMgr.h:140
 TProofMgr.h:141
 TProofMgr.h:142
 TProofMgr.h:143
 TProofMgr.h:144
 TProofMgr.h:145
 TProofMgr.h:146
 TProofMgr.h:147
 TProofMgr.h:148
 TProofMgr.h:149
 TProofMgr.h:150
 TProofMgr.h:151
 TProofMgr.h:152
 TProofMgr.h:153
 TProofMgr.h:154
 TProofMgr.h:155
 TProofMgr.h:156
 TProofMgr.h:157
 TProofMgr.h:158
 TProofMgr.h:159
 TProofMgr.h:160
 TProofMgr.h:161
 TProofMgr.h:162
 TProofMgr.h:163
 TProofMgr.h:164
 TProofMgr.h:165
 TProofMgr.h:166
 TProofMgr.h:167
 TProofMgr.h:168
 TProofMgr.h:169
 TProofMgr.h:170
 TProofMgr.h:171
 TProofMgr.h:172
 TProofMgr.h:173
 TProofMgr.h:174
 TProofMgr.h:175
 TProofMgr.h:176
 TProofMgr.h:177
 TProofMgr.h:178
 TProofMgr.h:179
 TProofMgr.h:180
 TProofMgr.h:181
 TProofMgr.h:182
 TProofMgr.h:183
 TProofMgr.h:184
 TProofMgr.h:185
 TProofMgr.h:186
 TProofMgr.h:187
 TProofMgr.h:188
 TProofMgr.h:189
 TProofMgr.h:190
 TProofMgr.h:191
 TProofMgr.h:192
 TProofMgr.h:193
 TProofMgr.h:194
 TProofMgr.h:195