improve the async action and getStateVariable

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1764 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
joss17 2009-10-30 00:39:43 +00:00
parent 81e1978a83
commit 6eaf201120
2 changed files with 144 additions and 117 deletions

View File

@ -729,9 +729,19 @@ bool CUPnPService::Execute(
const std::string CUPnPService::GetStateVariable( const std::string CUPnPService::GetStateVariable(
const std::string &stateVariableName) const std::string &stateVariableName)
{ {
//this getvar is just to make an event happening std::map<std::string, std::string>::iterator it;
it = propertyMap.find(stateVariableName);
if (it != propertyMap.end()) {
std::cerr << "GetStateVariable(" << stateVariableName << ") = ";
std::cerr << (*it).second << std::endl;
return (*it).second;
} else {
//property map is not populated with the specified value.
//we will try to get it with an event
//this getvar is just to make the event happening
DOMString StVarVal; DOMString StVarVal;
int ret = UpnpGetServiceVarStatus( UpnpGetServiceVarStatus(
m_UPnPControlPoint.GetUPnPClientHandle(), m_UPnPControlPoint.GetUPnPClientHandle(),
GetAbsControlURL().c_str(), GetAbsControlURL().c_str(),
stateVariableName.c_str(), stateVariableName.c_str(),
@ -749,17 +759,18 @@ const std::string CUPnPService::GetStateVariable(
break; break;
} }
} }
std::cerr << "GetStateVariable pause finished."; }
std::cerr << "GetStateVariable(" << stateVariableName << ") = "; //propertyMap should be populated by nom
std::map<std::string, std::string>::iterator it;
it = propertyMap.find(stateVariableName); it = propertyMap.find(stateVariableName);
if (it == propertyMap.end()) { if (it != propertyMap.end()) {
std::cerr << "Empty String" << std::endl; std::cerr << "GetStateVariable(" << stateVariableName << ") = ";
return stdEmptyString;
} else {
std::cerr << (*it).second << std::endl; std::cerr << (*it).second << std::endl;
return (*it).second; return (*it).second;
} else {
std::cerr << "GetStateVariable(" << stateVariableName << ") = ";
std::cerr << "Empty String" << std::endl;
return stdEmptyString;
} }
} }
} }
@ -929,6 +940,11 @@ m_WanService(NULL)
} }
//clean the PortMappingNumberOfEntries as it is erroneus on the first event with the french neufbox
if (WanServiceDetected()) {
m_WanService->propertyMap.erase("PortMappingNumberOfEntries");
}
std::cerr << "CUPnPControlPoint Constructor finished" << std::endl; std::cerr << "CUPnPControlPoint Constructor finished" << std::endl;
return; return;
@ -976,6 +992,14 @@ bool CUPnPControlPoint::AddPortMappings(
bool ok = false; bool ok = false;
// Check the number of port mappings before // Check the number of port mappings before
//have a little break in case we just modified the variable, so we have to wait for an event
std::cerr << "GetStateVariable pausing in case of an UPnP event incomming.";
time_t begin_time = time(NULL);
while (true) {
if (time(NULL) - begin_time > 7) {
break;
}
}
std::istringstream OldPortMappingNumberOfEntries( std::istringstream OldPortMappingNumberOfEntries(
m_WanService->GetStateVariable( m_WanService->GetStateVariable(
"PortMappingNumberOfEntries")); "PortMappingNumberOfEntries"));
@ -995,14 +1019,15 @@ bool CUPnPControlPoint::AddPortMappings(
} }
} }
// Debug only // Not very good, must find a better test : check the new number of port entries
#ifdef UPNP_DEBUG //have a little break in case we just modified the variable, so we have to wait for an event
std::cerr << "CUPnPControlPoint::AddPortMappings: " std::cerr << "GetStateVariable pausing in case of an UPnP event incomming.";
"m_ActivePortMappingsMap.size() == " << begin_time = time(NULL);
m_ActivePortMappingsMap.size() << std::endl; while (true) {
#endif if (time(NULL) - begin_time > 7) {
break;
// Not very good, must find a better test }
}
std::istringstream NewPortMappingNumberOfEntries( std::istringstream NewPortMappingNumberOfEntries(
m_WanService->GetStateVariable( m_WanService->GetStateVariable(
"PortMappingNumberOfEntries")); "PortMappingNumberOfEntries"));
@ -1024,10 +1049,16 @@ std::string CUPnPControlPoint::getExternalAddress()
"WAN Service not detected." << std::endl; "WAN Service not detected." << std::endl;
return false; return false;
} }
PrivateGetExternalIpAdress();
std::string result = m_WanService->GetStateVariable("NewExternalIPAddress"); std::string result = m_WanService->GetStateVariable("NewExternalIPAddress");
std::cerr << " m_WanService->GetStateVariable(NewExternalIPAddress) = " << result << std::endl;
if (result == "") {
PrivateGetExternalIpAdress();
result = m_WanService->GetStateVariable("NewExternalIPAddress");
std::cerr << " m_WanService->GetStateVariable(NewExternalIPAddress) = " << result << std::endl;
if (result == "") { if (result == "") {
result = m_WanService->GetStateVariable("ExternalIPAddress"); result = m_WanService->GetStateVariable("ExternalIPAddress");
std::cerr << " m_WanService->GetStateVariable(ExternalIPAddress) = " << result << std::endl;
}
} }
return result; return result;
} }

