2008-01-25 01:36:40 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/pqi: p3cfgmgr.cc
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2007-2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-03-29 09:58:28 -04:00
|
|
|
#include "util/rsdir.h"
|
2009-03-20 19:37:06 -04:00
|
|
|
#include "rsiface/rspeers.h"
|
2008-01-25 01:36:40 -05:00
|
|
|
#include "pqi/p3cfgmgr.h"
|
2008-02-08 07:39:40 -05:00
|
|
|
#include "pqi/p3authmgr.h"
|
2008-01-25 01:36:40 -05:00
|
|
|
#include "pqi/pqibin.h"
|
2009-05-08 20:19:54 -04:00
|
|
|
#include "pqi/pqistore.h"
|
2009-03-26 09:24:27 -04:00
|
|
|
#include "pqi/pqinotify.h"
|
|
|
|
#include <errno.h>
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
#include "serialiser/rsconfigitems.h"
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-07-09 05:55:09 -04:00
|
|
|
/****
|
|
|
|
* #define CONFIG_DEBUG 1
|
|
|
|
***/
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-08 07:39:40 -05:00
|
|
|
p3ConfigMgr::p3ConfigMgr(p3AuthMgr *am, std::string dir, std::string fname, std::string signame)
|
|
|
|
:mAuthMgr(am), basedir(dir), metafname(fname), metasigfname(signame),
|
2008-02-07 11:18:34 -05:00
|
|
|
mConfigSaveActive(true)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3ConfigMgr::tick()
|
|
|
|
{
|
|
|
|
bool toSave = false;
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* iterate through and check if any have changed */
|
|
|
|
std::map<uint32_t, pqiConfig *>::iterator it;
|
|
|
|
for(it = configs.begin(); it != configs.end(); it++)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
if (it->second->HasConfigChanged(0))
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::tick() Config Changed - Element: ";
|
|
|
|
std::cerr << it->first;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
toSave = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
/* disable saving before exit */
|
|
|
|
if (!mConfigSaveActive)
|
|
|
|
{
|
|
|
|
toSave = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
if (toSave)
|
|
|
|
{
|
|
|
|
saveConfiguration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void p3ConfigMgr::saveConfiguration()
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::saveConfiguration()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RsConfigKeyValueSet *item = new RsConfigKeyValueSet();
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
std::map<uint32_t, pqiConfig *>::iterator it;
|
|
|
|
for(it = configs.begin(); it != configs.end(); it++)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
if (it->second->HasConfigChanged(1))
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::saveConfiguration() Saving Element: ";
|
|
|
|
std::cerr << it->first;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
it->second->saveConfiguration();
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
/* save metaconfig */
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::saveConfiguration() Element: ";
|
|
|
|
std::cerr << it->first << " Hash: " << it->second->Hash();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-02-08 07:39:40 -05:00
|
|
|
if (it->second->Hash() == "")
|
|
|
|
{
|
|
|
|
/* skip if no hash */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
RsTlvKeyValue kv;
|
|
|
|
{
|
|
|
|
std::ostringstream out;
|
|
|
|
out << it->first;
|
|
|
|
kv.key = out.str();
|
|
|
|
}
|
|
|
|
kv.value = it->second->Hash();
|
|
|
|
item->tlvkvs.pairs.push_back(kv);
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::saveConfiguration() Complete MetaConfigItem: ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
item->print(std::cerr, 20);
|
|
|
|
|
|
|
|
#endif
|
2008-03-23 15:11:36 -04:00
|
|
|
/* construct filename */
|
|
|
|
std::string filename1 = basedir;
|
|
|
|
std::string filename2 = basedir;
|
|
|
|
if (basedir != "")
|
|
|
|
{
|
|
|
|
filename1 += "/";
|
|
|
|
filename2 += "/";
|
|
|
|
}
|
|
|
|
filename1 += metasigfname;
|
|
|
|
filename2 += metafname;
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
/* Write the data to a stream */
|
2008-02-08 07:39:40 -05:00
|
|
|
uint32_t bioflags = BIN_FLAGS_WRITEABLE;
|
|
|
|
BinMemInterface *membio = new BinMemInterface(1000, bioflags);
|
2008-02-07 11:18:34 -05:00
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
rss->addSerialType(new RsGeneralConfigSerialiser());
|
2009-05-13 15:54:33 -04:00
|
|
|
pqistore store(rss, "CONFIG", membio, BIN_FLAGS_WRITEABLE);
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2009-05-12 17:55:50 -04:00
|
|
|
store.SendItem(item);
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2008-02-08 07:39:40 -05:00
|
|
|
/* sign data */
|
|
|
|
std::string signature;
|
|
|
|
mAuthMgr->SignData(membio->memptr(), membio->memsize(), signature);
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2008-02-08 07:39:40 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::saveConfiguration() MetaFile Signature:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << signature;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-03-23 15:11:36 -04:00
|
|
|
if (!membio->writetofile(filename2.c_str()))
|
2008-02-08 07:39:40 -05:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG
|
2009-05-13 15:54:33 -04:00
|
|
|
std::cerr << "p3ConfigMgr::saveConfiguration() Failed to Write MetaFile " << filename2 ;
|
2008-02-08 07:39:40 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* write signature to configuration */
|
2008-02-08 07:39:40 -05:00
|
|
|
BinMemInterface *signbio = new BinMemInterface(signature.c_str(),
|
|
|
|
signature.length(), BIN_FLAGS_READABLE);
|
|
|
|
|
2008-03-23 15:11:36 -04:00
|
|
|
if (!signbio->writetofile(filename1.c_str()))
|
2008-02-08 07:39:40 -05:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG
|
2009-05-13 15:54:33 -04:00
|
|
|
std::cerr << "p3ConfigMgr::saveConfiguration() Failed to Write MetaSignFile" << filename1 ;
|
2008-02-08 07:39:40 -05:00
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2008-04-02 09:55:45 -04:00
|
|
|
delete signbio;
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void p3ConfigMgr::loadConfiguration()
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-03-23 15:11:36 -04:00
|
|
|
/* construct filename */
|
|
|
|
std::string filename1 = basedir;
|
|
|
|
std::string filename2 = basedir;
|
|
|
|
if (basedir != "")
|
|
|
|
{
|
|
|
|
filename1 += "/";
|
|
|
|
filename2 += "/";
|
|
|
|
}
|
|
|
|
filename1 += metasigfname;
|
|
|
|
filename2 += metafname;
|
|
|
|
|
2008-02-08 07:39:40 -05:00
|
|
|
/* write signature to configuration */
|
|
|
|
BinMemInterface *signbio = new BinMemInterface(1000, BIN_FLAGS_READABLE);
|
|
|
|
|
2008-03-23 15:11:36 -04:00
|
|
|
if (!signbio->readfromfile(filename1.c_str()))
|
2008-02-08 07:39:40 -05:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() Failed to Load MetaSignFile";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-03-23 15:11:36 -04:00
|
|
|
|
|
|
|
/* HACK to load the old one (with the wrong directory)
|
|
|
|
* THIS SHOULD BE REMOVED IN A COUPLE OF VERSIONS....
|
|
|
|
* ONLY HERE TO CORRECT BAD MISTAKE IN EARLIER VERSIONS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
filename1 = metasigfname;
|
|
|
|
filename2 = metafname;
|
|
|
|
|
|
|
|
if (!signbio->readfromfile(filename1.c_str()))
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() HACK: Failed to Load ALT MetaSignFile";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() HACK: Loaded ALT MetaSignFile";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-02-08 07:39:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string oldsignature((char *) signbio->memptr(), signbio->memsize());
|
|
|
|
delete signbio;
|
|
|
|
|
|
|
|
BinMemInterface *membio = new BinMemInterface(1000, BIN_FLAGS_READABLE);
|
|
|
|
|
2008-03-23 15:11:36 -04:00
|
|
|
if (!membio->readfromfile(filename2.c_str()))
|
2008-02-08 07:39:40 -05:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() Failed to Load MetaFile";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2009-05-13 15:54:33 -04:00
|
|
|
// delete membio;
|
|
|
|
// return ;
|
2008-02-08 07:39:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get signature */
|
|
|
|
std::string signature;
|
|
|
|
mAuthMgr->SignData(membio->memptr(), membio->memsize(), signature);
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() New MetaFile Signature:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << signature;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() Orig MetaFile Signature:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << oldsignature;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (signature != oldsignature)
|
|
|
|
{
|
|
|
|
/* Failed */
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() Signature Check Failed";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
membio->fseek(0); /* go to start */
|
2008-02-07 11:18:34 -05:00
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
rss->addSerialType(new RsGeneralConfigSerialiser());
|
2009-05-12 17:55:50 -04:00
|
|
|
pqistore stream(rss, "CONFIG", membio, 0);
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
RsItem *rsitem = stream.GetItem();
|
|
|
|
|
|
|
|
RsConfigKeyValueSet *item = dynamic_cast<RsConfigKeyValueSet *>(rsitem);
|
|
|
|
if (!item)
|
|
|
|
{
|
|
|
|
delete rsitem;
|
|
|
|
return;
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() Loaded MetaConfigItem: ";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
item->print(std::cerr, 20);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* extract info from KeyValueSet */
|
|
|
|
std::list<RsTlvKeyValue>::iterator it;
|
|
|
|
for(it = item->tlvkvs.pairs.begin(); it != item->tlvkvs.pairs.end(); it++)
|
|
|
|
{
|
|
|
|
/* find the configuration */
|
|
|
|
uint32_t confId = atoi(it->key.c_str());
|
|
|
|
std::string hashin = it->value;
|
|
|
|
|
2008-06-16 16:37:48 -04:00
|
|
|
/*********************** HACK TO CHANGE CACHE CONFIG ID *********
|
|
|
|
* REMOVE IN A MONTH OR TWO
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (confId == CONFIG_TYPE_CACHE_OLDID)
|
|
|
|
{
|
|
|
|
confId = CONFIG_TYPE_CACHE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************** HACK TO CHANGE CACHE CONFIG ID *********/
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
std::map<uint32_t, pqiConfig *>::iterator cit;
|
|
|
|
cit = configs.find(confId);
|
|
|
|
if (cit != configs.end())
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() Element: ";
|
|
|
|
std::cerr << confId << " Hash: " << hashin;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
(cit->second)->loadConfiguration(hashin);
|
|
|
|
/* force config to NOT CHANGED */
|
|
|
|
cit->second->HasConfigChanged(0);
|
|
|
|
cit->second->HasConfigChanged(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-02 09:55:45 -04:00
|
|
|
delete item;
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3ConfigMgr::loadConfiguration() Done!";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
void p3ConfigMgr::addConfiguration(std::string file, pqiConfig *conf)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
|
|
|
|
/* construct filename */
|
|
|
|
std::string filename = basedir;
|
|
|
|
if (basedir != "")
|
|
|
|
{
|
|
|
|
filename += "/";
|
|
|
|
}
|
|
|
|
filename += file;
|
|
|
|
|
|
|
|
conf->setFilename(filename);
|
|
|
|
configs[conf->Type()] = conf;
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
void p3ConfigMgr::completeConfiguration()
|
|
|
|
{
|
|
|
|
saveConfiguration();
|
|
|
|
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
mConfigSaveActive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
p3Config::p3Config(uint32_t t)
|
|
|
|
:pqiConfig(t)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
bool p3Config::loadConfiguration(std::string &loadHash)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
std::list<RsItem *> load;
|
|
|
|
std::list<RsItem *>::iterator it;
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
std::string fname = Filename();
|
|
|
|
|
|
|
|
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
|
|
|
|
uint32_t stream_flags = BIN_FLAGS_READABLE;
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
BinInterface *bio = new BinFileInterface(fname.c_str(), bioflags);
|
2009-05-12 17:55:50 -04:00
|
|
|
pqistore stream(setupSerialiser(), "CONFIG", bio, stream_flags);
|
2008-01-25 01:36:40 -05:00
|
|
|
RsItem *item = NULL;
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
while(NULL != (item = stream.GetItem()))
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3Config::loadConfiguration() loaded item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
item->print(std::cerr, 0);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-01-25 01:36:40 -05:00
|
|
|
load.push_back(item);
|
|
|
|
}
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3Config::loadConfiguration() loaded " << load.size();
|
|
|
|
std::cerr << " Elements from File: " << fname;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* check hash */
|
2008-02-07 11:18:34 -05:00
|
|
|
std::string hashstr = bio->gethash();
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
if (hashstr != loadHash)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3Config::loadConfiguration() ERROR: Hash != MATCHloaded";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* bad load */
|
|
|
|
for(it = load.begin(); it != load.end(); it++)
|
|
|
|
{
|
|
|
|
delete (*it);
|
|
|
|
}
|
2008-02-08 07:39:40 -05:00
|
|
|
|
|
|
|
setHash("");
|
2008-01-25 01:36:40 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHash(hashstr);
|
|
|
|
|
|
|
|
/* else okay */
|
|
|
|
return loadList(load);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
bool p3Config::saveConfiguration()
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
bool cleanup = true;
|
|
|
|
std::list<RsItem *> toSave = saveList(cleanup);
|
|
|
|
|
|
|
|
std::string fname = Filename();
|
2009-03-20 19:37:06 -04:00
|
|
|
std::string fnametmp = Filename()+".tmp";
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2009-03-20 19:37:06 -04:00
|
|
|
std::cerr << "Writting p3config file " << fname.c_str() << std::endl ;
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3Config::saveConfiguration() toSave " << toSave.size();
|
|
|
|
std::cerr << " Elements to File: " << fname;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
|
|
|
|
uint32_t stream_flags = BIN_FLAGS_WRITEABLE;
|
2009-03-20 19:37:06 -04:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
if (!cleanup)
|
|
|
|
stream_flags |= BIN_FLAGS_NO_DELETE;
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2009-03-20 19:37:06 -04:00
|
|
|
BinInterface *bio = new BinFileInterface(fnametmp.c_str(), bioflags);
|
2009-05-12 17:55:50 -04:00
|
|
|
pqistore *stream = new pqistore(setupSerialiser(), "CONFIG", bio, stream_flags);
|
2009-03-20 19:37:06 -04:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
std::list<RsItem *>::iterator it;
|
2009-03-20 19:37:06 -04:00
|
|
|
|
|
|
|
bool written = true ;
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
for(it = toSave.begin(); it != toSave.end(); it++)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3Config::saveConfiguration() save item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
(*it)->print(std::cerr, 0);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2009-03-20 19:37:06 -04:00
|
|
|
written = written && stream->SendItem(*it);
|
|
|
|
|
|
|
|
// std::cerr << "written = " << written << std::endl ;
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* store the hash */
|
2008-02-07 11:18:34 -05:00
|
|
|
setHash(bio->gethash());
|
|
|
|
saveDone(); /* callback to inherited class to unlock any Mutexes
|
|
|
|
* protecting saveList() data
|
|
|
|
*/
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2009-03-20 19:37:06 -04:00
|
|
|
delete stream ;
|
|
|
|
|
|
|
|
if(!written)
|
|
|
|
return false ;
|
|
|
|
|
|
|
|
std::cerr << "renaming " << fnametmp.c_str() << " to " << fname.c_str() << std::endl ;
|
|
|
|
|
2009-03-29 09:58:28 -04:00
|
|
|
if(!RsDirUtil::renameFile(fnametmp,fname))
|
2009-03-26 09:24:27 -04:00
|
|
|
{
|
|
|
|
std::ostringstream errlog;
|
|
|
|
#ifdef WIN32
|
2009-03-29 09:58:28 -04:00
|
|
|
errlog << "Error " << GetLastError() ;
|
2009-03-26 09:24:27 -04:00
|
|
|
#else
|
2009-03-29 09:58:28 -04:00
|
|
|
errlog << "Error " << errno ;
|
2009-03-26 09:24:27 -04:00
|
|
|
#endif
|
|
|
|
getPqiNotify()->AddSysMessage(0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + fname + ": got error "+errlog.str());
|
2009-03-20 19:37:06 -04:00
|
|
|
return false ;
|
2009-03-26 09:24:27 -04:00
|
|
|
}
|
2009-03-20 19:37:06 -04:00
|
|
|
|
|
|
|
std::cerr << "Successfully wrote p3config file " << fname.c_str() << std::endl ;
|
2008-01-25 01:36:40 -05:00
|
|
|
/* else okay */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**************************** CONFIGURATION CLASSES ********************/
|
|
|
|
|
|
|
|
p3GeneralConfig::p3GeneralConfig()
|
2008-02-07 11:18:34 -05:00
|
|
|
:p3Config(CONFIG_TYPE_GENERAL)
|
2008-01-25 01:36:40 -05:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// General Configuration System
|
|
|
|
std::string p3GeneralConfig::getSetting(std::string opt)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3GeneralConfig::getSetting(" << opt << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* extract from config */
|
|
|
|
std::map<std::string, std::string>::iterator it;
|
|
|
|
if (settings.end() == (it = settings.find(opt)))
|
|
|
|
{
|
|
|
|
std::string nullstring;
|
|
|
|
return nullstring;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3GeneralConfig::setSetting(std::string opt, std::string val)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3GeneralConfig::setSetting(" << opt << " = " << val << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* extract from config */
|
|
|
|
std::map<std::string, std::string>::iterator it;
|
|
|
|
if (settings.end() != (it = settings.find(opt)))
|
|
|
|
{
|
|
|
|
if (it->second == val)
|
|
|
|
{
|
|
|
|
/* no change */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings[opt] = val;
|
2008-02-07 11:18:34 -05:00
|
|
|
}
|
|
|
|
/* outside mutex */
|
|
|
|
IndicateConfigChanged();
|
2008-01-25 01:36:40 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsSerialiser *p3GeneralConfig::setupSerialiser()
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
rss->addSerialType(new RsGeneralConfigSerialiser());
|
2008-01-25 01:36:40 -05:00
|
|
|
return rss;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<RsItem *> p3GeneralConfig::saveList(bool &cleanup)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3GeneralConfig::saveList() KV sets: " << settings.size();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cleanup = true;
|
2008-01-25 01:36:40 -05:00
|
|
|
std::list<RsItem *> savelist;
|
2008-02-07 11:18:34 -05:00
|
|
|
|
|
|
|
RsConfigKeyValueSet *item = new RsConfigKeyValueSet();
|
2008-01-25 01:36:40 -05:00
|
|
|
std::map<std::string, std::string>::iterator it;
|
|
|
|
for(it = settings.begin(); it != settings.end(); it++)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
RsTlvKeyValue kv;
|
|
|
|
kv.key = it->first;
|
|
|
|
kv.value = it->second;
|
|
|
|
item->tlvkvs.pairs.push_back(kv);
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
/* make sure we don't overload it */
|
|
|
|
if (item->tlvkvs.TlvSize() > 4000)
|
|
|
|
{
|
|
|
|
savelist.push_back(item);
|
|
|
|
item = new RsConfigKeyValueSet();
|
|
|
|
}
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
if (item->tlvkvs.pairs.size() > 0)
|
|
|
|
{
|
|
|
|
savelist.push_back(item);
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
2008-02-07 11:18:34 -05:00
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
return savelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3GeneralConfig::loadList(std::list<RsItem *> load)
|
|
|
|
{
|
2008-02-07 11:18:34 -05:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
std::cerr << "p3GeneralConfig::loadList() count: " << load.size();
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
|
2008-01-25 01:36:40 -05:00
|
|
|
/* add into settings */
|
2008-02-07 11:18:34 -05:00
|
|
|
RsConfigKeyValueSet *item = NULL;
|
|
|
|
std::list<RsItem *>::iterator it;
|
|
|
|
std::list<RsTlvKeyValue>::iterator kit;
|
|
|
|
|
|
|
|
for(it = load.begin(); it != load.end();)
|
|
|
|
{
|
|
|
|
item = dynamic_cast<RsConfigKeyValueSet *>(*it);
|
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
for(kit = item->tlvkvs.pairs.begin();
|
|
|
|
kit != item->tlvkvs.pairs.end(); kit++)
|
|
|
|
{
|
|
|
|
settings[kit->key] = kit->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
delete (*it);
|
|
|
|
it = load.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**** MUTEX NOTE:
|
|
|
|
* have protected all, but think that
|
|
|
|
* only the Indication and hash really need it
|
|
|
|
*/
|
|
|
|
|
|
|
|
pqiConfig::pqiConfig(uint32_t t)
|
|
|
|
:ConfInd(2), type(t)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pqiConfig::~pqiConfig()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t pqiConfig::Type()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string pqiConfig::Filename()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string pqiConfig::Hash()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pqiConfig::IndicateConfigChanged()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
ConfInd.IndicateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pqiConfig::HasConfigChanged(uint16_t idx)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
return ConfInd.Changed(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pqiConfig::setFilename(std::string name)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
filename = name;
|
|
|
|
}
|
2008-01-25 01:36:40 -05:00
|
|
|
|
2008-02-07 11:18:34 -05:00
|
|
|
void pqiConfig::setHash(std::string h)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
|
|
|
|
hash = h;
|
2008-01-25 01:36:40 -05:00
|
|
|
}
|
|
|
|
|