From b3f963e6d7bdbd7159fd470efc22872c2a45c60a Mon Sep 17 00:00:00 2001
From: Georg Lukas <georg@op-co.de>
Date: Wed, 25 Mar 2009 14:02:41 +0100
Subject: config: code cleanup; reduced redundancy

---
 src/awds/config.h |   30 +-------------
 src/config.cc     |  106 ++++++++++++++---------------------------------------
 2 files changed, 31 insertions(+), 105 deletions(-)

diff --git a/src/awds/config.h b/src/awds/config.h
index 2af0ac724c7b090101bc6bade4ab28590f9c0f21..e08e7f1b33c8c5e4f7d04ca338b5999bcf924a69 100644
--- a/src/awds/config.h
+++ b/src/awds/config.h
@@ -15,7 +15,8 @@ private:
 
 protected:
 
-	std::map<const char*, ConfigVariable* > remoteVariables;
+	typedef std::map<std::string, ConfigVariable* > VariablesMap;
+	VariablesMap remoteVariables;
 	std::map<gea::AbsTime,unsigned int> requests;
 	std::map<gea::AbsTime,std::string> response;
 
@@ -33,32 +34,7 @@ public:
 	static int responseHandler (void *data, ReadMarshalStream*  msIn, const awds::NodeId& src);
 	static void requestCreator (void *data, WriteMarshalStream* msOut);
 	static void finishedHandler (void *data);
-	unsigned int hexToInt(char* string)
-	{
-		printf("\n\n");
-		unsigned int retval=0;
-		int faktor=1;
-		int length= strlen(string);
-		for (int i=length-1; i>=0; i--)
-		{
-			if(string[i]>64&&string[i]<91)
-			{
-				printf("1:%u|%u| ",string[i]-55,(unsigned int)(string[i]-55)*faktor);
-				retval+=(unsigned int)(string[i]-55)*faktor;
-			}
-			else
-			if(string[i]>47&&string[i]<58)
-			{
-				printf("2:%u|%u|",string[i]-48,(unsigned int)(string[i]-48)*faktor);
-				retval+=(unsigned int)(string[i]-48)*faktor;
-			}
-			faktor*=16;
-			printf("%u|\n",retval);
-		}
-		return retval;
-	}
-
 
 };
 
-#endif
\ No newline at end of file
+#endif
diff --git a/src/config.cc b/src/config.cc
index ec11e53e0e44d2b677505696fee74a8cb6fbc434..0ebfd17e5a3fcd0f5774561bee816b9891a238e1 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -72,22 +72,8 @@ void Config::requestCreator(void *data, WriteMarshalStream* msOut)
 	}
 
 	struct rcMsgReq* rcmo=(struct rcMsgReq*)data;
-
-	if(rcmo->nameLength<1&&rcmo->valLength<1)//0? we just want a list of configmodules!
-	{
-		int lengthOut=0;
-		*msOut << (uint32_t) lengthOut;
-		return;
-	}
-	uint32_t numberOfEntries=0;
-	if(rcmo->nameLength)numberOfEntries++;
-	if(rcmo->valLength)numberOfEntries++;
-	*msOut << numberOfEntries;
-	if(rcmo->nameLength)
-		*msOut << std::string(rcmo->message, rcmo->nameLength);
-	if(rcmo->valLength)
-		*msOut << std::string(rcmo->message+rcmo->nameLength, rcmo->valLength);
-	
+	*msOut << std::string(rcmo->message, rcmo->nameLength);
+	*msOut << std::string(rcmo->message+rcmo->nameLength, rcmo->valLength);
 }
 
 static const char *config_cmd_usage =
