TNetDirectory.cxx

Go to the documentation of this file.
00001 //
00002 // TNetDirectory.cxx
00003 //
00004 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <assert.h>
00008 
00009 #include "TNetDirectory.h"
00010 
00011 #include "TSocket.h"
00012 #include "TMessage.h"
00013 #include "TClass.h"
00014 #include "TObjString.h"
00015 
00016 static bool gVerbose = false;
00017 
00018 class TNetDirectoryConnection
00019 {
00020   TSocket* fSocket;
00021 
00022 public:
00023 
00024   TNetDirectoryConnection(const char* host, int port)
00025   {
00026     fSocket = new TSocket(host, port);
00027     printf("Connected to %s:%d\n", host, port);
00028   }
00029 
00030   int Reconnect()
00031   {
00032     std::string host = fSocket->GetName();
00033     int         port = fSocket->GetPort();
00034     fSocket->Close();
00035     delete fSocket;
00036     fSocket = new TSocket(host.c_str(), port);
00037     printf("Connected to %s:%d\n", host.c_str(), port);
00038     return 0;
00039   }
00040 
00041   void Request(const char* req)
00042   {
00043     if (gVerbose)
00044       printf("Request [%s]\n", req);
00045     int s = fSocket->Send(req);
00046     //printf("Request sent %d\n", s);
00047   }
00048 
00049   TObject* ReadObject(TClass* type)
00050   {
00051     TMessage *mr = 0;
00052     int r = fSocket->Recv(mr);
00053     //printf("ReadObject recv %d\n", r);
00054     if (r <= 0) {
00055       printf("Error reading from socket!\n");
00056       return NULL;
00057     }
00058 
00059     TObject *obj = NULL;
00060 
00061     if (mr) {
00062       //mr->Print();
00063       obj = (TObject*)mr->ReadObjectAny(mr->GetClass());
00064     }
00065 
00066     //printf("mr %p, obj %p, class %p, %s\n", mr, obj, mr->GetClass(), mr->GetClass()->GetName());
00067 
00068     if (obj) {
00069       //obj->Print();
00070 
00071       if (!obj->InheritsFrom(type)) {
00072 
00073         if (obj->IsA() == TObjString::Class()) {
00074             TObjString *ostr = (TObjString*)obj;
00075             printf("Instead of a %s, received a %s reading \'%s\'\n", type->GetName(), obj->IsA()->GetName(), ostr->GetName());
00076             return NULL;
00077         }
00078 
00079         printf("Object type mismatch, received %s, expected %s\n", obj->IsA()->GetName(), type->GetName());
00080         return NULL;
00081       }
00082     }
00083 
00084     if (mr)
00085       delete mr;
00086 
00087     return obj;
00088   }
00089 };
00090 
00091 TNetDirectory::TNetDirectory(const char *remoteServerName, TDirectory* motherDir)
00092   : TDirectory(remoteServerName, remoteServerName, "", motherDir)
00093 {
00094   if (gVerbose)
00095     printf("TNetDirectory::ctor: %s\n", remoteServerName);
00096   int port = 9091;
00097   char hostname[256];
00098   strcpy(hostname, remoteServerName);
00099   char* s = strchr(hostname, ':');
00100   if (s)
00101     {
00102       *s = 0;
00103       port = atoi(s+1);
00104     }
00105   fConn = new TNetDirectoryConnection(hostname, port);
00106   fPath = "";
00107 }
00108 
00109 TNetDirectory::TNetDirectory(TNetDirectoryConnection* conn, const char* dirname, const std::string& path, TDirectory* motherDir)
00110   : TDirectory(dirname, dirname, "", motherDir)
00111 {
00112   if (gVerbose)
00113     printf("TNetDirectory::ctor: conn %p, path [%s]\n", conn, path.c_str());
00114   fConn = conn;
00115   fPath = path;
00116 }
00117 
00118 TNetDirectory::~TNetDirectory()
00119 {
00120   if (gVerbose)
00121     printf("TNetDirectory::dtor\n");
00122   fConn = NULL;
00123 }
00124 
00125 int TNetDirectory::Reconnect()
00126 {
00127   return fConn->Reconnect();
00128 }
00129 
00130 void TNetDirectory::ResetTH1(const char *name)
00131 {
00132   if (gVerbose)
00133     printf("TNetDirectory(%s)::ResetTH1(%s)\n", fPath.c_str(), name);
00134 
00135   std::string req = "ResetTH1 ";
00136   if (fPath.length() > 0)
00137     {
00138       req += fPath;
00139       req += "/";
00140     }
00141 
00142   if (name && strlen(name)>0)
00143     {
00144       req += name;
00145     }
00146 
00147   fConn->Request(req.c_str());
00148   TObject *obj = fConn->ReadObject(TObject::Class());
00149   delete obj;
00150 }
00151 
00152 void        TNetDirectory::Append(TObject *obj)
00153 {
00154   if (gVerbose)
00155     printf("TNetDirectory(%s)::Append(%p, name: %s)\n", fPath.c_str(), obj, obj->GetName());
00156 }
00157 
00158 void        TNetDirectory::Browse(TBrowser *b) { assert(!"not implemented"); }
00159 void        TNetDirectory::Clear(Option_t *option) { assert(!"not implemented"); }
00160 void        TNetDirectory::Close(Option_t *option) { assert(!"not implemented"); }
00161 Bool_t      TNetDirectory::cd(const char *path) { assert(!"not implemented"); }
00162 void        TNetDirectory::DeleteAll(Option_t *option) { assert(!"not implemented"); }
00163 void        TNetDirectory::Delete(const char *namecycle) { assert(!"not implemented"); }
00164 void        TNetDirectory::Draw(Option_t *option) { assert(!"not implemented"); }
00165 void        TNetDirectory::FillBuffer(char *&buffer) { assert(!"not implemented"); }
00166 TKey       *TNetDirectory::FindKey(const char *keyname) const { assert(!"not implemented"); };
00167 TKey       *TNetDirectory::FindKeyAny(const char *keyname) const { assert(!"not implemented"); };
00168 
00169 TObject    *TNetDirectory::FindObject(const char *name) const
00170 {
00171   if (gVerbose)
00172     printf("TNetDirectory(%s)::FindObject(%s)\n", fPath.c_str(), name);
00173 
00174   for (unsigned int i=0; i<fSubDirs.size(); i++)
00175     {
00176       TNetDirectory *s = fSubDirs[i];
00177       if (strcmp(name, s->GetName()) == 0)
00178         {
00179           //printf("Return subdirectory %p [%s]\n", s, s->GetName());
00180           return s;
00181         }
00182     }
00183 
00184   std::string req = "FindObjectByName ";
00185   if (fPath.length() > 0)
00186     {
00187       req += fPath;
00188       req += "/";
00189     }
00190   req += name;
00191 
00192   fConn->Request(req.c_str());
00193   TObject *obj = fConn->ReadObject(TObject::Class());
00194 
00195   if (obj && strcmp(obj->IsA()->GetName(), "TObjString") == 0)
00196     {
00197       const char* s = ((TObjString*)obj)->GetName();
00198       if (strncmp(s, "TDirectory ", 11) == 0)
00199         {
00200           return NULL;
00201         }
00202     }
00203 
00204   return obj;
00205 }
00206 
00207 TObject    *TNetDirectory::FindObject(const TObject *obj) const
00208 {
00209   assert(!"not implemented"); 
00210 }
00211 
00212 TObject    *TNetDirectory::FindObjectAny(const char *name) const { assert(!"not implemented"); }
00213 
00214 TObject    *TNetDirectory::Get(const char *namecycle)
00215 {
00216   if (gVerbose)
00217     printf("TNetDirectory(%s)::Get(%s)\n", fPath.c_str(), namecycle);
00218 
00219   std::string req = "FindObjectByName "; // "Get ";
00220   if (fPath.length() > 0)
00221     {
00222       req += fPath;
00223       req += "/";
00224     }
00225   req += namecycle;
00226 
00227   fConn->Request(req.c_str());
00228   TObject *obj = fConn->ReadObject(TObject::Class());
00229 
00230   if (obj && strcmp(obj->IsA()->GetName(), "TObjString") == 0)
00231     {
00232       const char* s = ((TObjString*)obj)->GetName();
00233       if (strncmp(s, "TDirectory ", 11) == 0)
00234         {
00235           const char* dirname = s+11;
00236           if (gVerbose)
00237             printf("Got TDirectory %s\n", dirname);
00238           TNetDirectory *s = new TNetDirectory(fConn, dirname, fPath + "/" + dirname, this);
00239           fSubDirs.push_back(s);
00240           return s;
00241         }
00242     }
00243 
00244   return obj;
00245 }
00246 
00247 TDirectory *TNetDirectory::GetDirectory(const char *namecycle, Bool_t printError, const char *funcname) { assert(!"not implemented"); }
00248 void       *TNetDirectory::GetObjectChecked(const char *namecycle, const char* classname) { assert(!"not implemented"); }
00249 void       *TNetDirectory::GetObjectChecked(const char *namecycle, const TClass* cl) { assert(!"not implemented"); }
00250 void       *TNetDirectory::GetObjectUnchecked(const char *namecycle) { assert(!"not implemented"); }
00251 Int_t       TNetDirectory::GetBufferSize() const { assert(!"not implemented"); }
00252 TFile      *TNetDirectory::GetFile() const { return NULL; assert(!"not implemented"); return NULL; }
00253 TKey       *TNetDirectory::GetKey(const char *name, Short_t cycle) const { assert(!"not implemented"); }
00254 TList      *TNetDirectory::GetList() const { assert(!"not implemented"); }
00255 
00256 TList      *TNetDirectory::GetListOfKeys() const
00257 {
00258   if (gVerbose)
00259     printf("TNetDirectory(%s)::GetListOfKeys()\n", fPath.c_str());
00260 
00261   std::string req = "GetListOfKeys";
00262   if (fPath.length() > 0)
00263     {
00264       req += " ";
00265       req += fPath;
00266     }
00267 
00268   fConn->Request(req.c_str());
00269   TList *keys = (TList*)fConn->ReadObject(TList::Class());
00270   if (keys == NULL)
00271     //    return fKeys;
00272     return NULL;
00273   //keys->Print();
00274   //keys->ls();
00275   return keys;
00276 }
00277 
00278 Int_t       TNetDirectory::GetNbytesKeys() const { assert(!"not implemented"); }
00279 Int_t       TNetDirectory::GetNkeys() const { assert(!"not implemented"); }
00280 Long64_t    TNetDirectory::GetSeekDir() const { assert(!"not implemented"); }
00281 Long64_t    TNetDirectory::GetSeekParent() const { assert(!"not implemented"); }
00282 Long64_t    TNetDirectory::GetSeekKeys() const { assert(!"not implemented"); }
00283 const char *TNetDirectory::GetPathStatic() const { assert(!"not implemented"); }
00284 const char *TNetDirectory::GetPath() const { assert(!"not implemented"); }
00285 void        TNetDirectory::ls(Option_t *option) const { assert(!"not implemented"); }
00286 TDirectory *TNetDirectory::mkdir(const char *name, const char *title) { assert(!"not implemented"); }
00287 void        TNetDirectory::Paint(Option_t *option) { assert(!"not implemented"); }
00288 
00289 void        TNetDirectory::Print(Option_t *option) const
00290 {
00291   TDirectory::Print(option);
00292 }
00293 
00294 void        TNetDirectory::Purge(Short_t nkeep) { assert(!"not implemented"); }
00295 void        TNetDirectory::pwd() const { assert(!"not implemented"); }
00296 void        TNetDirectory::ReadAll(Option_t *option) { assert(!"not implemented"); }
00297 Int_t       TNetDirectory::ReadKeys() { assert(!"not implemented"); }
00298 void        TNetDirectory::RecursiveRemove(TObject *obj) { assert(!"not implemented"); }
00299 void        TNetDirectory::rmdir(const char *name) { assert(!"not implemented"); }
00300 void        TNetDirectory::Save() { assert(!"not implemented"); }
00301 void        TNetDirectory::SaveSelf(Bool_t force) { assert(!"not implemented"); }
00302 void        TNetDirectory::SetBufferSize(Int_t bufsize) { assert(!"not implemented"); }
00303 void        TNetDirectory::SetName(const char* newname) { assert(!"not implemented"); }
00304 Int_t       TNetDirectory::Sizeof() const { assert(!"not implemented"); }
00305 Int_t       TNetDirectory::Write(const char *name, Int_t opt, Int_t bufsiz) { assert(!"not implemented"); }
00306 Int_t       TNetDirectory::Write(const char *name, Int_t opt, Int_t bufsiz) const { assert(!"not implemented"); }
00307 Int_t       TNetDirectory::WriteTObject(const TObject *obj, const char *name, Option_t *option) { assert(!"not implemented"); }
00308 Int_t       TNetDirectory::WriteObjectAny(const void *obj, const char *classname, const char *name, Option_t *option) { assert(!"not implemented"); }
00309 Int_t       TNetDirectory::WriteObjectAny(const void *obj, const TClass *cl, const char *name, Option_t *option) { assert(!"not implemented"); }
00310 void        TNetDirectory::WriteDirHeader() { assert(!"not implemented"); }
00311 void        TNetDirectory::WriteKeys() { assert(!"not implemented"); }
00312 
00313 //end

Generated on 12 Feb 2016 for ROOT Analyzer by  doxygen 1.6.1