ROOT logo
// @(#)root/base:$Id: TRemoteObject.cxx 20877 2007-11-19 11:17:07Z rdm $
// Author: Bertrand Bellenot   19/06/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.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TRemoteObject                                                        //
//                                                                      //
// The TRemoteObject class provides protocol for browsing ROOT objects  //
// from a remote ROOT session.                                          //
// It contains informations on the real remote object as:               //
//  - Object Properties (i.e. file stat if the object is a TSystemFile) //
//  - Object Name                                                       //
//  - Class Name                                                        //
//  - TKey Object Name (if the remote object is a TKey)                 //
//  - TKey Class Name (if the remote object is a TKey)                  //
//  - Remote object address                                             //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include "TApplication.h"
#include "TROOT.h"
#include "TRemoteObject.h"
#include "TSystem.h"
#include "TBrowser.h"
#include "TOrdCollection.h"
#include "TList.h"
#include "TClass.h"

ClassImp(TRemoteObject);

//______________________________________________________________________________
TRemoteObject::TRemoteObject()
{
   // Create a remote object.
}

//______________________________________________________________________________
TRemoteObject::TRemoteObject(const char *name, const char *title,
                             const char *classname) : TNamed(name, title)
{
   // Create a remote object.

   fIsFolder = kFALSE;
   fClassName = classname;
   if ((fClassName == "TSystemDirectory") ||
       (fClassName == "TFile"))
      fIsFolder = kTRUE;
   if (!strcmp(classname, "TSystemDirectory") ||
       !strcmp(classname, "TSystemFile")) {
      gSystem->GetPathInfo(name, fFileStat);
   }
   Long_t raddr = (Long_t) this;
   fRemoteAddress = raddr;
}

//______________________________________________________________________________
TRemoteObject::~TRemoteObject()
{
   // Delete remote object.

}

//______________________________________________________________________________
void TRemoteObject::Browse(TBrowser *b)
{
   // Browse remote object.

   TList *ret;
   TRemoteObject *robj;
   const char *file;

   if (fClassName == "TSystemFile") {
      if (b)
         b->ExecuteDefaultAction(this);
      return;
   }
   if (fClassName == "TKey") {
      if (b->GetRefreshFlag())
         b->SetRefreshFlag(kFALSE);
      gApplication->SetBit(TApplication::kProcessRemotely);
      TObject *obj = (TObject *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseKey(\"%s\");", GetName()));
      if (b && obj) {
         if (obj->IsA()->GetMethodWithPrototype("SetDirectory", "TDirectory*"))
            gROOT->ProcessLine(Form("((%s *)0x%lx)->SetDirectory(0);", obj->ClassName(), obj));
         obj->Browse(b);
         b->SetRefreshFlag(kTRUE);
      }
   }
   if (fClassName == "TSystemDirectory") {
      if (b->GetRefreshFlag())
         b->SetRefreshFlag(kFALSE);
      gApplication->SetBit(TApplication::kProcessRemotely);
      ret = (TList *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseDirectory(\"%s\");", GetTitle()));
      if (ret) {
         TIter next(ret);
         while ((robj = (TRemoteObject *)next())) {
            file = robj->GetName();
            if (b->TestBit(TBrowser::kNoHidden) && file[0] == '.' && file[1] != '.' )
               continue;
            b->Add(robj, robj->GetName());
         }
      }
      return;
   }
   if (fClassName == "TFile") {
      if (b->GetRefreshFlag())
         b->SetRefreshFlag(kFALSE);
      gApplication->SetBit(TApplication::kProcessRemotely);
      ret = (TList *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseFile(\"%s\");", GetName()));
      if (ret) {
         TIter next(ret);
         while ((robj = (TRemoteObject *)next())) {
            file = robj->GetName();
            b->Add(robj, robj->GetName());
         }
      }
      return;
   }
}

