PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms 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
  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
 26#include "linden_common.h"
 27
 28#include "lllivefile.h"
 29#include "llframetimer.h"
 30#include "lleventtimer.h"
 31
 32const F32 DEFAULT_CONFIG_FILE_REFRESH = 5.0f;
 33
 34
 35class LLLiveFile::Impl
 36{
 37public:
 38	Impl(const std::string& filename, const F32 refresh_period);
 39	~Impl();
 40	
 41	bool check();
 42	void changed();
 43	
 44	bool mForceCheck;
 45	F32 mRefreshPeriod;
 46	LLFrameTimer mRefreshTimer;
 47
 48	std::string mFilename;
 49	time_t mLastModTime;
 50	time_t mLastStatTime;
 51	bool mLastExists;
 52	
 53	LLEventTimer* mEventTimer;
 54};
 55
 56LLLiveFile::Impl::Impl(const std::string& filename, const F32 refresh_period)
 57	:
 58	mForceCheck(true),
 59	mRefreshPeriod(refresh_period),
 60	mFilename(filename),
 61	mLastModTime(0),
 62	mLastStatTime(0),
 63	mLastExists(false),
 64	mEventTimer(NULL)
 65{
 66}
 67
 68LLLiveFile::Impl::~Impl()
 69{
 70	delete mEventTimer;
 71}
 72
 73LLLiveFile::LLLiveFile(const std::string& filename, const F32 refresh_period)
 74	: impl(* new Impl(filename, refresh_period))
 75{
 76}
 77
 78LLLiveFile::~LLLiveFile()
 79{
 80	delete &impl;
 81}
 82
 83
 84bool LLLiveFile::Impl::check()
 85{
 86	if (!mForceCheck && mRefreshTimer.getElapsedTimeF32() < mRefreshPeriod)
 87	{
 88		// Skip the check if not enough time has elapsed and we're not
 89		// forcing a check of the file
 90		return false;
 91	}
 92	mForceCheck = false;
 93	mRefreshTimer.reset();
 94
 95	// Stat the file to see if it exists and when it was last modified.
 96	llstat stat_data;
 97	int res = LLFile::stat(mFilename, &stat_data);
 98
 99	if (res)
100	{
101		// Couldn't stat the file, that means it doesn't exist or is
102		// broken somehow.  Clear flags and return.
103		if (mLastExists)
104		{
105			mLastExists = false;
106			return true;	// no longer existing is a change!
107		}
108		return false;
109	}
110
111	// The file exists, decide if we want to load it.
112	if (mLastExists)
113	{
114		// The file existed last time, don't read it if it hasn't changed since
115		// last time.
116		if (stat_data.st_mtime <= mLastModTime)
117		{
118			return false;
119		}
120	}
121
122	// We want to read the file.  Update status info for the file.
123	mLastExists = true;
124	mLastStatTime = stat_data.st_mtime;
125	return true;
126}
127
128void LLLiveFile::Impl::changed()
129{
130	// we wanted to read this file, and we were successful.
131	mLastModTime = mLastStatTime;
132}
133
134bool LLLiveFile::checkAndReload()
135{
136	bool changed = impl.check();
137	if (changed)
138	{
139		if(loadFile())
140		{
141			impl.changed();
142			this->changed();
143		}
144		else
145		{
146			changed = false;
147		}
148	}
149	return changed;
150}
151
152std::string LLLiveFile::filename() const
153{
154	return impl.mFilename;
155}
156
157namespace
158{
159	class LiveFileEventTimer : public LLEventTimer
160	{
161	public:
162		LiveFileEventTimer(LLLiveFile& f, F32 refresh)
163			: LLEventTimer(refresh), mLiveFile(f)
164			{ }
165			
166		BOOL tick()
167		{ 
168			mLiveFile.checkAndReload(); 
169			return FALSE;
170		}
171	
172	private:
173		LLLiveFile& mLiveFile;
174	};
175	
176}
177
178void LLLiveFile::addToEventTimer()
179{
180	impl.mEventTimer = new LiveFileEventTimer(*this, impl.mRefreshPeriod);
181}
182
183void LLLiveFile::setRefreshPeriod(F32 seconds)
184{
185	if (seconds < 0.f)
186	{
187		seconds = -seconds;
188	}
189	impl.mRefreshPeriod = seconds;
190}
191