LocalDataLogger.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. // LocalDataLogger.cpp: implementation of the CLocalDataLogger class.
  14. //
  15. //////////////////////////////////////////////////////////////////////
  16. #include "LocalDataLogger.hpp"
  17. #include "jutil.hpp"
  18. #include "jfile.ipp"
  19. #define RETRIES 20
  20. //////////////////////////////////////////////////////////////////////
  21. // Construction/Destruction
  22. //////////////////////////////////////////////////////////////////////
  23. CLocalDataLogger::CLocalDataLogger()
  24. {
  25. }
  26. CLocalDataLogger::CLocalDataLogger(IPropertyTree *cfg, const char *process, const char *service, const char* UrlRoot)
  27. {
  28. StringBuffer _directoryCache,xpath;
  29. xpath.appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/localCache/", process, service);
  30. IPropertyTree* cacheprop = cfg->queryBranch(xpath.str());
  31. if(cacheprop==0)
  32. {
  33. WARNLOG(-1,"No local cache defined for %s service.",service);
  34. return;
  35. }
  36. Init(cacheprop->queryProp("@cache"),cacheprop->queryProp("@fileExtension"),UrlRoot,cacheprop->getPropInt("@timerPeriod"),cacheprop->getPropInt("@cacheTimeout"));
  37. }
  38. CLocalDataLogger::CLocalDataLogger(const char* logDirectory,const char* ext,unsigned TimerPeriod,unsigned CacheTimeoutPeriod)
  39. {
  40. Init(logDirectory,ext,"",TimerPeriod,CacheTimeoutPeriod);
  41. }
  42. void CLocalDataLogger::Init(const char* logDirectory,const char* ext,const char* UrlRoot, unsigned TimerPeriod,unsigned CacheTimeoutPeriod)
  43. {
  44. if(!logDirectory || !ext)
  45. throw MakeStringException(-1,"Invalid parameters passed to cLocalDataLogger::Init");
  46. StringBuffer pathtodir;
  47. pathtodir.appendf("%s",logDirectory);
  48. Owned<IFile> pDirectory = createIFile(pathtodir.str());
  49. if(pDirectory->exists() == false)
  50. pDirectory->createDirectory();
  51. m_UrlRoot.appendf("%s",UrlRoot);
  52. m_logDirectory.append(logDirectory);
  53. StringBuffer dirpath;
  54. dirpath.appendf("%s",logDirectory);
  55. m_CleanupThread.setown(new CFileCleanupThread(dirpath,ext,TimerPeriod,CacheTimeoutPeriod));
  56. m_CleanupThread->start();
  57. }
  58. CLocalDataLogger::~CLocalDataLogger()
  59. {
  60. if (m_CleanupThread.get())
  61. m_CleanupThread->finish();
  62. }
  63. StringBuffer& CLocalDataLogger::generateUniqueName(StringBuffer& returnName)
  64. {
  65. CriticalBlock b(crit);
  66. Owned<IJlibDateTime> _timeNow = createDateTimeNow();
  67. SCMStringBuffer _dateString;
  68. _timeNow->getDateString(_dateString);
  69. returnName.appendf("%u_%s",getRandom(),_dateString.str());
  70. return returnName;
  71. }
  72. StringBuffer& CLocalDataLogger::getFilePath(const char* fileName,StringBuffer& returnPath)
  73. {
  74. returnPath.appendf("%s/%s.HTML",m_logDirectory.str(),fileName);
  75. return returnPath;
  76. }
  77. StringBuffer& CLocalDataLogger::writeData(const StringBuffer& dataToCache,const StringBuffer& tokenName,StringBuffer& returnPath)
  78. {
  79. DBGLOG("CLocalDataLogger::writeData");
  80. StringBuffer tmpFile;
  81. getFilePath(tokenName.str(),tmpFile);
  82. Owned<IFile> file = createIFile(tmpFile.str());
  83. for (int i = 0; i < RETRIES; ++i)
  84. {
  85. try{
  86. Owned<IFileIO> io = file->open(IFOwrite);
  87. size32_t filesize = dataToCache.length();
  88. void* filedata = (void*)dataToCache.str();
  89. io->write(0,filesize ,filedata );
  90. if(m_UrlRoot.length()==0)
  91. returnPath.appendf("/Cache?Name=%s.HTML",tokenName.str());
  92. else
  93. returnPath.appendf("/%s/Cache?Name=%s.HTML",m_UrlRoot.str(),tokenName.str());
  94. break;
  95. }
  96. catch(IOSException *ose)
  97. {
  98. //The web site could be serving up the page as we try to update it...
  99. ose->Release();
  100. Sleep(10);
  101. }
  102. catch(...)
  103. {
  104. DBGLOG("Unknown exception thrown while reading local data logger file");
  105. }
  106. }
  107. return returnPath;
  108. }
  109. MemoryBuffer& CLocalDataLogger::readData(MemoryBuffer& dataCached,const char* cachedName)
  110. {
  111. DBGLOG("CLocalDataLogger::readData");
  112. StringBuffer fileName;
  113. if (strstr(cachedName,"files_") != 0)
  114. {
  115. const char* filestr = strstr(cachedName,"files_") + 7;
  116. fileName.append(filestr);
  117. }
  118. else
  119. fileName.append(cachedName);
  120. StringBuffer filepath;
  121. //need to keep it consistant with the files_ method of obtaining files.....
  122. filepath.appendf("%s/%s",m_logDirectory.str(),fileName.str());
  123. try{
  124. DBGLOG("about to create file reference");
  125. Owned<IFile> file = createIFile(filepath);
  126. if (file)
  127. {
  128. for (int i = 0; i < RETRIES; ++i)
  129. {
  130. DBGLOG("Trying to open for the %d time",i);
  131. try
  132. {
  133. DBGLOG("Trying to open %s",filepath.str());
  134. Owned<IFileIO> io = file->open(IFOread);
  135. if (io)
  136. {
  137. DBGLOG("Managed to open");
  138. offset_t filesize = io->size();
  139. size32_t memfilesize = (size32_t)filesize;
  140. //Check size isn't >= 2^32
  141. assertex(filesize == memfilesize);
  142. io->read(0, memfilesize, dataCached.reserveTruncate(memfilesize));
  143. DBGLOG("Managed to read");
  144. return dataCached;
  145. }
  146. }
  147. catch(IOSException *ose)
  148. {
  149. //The web site could be serving up the page as we try to update it...
  150. ose->Release();
  151. Sleep(10);
  152. }
  153. catch(...)
  154. {
  155. DBGLOG("Unknown exception thrown while reading local data logger file");
  156. }
  157. }
  158. }
  159. }
  160. catch(...)
  161. {
  162. DBGLOG("Unknown exception thrown while reading local data logger file2");
  163. }
  164. return dataCached;
  165. }