//______________________________________________________________________________
TList *TRemoteObject::Browse()
{
   // Browse OS system directories.

   // Collections to keep track of all browser objects that have been
   // generated. It's main goal is to prevent the contineous
   // allocations of new objects with the same names during browsing.
   TList *objects  = new TList;

   static Int_t level = 0;
   const char *name = GetTitle();
   TRemoteObject *sdir;

   if (GetName()[0] == '.' && GetName()[1] == '.')
      SetName(gSystem->BaseName(name));

   TSystemDirectory dir(name, name);
   TList *files = dir.GetListOfFiles();
   if (files) {
      files->Sort();
      TIter next(files);
      TSystemFile *file;
      TString fname;
      // directories first 
      while ((file=(TSystemFile*)next())) {
         fname = file->GetName();
         if (file->IsDirectory()) {
            level++;
            TString sdirpath;
            if (!strcmp(fname.Data(), "."))
               sdirpath = name;
            else if (!strcmp(fname.Data(), ".."))
               sdirpath = gSystem->DirName(name);
            else {
               sdirpath =  name;
               if (!sdirpath.EndsWith("/"))
                  sdirpath += "/";
               sdirpath += fname.Data();
            }
            sdir = new TRemoteObject(fname.Data(), sdirpath.Data(), "TSystemDirectory");
            objects->Add(sdir);
            level--;
         }
      }
      // then files... 
      TIter nextf(files);
      while ((file=(TSystemFile*)nextf())) {
         fname = file->GetName();
         if (!file->IsDirectory()) {
            sdir = new TRemoteObject(fname.Data(), gSystem->WorkingDirectory(), "TSystemFile");
            objects->Add(sdir);
         }
      }
      delete files;
   }
   return objects;
}

//______________________________________________________________________________
Bool_t TRemoteObject::GetFileStat(FileStat_t *buf)
{
   // Get remote file status.

   buf->fDev    = fFileStat.fDev;
   buf->fIno    = fFileStat.fIno;
   buf->fMode   = fFileStat.fMode;
   buf->fUid    = fFileStat.fUid;
   buf->fGid    = fFileStat.fGid;
   buf->fSize   = fFileStat.fSize;
   buf->fMtime  = fFileStat.fMtime;
   buf->fIsLink = fFileStat.fIsLink;
   return kTRUE;
}