View File

@ -100,8 +100,6 @@ bool upnphandler::background_setup_upnp(bool start, bool stop)
bool upnphandler::start_upnp() bool upnphandler::start_upnp()
{ {
RsStackMutex stack(dataMtx); /* LOCK STACK MUTEX */
if (!(upnpState >= RS_UPNP_S_READY)) if (!(upnpState >= RS_UPNP_S_READY))
{ {
std::cerr << "upnphandler::start_upnp() Not Ready" << std::endl; std::cerr << "upnphandler::start_upnp() Not Ready" << std::endl;
@ -111,6 +109,10 @@ bool upnphandler::start_upnp()
char eprot1[] = "TCP"; char eprot1[] = "TCP";
char eprot2[] = "UDP"; char eprot2[] = "UDP";
struct sockaddr_in localAddr;
{
RsStackMutex stack(dataMtx); /* LOCK STACK MUTEX */
/* if we're to load -> load */ /* if we're to load -> load */
/* select external ports */ /* select external ports */
eport_curr = eport; eport_curr = eport;
@ -136,7 +138,7 @@ bool upnphandler::start_upnp()
char eport1[256]; char eport1[256];
upnp_iaddr.sin_port = htons(iport); upnp_iaddr.sin_port = htons(iport);
struct sockaddr_in localAddr = upnp_iaddr; localAddr = upnp_iaddr;
uint32_t linaddr = ntohl(localAddr.sin_addr.s_addr); uint32_t linaddr = ntohl(localAddr.sin_addr.s_addr);
snprintf(in_port1, 256, "%d", ntohs(localAddr.sin_port)); snprintf(in_port1, 256, "%d", ntohs(localAddr.sin_port));
@ -153,6 +155,7 @@ bool upnphandler::start_upnp()
std::cerr << " ePort: " << eport1; std::cerr << " ePort: " << eport1;
std::cerr << " eProt: " << eprot1; std::cerr << " eProt: " << eprot1;
std::cerr << std::endl; std::cerr << std::endl;
}
//build port mapping config //build port mapping config
std::vector<CUPnPPortMapping> upnpPortMapping1; std::vector<CUPnPPortMapping> upnpPortMapping1;
@ -160,45 +163,25 @@ bool upnphandler::start_upnp()
upnpPortMapping1.push_back(cUPnPPortMapping1); upnpPortMapping1.push_back(cUPnPPortMapping1);
bool res = cUPnPControlPoint->AddPortMappings(upnpPortMapping1); bool res = cUPnPControlPoint->AddPortMappings(upnpPortMapping1);
if (res) {
upnpState = RS_UPNP_S_ACTIVE;
} else {
upnpState = RS_UPNP_S_TCP_FAILED;
std::vector<CUPnPPortMapping> upnpPortMapping2; std::vector<CUPnPPortMapping> upnpPortMapping2;
CUPnPPortMapping cUPnPPortMapping2 = CUPnPPortMapping(eport_curr, ntohs(localAddr.sin_port), "UDP", true, "udp retroshare redirection"); CUPnPPortMapping cUPnPPortMapping2 = CUPnPPortMapping(eport_curr, ntohs(localAddr.sin_port), "UDP", true, "udp retroshare redirection");
upnpPortMapping2.push_back(cUPnPPortMapping2); upnpPortMapping2.push_back(cUPnPPortMapping2);
bool res2 = cUPnPControlPoint->AddPortMappings(upnpPortMapping2); bool res2 = cUPnPControlPoint->AddPortMappings(upnpPortMapping2);
if (res2) {
struct sockaddr_in extAddr;
bool extAddrResult = getExternalAddress(extAddr);
{
RsStackMutex stack(dataMtx); /* LOCK STACK MUTEX */
if (extAddrResult && (res || res2)) {
upnpState = RS_UPNP_S_ACTIVE; upnpState = RS_UPNP_S_ACTIVE;
} else { } else {
//upnpState = RS_UPNP_S_ACTIVE;
upnpState = RS_UPNP_S_UDP_FAILED; upnpState = RS_UPNP_S_UDP_FAILED;
} }
}
/* now store the external address */
std::string externalAdress = cUPnPControlPoint->getExternalAddress();
sockaddr_clear(&upnp_eaddr);
if(!externalAdress.empty())
{
const char* externalIPAddress = externalAdress.c_str();
std::cerr << "Stored External address: " << externalIPAddress;
std::cerr << ":" << eport_curr;
std::cerr << std::endl;
inet_aton(externalIPAddress, &(upnp_eaddr.sin_addr));
upnp_eaddr.sin_family = AF_INET;
upnp_eaddr.sin_port = htons(eport_curr);
}
else
{
std::cerr << "FAILED To get external Address";
std::cerr << std::endl;
}
toStart = false; toStart = false;
}
return (upnpState == RS_UPNP_S_ACTIVE); return (upnpState == RS_UPNP_S_ACTIVE);
@ -414,15 +397,28 @@ bool upnphandler::getInternalAddress(struct sockaddr_in &addr)
bool upnphandler::getExternalAddress(struct sockaddr_in &addr) bool upnphandler::getExternalAddress(struct sockaddr_in &addr)
{ {
// std::cerr << "UPnPHandler::getExternalAddress() pre Lock!" << std::endl; std::string externalAdress = cUPnPControlPoint->getExternalAddress();
if(!externalAdress.empty())
{
const char* externalIPAddress = externalAdress.c_str();
std::cerr << "Stored External address: " << externalIPAddress;
std::cerr << ":" << eport_curr;
std::cerr << std::endl;
dataMtx.lock(); /*** LOCK MUTEX ***/ dataMtx.lock(); /*** LOCK MUTEX ***/
// std::cerr << "UPnPHandler::getExternalAddress() postLock!" << std::endl; sockaddr_clear(&upnp_eaddr);
inet_aton(externalIPAddress, &(upnp_eaddr.sin_addr));
std::cerr << "UPnPHandler::getExternalAddress()" << std::endl; upnp_eaddr.sin_family = AF_INET;
addr = upnp_eaddr; upnp_eaddr.sin_port = htons(eport_curr);
bool valid = (upnpState == RS_UPNP_S_ACTIVE);
dataMtx.unlock(); /*** UNLOCK MUTEX ***/ dataMtx.unlock(); /*** UNLOCK MUTEX ***/
return valid; addr = upnp_eaddr;
return true;
}
else
{
return false;
}
} }