/*
*
* ProLinga-Repository
*
* Copyright (C) 2002-2008 Xobas Software.
* All rights reserved.
*
* This file is part of ProLinga-Repository.
*
* ProLinga-Repository is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ProLinga-Repository 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ProLinga-Repository. If not, see .
*
* More information is available at the following addresses:
*
* Website : http://www.prolinga.org
*
* Email : prolinga-list@prolinga.org
*
*
*/
#include "RepCommon.h"
#include
#include
#include
#include "RepConfig.hpp"
#include "RepMain.hpp"
ConfigRep::ConfigRep(const char *configFileName)
{
xmlDocPtr docConf;
xmlNodePtr curConf;
xmlXPathContextPtr ctxConf;
xmlXPathObjectPtr resConf;
char productPath[255], configValue[255];
/* Init */
startPtr = new ConfigRepSetting;
/* Parsing file */
docConf = xmlParseFile(configFileName);
if (docConf == NULL)
{
fprintf(stderr, "Error parsing config file. \n");
xmlFreeDoc(docConf);
return;
}
curConf = xmlDocGetRootElement(docConf);
if (curConf == NULL)
{
fprintf(stderr, "Empty document. \n");
xmlFreeDoc(docConf);
return;
}
if (xmlStrcmp(curConf->name, (const xmlChar *)"ProLinga"))
{
fprintf(stderr, "Invalid Config File");
xmlFreeDoc(docConf);
return;
}
/* Init Xpath */
xmlXPathInit();
/* Create XPath environment */
ctxConf = xmlXPathNewContext(docConf);
/* Create config path Client */
strcpy(productPath, "/ProLinga/Configuration/Service[@Name='Repository']/Client/");
/* Get Client host name/ip */
strcpy(configValue, productPath);
strcat(configValue, "ClientHost");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "ClientHost", (char *)xmlXPathCastToString(resConf));
/* Get Client port number */
strcpy(configValue, productPath);
strcat(configValue, "ClientPort");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "ClientPort", (int)xmlXPathCastToNumber(resConf));
/* Get Client Compression Level */
strcpy(configValue, productPath);
strcat(configValue, "ClientCompressionLevel");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "ClientCompressionLevel", (int)xmlXPathCastToNumber(resConf));
/* Create config path Server */
strcpy(productPath, "/ProLinga/Configuration/Service[@Name='Repository']/Server/");
/* Get Service Id */
strcpy(configValue, productPath);
strcat(configValue, "ServiceId");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "ServiceId", (char *)xmlXPathCastToString(resConf));
/* Get Server host name/ip */
strcpy(configValue, productPath);
strcat(configValue, "ServerHost");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "ServerHost", (char *)xmlXPathCastToString(resConf));
/* Get Server port number */
strcpy(configValue, productPath);
strcat(configValue, "ServerPort");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "ServerPort", (int)xmlXPathCastToNumber(resConf));
/* Get System Path */
strcpy(configValue, productPath);
strcat(configValue, "SystemPath");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "SystemPath", (char *)xmlXPathCastToString(resConf));
/* Number Of Threads */
strcpy(configValue, productPath);
strcat(configValue, "NumberOfThreads");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "NumberOfThreads", (int)xmlXPathCastToNumber(resConf));
/* Get Log file */
strcpy(configValue, productPath);
strcat(configValue, "LogFile");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "LogFile", (char *)xmlXPathCastToString(resConf));
/* Get Log Level */
strcpy(configValue, productPath);
strcat(configValue, "LogLevel");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "LogLevel", (int)xmlXPathCastToNumber(resConf));
/* Get Server Compression Level */
strcpy(configValue, productPath);
strcat(configValue, "ServerCompressionLevel");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "ServerCompressionLevel", (int)xmlXPathCastToNumber(resConf));
/* Clean up */
xmlXPathFreeContext(ctxConf);
xmlFreeDoc(docConf);
}
ConfigRep::~ConfigRep()
{
if (startPtr != NULL)
startPtr->deleteList(&startPtr);
}
char *ConfigRep::getStringValue(const char *name, const char *defValue)
{
return startPtr->getStringValue(startPtr, name, defValue);
}
int ConfigRep::getNumberValue(const char *name, const int defValue)
{
return startPtr->getNumberValue(startPtr, name, defValue);
}
void ConfigRep::putStringValue(const char *name, const char *value)
{
startPtr->putStringValue(&startPtr, name, value);
}
void ConfigRep::putNumberValue(const char *name, const int value)
{
startPtr->putNumberValue(&startPtr, name, value);
}
void ConfigRep::writeConfig(const char *logFileName) const
{
FILE *log;
time_t t;
time(&t);
/* Open log file */
log = fopen(startPtr->getStringValue(startPtr, "LogFile", DEF_LOG_FILE), "a");
/* Write settings */
fprintf(log,"-----------------------------------------------------------------\n");
fprintf(log, "Configuration ProLinga-Repository Server - %s \n", ctime(&t));
fprintf(log, "Log File : %s\n", startPtr->getStringValue(startPtr, "LogFile", DEF_LOG_FILE));
fprintf(log, "Log Level : %d\n", startPtr->getNumberValue(startPtr, "LogLevel", DEF_LOG_LEVEL));
fprintf(log, "Server Host : %s\n", startPtr->getStringValue(startPtr, "ServerHost", DEF_SERVER_HOST));
fprintf(log, "Server Port : %d\n", startPtr->getNumberValue(startPtr, "ServerPort", DEF_SERVER_PORT));
fprintf(log, "Server Compression Level : %d\n", startPtr->getNumberValue(startPtr, "ServerCompressionLevel", DEF_SERVER_COMP_LEVEL));
fprintf(log, "Number of Threads : %d\n", startPtr->getNumberValue(startPtr, "NumberOfThreads", DEF_THREADS));
fprintf(log, "System Path : %s\n", startPtr->getStringValue(startPtr, "SystemPath", DEF_SYSTEM_PATH));
fprintf(log,"-----------------------------------------------------------------\n");
/*Close log file */
fclose(log);
}
void ConfigRep::printList(void) const
{
startPtr->printList(startPtr);
}
ConfigRepSetting::ConfigRepSetting()
{
strcpy(confsetName, "");
confsetStringValue = NULL;
confsetNumberValue = 0;
nextPtr = NULL;
}
ConfigRepSetting::~ConfigRepSetting()
{
/* Nothing ? */
}
char *ConfigRepSetting::getStringValue(ConfigRepSettingPtr confsetPtr, const char *name, const char *defValue)
{
ConfigRepSettingPtr curPtr;
/* Get value from memory */
curPtr = confsetPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->confsetName, name) == 0)
return curPtr->confsetStringValue;
curPtr = curPtr->nextPtr;
}
return (char *)defValue;
}
int ConfigRepSetting::getNumberValue(ConfigRepSettingPtr confsetPtr, const char *name, const int defValue)
{
ConfigRepSettingPtr curPtr;
/* Get value from memory */
curPtr = confsetPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->confsetName, name) == 0)
return curPtr->confsetNumberValue;
curPtr = curPtr->nextPtr;
}
return defValue;
}
void ConfigRepSetting::putStringValue(ConfigRepSettingPtr *confsetPtr, const char *name, const char *value)
{
ConfigRepSettingPtr curPtr, newPtr;
/* First check if already in list (update) */
curPtr = *confsetPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->confsetName, name) == 0)
{
if (curPtr->confsetStringValue != NULL)
delete curPtr->confsetStringValue;
curPtr->confsetStringValue = new char[(strlen(value)+1)];
strcpy(curPtr->confsetStringValue, value);
return;
}
curPtr = curPtr->nextPtr;
}
/* Create new Config instance */
newPtr = new ConfigRepSetting;
strcpy(newPtr->confsetName, name);
newPtr->confsetStringValue = new char[(strlen(value)+1)];
strcpy(newPtr->confsetStringValue, value);
/* Prepend in list */
newPtr->nextPtr = *confsetPtr;
*confsetPtr = newPtr;
}
void ConfigRepSetting::putNumberValue(ConfigRepSettingPtr *confsetPtr, const char *name, const int value)
{
ConfigRepSettingPtr curPtr, newPtr;
/* First check if already in list (update) */
curPtr = *confsetPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->confsetName, name) == 0)
{
curPtr->confsetNumberValue = value;
return;
}
curPtr = curPtr->nextPtr;
}
/* Create new Config instance */
newPtr = new ConfigRepSetting;
strcpy(newPtr->confsetName, name);
newPtr->confsetNumberValue = value;
/* Prepend in list */
newPtr->nextPtr = *confsetPtr;
*confsetPtr = newPtr;
}
void ConfigRepSetting::deleteList(ConfigRepSettingPtr *confsetPtr)
{
ConfigRepSettingPtr tmpPtr;
while (*confsetPtr != NULL)
{
tmpPtr = *confsetPtr;
*confsetPtr = (*confsetPtr)->nextPtr;
delete tmpPtr;
}
}
void ConfigRepSetting::printList(ConfigRepSettingPtr confsetPtr) const
{
ConfigRepSettingPtr curPtr;
curPtr = confsetPtr;
while (curPtr != NULL)
{
printf("Config Setting Name %s\n", curPtr->confsetName);
curPtr = curPtr->nextPtr;
}
}