@@ -116,14 +102,14 @@ int Config::remoteconfig_command_fn(ShellClient &sc, void *data, int argc, char
 {
 	Config *self = static_cast<Config*>(data);
 	self->shellClient = &sc;
-	self->shellOut=sc.sockout;	
+	self->shellOut=sc.sockout;
 	REP_MAP_OBJ(SppManager2 *,sppManager);
-    	if ( (argc >= 2) )
+	if ( (argc >= 2) )
 	{
 		struct rcMsgReq rcmo;
 		memset(rcmo.message,0,MAX_MESSAGE_SIZE);
 		if(argc>=3)//lets build the request
-		{	
+		{
 
 			rcmo.nameLength = strlen(argv[2]);
 			rcmo.valLength=0;
@@ -299,88 +285,52 @@ int Config::requestHandler (void *data, ReadMarshalStream*  msIn, WriteMarshalSt
 		return -1;
 	}
 
+	std::string name;
+	std::string value;
 
-	uint32_t numberOfStrings;
-	*msIn >> numberOfStrings;
+	*msIn >> name;
+	*msIn >> value;
 
-	if(numberOfStrings<1)//no params? well then show me what you've got
+	if(name.length() == 0)//no params? well then show me what you've got
 	{
-		uint32_t numberOfEntries = config->remoteVariables.size();;
+		uint32_t numberOfEntries = config->remoteVariables.size();
 		uint32_t status = 0;
 
 		*msOut << status;
 		*msOut << numberOfEntries;
 
-		for(std::map<const char*,ConfigVariable*>::iterator it2=config->remoteVariables.begin();it2!=config->remoteVariables.end();it2++)
+		for(VariablesMap::iterator it=config->remoteVariables.begin();it!=config->remoteVariables.end();it++)
 		{
-			*msOut << std::string(it2->first);
+			*msOut << std::string(it->first);
 		}
 		return CONFIG_VARIABLE_SUCCESS;
-	}
-	std::string name;
-	std::string value;
-
-	if(numberOfStrings>=1)*msIn>>name;
-	if(numberOfStrings==2)*msIn>>value;
-
-	if(name.length()>0)//we need the modulename AND the variable name
-	{
-		bool found=false;
-		//std::map<const char*,ConfigVariable*>::iterator it = config->remoteVariables.find(name);//why the heck is THIS not working?
-		for(std::map<const char*,ConfigVariable*>::iterator it2=config->remoteVariables.begin();it2!=config->remoteVariables.end();it2++)
+	} else {
+		VariablesMap::iterator it = config->remoteVariables.find(name);//why the heck is THIS not working?
+		uint32_t statusOut = CONFIG_VARIABLE_FAILURE;
+		uint32_t numberOfStringsOut = 0;
+		if(it != config->remoteVariables.end())//at least this works but....
 		{
+			if(value.length() > 0) {
+				statusOut = (it->second)->fromString(value);//ok set the values
+			} else
+				statusOut = CONFIG_VARIABLE_SUCCESS;
 
-			if(!strcmp(it2->first,name.c_str()))//at least this works but....
-			{
-				found=true;
-				if(value.length()>0)
-				{
-					(it2->second)->fromString(std::string(value));//ok set the values
-					std::string s= (it2->second)->toString();
-					int length=s.size();
-					uint32_t numberOfStringsOut = 1;
-					uint32_t statusOut = 0;
-					*msOut << statusOut;
-					*msOut << numberOfStringsOut;
-					*msOut <<s ;
-				}
-				else //just read the value
-				{
-					std::string s= (it2->second)->toString();
-					uint32_t numberOfStringsOut = 1;
-					uint32_t statusOut = 0;
-					*msOut << statusOut;
-					*msOut << numberOfStringsOut;
-					*msOut << s;
-				}
-			}
-		}
-
-		if(!found)//no modulename configvar found maybe typo?!
-		{
+			numberOfStringsOut++;
+			std::string s = (it->second)->toString();
 
+			*msOut << statusOut;
+			*msOut << numberOfStringsOut;
+			*msOut << s;
+		} else {
 			if(config->shellClient)
 				*config->shellClient->sockout << "Config::requestHandler Request sorry i don't know this one: " << name << std::endl;
 			GEA.dbg() << "Config::requestHandler Request sorry i don't know this one: " << name << std::endl;
-			uint32_t statusOut = CONFIG_VARIABLE_FAILURE;//TODO Maybe send this message back?
-			uint32_t numberOfStringsOut=0;
 
 			*msOut << statusOut;
 			*msOut << numberOfStringsOut;
 			return -1;
 		}
 	}
-	else //TODO this should be obsolet now?
-	{
-		uint32_t statusOut = CONFIG_VARIABLE_FAILURE;
-		*msOut << statusOut;
-		uint32_t numberOfStringsOut=0;
-		*msOut << numberOfStringsOut;
-		if(config->shellClient)
-			*config->shellClient->sockout << "Something went terribly wrong in Config::request_handler :("<< endl;
- 		GEA.dbg() << "Something went terribly wrong in Config::request_handler :("<< endl;
-		return 0;
-	}
 	return 0;
 }
 
-- 
1.6.2.1


