From 557088b6550eb98672d5e8045cd4f2b9df89a22b Mon Sep 17 00:00:00 2001
From: Georg Lukas <georg@op-co.de>
Date: Tue, 31 Mar 2009 04:00:48 +0200
Subject: finally proper shell-block code

---
 src/config.cc |   26 +++++++++++++++-----------
 1 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/config.cc b/src/config.cc
index e2e2097858ed68be0b5997823ca14bcdc34f755d..e57580471e3c0f22dd95363cf0328904398bc962 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -91,7 +91,10 @@ int Config::remoteCall(std::string nodeList,std::string variableName,std::string
 		return -1;
 	}
 
+	if(shellClient!=NULL)
+		shellClient->block();
 	struct rcMsgReq rcmo;
+	bool should_block = false;
 	memset(rcmo.message,0,MAX_MESSAGE_SIZE);
 	rcmo.nameLength=0;
 	rcmo.valLength=0;
@@ -104,17 +107,15 @@ int Config::remoteCall(std::string nodeList,std::string variableName,std::string
 	}
 	if(!nodeList.compare("..")) //broadcast
 	{
-		if(shellClient!=NULL)
-			if(shellClient->state==ShellClient::CS_Idle)
-				shellClient->block();
 		gea::AbsTime t_start = gea::AbsTime(GEA.lastEventTime);
-		sppManager->sendRequestAll( SPP_CONFIG_ID, (void*) &rcmo, Config::requestCreator, &t_start, Config::responseHandler,&t_start,Config::finishedHandler);
+		requestCount = -1;
+		should_block = true;
+		sppManager->sendRequestAll(SPP_CONFIG_ID, (void*)&rcmo, Config::requestCreator,
+				&t_start, Config::responseHandler,
+				&t_start, Config::finishedHandler);
 	}
 	else
 	{
-		if(shellClient)
-			if(shellClient->state==ShellClient::CS_Idle)
-				shellClient->block();
 		char nodes[nodeList.length()+1];
 		memset(nodes,0,nodeList.length()+1);
 		memcpy(nodes,nodeList.c_str(),nodeList.length());
@@ -129,6 +130,7 @@ int Config::remoteCall(std::string nodeList,std::string variableName,std::string
 			{
 				if(awdsRouting->getNodeByName(dest,pch))
 				{
+					should_block = true;
 					requestCount++;
 					sppManager->sendRequest(dest,SPP_CONFIG_ID,(void*) &rcmo, Config::requestCreator, &t_start, Config::responseHandler);
 				}
@@ -138,6 +140,9 @@ int Config::remoteCall(std::string nodeList,std::string variableName,std::string
 			pch = strtok_r(NULL, ",", &p);
 		}
 	}
+	/* no requests were sent, unblock again */
+	if(!should_block)
+		shellClient->unblock();
 	return 0;
 }
 
@@ -184,12 +189,13 @@ int Config::remoteconfig_command_fn(ShellClient &sc, void *data, int argc, char
 void Config::finishedHandler (void *data)
 {
 	REP_MAP_OBJ(Config *,config);
-	if(config)
+	GEA.dbg() << "finishedHandler" << config->requestCount << endl;
+	if(config->requestCount != 0)
 	{
 		if(config->shellClient)
-			if(config->shellClient->state==ShellClient::CS_Blocked)
 				config->shellClient->unblock();
 	}
+	config->requestCount = 0;
 }
 
 int Config::responseHandler (void *data, ReadMarshalStream*  msIn, const awds::NodeId& src) 
@@ -207,7 +213,6 @@ int Config::responseHandler (void *data, ReadMarshalStream*  msIn, const awds::N
 		if(config->requestCount>0)
 			config->requestCount--;
 		if(config->shellClient)
-			if(config->shellClient->state==ShellClient::CS_Blocked)
 				config->shellClient->unblock();
 		return -1;
 	}
@@ -245,7 +250,6 @@ int Config::responseHandler (void *data, ReadMarshalStream*  msIn, const awds::N
 	}
 	if(config->requestCount==0)
 		if(config->shellClient)
-			if(config->shellClient->state==ShellClient::CS_Blocked)
 				config->shellClient->unblock();
 
 	return CONFIG_VARIABLE_SUCCESS;
-- 
1.6.2.1

