PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/lllivefile.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 191 lines | 128 code | 29 blank | 34 comment | 9 complexity | 1dfc563251a97d6efdcae0728e674d8a MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lllivefile.cpp
  3. *
  4. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #include "linden_common.h"
  26. #include "lllivefile.h"
  27. #include "llframetimer.h"
  28. #include "lleventtimer.h"
  29. const F32 DEFAULT_CONFIG_FILE_REFRESH = 5.0f;
  30. class LLLiveFile::Impl
  31. {
  32. public:
  33. Impl(const std::string& filename, const F32 refresh_period);
  34. ~Impl();
  35. bool check();
  36. void changed();
  37. bool mForceCheck;
  38. F32 mRefreshPeriod;
  39. LLFrameTimer mRefreshTimer;
  40. std::string mFilename;
  41. time_t mLastModTime;
  42. time_t mLastStatTime;
  43. bool mLastExists;
  44. LLEventTimer* mEventTimer;
  45. };
  46. LLLiveFile::Impl::Impl(const std::string& filename, const F32 refresh_period)
  47. :
  48. mForceCheck(true),
  49. mRefreshPeriod(refresh_period),
  50. mFilename(filename),
  51. mLastModTime(0),
  52. mLastStatTime(0),
  53. mLastExists(false),
  54. mEventTimer(NULL)
  55. {
  56. }
  57. LLLiveFile::Impl::~Impl()
  58. {
  59. delete mEventTimer;
  60. }
  61. LLLiveFile::LLLiveFile(const std::string& filename, const F32 refresh_period)
  62. : impl(* new Impl(filename, refresh_period))
  63. {
  64. }
  65. LLLiveFile::~LLLiveFile()
  66. {
  67. delete &impl;
  68. }
  69. bool LLLiveFile::Impl::check()
  70. {
  71. if (!mForceCheck && mRefreshTimer.getElapsedTimeF32() < mRefreshPeriod)
  72. {
  73. // Skip the check if not enough time has elapsed and we're not
  74. // forcing a check of the file
  75. return false;
  76. }
  77. mForceCheck = false;
  78. mRefreshTimer.reset();
  79. // Stat the file to see if it exists and when it was last modified.
  80. llstat stat_data;
  81. int res = LLFile::stat(mFilename, &stat_data);
  82. if (res)
  83. {
  84. // Couldn't stat the file, that means it doesn't exist or is
  85. // broken somehow. Clear flags and return.
  86. if (mLastExists)
  87. {
  88. mLastExists = false;
  89. return true; // no longer existing is a change!
  90. }
  91. return false;
  92. }
  93. // The file exists, decide if we want to load it.
  94. if (mLastExists)
  95. {
  96. // The file existed last time, don't read it if it hasn't changed since
  97. // last time.
  98. if (stat_data.st_mtime <= mLastModTime)
  99. {
  100. return false;
  101. }
  102. }
  103. // We want to read the file. Update status info for the file.
  104. mLastExists = true;
  105. mLastStatTime = stat_data.st_mtime;
  106. return true;
  107. }
  108. void LLLiveFile::Impl::changed()
  109. {
  110. // we wanted to read this file, and we were successful.
  111. mLastModTime = mLastStatTime;
  112. }
  113. bool LLLiveFile::checkAndReload()
  114. {
  115. bool changed = impl.check();
  116. if (changed)
  117. {
  118. if(loadFile())
  119. {
  120. impl.changed();
  121. this->changed();
  122. }
  123. else
  124. {
  125. changed = false;
  126. }
  127. }
  128. return changed;
  129. }
  130. std::string LLLiveFile::filename() const
  131. {
  132. return impl.mFilename;
  133. }
  134. namespace
  135. {
  136. class LiveFileEventTimer : public LLEventTimer
  137. {
  138. public:
  139. LiveFileEventTimer(LLLiveFile& f, F32 refresh)
  140. : LLEventTimer(refresh), mLiveFile(f)
  141. { }
  142. BOOL tick()
  143. {
  144. mLiveFile.checkAndReload();
  145. return FALSE;
  146. }
  147. private:
  148. LLLiveFile& mLiveFile;
  149. };
  150. }
  151. void LLLiveFile::addToEventTimer()
  152. {
  153. impl.mEventTimer = new LiveFileEventTimer(*this, impl.mRefreshPeriod);
  154. }
  155. void LLLiveFile::setRefreshPeriod(F32 seconds)
  156. {
  157. if (seconds < 0.f)
  158. {
  159. seconds = -seconds;
  160. }
  161. impl.mRefreshPeriod = seconds;
  162. }