Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TRemoteObject.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Bertrand Bellenot 19/06/2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TRemoteObject
13 \ingroup Base
14 
15 The TRemoteObject class provides protocol for browsing ROOT objects
16 from a remote ROOT session.
17 
18 It contains information on the real remote object as:
19 
20  - Object Properties (i.e. file stat if the object is a TSystemFile)
21  - Object Name
22  - Class Name
23  - TKey Object Name (if the remote object is a TKey)
24  - TKey Class Name (if the remote object is a TKey)
25  - Remote object address
26 */
27 
28 #include "TApplication.h"
29 #include "TROOT.h"
30 #include "TRemoteObject.h"
31 #include "TSystem.h"
32 #include "TBrowser.h"
33 #include "TOrdCollection.h"
34 #include "TList.h"
35 #include "TClass.h"
36 
37 ClassImp(TRemoteObject);
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Create a remote object.
41 
42 TRemoteObject::TRemoteObject()
43 {
44  fIsFolder = kFALSE;
45  fRemoteAddress = 0;
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Create a remote object.
50 
51 TRemoteObject::TRemoteObject(const char *name, const char *title,
52  const char *classname) : TNamed(name, title)
53 {
54  fIsFolder = kFALSE;
55  fClassName = classname;
56  if ((fClassName == "TSystemDirectory") ||
57  (fClassName == "TFile"))
58  fIsFolder = kTRUE;
59  if (!strcmp(classname, "TSystemDirectory") ||
60  !strcmp(classname, "TSystemFile")) {
61  gSystem->GetPathInfo(name, fFileStat);
62  }
63  Long_t raddr = (Long_t) this;
64  fRemoteAddress = raddr;
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Delete remote object.
69 
70 TRemoteObject::~TRemoteObject()
71 {
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Browse remote object.
76 
77 void TRemoteObject::Browse(TBrowser *b)
78 {
79  TList *ret;
80  TRemoteObject *robj;
81  const char *file;
82 
83  if (fClassName == "TSystemFile") {
84  if (b)
85  b->ExecuteDefaultAction(this);
86  return;
87  }
88  if (fClassName == "TKey") {
89  if (b->GetRefreshFlag())
90  b->SetRefreshFlag(kFALSE);
91  gApplication->SetBit(TApplication::kProcessRemotely);
92  TObject *obj = (TObject *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseKey(\"%s\");", GetName()));
93  if (obj) {
94  if (obj->IsA()->GetMethodWithPrototype("SetDirectory", "TDirectory*"))
95  gROOT->ProcessLine(Form("((%s *)0x%lx)->SetDirectory(0);", obj->ClassName(), (ULong_t)obj));
96  obj->Browse(b);
97  b->SetRefreshFlag(kTRUE);
98  }
99  }
100  if (fClassName == "TSystemDirectory") {
101  if (b->GetRefreshFlag())
102  b->SetRefreshFlag(kFALSE);
103  gApplication->SetBit(TApplication::kProcessRemotely);
104  ret = (TList *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseDirectory(\"%s\");", GetTitle()));
105  if (ret) {
106  TIter next(ret);
107  while ((robj = (TRemoteObject *)next())) {
108  file = robj->GetName();
109  if (b->TestBit(TBrowser::kNoHidden) && file[0] == '.' && file[1] != '.' )
110  continue;
111  b->Add(robj, robj->GetName());
112  }
113  }
114  return;
115  }
116  if (fClassName == "TFile") {
117  if (b->GetRefreshFlag())
118  b->SetRefreshFlag(kFALSE);
119  gApplication->SetBit(TApplication::kProcessRemotely);
120  ret = (TList *)gROOT->ProcessLine(Form("((TApplicationServer *)gApplication)->BrowseFile(\"%s\");", GetName()));
121  if (ret) {
122  TIter next(ret);
123  while ((robj = (TRemoteObject *)next())) {
124  //file = robj->GetName();
125  b->Add(robj, robj->GetName());
126  }
127  }
128  return;
129  }
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Browse OS system directories.
134 
135 TList *TRemoteObject::Browse()
136 {
137  // Collections to keep track of all browser objects that have been
138  // generated. It's main goal is to prevent the continuous
139  // allocations of new objects with the same names during browsing.
140  TList *objects = new TList;
141 
142  static Int_t level = 0;
143  const char *name = GetTitle();
144  TRemoteObject *sdir;
145 
146  if (GetName()[0] == '.' && GetName()[1] == '.')
147  SetName(gSystem->BaseName(name));
148 
149  TSystemDirectory dir(name, name);
150  TList *files = dir.GetListOfFiles();
151  if (files) {
152  files->Sort();
153  TIter next(files);
154  TSystemFile *file;
155  TString fname;
156  // directories first
157  while ((file=(TSystemFile*)next())) {
158  fname = file->GetName();
159  if (file->IsDirectory()) {
160  level++;
161  TString sdirpath;
162  if (!strcmp(fname.Data(), "."))
163  sdirpath = name;
164  else if (!strcmp(fname.Data(), ".."))
165  sdirpath = gSystem->DirName(name);
166  else {
167  sdirpath = name;
168  if (!sdirpath.EndsWith("/"))
169  sdirpath += "/";
170  sdirpath += fname.Data();
171  }
172  sdir = new TRemoteObject(fname.Data(), sdirpath.Data(), "TSystemDirectory");
173  objects->Add(sdir);
174  level--;
175  }
176  }
177  // then files...
178  TIter nextf(files);
179  while ((file=(TSystemFile*)nextf())) {
180  fname = file->GetName();
181  if (!file->IsDirectory()) {
182  sdir = new TRemoteObject(fname.Data(), gSystem->WorkingDirectory(), "TSystemFile");
183  objects->Add(sdir);
184  }
185  }
186  delete files;
187  }
188  return objects;
189 }
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 /// Get remote file status.
193 
194 Bool_t TRemoteObject::GetFileStat(FileStat_t *buf)
195 {
196  buf->fDev = fFileStat.fDev;
197  buf->fIno = fFileStat.fIno;
198  buf->fMode = fFileStat.fMode;
199  buf->fUid = fFileStat.fUid;
200  buf->fGid = fFileStat.fGid;
201  buf->fSize = fFileStat.fSize;
202  buf->fMtime = fFileStat.fMtime;
203  buf->fIsLink = fFileStat.fIsLink;
204  return kTRUE;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Remote object streamer.
209 
210 void TRemoteObject::Streamer(TBuffer &b)
211 {
212  if (b.IsReading()) {
213  b >> fFileStat.fDev;
214  b >> fFileStat.fIno;
215  b >> fFileStat.fMode;
216  b >> fFileStat.fUid;
217  b >> fFileStat.fGid;
218  b >> fFileStat.fSize;
219  b >> fFileStat.fMtime;
220  b >> fFileStat.fIsLink;
221  b >> fIsFolder;
222  b >> fRemoteAddress;
223  b >> fClassName;
224  b >> fKeyObjectName;
225  b >> fKeyClassName;
226  }
227  else {
228  b << fFileStat.fDev;
229  b << fFileStat.fIno;
230  b << fFileStat.fMode;
231  b << fFileStat.fUid;
232  b << fFileStat.fGid;
233  b << fFileStat.fSize;
234  b << fFileStat.fMtime;
235  b << fFileStat.fIsLink;
236  b << fIsFolder;
237  b << fRemoteAddress;
238  b << fClassName;
239  b << fKeyObjectName;
240  b << fKeyClassName;
241  }
242  TNamed::Streamer(b);
243 }