21 #define THREADTYPE void
23 #if defined( OS_WINNT )
24 #define THREADRETURN 0
25 #define THREADTYPE DWORD WINAPI
28 #define THREADTYPE int
29 #define THREADRETURN 0
35 #include <TDirectory.h>
39 #include <TServerSocket.h>
42 #include <TObjString.h>
59 static TObject*
FollowPath(TObject* container,
char* path)
64 printf(
"Follow path [%s] in container %p\n", path, container);
69 char* s = strchr(path,
'/');
76 if (container->InheritsFrom(TDirectory::Class()))
77 obj = ((
TDirectory*)container)->FindObject(path);
78 else if (container->InheritsFrom(TFolder::Class()))
79 obj = ((TFolder*)container)->FindObject(path);
80 else if (container->InheritsFrom(TCollection::Class()))
81 obj = ((TCollection*)container)->FindObject(path);
84 printf(
"ERROR: Container \'%s\' of type %s is not a TDirectory, TFolder or TCollection\n", container->GetName(), container->IsA()->GetName());
105 obj = gROOT->GetListOfFiles()->FindObject(name);
108 obj = gROOT->FindObjectAny(name);
117 printf(
"Extract top level object from [%s]\n", path);
122 char* s = strchr(path,
'/');
136 for (
unsigned int i=0; i<
gExports.size(); i++)
138 const char* ename =
gExports[i].c_str();
140 if (strcmp(path, ename) == 0)
151 printf(
"ERROR: Top level object \'%s\' not found in exports list\n", path);
161 printf(
"Follow path [%s]\n", path);
182 printf(
"ResetObject object %p name [%s] type [%s]\n", obj, obj->GetName(), obj->IsA()->GetName());
184 if (obj->InheritsFrom(TH1::Class()))
186 ((TH1*)obj)->Reset();
188 else if (obj->InheritsFrom(TDirectory::Class()))
196 TObject *obj = next();
210 xfile =
new TFile(
"/dev/null");
212 TClass *xclass = obj->IsA();
214 if (xclass->InheritsFrom(TDirectory::Class()))
215 xclass = TDirectory::Class();
216 else if (xclass->InheritsFrom(TFolder::Class()))
217 xclass = TDirectory::Class();
218 else if (xclass->InheritsFrom(TCollection::Class()))
219 xclass = TDirectory::Class();
222 name = obj->GetName();
225 return new TKey(name, obj->GetTitle(), xclass, 0, xfile);
237 TSocket *sock = (TSocket *) arg;
238 TMessage message(kMESS_OBJECT);
243 int rd = sock->Recv(request,
sizeof(request));
247 fprintf(stderr,
"TNetDirectory connection from %s closed\n", sock->GetInetAddress().GetHostName());
254 printf(
"Request [%s] from %s\n", request, sock->GetInetAddress().GetHostName());
256 if (strcmp(request,
"GetListOfKeys") == 0)
263 TList* keys =
new TList();
265 for (
unsigned int i=0; i<
gExports.size(); i++)
267 const char* ename =
gExports[i].c_str();
274 fprintf(stderr,
"GetListOfKeys: Exported name \'%s\' cannot be found!\n", xname);
278 TKey* key =
MakeKey(obj, 1, gROOT, ename);
284 printf(
"Sending keys %p\n", keys);
288 message.Reset(kMESS_OBJECT);
289 message.WriteObject(keys);
294 else if (strncmp(request,
"GetListOfKeys ", 14) == 0)
298 char* dirname = request + 14;
302 if (obj && obj->InheritsFrom(TDirectory::Class()))
325 TObject *obj = next();
332 const char* name = obj->GetName();
334 if (!keys->FindObject(name))
336 TKey* key =
MakeKey(obj, 1, dir);
344 message.Reset(kMESS_OBJECT);
345 message.WriteObject(keys);
349 else if (obj && obj->InheritsFrom(TFolder::Class()))
351 TFolder* folder = (TFolder*)obj;
356 TIterator *iterator = folder->GetListOfFolders()->MakeIterator();
358 TList* keys =
new TList();
366 const char* name = obj->GetName();
368 if (!keys->FindObject(name))
370 TKey* key =
MakeKey(obj, 1, gROOT);
379 printf(
"Sending keys %p\n", keys);
383 message.Reset(kMESS_OBJECT);
384 message.WriteObject(keys);
387 else if (obj && obj->InheritsFrom(TCollection::Class()))
389 TCollection* collection = (TCollection*)obj;
394 TIterator *iterator = collection->MakeIterator();
396 TList* keys =
new TList();
404 const char* name = obj->GetName();
406 if (!keys->FindObject(name))
408 TKey* key =
MakeKey(obj, 1, gROOT);
417 printf(
"Sending keys %p\n", keys);
421 message.Reset(kMESS_OBJECT);
422 message.WriteObject(keys);
427 fprintf(stderr,
"netDirectoryServer: ERROR: obj %p name %s, type %s is not a directory!\n", obj, obj->GetName(), obj->IsA()->GetName());
428 TObjString s(
"Not a directory");
429 message.Reset(kMESS_OBJECT);
430 message.WriteObject(&s);
434 fprintf(stderr,
"netDirectoryServer: ERROR: obj %p not found\n", obj);
435 TObjString s(
"Not found");
436 message.Reset(kMESS_OBJECT);
437 message.WriteObject(&s);
443 else if (strncmp(request,
"FindObjectByName ", 17) == 0)
447 char* top = request + 17;
459 sprintf(str,
"TDirectory %s", obj->GetName());
461 for (
unsigned int i=0; i<
gExports.size(); i++)
463 const char* ename =
gExports[i].c_str();
466 if (strcmp(xname, obj->GetName()) == 0)
468 sprintf(str,
"TDirectory %s", ename);
473 obj =
new TObjString(str);
480 if (obj && obj->InheritsFrom(TDirectory::Class()))
483 sprintf(str,
"TDirectory %s", obj->GetName());
484 obj =
new TObjString(str);
487 if (obj && obj->InheritsFrom(TFolder::Class()))
490 sprintf(str,
"TDirectory %s", obj->GetName());
491 obj =
new TObjString(str);
494 if (obj && obj->InheritsFrom(TCollection::Class()))
497 sprintf(str,
"TDirectory %s", obj->GetName());
498 obj =
new TObjString(str);
504 printf(
"Sending object %p name \'%s\' class \'%s\'\n", obj, obj->GetName(), obj->IsA()->GetName());
506 printf(
"Sending object %p\n", obj);
510 message.Reset(kMESS_OBJECT);
511 message.WriteObject(obj);
515 else if (strncmp(request,
"ResetTH1 ", 9) == 0)
519 char* path = request + 9;
521 if (strlen(path) > 1)
530 for (
unsigned int i=0; i<
gExports.size(); i++)
532 const char* ename =
gExports[i].c_str();
539 fprintf(stderr,
"ResetTH1: Exported name \'%s\' cannot be found!\n", xname);
547 TObjString s(
"Success");
549 message.Reset(kMESS_OBJECT);
550 message.WriteObject(&s);
556 fprintf(stderr,
"netDirectoryServer: Received unknown request \"%s\"\n", request);
558 TObjString s(
"Unknown request");
559 message.Reset(kMESS_OBJECT);
560 message.WriteObject(&s);
576 int port = *(
int *) arg;
578 fprintf(stderr,
"NetDirectory server listening on port %d...\n", port);
579 TServerSocket *lsock =
new TServerSocket(port, kTRUE);
583 TSocket *sock = lsock->Accept();
587 printf(
"TNetDirectory server accept() error\n");
592 fprintf(stderr,
"TNetDirectory connection from %s\n", sock->GetInetAddress().GetHostName());
598 LPDWORD lpThreadId = 0;
621 printf(
"Export TDirectory %p named [%s] of type [%s] as [%s]\n", dir, dir->GetName(), dir->IsA()->GetName(), exportName);
624 for (
unsigned int i=0; i<
gExports.size(); i++)
626 const char* ename =
gExports[i].c_str();
627 if (strcmp(ename, exportName) == 0)
639 printf(
"Export TFolder %p named [%s] of type [%s] as [%s]\n", folder, folder->GetName(), folder->IsA()->GetName(), exportName);
642 for (
unsigned int i=0; i<
gExports.size(); i++)
644 const char* ename =
gExports[i].c_str();
645 if (strcmp(ename, exportName) == 0)
657 printf(
"Export TCollection %p named [%s] of type [%s] as [%s]\n", collection, collection->GetName(), collection->IsA()->GetName(), exportName);
660 for (
unsigned int i=0; i<
gExports.size(); i++)
662 const char* ename =
gExports[i].c_str();
663 if (strcmp(ename, exportName) == 0)
687 static int pport = port;
689 TThread *thread =
new TThread(
"NetDirectoryServer",
socket_listener, &pport);
692 LPDWORD lpThreadId = 0;
void StartLockRootTimer(int period_msec=100)
virtual TList * GetList() const
virtual TList * GetListOfKeys() const
void root_socket_server(void *arg)
static std::deque< std::string > gExports
static TObject * FollowPath(TObject *container, char *path)
static std::map< std::string, std::string > gExportNames
static TObject * TopLevel(char *path, char **opath)
void NetDirectoryExport(TDirectory *dir, const char *exportName)
static void ResetObject(TObject *obj)
static THREADTYPE socket_listener(void *arg)
static TKey * MakeKey(TObject *obj, int cycle, TDirectory *dir, const char *name=NULL)
void VerboseNetDirectoryServer(bool verbose)
static TObject * FindTopLevelObject(const char *name)
static bool gAlreadyRunning
static THREADTYPE root_server_thread(void *arg)
void StartNetDirectoryServer(int port, TDirectory *dir)