/*
*
* ProLinga-Run
*
* Copyright (C) 2002-2008 Xobas Software.
* All rights reserved.
*
* This file is part of ProLinga-Run.
*
* ProLinga-Run 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-Run 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-Run. If not, see .
*
* More information is available at the following addresses:
*
* Website : http://www.prolinga.org
*
* Email : prolinga-list@prolinga.org
*
*
*/
#include "RunCommon.h"
#include
#include
#include
#include "RunConfig.hpp"
ConfigRun::ConfigRun(const char *configFileName)
{
xmlDocPtr docConf;
xmlNodePtr curConf;
xmlXPathContextPtr ctxConf;
xmlXPathObjectPtr resConf;
char productPath[255], configValue[255];
/* Init */
startPtr = new ConfigRunSetting;
/* Parsing file */
docConf = xmlParseFile(configFileName);
if (docConf == NULL)
{
fprintf(stderr, "Error parsing config file : %s. \n", configFileName);
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='" PRODUCT_NAME "']/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 Server 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 Number of Threads */
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='" PRODUCT_NAME "']/Server/");
/* 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 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));
/* Get Remote repository server name/ip */
strcpy(configValue, productPath);
strcat(configValue, "RepositoryHost");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "RepositoryHost", (char *)xmlXPathCastToString(resConf));
/* Get Remote repository port number */
strcpy(configValue, productPath);
strcat(configValue, "RepositoryPort");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "RepositoryPort", (int)xmlXPathCastToNumber(resConf));
/* Get Remote repository compression level */
strcpy(configValue, productPath);
strcat(configValue, "RepositoryCompressionLevel");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "RepositoryCompressionLevel", (int)xmlXPathCastToNumber(resConf));
/* Get Remote validate server name/ip */
strcpy(configValue, productPath);
strcat(configValue, "ValidateHost");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "ValidateHost", (char *)xmlXPathCastToString(resConf));
/* Get Remote validate port number */
strcpy(configValue, productPath);
strcat(configValue, "ValidatePort");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "ValidatePort", (int)xmlXPathCastToNumber(resConf));
/* Get Remote validate compression level */
strcpy(configValue, productPath);
strcat(configValue, "ValidateCompressionLevel");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "ValidateCompressionLevel", (int)xmlXPathCastToNumber(resConf));
/* Get Remote data server name/ip */
strcpy(configValue, productPath);
strcat(configValue, "DataHost");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "DataHost", (char *)xmlXPathCastToString(resConf));
/* Get Remote data port number */
strcpy(configValue, productPath);
strcat(configValue, "DataPort");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "DataPort", (int)xmlXPathCastToNumber(resConf));
/* Get Remote data compression level */
strcpy(configValue, productPath);
strcat(configValue, "DataCompressionLevel");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putNumberValue(&startPtr, "DataCompressionLevel", (int)xmlXPathCastToNumber(resConf));
/* Get External Screen Painter */
strcpy(configValue, productPath);
strcat(configValue, "ExternalScreenPainter");
resConf = xmlXPathEval((const xmlChar *)configValue, ctxConf);
if (resConf->nodesetval != NULL && resConf->nodesetval->nodeNr > 0)
startPtr->putStringValue(&startPtr, "ExternalScreenPainter", (char *)xmlXPathCastToString(resConf));
/* Clean up */
xmlXPathFreeContext(ctxConf);
xmlFreeDoc(docConf);
}
ConfigRun::~ConfigRun()
{
if (startPtr != NULL)
startPtr->deleteList(&startPtr);
}
char *ConfigRun::getStringValue(const char *name, const char *defValue)
{
return startPtr->getStringValue(startPtr, name, defValue);
}
int ConfigRun::getNumberValue(const char *name, const int defValue)
{
return startPtr->getNumberValue(startPtr, name, defValue);
}
void ConfigRun::putStringValue(const char *name, const char *value)
{
startPtr->putStringValue(&startPtr, name, value);
}
void ConfigRun::putNumberValue(const char *name, const int value)
{
startPtr->putNumberValue(&startPtr, name, value);
}
void ConfigRun::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-Run 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, "Number of Threads : %d\n", startPtr->getNumberValue(startPtr, "NumberOfThreads", DEF_NUMBER_THREADS));
fprintf(log,"-----------------------------------------------------------------\n");
/*Close log file */
fclose(log);
}
void ConfigRun::printList(void) const
{
startPtr->printList(startPtr);
}
ConfigRunSetting::ConfigRunSetting()
{
strcpy(confsetName, "");
confsetStringValue = NULL;
confsetNumberValue = 0;
nextPtr = NULL;
}
ConfigRunSetting::~ConfigRunSetting()
{
/* Nothing ? */
}
char *ConfigRunSetting::getStringValue(ConfigRunSettingPtr confsetPtr, const char *name, const char *defValue)
{
ConfigRunSettingPtr curPtr;
/* Get value from memory */
curPtr = confsetPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->confsetName, name) == 0)
return curPtr->confsetStringValue;
curPtr = curPtr->nextPtr;
}
/* Return */
return (char *)defValue;
}
int ConfigRunSetting::getNumberValue(ConfigRunSettingPtr confsetPtr, const char *name, const int defValue)
{
ConfigRunSettingPtr curPtr;
/* Get value from memory */
curPtr = confsetPtr;
while (curPtr != NULL)
{
if (strcmp(curPtr->confsetName, name) == 0)
return curPtr->confsetNumberValue;
curPtr = curPtr->nextPtr;
}
/* Return */
return defValue;
}
void ConfigRunSetting::putStringValue(ConfigRunSettingPtr *confsetPtr, const char *name, const char *value)
{
ConfigRunSettingPtr 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 ConfigRunSetting;
strcpy(newPtr->confsetName, name);
newPtr->confsetStringValue = new char[(strlen(value)+1)];
strcpy(newPtr->confsetStringValue, value);
/* Prepend in list */
newPtr->nextPtr = *confsetPtr;
*confsetPtr = newPtr;
}
void ConfigRunSetting::putNumberValue(ConfigRunSettingPtr *confsetPtr, const char *name, const int value)
{
ConfigRunSettingPtr 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 ConfigRunSetting;
strcpy(newPtr->confsetName, name);
newPtr->confsetNumberValue = value;
/* Prepend in list */
newPtr->nextPtr = *confsetPtr;
*confsetPtr = newPtr;
}
void ConfigRunSetting::deleteList(ConfigRunSettingPtr *confsetPtr)
{
ConfigRunSettingPtr tmpPtr;
while (*confsetPtr != NULL)
{
tmpPtr = *confsetPtr;
*confsetPtr = (*confsetPtr)->nextPtr;
delete tmpPtr;
}
}
void ConfigRunSetting::printList(ConfigRunSettingPtr confsetPtr) const
{
ConfigRunSettingPtr curPtr;
curPtr = confsetPtr;
while (curPtr != NULL)
{
printf("Config Setting Name %s\n", curPtr->confsetName);
curPtr = curPtr->nextPtr;
}
}