Skip to content

Commit

Permalink
Merge branch 'wms_filewatch'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed May 13, 2011
2 parents 6327b2e + 5579560 commit 3ec8059
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/mapserver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ SET ( qgis_mapserv_SRCS

SET (qgis_mapserv_MOC_HDRS
qgsftptransaction.h
qgscapabilitiescache.h
qgsconfigcache.h
)

SET (qgis_mapserv_RCCS
Expand Down
17 changes: 17 additions & 0 deletions src/mapserver/qgscapabilitiescache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
***************************************************************************/

#include "qgscapabilitiescache.h"
#include "qgsmapserverlogger.h"
#include <QCoreApplication>

QgsCapabilitiesCache::QgsCapabilitiesCache()
{
QObject::connect( &mFileSystemWatcher, SIGNAL( fileChanged( const QString& ) ), this, SLOT( removeChangedEntry( const QString& ) ) );
}

QgsCapabilitiesCache::~QgsCapabilitiesCache()
Expand All @@ -27,6 +30,7 @@ QgsCapabilitiesCache::~QgsCapabilitiesCache()

const QDomDocument* QgsCapabilitiesCache::searchCapabilitiesDocument( const QString& configFilePath ) const
{
QCoreApplication::processEvents(); //get updates from file system watcher
QHash< QString, QDomDocument >::const_iterator it = mCachedCapabilities.find( configFilePath );
if( it == mCachedCapabilities.constEnd() )
{
Expand All @@ -44,7 +48,20 @@ void QgsCapabilitiesCache::insertCapabilitiesDocument( const QString& configFile
{
//remove another cache entry to avoid memory problems
QHash<QString, QDomDocument>::iterator capIt = mCachedCapabilities.begin();
mFileSystemWatcher.removePath( capIt.key() );
mCachedCapabilities.erase( capIt );
}
mCachedCapabilities.insert( configFilePath, doc->cloneNode().toDocument() );
mFileSystemWatcher.addPath( configFilePath );
}

void QgsCapabilitiesCache::removeChangedEntry( const QString& path )
{
QgsMSDebugMsg( "Remove capabilities cache entry because file changed" );
QHash< QString, QDomDocument >::iterator it = mCachedCapabilities.find( path );
if( it != mCachedCapabilities.end() )
{
mCachedCapabilities.erase( it );
}
mFileSystemWatcher.removePath( path );
}
10 changes: 9 additions & 1 deletion src/mapserver/qgscapabilitiescache.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
#define QGSCAPABILITIESCACHE_H

#include <QDomDocument>
#include <QFileSystemWatcher>
#include <QHash>
#include <QObject>

/**A cache for capabilities xml documents (by configuration file path)*/
class QgsCapabilitiesCache
class QgsCapabilitiesCache: public QObject
{
Q_OBJECT
public:
QgsCapabilitiesCache();
~QgsCapabilitiesCache();
Expand All @@ -35,6 +38,11 @@ class QgsCapabilitiesCache

private:
QHash< QString, QDomDocument > mCachedCapabilities;
QFileSystemWatcher mFileSystemWatcher;

private slots:
/**Removes changed entry from this cache*/
void removeChangedEntry( const QString& path );
};

#endif // QGSCAPABILITIESCACHE_H
23 changes: 22 additions & 1 deletion src/mapserver/qgsconfigcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
#include "qgsmslayercache.h"
#include "qgsprojectparser.h"
#include "qgssldparser.h"
#include <QCoreApplication>

QgsConfigCache::QgsConfigCache()
{
QObject::connect( &mFileSystemWatcher, SIGNAL( fileChanged( const QString& ) ), this, SLOT( removeChangedEntry( const QString& ) ) );
}

QgsConfigCache::~QgsConfigCache()
Expand All @@ -36,6 +38,7 @@ QgsConfigCache::~QgsConfigCache()

QgsConfigParser* QgsConfigCache::searchConfiguration( const QString& filePath )
{
QCoreApplication::processEvents(); //check for updates from file system watcher
QgsConfigParser* p = 0;
QHash<QString, QgsConfigParser*>::const_iterator configIt = mCachedConfigurations.find( filePath );
if ( configIt == mCachedConfigurations.constEnd() )
Expand Down Expand Up @@ -63,7 +66,12 @@ QgsConfigParser* QgsConfigCache::insertConfiguration( const QString& filePath )
{
//remove a cache entry to avoid memory problems
QHash<QString, QgsConfigParser*>::iterator configIt = mCachedConfigurations.begin();
mCachedConfigurations.erase( configIt );
if ( configIt != mCachedConfigurations.end() )
{
mFileSystemWatcher.removePath( configIt.key() );
delete configIt.value();
mCachedConfigurations.erase( configIt );
}
}

//first open file
Expand Down Expand Up @@ -107,6 +115,19 @@ QgsConfigParser* QgsConfigCache::insertConfiguration( const QString& filePath )
}

mCachedConfigurations.insert( filePath, configParser );
mFileSystemWatcher.addPath( filePath );
delete configFile;
return configParser;
}

void QgsConfigCache::removeChangedEntry( const QString& path )
{
QgsMSDebugMsg( "Remove config cache entry because file changed" );
QHash<QString, QgsConfigParser*>::iterator configIt = mCachedConfigurations.find( path );
if ( configIt != mCachedConfigurations.end() )
{
delete configIt.value();
mCachedConfigurations.erase( configIt );
}
mFileSystemWatcher.removePath( path );
}
11 changes: 10 additions & 1 deletion src/mapserver/qgsconfigcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
#ifndef QGSCONFIGCACHE_H
#define QGSCONFIGCACHE_H

#include <QFileSystemWatcher>
#include <QHash>
#include <QObject>
#include <QString>

class QgsConfigParser;

/**A cache for configuration XML (useful because of the mapfile parameter)*/
class QgsConfigCache
class QgsConfigCache: public QObject
{
Q_OBJECT
public:
QgsConfigCache();
~QgsConfigCache();
Expand All @@ -40,6 +43,12 @@ class QgsConfigCache
QgsConfigParser* insertConfiguration( const QString& filePath );
/**Cached XML configuration documents. Key: file path, value: config parser. Default configuration has key '$default$'*/
QHash<QString, QgsConfigParser*> mCachedConfigurations;
/**Check for configuration file updates (remove entry from cache if file changes)*/
QFileSystemWatcher mFileSystemWatcher;

private slots:
/**Removes changed entry from this cache*/
void removeChangedEntry( const QString& path );
};

#endif // QGSCONFIGCACHE_H

0 comments on commit 3ec8059

Please sign in to comment.