ROOTANA
TNetDirectory.cxx
Go to the documentation of this file.
1 //
2 // TNetDirectory.cxx
3 //
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 
9 #include "TNetDirectory.h"
10 
11 #include "TSocket.h"
12 #include "TMessage.h"
13 #include "TClass.h"
14 #include "TObjString.h"
15 
16 static bool gVerbose = false;
17 
19 {
20  TSocket* fSocket;
21 
22 public:
23 
24  TNetDirectoryConnection(const char* host, int port)
25  {
26  fSocket = new TSocket(host, port);
27  printf("Connected to %s:%d\n", host, port);
28  }
29 
30  int Reconnect()
31  {
32  std::string host = fSocket->GetName();
33  int port = fSocket->GetPort();
34  fSocket->Close();
35  delete fSocket;
36  fSocket = new TSocket(host.c_str(), port);
37  printf("Connected to %s:%d\n", host.c_str(), port);
38  return 0;
39  }
40 
41  void Request(const char* req)
42  {
43  if (gVerbose)
44  printf("Request [%s]\n", req);
45  int s = fSocket->Send(req);
46  if (gVerbose)
47  printf("Request sent %d\n", s);
48  }
49 
50  TObject* ReadObject(TClass* type)
51  {
52  TMessage *mr = 0;
53  int r = fSocket->Recv(mr);
54  //printf("ReadObject recv %d\n", r);
55  if (r <= 0) {
56  printf("Error reading from socket!\n");
57  return NULL;
58  }
59 
60  TObject *obj = NULL;
61 
62  if (mr) {
63  //mr->Print();
64  obj = (TObject*)mr->ReadObjectAny(mr->GetClass());
65  }
66 
67  //printf("mr %p, obj %p, class %p, %s\n", mr, obj, mr->GetClass(), mr->GetClass()->GetName());
68 
69  if (obj) {
70  //obj->Print();
71 
72  if (!obj->InheritsFrom(type)) {
73 
74  if (obj->IsA() == TObjString::Class()) {
75  TObjString *ostr = (TObjString*)obj;
76  printf("Instead of a %s, received a %s reading \'%s\'\n", type->GetName(), obj->IsA()->GetName(), ostr->GetName());
77  return NULL;
78  }
79 
80  printf("Object type mismatch, received %s, expected %s\n", obj->IsA()->GetName(), type->GetName());
81  return NULL;
82  }
83  }
84 
85  if (mr)
86  delete mr;
87 
88  return obj;
89  }
90 };
91 
92 TNetDirectory::TNetDirectory(const char *remoteServerName, TDirectory* motherDir)
93  : TDirectory(remoteServerName, remoteServerName, "", motherDir)
94 {
95  if (gVerbose)
96  printf("TNetDirectory::ctor: %s\n", remoteServerName);
97  int port = 9091;
98  char hostname[256];
99  strcpy(hostname, remoteServerName);
100  char* s = strchr(hostname, ':');
101  if (s)
102  {
103  *s = 0;
104  port = atoi(s+1);
105  }
106  fConn = new TNetDirectoryConnection(hostname, port);
107  fPath = "";
108 }
109 
110 TNetDirectory::TNetDirectory(TNetDirectoryConnection* conn, const char* dirname, const std::string& path, TDirectory* motherDir)
111  : TDirectory(dirname, dirname, "", motherDir)
112 {
113  if (gVerbose)
114  printf("TNetDirectory::ctor: conn %p, path [%s]\n", conn, path.c_str());
115  fConn = conn;
116  fPath = path;
117 }
118 
120 {
121  if (gVerbose)
122  printf("TNetDirectory::dtor\n");
123  fConn = NULL;
124 }
125 
127 {
128  return fConn->Reconnect();
129 }
130 
131 void TNetDirectory::ResetTH1(const char *name)
132 {
133  if (gVerbose)
134  printf("TNetDirectory(%s)::ResetTH1(%s)\n", fPath.c_str(), name);
135 
136  std::string req = "ResetTH1 ";
137  if (fPath.length() > 0)
138  {
139  req += fPath;
140  req += "/";
141  }
142 
143  if (name && strlen(name)>0)
144  {
145  req += name;
146  }
147 
148  fConn->Request(req.c_str());
149  TObject *obj = fConn->ReadObject(TObject::Class());
150  delete obj;
151 }
152 
153 void TNetDirectory::Append(TObject *obj, Bool_t replace)
154 {
155  if (gVerbose)
156  printf("TNetDirectory(%s)::Append(%p, name: %s)\n", fPath.c_str(), obj, obj->GetName());
157 }
158 
159 void TNetDirectory::Browse(TBrowser *b) { assert(!"not implemented"); }
160 void TNetDirectory::Clear(Option_t *option) { assert(!"not implemented"); }
161 void TNetDirectory::Close(Option_t *option) { assert(!"not implemented"); }
162 Bool_t TNetDirectory::cd(const char *path) { assert(!"not implemented"); }
163 void TNetDirectory::DeleteAll(Option_t *option) { assert(!"not implemented"); }
164 void TNetDirectory::Delete(const char *namecycle) { assert(!"not implemented"); }
165 void TNetDirectory::Draw(Option_t *option) { assert(!"not implemented"); }
166 void TNetDirectory::FillBuffer(char *&buffer) { assert(!"not implemented"); }
167 TKey *TNetDirectory::FindKey(const char *keyname) const { assert(!"not implemented"); };
168 TKey *TNetDirectory::FindKeyAny(const char *keyname) const { assert(!"not implemented"); };
169 
170 TObject *TNetDirectory::FindObject(const char *name) const
171 {
172  if (gVerbose)
173  printf("TNetDirectory(%s)::FindObject(%s)\n", fPath.c_str(), name);
174 
175  for (unsigned int i=0; i<fSubDirs.size(); i++)
176  {
177  TNetDirectory *s = fSubDirs[i];
178  if (strcmp(name, s->GetName()) == 0)
179  {
180  //printf("Return subdirectory %p [%s]\n", s, s->GetName());
181  return s;
182  }
183  }
184 
185  std::string req = "FindObjectByName ";
186  if (fPath.length() > 0)
187  {
188  req += fPath;
189  req += "/";
190  }
191  req += name;
192 
193  fConn->Request(req.c_str());
194  TObject *obj = fConn->ReadObject(TObject::Class());
195 
196  if (obj && strcmp(obj->IsA()->GetName(), "TObjString") == 0)
197  {
198  const char* s = ((TObjString*)obj)->GetName();
199  if (strncmp(s, "TDirectory ", 11) == 0)
200  {
201  return NULL;
202  }
203  }
204 
205  return obj;
206 }
207 
208 TObject *TNetDirectory::FindObject(const TObject *obj) const
209 {
210  assert(!"not implemented");
211 }
212 
213 TObject *TNetDirectory::FindObjectAny(const char *name) const { assert(!"not implemented"); }
214 
215 TObject *TNetDirectory::Get(const char *namecycle)
216 {
217  if (gVerbose)
218  printf("TNetDirectory(%s)::Get(%s)\n", fPath.c_str(), namecycle);
219 
220  std::string req = "FindObjectByName "; // "Get ";
221  if (fPath.length() > 0)
222  {
223  req += fPath;
224  req += "/";
225  }
226  req += namecycle;
227 
228  fConn->Request(req.c_str());
229  TObject *obj = fConn->ReadObject(TObject::Class());
230 
231  if (obj && strcmp(obj->IsA()->GetName(), "TObjString") == 0)
232  {
233  const char* s = ((TObjString*)obj)->GetName();
234  if (strncmp(s, "TDirectory ", 11) == 0)
235  {
236  const char* dirname = s+11;
237  if (gVerbose)
238  printf("Got TDirectory %s\n", dirname);
239  TNetDirectory *s = new TNetDirectory(fConn, dirname, fPath + "/" + dirname, this);
240  fSubDirs.push_back(s);
241  return s;
242  }
243  }
244 
245  return obj;
246 }
247 
248 TDirectory *TNetDirectory::GetDirectory(const char *namecycle, Bool_t printError, const char *funcname) { assert(!"not implemented"); }
249 void *TNetDirectory::GetObjectChecked(const char *namecycle, const char* classname) { assert(!"not implemented"); }
250 void *TNetDirectory::GetObjectChecked(const char *namecycle, const TClass* cl) { assert(!"not implemented"); }
251 void *TNetDirectory::GetObjectUnchecked(const char *namecycle) { assert(!"not implemented"); }
252 Int_t TNetDirectory::GetBufferSize() const { assert(!"not implemented"); }
253 TFile *TNetDirectory::GetFile() const { return NULL; assert(!"not implemented"); return NULL; }
254 TKey *TNetDirectory::GetKey(const char *name, Short_t cycle) const { assert(!"not implemented"); }
255 TList *TNetDirectory::GetList() const { assert(!"not implemented"); }
256 
258 {
259  if (gVerbose)
260  printf("TNetDirectory(%s)::GetListOfKeys()\n", fPath.c_str());
261 
262  std::string req = "GetListOfKeys";
263  if (fPath.length() > 0)
264  {
265  req += " ";
266  req += fPath;
267  }
268 
269  fConn->Request(req.c_str());
270  TList *keys = (TList*)fConn->ReadObject(TList::Class());
271  if (keys == NULL)
272  // return fKeys;
273  return NULL;
274  //keys->Print();
275  //keys->ls();
276  return keys;
277 }
278 
279 Int_t TNetDirectory::GetNbytesKeys() const { assert(!"not implemented"); }
280 Int_t TNetDirectory::GetNkeys() const { assert(!"not implemented"); }
281 Long64_t TNetDirectory::GetSeekDir() const { assert(!"not implemented"); }
282 Long64_t TNetDirectory::GetSeekParent() const { assert(!"not implemented"); }
283 Long64_t TNetDirectory::GetSeekKeys() const { assert(!"not implemented"); }
284 const char *TNetDirectory::GetPathStatic() const { assert(!"not implemented"); }
285 const char *TNetDirectory::GetPath() const { assert(!"not implemented"); }
286 void TNetDirectory::ls(Option_t *option) const { assert(!"not implemented"); }
287 TDirectory *TNetDirectory::mkdir(const char *name, const char *title, Bool_t) { assert(!"not implemented"); }
288 void TNetDirectory::Paint(Option_t *option) { assert(!"not implemented"); }
289 
290 void TNetDirectory::Print(Option_t *option) const
291 {
292  TDirectory::Print(option);
293 }
294 
295 void TNetDirectory::Purge(Short_t nkeep) { assert(!"not implemented"); }
296 void TNetDirectory::pwd() const { assert(!"not implemented"); }
297 void TNetDirectory::ReadAll(Option_t *option) { assert(!"not implemented"); }
298 Int_t TNetDirectory::ReadKeys(Bool_t) { assert(!"not implemented"); }
299 void TNetDirectory::RecursiveRemove(TObject *obj) { assert(!"not implemented"); }
300 void TNetDirectory::rmdir(const char *name) { assert(!"not implemented"); }
301 void TNetDirectory::Save() { assert(!"not implemented"); }
302 void TNetDirectory::SaveSelf(Bool_t force) { assert(!"not implemented"); }
303 void TNetDirectory::SetBufferSize(Int_t bufsize) { assert(!"not implemented"); }
304 void TNetDirectory::SetName(const char* newname) { assert(!"not implemented"); }
305 Int_t TNetDirectory::Sizeof() const { assert(!"not implemented"); }
306 Int_t TNetDirectory::Write(const char *name, Int_t opt, Int_t bufsiz) { assert(!"not implemented"); }
307 Int_t TNetDirectory::Write(const char *name, Int_t opt, Int_t bufsiz) const { assert(!"not implemented"); }
308 Int_t TNetDirectory::WriteTObject(const TObject *obj, const char *name, Option_t *option, Int_t) { assert(!"not implemented"); }
309 Int_t TNetDirectory::WriteObjectAny(const void *obj, const char *classname, const char *name, Option_t *option, Int_t) { assert(!"not implemented"); }
310 Int_t TNetDirectory::WriteObjectAny(const void *obj, const TClass *cl, const char *name, Option_t *option, Int_t) { assert(!"not implemented"); }
311 void TNetDirectory::WriteDirHeader() { assert(!"not implemented"); }
312 void TNetDirectory::WriteKeys() { assert(!"not implemented"); }
313 
314 //end
static bool gVerbose
virtual Int_t ReadKeys()
virtual void Print(Option_t *option="") const
TObject * ReadObject(TClass *type)
TNetDirectoryConnection(const char *host, int port)
void Request(const char *req)
virtual void DeleteAll(Option_t *option="")
virtual TKey * FindKeyAny(const char *keyname) const
virtual TDirectory * GetDirectory(const char *namecycle, Bool_t printError=false, const char *funcname="GetDirectory")
virtual void ReadAll(Option_t *option="")
virtual Int_t GetBufferSize() const
virtual void Print(Option_t *option="") const
virtual void Clear(Option_t *option="")
virtual void SetName(const char *newname)
virtual void Purge(Short_t nkeep=1)
virtual Long64_t GetSeekKeys() const
virtual TObject * Get(const char *namecycle)
virtual Int_t WriteTObject(const TObject *obj, const char *name, Option_t *option, Int_t bufsize)
virtual TObject * FindObject(const char *name) const
virtual Bool_t cd(const char *path=0)
virtual Int_t Sizeof() const
virtual ~TNetDirectory()
TNetDirectoryConnection * fConn
Definition: TNetDirectory.h:36
virtual const char * GetPathStatic() const
virtual TList * GetList() const
std::deque< TNetDirectory * > fSubDirs
Definition: TNetDirectory.h:38
virtual TKey * FindKey(const char *keyname) const
virtual void rmdir(const char *name)
virtual void Browse(TBrowser *b)
virtual Int_t GetNbytesKeys() const
virtual void Draw(Option_t *option="")
virtual void ls(Option_t *option="") const
virtual void SetBufferSize(Int_t bufsize)
void ResetTH1(const char *name)
virtual void RecursiveRemove(TObject *obj)
std::string fPath
pointer to network connection
Definition: TNetDirectory.h:37
virtual void SaveSelf(Bool_t force=kFALSE)
virtual void Append(TObject *obj, Bool_t replace)
virtual Int_t WriteObjectAny(const void *obj, const char *classname, const char *name, Option_t *option, Int_t bufsize)
virtual void Save()
virtual void WriteDirHeader()
virtual Long64_t GetSeekParent() const
virtual Int_t GetNkeys() const
virtual void pwd() const
virtual void Delete(const char *namecycle="")
virtual TList * GetListOfKeys() const
virtual const char * GetPath() const
virtual void WriteKeys()
virtual TObject * FindObjectAny(const char *name) const
virtual void Close(Option_t *option="")
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
virtual Long64_t GetSeekDir() const
virtual TKey * GetKey(const char *name, Short_t cycle=9999) const
virtual void Paint(Option_t *option="")
virtual void * GetObjectUnchecked(const char *namecycle)
virtual TDirectory * mkdir(const char *name, const char *title, Bool_t returnExistingDirectory)
virtual TFile * GetFile() const
virtual void * GetObjectChecked(const char *namecycle, const char *classname)
virtual void FillBuffer(char *&buffer)