From 65171dadea99d98d4bd0f495b08dadbe85dca35a Mon Sep 17 00:00:00 2001
From: meini <meini@meini-desktop.ARBEITSGRUPPE>
Date: Thu, 19 Mar 2009 15:51:03 +0100
Subject: segfault bugfix + output to shell

---
 src/remoteConfig.cc |  268 ++++++++++++++++++++++++++++++++++-----------------
 src/remoteConfig.h  |   26 +++++-
 2 files changed, 205 insertions(+), 89 deletions(-)

diff --git a/src/remoteConfig.cc b/src/remoteConfig.cc
index 82c994de8f541aeb5b9caac7e974102620f4ccef..1c9bda7c669cf5d104538d6b3d0a2c4f3b557480 100644
--- a/src/remoteConfig.cc
+++ b/src/remoteConfig.cc
@@ -1,4 +1,5 @@
 
+#include <sstream>
 #include <gea/API.h>
 #include <gea/gea_main.h>
 #include <gea/ObjRepository.h>
@@ -9,74 +10,69 @@
 using namespace std;
 using namespace awds;
 
+/**
+communication flow:
+requestmessages:
+	4 Bytes		 lengthOfMessage Bytes
+	lengthOfMessage |Message
+replymessages:
+	4 Bytes		|4 Bytes	|lengthOfMessage Bytes
+	statusflags	|lengthOfMessage|Message
+**/
 RemoteConfig::RemoteConfig()
 {
 
 }