//______________________________________________________________________________
void TRemoteObject::Streamer(TBuffer &b)
{
   // Remote object streamer.

   if (b.IsReading()) {
      b >> fFileStat.fDev;
      b >> fFileStat.fIno;
      b >> fFileStat.fMode;
      b >> fFileStat.fUid;
      b >> fFileStat.fGid;
      b >> fFileStat.fSize;
      b >> fFileStat.fMtime;
      b >> fFileStat.fIsLink;
      b >> fIsFolder;
      b >> fRemoteAddress;
      b >> fClassName;
      b >> fKeyObjectName;
      b >> fKeyClassName;
   }
   else {
      b << fFileStat.fDev;
      b << fFileStat.fIno;
      b << fFileStat.fMode;
      b << fFileStat.fUid;
      b << fFileStat.fGid;
      b << fFileStat.fSize;
      b << fFileStat.fMtime;
      b << fFileStat.fIsLink;
      b << fIsFolder;
      b << fRemoteAddress;
      b << fClassName;
      b << fKeyObjectName;
      b << fKeyClassName;
   }
   TNamed::Streamer(b);
}

 TRemoteObject.cxx:1
 TRemoteObject.cxx:2
 TRemoteObject.cxx:3
 TRemoteObject.cxx:4
 TRemoteObject.cxx:5
 TRemoteObject.cxx:6
 TRemoteObject.cxx:7
 TRemoteObject.cxx:8
 TRemoteObject.cxx:9
 TRemoteObject.cxx:10
 TRemoteObject.cxx:11
 TRemoteObject.cxx:12
 TRemoteObject.cxx:13
 TRemoteObject.cxx:14
 TRemoteObject.cxx:15
 TRemoteObject.cxx:16
 TRemoteObject.cxx:17
 TRemoteObject.cxx:18
 TRemoteObject.cxx:19
 TRemoteObject.cxx:20
 TRemoteObject.cxx:21
 TRemoteObject.cxx:22
 TRemoteObject.cxx:23
 TRemoteObject.cxx:24
 TRemoteObject.cxx:25
 TRemoteObject.cxx:26
 TRemoteObject.cxx:27
 TRemoteObject.cxx:28
 TRemoteObject.cxx:29
 TRemoteObject.cxx:30
 TRemoteObject.cxx:31
 TRemoteObject.cxx:32
 TRemoteObject.cxx:33
 TRemoteObject.cxx:34
 TRemoteObject.cxx:35
 TRemoteObject.cxx:36
 TRemoteObject.cxx:37
 TRemoteObject.cxx:38
 TRemoteObject.cxx:39
 TRemoteObject.cxx:40
 TRemoteObject.cxx:41
 TRemoteObject.cxx:42
 TRemoteObject.cxx:43
 TRemoteObject.cxx:44
 TRemoteObject.cxx:45
 TRemoteObject.cxx:46
 TRemoteObject.cxx:47
 TRemoteObject.cxx:48
 TRemoteObject.cxx:49
 TRemoteObject.cxx:50
 TRemoteObject.cxx:51
 TRemoteObject.cxx:52
 TRemoteObject.cxx:53
 TRemoteObject.cxx:54
 TRemoteObject.cxx:55
 TRemoteObject.cxx:56
 TRemoteObject.cxx:57
 TRemoteObject.cxx:58
 TRemoteObject.cxx:59
 TRemoteObject.cxx:60
 TRemoteObject.cxx:61
 TRemoteObject.cxx:62
 TRemoteObject.cxx:63
 TRemoteObject.cxx:64
 TRemoteObject.cxx:65
 TRemoteObject.cxx:66
 TRemoteObject.cxx:67
 TRemoteObject.cxx:68
 TRemoteObject.cxx:69
 TRemoteObject.cxx:70
 TRemoteObject.cxx:71
 TRemoteObject.cxx:72
 TRemoteObject.cxx:73
 TRemoteObject.cxx:74
 TRemoteObject.cxx:75
 TRemoteObject.cxx:76
 TRemoteObject.cxx:77
 TRemoteObject.cxx:78
 TRemoteObject.cxx:79
 TRemoteObject.cxx:80
 TRemoteObject.cxx:81
 TRemoteObject.cxx:82
 TRemoteObject.cxx:83
 TRemoteObject.cxx:84
 TRemoteObject.cxx:85
 TRemoteObject.cxx:86
 TRemoteObject.cxx:87
 TRemoteObject.cxx:88
 TRemoteObject.cxx:89
 TRemoteObject.cxx:90
 TRemoteObject.cxx:91
 TRemoteObject.cxx:92
 TRemoteObject.cxx:93
 TRemoteObject.cxx:94
 TRemoteObject.cxx:95
 TRemoteObject.cxx:96
 TRemoteObject.cxx:97
 TRemoteObject.cxx:98
 TRemoteObject.cxx:99
 TRemoteObject.cxx:100
 TRemoteObject.cxx:101
 TRemoteObject.cxx:102
 TRemoteObject.cxx:103
 TRemoteObject.cxx:104
 TRemoteObject.cxx:105
 TRemoteObject.cxx:106
 TRemoteObject.cxx:107
 TRemoteObject.cxx:108
 TRemoteObject.cxx:109
 TRemoteObject.cxx:110
 TRemoteObject.cxx:111
 TRemoteObject.cxx:112
 TRemoteObject.cxx:113
 TRemoteObject.cxx:114
 TRemoteObject.cxx:115
 TRemoteObject.cxx:116
 TRemoteObject.cxx:117
 TRemoteObject.cxx:118
 TRemoteObject.cxx:119
 TRemoteObject.cxx:120
 TRemoteObject.cxx:121
 TRemoteObject.cxx:122
 TRemoteObject.cxx:123
 TRemoteObject.cxx:124
 TRemoteObject.cxx:125
 TRemoteObject.cxx:126
 TRemoteObject.cxx:127
 TRemoteObject.cxx:128
 TRemoteObject.cxx:129
 TRemoteObject.cxx:130
 TRemoteObject.cxx:131
 TRemoteObject.cxx:132
 TRemoteObject.cxx:133
 TRemoteObject.cxx:134
 TRemoteObject.cxx:135
 TRemoteObject.cxx:136
 TRemoteObject.cxx:137
 TRemoteObject.cxx:138
 TRemoteObject.cxx:139
 TRemoteObject.cxx:140
 TRemoteObject.cxx:141
 TRemoteObject.cxx:142
 TRemoteObject.cxx:143
 TRemoteObject.cxx:144
 TRemoteObject.cxx:145
 TRemoteObject.cxx:146
 TRemoteObject.cxx:147
 TRemoteObject.cxx:148
 TRemoteObject.cxx:149
 TRemoteObject.cxx:150
 TRemoteObject.cxx:151
 TRemoteObject.cxx:152
 TRemoteObject.cxx:153
 TRemoteObject.cxx:154
 TRemoteObject.cxx:155
 TRemoteObject.cxx:156
 TRemoteObject.cxx:157
 TRemoteObject.cxx:158
 TRemoteObject.cxx:159
 TRemoteObject.cxx:160
 TRemoteObject.cxx:161
 TRemoteObject.cxx:162
 TRemoteObject.cxx:163
 TRemoteObject.cxx:164
 TRemoteObject.cxx:165
 TRemoteObject.cxx:166
 TRemoteObject.cxx:167
 TRemoteObject.cxx:168
 TRemoteObject.cxx:169
 TRemoteObject.cxx:170
 TRemoteObject.cxx:171
 TRemoteObject.cxx:172
 TRemoteObject.cxx:173
 TRemoteObject.cxx:174
 TRemoteObject.cxx:175
 TRemoteObject.cxx:176
 TRemoteObject.cxx:177
 TRemoteObject.cxx:178
 TRemoteObject.cxx:179
 TRemoteObject.cxx:180
 TRemoteObject.cxx:181
 TRemoteObject.cxx:182
 TRemoteObject.cxx:183
 TRemoteObject.cxx:184
 TRemoteObject.cxx:185
 TRemoteObject.cxx:186
 TRemoteObject.cxx:187
 TRemoteObject.cxx:188
 TRemoteObject.cxx:189
 TRemoteObject.cxx:190
 TRemoteObject.cxx:191
 TRemoteObject.cxx:192
 TRemoteObject.cxx:193
 TRemoteObject.cxx:194
 TRemoteObject.cxx:195
 TRemoteObject.cxx:196
 TRemoteObject.cxx:197
 TRemoteObject.cxx:198
 TRemoteObject.cxx:199
 TRemoteObject.cxx:200
 TRemoteObject.cxx:201
 TRemoteObject.cxx:202
 TRemoteObject.cxx:203
 TRemoteObject.cxx:204
 TRemoteObject.cxx:205
 TRemoteObject.cxx:206
 TRemoteObject.cxx:207
 TRemoteObject.cxx:208
 TRemoteObject.cxx:209
 TRemoteObject.cxx:210
 TRemoteObject.cxx:211
 TRemoteObject.cxx:212
 TRemoteObject.cxx:213
 TRemoteObject.cxx:214
 TRemoteObject.cxx:215
 TRemoteObject.cxx:216
 TRemoteObject.cxx:217
 TRemoteObject.cxx:218
 TRemoteObject.cxx:219
 TRemoteObject.cxx:220
 TRemoteObject.cxx:221
 TRemoteObject.cxx:222
 TRemoteObject.cxx:223
 TRemoteObject.cxx:224
 TRemoteObject.cxx:225
 TRemoteObject.cxx:226
 TRemoteObject.cxx:227
 TRemoteObject.cxx:228
 TRemoteObject.cxx:229
 TRemoteObject.cxx:230
 TRemoteObject.cxx:231
 TRemoteObject.cxx:232
 TRemoteObject.cxx:233
 TRemoteObject.cxx:234
 TRemoteObject.cxx:235
 TRemoteObject.cxx:236
 TRemoteObject.cxx:237
 TRemoteObject.cxx:238
 TRemoteObject.cxx:239
 TRemoteObject.cxx:240
 TRemoteObject.cxx:241