connectivity/com.nokia.tcf/native/TCFNative/TCFClient/ClientManager.cpp
changeset 914 46f36a149510
parent 505 6de8d9cfdda1
child 1481 c7f22cc57d44
--- a/connectivity/com.nokia.tcf/native/TCFNative/TCFClient/ClientManager.cpp	Tue Feb 09 08:09:33 2010 -0600
+++ b/connectivity/com.nokia.tcf/native/TCFNative/TCFClient/ClientManager.cpp	Wed Feb 10 13:33:18 2010 -0600
@@ -488,7 +488,32 @@
 	TCDEBUGLOGA1("CClientManager::StartServer end numRefs = %d\n", pData->numRefs);
 	return ret;
 }
-
+BOOL CClientManager::IsTCFServerActive(DWORD processId)
+{
+	HANDLE h = ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, processId);
+	if (h)
+	{
+		// is it really still alive?
+		DWORD exitCode = -5;
+		BOOL exitCall = ::GetExitCodeProcess(h, &exitCode);
+		::CloseHandle(h);
+		if (exitCall == TRUE && exitCode != STILL_ACTIVE)
+		{
+			// TCFServer is really dead
+			return FALSE;
+		}
+		else
+		{
+			// TCFServer is still active
+			return TRUE;
+		}
+	}
+	else
+	{
+		// TCFServer is really dead
+		return FALSE;
+	}
+}
 long CClientManager::StopServer()
 {
 	long ret = TCAPI_ERR_NONE;
@@ -516,23 +541,34 @@
 		// substract ref count
 		pData->numRefs--;
 		if (pData->numRefs < 0) pData->numRefs = 0;
+
+		bool sendStop = true;
+		if (!IsTCFServerActive(pData->serverProcess.dwProcessId))
+		{
+			sendStop = false;
+			pData->numRefs = 0;
+		}
+
 		// if refcount == 0 then really stop the server process
 		if (pData->numRefs == 0)
 		{
-			// last client process is closing
-			// tell server to exit
-			ServerCommandData cmdrsp;
-			cmdrsp.command = eCmdExit;
-			
-			TCDEBUGLOGS(" SendCommand eCmdExit\n");
-			m_Server->SendCommand(&cmdrsp);
-			TCDEBUGLOGS(" GetResponse eExit\n");
-			m_Server->GetResponse(&cmdrsp);
-			
-			// wait for process to exit
-			TCDEBUGLOGS(" WaitForSingleObject start\n");
-			DWORD waitErr = ::WaitForSingleObject(m_hServer, 10000L /*INFINITE*/);
-			TCDEBUGLOGA1("CClientManager::StopServer WaitForSingleObject = %d\n", waitErr);
+			if (sendStop)
+			{
+				// last client process is closing
+				// tell server to exit
+				ServerCommandData cmdrsp;
+				cmdrsp.command = eCmdExit;
+				
+				TCDEBUGLOGS(" SendCommand eCmdExit\n");
+				m_Server->SendCommand(&cmdrsp);
+				TCDEBUGLOGS(" GetResponse eExit\n");
+				m_Server->GetResponse(&cmdrsp);
+				
+				// wait for process to exit
+				TCDEBUGLOGS(" WaitForSingleObject start\n");
+				DWORD waitErr = ::WaitForSingleObject(m_hServer, 10000L /*INFINITE*/);
+				TCDEBUGLOGA1("CClientManager::StopServer WaitForSingleObject = %d\n", waitErr);
+			}
 
 			// now close our handle to server process
 			if (m_hServer != NULL)
@@ -580,11 +616,14 @@
 BOOL CClientManager::IsServerRunning()
 {
 	pServerProcessData pData = m_Server->GetProcessPtr();
-	if (pData->serverProcess.hProcess != NULL)
+	if (IsTCFServerActive(pData->serverProcess.dwProcessId))
+	{
 		return TRUE;
+	}
 	else
+	{
 		return FALSE;
-
+	}
 }
 
 void CClientManager::CreateLockFile(DWORD processId)
@@ -780,6 +819,7 @@
 				}
 				if (numDeadCallers == numIds)
 				{
+					// All clients of this TCFServer are dead
 					// terminate the TCFServer, and delete lock file
 					pData->numRefs = 0;
 					::remove(m_ServerLockFile);
@@ -797,27 +837,43 @@
 				}
 				else
 				{
-					// leave TCFServer running, recreate lock file and save live callers
-					::remove(m_ServerLockFile);
-					f = fopen(m_ServerLockFile, "wt");
-					if (f)
+					// some java clients are still alive
+					//   check to see if TCFServer is still alive
+					if (IsTCFServerActive(serverIds[0]))
 					{
-						for (int i = 0; i < numIds; i++)
+						// TCFServer is still active
+						// leave TCFServer running, recreate lock file and save live callers
+						::remove(m_ServerLockFile);
+						f = fopen(m_ServerLockFile, "wt");
+						if (f)
 						{
-							if (liveCaller[i])
+							for (int i = 0; i < numIds; i++)
 							{
-								fprintf(f, "%ld %ld\n", creatorIds[i], serverIds[i]);
+								if (liveCaller[i])
+								{
+									fprintf(f, "%ld %ld\n", creatorIds[i], serverIds[i]);
+								}
 							}
+							fclose(f);
 						}
-						fclose(f);
+						pData->numRefs -= numDeadCallers;
+						if (pData->numRefs < 0) pData->numRefs = 0;
 					}
-					pData->numRefs -= numDeadCallers;
-					if (pData->numRefs < 0) pData->numRefs = 0;
+					else
+					{
+						// TCFServer is really dead
+						pData->numRefs = 0;
+						::remove(m_ServerLockFile);
+					}
 				}
 			}
 			else
 			{
 				// error opening lock file
+				// perhaps the user deleted it,
+				//   if so, we assume he has also deleted the TCFServer as we now have no way of verifying if the 
+				//   process is dead.
+				pData->numRefs = 0;
 				DWORD err = ::GetLastError();
 				TCDEBUGLOGA2("CClientManager::TerminateServerThroughLockFile fopenErr=%d:%s\n", err, GetErrorText(err));
 			}