-/*
-int RemoteConfig::remoteConfigAnnounce(char* name,ConfigVariableType cvt,void* thisref,variableGetHandler vgh,variableSetHandler vsh,void* value)
-{
-	REP_MAP_OBJ(RemoteConfig *,remoteConfig);
-	if(thisref!=NULL&&name!=NULL)
-	{
-		if(cvt==CONFIG_VARIABLE_TYPE_INT) remoteConfig->remoteVariables[name]=new ConfigVariableInt(name,*((int*)value),vgh,vsh,thisref);
-		else if(cvt==CONFIG_VARIABLE_TYPE_STRING) remoteConfig->remoteVariables[name]=new ConfigVariableString(name,((char*)value),vgh,vsh,thisref);
-		else if(cvt==CONFIG_VARIABLE_TYPE_BOOL) remoteConfig->remoteVariables[name]=new ConfigVariableBool(name,*((bool*)value),vgh,vsh,thisref);
-		else if(cvt==CONFIG_VARIABLE_TYPE_FLOAT) remoteConfig->remoteVariables[name]=new ConfigVariableFloat(name,*((float*)value),vgh,vsh,thisref);
-		else if(cvt==CONFIG_VARIABLE_TYPE_DOUBLE) remoteConfig->remoteVariables[name]=new ConfigVariableDouble(name,*((double*)value),vgh,vsh,thisref);
-	}
-	return 0;
-}*/
 int RemoteConfig::remoteConfigAnnounce(char* name,ConfigVariable* cv)
 {
-	//REP_MAP_OBJ(RemoteConfig *,remoteConfig);remoteConfig->
 	remoteVariables[name]=cv;
 	return 0;
 }
 
 void RemoteConfig::requestCreator (void *data, WriteMarshalStream* msOut)
 {
+	REP_MAP_OBJ(RemoteConfig *,remoteConfig);//get it, but check on USE!
+
+	if(msOut==NULL)//no stream? useless to walk on!
+	{
+		if(remoteConfig)*remoteConfig->shellClient->sockout << "RemoteConfig::requestCreator msOut==NULL" << std::endl;
+		GEA.dbg() << "RemoteConfig::requestCreator msOut==NULL" << std::endl;
+		return;
+	}
+	if(data==NULL)//no data? that is quite strange
+	{
+		if(remoteConfig)*remoteConfig->shellClient->sockout <<  "RemoteConfig::requestCreator data==NULL" << std::endl;
+		GEA.dbg() << "RemoteConfig::requestCreator data==NULL" << std::endl;
+		int length=0;
+		*msOut << (uint32_t) length;
+		return;
+	}
 	unsigned int* pidata = (unsigned int*) data;
 	unsigned int len1=*pidata;
 	unsigned int len2=*(++pidata);
 	unsigned int len3=*(++pidata);
-	if(len1<1&&len2<1&&len3<1)
+	if(len1<1&&len2<1&&len3<1)//0? we just want a list of configmodules!
 	{
-	int length=0;
-	*msOut << (uint32_t) length;
-	return;
+		int length=0;
+		*msOut << (uint32_t) length;
+		return;
 	}
 	//TODO einzelne laengen abfangen
 	unsigned char uc=' ';
 	char* pcdata = (char*)(((char*)data)+sizeof(len1)+sizeof(len2)+sizeof(len3));
 	
 	int length=len1+len2+len3+2;
-	*msOut << (uint32_t) length;
-/*	printf("length: %i\n",length);
-		
-	printf("pcdata1: ");
-	for(int i=0;i<len1;i++)printf("%c",pcdata[i]);
-	printf("\n");	
-*/	
-	std::pair<void *, unsigned int> v1(pcdata,len1);
+	*msOut << (uint32_t) length;//ok put the length in we'll need it
+	
+	std::pair<void *, unsigned int> v1(pcdata,len1);//and the messages!
 	*msOut << v1;
 	*msOut << uc;
-	std::pair<void *, unsigned int> v2(pcdata+len1,len2);
-/*	
-	printf("pcdata2: ");
-	for(int i=0;i<len2;i++)printf("%c",(pcdata+len1)[i]);
-	printf("\n");
-*/	
+	std::pair<void *, unsigned int> v2(pcdata+len1,len2);//this one too
 	*msOut << v2;
 	*msOut << uc;
-	if(len3>0)
+	if(len3>0)//do we have a value?
 	{
-
-		printf("pcdata1: ");
-		for(int i=0;i<len3;i++)printf("%c",(pcdata+len1+len2)[i]);
-		printf("\n");
-
-		std::pair<void *, unsigned int> v3(pcdata+len1+len2,len3);
+		std::pair<void *, unsigned int> v3(pcdata+len1+len2,len3);//well then push it in!
 		*msOut << v3;
 	}
 		
@@ -109,20 +105,21 @@ int RemoteConfig::remoteConfigAddCommand() {
 
 int RemoteConfig::remoteconfig_command_fn(ShellClient &sc, void *data, int argc, char **argv)
 {
+	RemoteConfig *self = static_cast<RemoteConfig*>(data);
+	self->shellClient = &sc;
 	REP_MAP_OBJ(SppManager2 *,sppManager);
     	int cmd;
     	if ( (argc >= 2) )
 	{
 		cmd = 1;
-		//groesse abfragen
 		if(!strcmp(argv[1], "[..]")) // FUER ALLE KNOTEN
 		{
 			if(argc>=3)
-			{	
-
+			{//TODO CODE REUSE!!!!!!!!
+/*
 				for(int i=0;i<argc;i++)
 					printf("%i %s\n",i,argv[i]);
-
+*/
 				int length=strlen(argv[2]);
 				int length1=strlen(argv[3]);
 				if(argc==4)
@@ -141,7 +138,6 @@ int RemoteConfig::remoteconfig_command_fn(ShellClient &sc, void *data, int argc,
 				}
 				if(argc==5)
 				{
-					printf("hier?\n");
 					int length2=strlen(argv[4]);
 					char* buffer = new char[3*sizeof(unsigned int)+length+length1+length2];
 					char* pbuffer = buffer;
@@ -172,7 +168,69 @@ int RemoteConfig::remoteconfig_command_fn(ShellClient &sc, void *data, int argc,
 		}
 		else // einzeln parsen
 		{
-			//TODO nochmal überarbeiten Knotenliste wurde falsch geparsed!!
+			char* buffer=NULL;
+			if(argc>=3)
+			{	
+				int length=strlen(argv[2]);
+				int length1=strlen(argv[3]);
+
+				if(argc==4)
+				{
+					int i=0;
+					buffer = new char[3*sizeof(unsigned int)+length+length1];
+					char* pbuffer = buffer;
+					memcpy(buffer,&length,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+					memcpy(pbuffer,&length1,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+					memcpy(pbuffer,&i,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+					memcpy(pbuffer,argv[2],length);pbuffer+=length;
+					memcpy(pbuffer,argv[3],length1);pbuffer+=length1;
+					gea::AbsTime *t_start = new gea::AbsTime(GEA.lastEventTime);
+					//sppManager->sendRequestAll( 9876, buffer, RemoteConfig::requestCreator, t_start, emoteConfig::responseHandler,t_start,RemoteConfig::finishedHandler);
+
+				}
+				if(argc==5)
+				{
+					int length2=strlen(argv[4]);
+					buffer = new char[3*sizeof(unsigned int)+length+length1+length2];
+					char* pbuffer = buffer;
+					memcpy(buffer,&length,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+					memcpy(pbuffer,&length1,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+					memcpy(pbuffer,&length2,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+					memcpy(pbuffer,argv[2],length);pbuffer+=length;
+					memcpy(pbuffer,argv[3],length1);pbuffer+=length1;
+					memcpy(pbuffer,argv[4],length2);pbuffer+=length2;
+					gea::AbsTime *t_start = new gea::AbsTime(GEA.lastEventTime);
+					//sppManager->sendRequestAll( 9876, buffer, RemoteConfig::requestCreator, t_start, remoteConfig::responseHandler,t_start,RemoteConfig::finishedHandler);
+				}
+
+				//sppManager->sendRequestAll( 9761, &v, create_req, t_start, get_response);
+			}
+			char * pch;
+			int i=0;
+			pch = strtok (argv[1],",[]");
+			while (pch != NULL)//we might not need to split them up but to be on the safe side...
+			{
+				
+				i++;
+				gea::AbsTime *t_start = new gea::AbsTime(GEA.lastEventTime);
+				//sendRequest( const awds::NodeId& dest, uint32_t spp_id, void * data1, spp_request_creator  h1,void * data2, spp_response_handler h2);
+				if(!buffer)
+				{
+					buffer = new char[3*sizeof(unsigned int)];
+					char* pbuffer = buffer;
+					int zero=0;
+					memcpy(buffer,&zero,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+					memcpy(pbuffer,&zero,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+					memcpy(pbuffer,&zero,sizeof(unsigned int));pbuffer+=sizeof(unsigned int);
+				}
+				//TODO node id erzeugen
+				awds::NodeId dest;
+				
+
+				sppManager->sendRequest(dest,9876,buffer, RemoteConfig::requestCreator, t_start, RemoteConfig::responseHandler);
+				pch = strtok (NULL, ",[]");
+			}//TODO nochmal überarbeiten Knotenliste wurde falsch geparsed!!
+
 		}
     	}
 	else
@@ -188,51 +246,84 @@ void RemoteConfig::finishedHandler (void *data)
 //TODO zusammen mit dem einzelnen Knoten zum senden neu implementieren
 
 }
+
 int RemoteConfig::responseHandler (void *data, ReadMarshalStream*  msIn, const awds::NodeId& src) 
 {
+	REP_MAP_OBJ(RemoteConfig *,remoteConfig);//get but we'll check it on USE!
+
+	if(msIn==NULL)//ouch we need this!
+	{
+		if(remoteConfig)*remoteConfig->shellClient->sockout << "Response from " << src <<" with NULL" << std::endl;
+		GEA.dbg() << "Response from " << src <<" with NULL" << std::endl;
+		return -1;
+	}
 	uint32_t status;
 	*msIn >> status;
 	uint32_t length;
 	*msIn >> length;
-	if(status)
+	
+	if(status)//status>0? this is a problem!!
 	{
+		if(remoteConfig)*remoteConfig->shellClient->sockout << "Response from " << src <<" with length "<< length << ". Obviously there was an error!" << std::endl;
 		GEA.dbg() << "Response from " << src <<" with length "<< length << ". Obviously there was an error!" << std::endl;
+
 		return CONFIG_VARIABLE_FAILURE;
 	}
 	char* retval;
-	if(length>0)
+	if(length>0)//ah we've got a message nice, print it!
 	{
 		retval = new char[length+1];
 		memset(retval,0,length+1);
 		std::pair<void *, unsigned> v(retval,length);
 		*msIn >> v;
-		GEA.dbg() << "Response from " << src <<" with length "<< length <<"content in <> is: <" << retval <<">" << std::endl;
+		if(remoteConfig)*remoteConfig->shellClient->sockout << "Response from " << src <<" with length "<< length <<" content in <> is: <" << retval <<">" << std::endl;
+		GEA.dbg() << "Response from " << src <<" with length "<< length <<" content in <> is: <" << retval <<">" << std::endl;
+		if(retval) delete retval;
 	}
-	else
+	else //no Message? well at least no error!
+	{
+		if(remoteConfig)*remoteConfig->shellClient->sockout << "Response from " << src <<" with length "<< length << ". Strange but there was no error!" << std::endl;
 		GEA.dbg() << "Response from " << src <<" with length "<< length << ". Strange but there was no error!" << std::endl;
+	}
 	return CONFIG_VARIABLE_SUCCESS;
 }
 
 int RemoteConfig::requestHandler (void *data, ReadMarshalStream*  msIn, WriteMarshalStream* msOut, const awds::NodeId& src) 
 {
-	uint32_t length;
-	*msIn >> length;
 	REP_MAP_OBJ(RemoteConfig *,remoteConfig);
-	if(!remoteConfig)
+	if(msIn==NULL||msOut==NULL)//we definitely need those!
+	{
+		if(remoteConfig)*remoteConfig->shellClient->sockout << "Request from " << src <<" with NULL " << std::endl;
+		GEA.dbg() << "Request from " << src <<" with NULL " << std::endl;
+		return -1;
+	}
+	if(!remoteConfig)//we definitely need this one!
+	{
+		uint32_t lout = 0;
+		uint32_t status = 1;
+		*msOut << status;
+		*msOut << lout;
 		return -1;
-	if(length<1)
+	}
+	uint32_t length;
+	*msIn >> length;
+	if(length<1)//no params? well then show me what you've got
 	{
 		int len=0;
 		for(std::map<char*,ConfigVariable*>::iterator it2=remoteConfig->remoteVariables.begin();it2!=remoteConfig->remoteVariables.end();it2++)
 		{
 			len+=strlen(it2->first)+1;
 		}
+
 		uint32_t lout = len;
 		uint32_t status = 0;
+
 		*msOut << status;
 		*msOut << lout;
+
 		for(std::map<char*,ConfigVariable*>::iterator it2=remoteConfig->remoteVariables.begin();it2!=remoteConfig->remoteVariables.end();it2++)
 		{
+
 			std::pair<void *, unsigned> vOut(it2->first,strlen(it2->first));
 			*msOut << vOut;
 			unsigned char uc='\n';
@@ -242,69 +333,62 @@ int RemoteConfig::requestHandler (void *data, ReadMarshalStream*  msIn, WriteMar
 	}
 	char* names;
 	char* modulename[3];
-	printf("length:%i\n",length);
 	names = new char[length+1];
 	memset(names,0,length+1);
-	std::pair<void *, unsigned> v(names,length);
+	std::pair<void *, unsigned> v(names,length);//readparams
 	*msIn >> v;
-	printf("names1: %s\n",names);
 	names[length]=0;
-	printf("names2: %s\n",names);
 	char * pch;
 	int i=0;
 	for(int i=0;i<3;i++)modulename[i]=NULL;
 	//TODO mehrere streams auslesen? allerdings muessten dann mehrere ints übertragen werden wegen der länge?
 	pch = strtok (&(names[0])," ");
-	//GEA.dbg() << "strange things happen here!"<< endl;
-	while (pch != NULL&&i<3)
+
+	while (pch != NULL&&i<3)//we might not need to split them up but to be on the safe side...
 	{
 		modulename[i] = new char[strlen(pch)+1];
 		memset(modulename[i],0,strlen(pch)+1);
 		memcpy(modulename[i],pch,strlen(pch));
-		//modulename[i][strlen(pch)]=0;
 		pch = strtok (NULL, " ");
 		i++;
 	}
-	if(modulename[0]&&modulename[1])
+	if(modulename[0]&&modulename[1])//we need the modulename AND the variable name
 	{
 		int len1=strlen(modulename[0]);
 		int len2=strlen(modulename[1]);
-		if(len1<1||len2<1) return CONFIG_VARIABLE_FAILURE;
+		if(len1<1||len2<1) return CONFIG_VARIABLE_FAILURE;//well something went terribly wrong here
 		char* fullname = new char[len1+len2+2];
 		memset(fullname,0,len1+len2+2);
 		memcpy(fullname,modulename[0],len1);
 		fullname[len1]=' ';
-		printf("%s\n",modulename[0]);fflush(stdout);
-		printf("%s\n",modulename[1]);fflush(stdout);
-		memcpy(fullname+len1+1,modulename[1],len2);
-		//fullname[len1+len2+1]=0;
+		memcpy(fullname+len1+1,modulename[1],len2);//build the mapsearchstring
 		if (remoteConfig)
 		{
 			bool found=false;
-			std::map<char*,ConfigVariable*>::iterator it = remoteConfig->remoteVariables.find(fullname);// das geht so leider nicht? Oo
-			printf("fullname: <%s>\n",fullname);fflush(stdout);
+			std::map<char*,ConfigVariable*>::iterator it = remoteConfig->remoteVariables.find(fullname);//why the heck is THIS not working?
 			for(std::map<char*,ConfigVariable*>::iterator it2=remoteConfig->remoteVariables.begin();it2!=remoteConfig->remoteVariables.end();it2++)
 			{
-			//		printf("true::::<%s>\n",it2->first);
-			//	else printf("false::::<%s>\n",it2->first);
-			
 				//if(it!=remoteConfig->remoteVariables.end())
-				if(!strcmp(it2->first,fullname))
+				if(!strcmp(it2->first,fullname))//at least this works but....
 				{
-					printf("fullname: <%s>\n",fullname);fflush(stdout);
 					found=true;
-					if(modulename[2]!=NULL)
+					if(modulename[2]!=NULL)//TODO put it together as we now try to get the new value to here to double check the new value
 					{
-						(it2->second)->fromString(modulename[2]);
-						//(it->second)->set(modulename[3]); //ConfigVariable
-						//TODO WriteMarshalStream fuellen!
-						uint32_t lout = 0;
-						*msOut << lout;
+						(it2->second)->fromString(modulename[2]);//ok set the values
+						char* buffer = new char[1024];
+						memset(buffer,0,1024);
+						(it2->second)->toString(buffer); //ok get it to check!
+						int length=strlen(buffer);
+						uint32_t lout = length;
+						uint32_t status = 0;
+						*msOut << status;
 						*msOut << lout;
+						std::pair<void *, unsigned> vOut(buffer,length);
+						*msOut << vOut;
+						delete buffer;
 					}
-					else
+					else //just read the value
 					{
-						printf("testausgabe1\n");fflush(stdout);
 						char* buffer = new char[1024];
 						memset(buffer,0,1024);
 						(it2->second)->toString(buffer); //ConfigVariable
@@ -319,25 +403,33 @@ int RemoteConfig::requestHandler (void *data, ReadMarshalStream*  msIn, WriteMar
 					}
 				}
 			}
-			if(!found)
+			if(!found)//no modulename configvar found maybe typo?!
 			{
-				printf("oooops\n");fflush(stdout);
-				uint32_t lout = 1;
+				if(remoteConfig)*remoteConfig->shellClient->sockout << "RemoteConfig::requestHandler Request from sorry i don't know this one: " << fullname << std::endl;
+				GEA.dbg() << "RemoteConfig::requestHandler Request from sorry i don't know this one: " << fullname << std::endl;
+				return -1;
+				uint32_t lout = CONFIG_VARIABLE_FAILURE;//TODO Maybe send this message back?
 				*msOut << lout;
 				lout=0;
 				*msOut << lout;
 			}
 		}
-		else
+		else //TODO this should be obsolet now?
 		{
+			uint32_t lout = CONFIG_VARIABLE_FAILURE;
+			*msOut << lout;
+			lout=0;
+			*msOut << lout;
+			if(remoteConfig)*remoteConfig->shellClient->sockout << "Something went terribly wrong in RemoteConfig::request_handler :("<< endl;
  			GEA.dbg() << "Something went terribly wrong in RemoteConfig::request_handler :("<< endl;
 			return 0;
 		}
+		if(fullname)delete fullname;
 	}
-
+	//do some cleanup!
 	for (int i=0;i<3;i++)
 	if(modulename[i])delete modulename[i];
-	delete names;
+	if(names)delete names;
     return 0;
 }
 
diff --git a/src/remoteConfig.h b/src/remoteConfig.h
index 0d578799488ab8a5c544abd13d3fc36a2dce99f5..c00de6bd69e4c040dcd7d4f507c8db7a3edb73bf 100644
--- a/src/remoteConfig.h
+++ b/src/remoteConfig.h
@@ -18,7 +18,7 @@ protected:
 
 	std::map<char*, ConfigVariable* > remoteVariables;
 	std::map<unsigned int,unsigned int> requests;
-
+	ShellClient* shellClient;
 public:
 
 
@@ -32,6 +32,30 @@ 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;
+	}
 
 
 };
-- 
1.6.2.1


