// @(#)root/net:$Id$
// Author: G. Ganis  10/5/2007

/*************************************************************************
 * Copyright (C) 1995-2007, 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_TApplicationRemote
#define ROOT_TApplicationRemote

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TApplicationRemote                                                   //
//                                                                      //
// TApplicationRemote maps a remote session. It starts a remote session //
// and takes care of redirecting the commands to be processed to the    //
// remote session, to collect the graphic output objects and to display //
// them locally.                                                        //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_RRemoteProtocol
#include "RRemoteProtocol.h"
#endif
#ifndef ROOT_TApplication
#include "TApplication.h"
#endif
#ifndef ROOT_TMD5
#include "TMD5.h"
#endif
#ifndef ROOT_TUrl
#include "TUrl.h"
#endif
#ifndef ROOT_TNamed
#include "TNamed.h"
#endif
#ifndef ROOT_TMessage
#include "TMessage.h"
#endif
#ifndef ROOT_TSysEvtHandler
#include "TSysEvtHandler.h"
#endif


class THashList;
class TMonitor;
class TSocket;
class TBrowser;
class TRemoteObject;
class TSeqCollection;

class TApplicationRemote : public TApplication {

public:
   enum ESendFileOpt {
      kAscii            = 0x0,
      kBinary           = 0x1,
      kForce            = 0x2
   };
   // TApplication specific bits
   enum EStatusBits {
      kCollecting       = BIT(16)   // TRUE while collecting from server
   };

private:
   class TARFileStat : public TNamed {
      public:
         TARFileStat(const char *fn, TMD5 *md5, Long_t mt) :
                     TNamed(fn,fn), fMD5(*md5), fModtime(mt) { }
         TMD5   fMD5;        //file's md5
         Long_t fModtime;    //file's modification time
   };

   TString            fName;           //Unique name identifying this instance
   Int_t              fProtocol;       //server protocol version number
   TUrl               fUrl;            //server's url
   TSocket           *fSocket;         //socket connection to server
   TMonitor          *fMonitor;        //monitor for the input socket
   Bool_t             fInterrupt;      //flag interrupt state
   TSignalHandler    *fIntHandler;     //interrupt signal handler (ctrl-c)

   TString            fLogFilePath;    //Full remote path to log file
   THashList         *fFileList;       // List of files already uploaded

   TObject           *fReceivedObject; // last received object
   TSeqCollection    *fRootFiles;      // list of (remote) root files
   TRemoteObject     *fWorkingDir;     // working (remote) directory

   static Int_t       fgPortAttempts;  // number of attempts to find a port
   static Int_t       fgPortLower;     // lower bound for ports
   static Int_t       fgPortUpper;     // upper bound for ports

   Int_t         Broadcast(const TMessage &mess);
   Int_t         Broadcast(const char *mess, Int_t kind = kMESS_STRING, Int_t type = kRRT_Undef);
   Int_t         Broadcast(Int_t kind, Int_t type = kRRT_Undef) { return Broadcast(0, kind, type); }
   Int_t         BroadcastObject(const TObject *obj, Int_t kind = kMESS_OBJECT);
   Int_t         BroadcastRaw(const void *buffer, Int_t length);
   Bool_t        CheckFile(const char *file, Long_t modtime);
   Int_t         Collect(Long_t timeout = -1);
   Int_t         CollectInput();

   void          RecvLogFile(Int_t size);

public:
   TApplicationRemote(const char *url, Int_t debug = 0, const char *script = 0);
   virtual ~TApplicationRemote();

   virtual void  Browse(TBrowser *b);
   Bool_t        IsFolder() const { return kTRUE; }
   const char   *ApplicationName() const { return fName; }
   Long_t        ProcessLine(const char *line, Bool_t /*sync*/ = kFALSE, Int_t *error = 0);

   Int_t         SendFile(const char *file, Int_t opt = kAscii,
                          const char *rfile = 0);
   Int_t         SendObject(const TObject *obj);

   void          Interrupt(Int_t type = kRRI_Hard);
   Bool_t        IsValid() const { return (fSocket) ? kTRUE : kFALSE; }

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

   void          Terminate(Int_t status = 0);

   static void   SetPortParam(Int_t lower = -1, Int_t upper = -1, Int_t attempts = -1);

   ClassDef(TApplicationRemote,0)  //Remote Application Interface
};

//
// TApplicationRemote Interrupt signal handler
//
class TARInterruptHandler : public TSignalHandler {
private:
   TApplicationRemote *fApplicationRemote;
public:
   TARInterruptHandler(TApplicationRemote *r)
      : TSignalHandler(kSigInterrupt, kFALSE), fApplicationRemote(r) { }
   Bool_t Notify();
};

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