[IOT-1388] fixed duplicate start server in EDR adapter.
authorjihwan.seo <jihwan.seo@samsung.com>
Wed, 5 Oct 2016 11:00:18 +0000 (20:00 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Mon, 10 Oct 2016 05:58:57 +0000 (05:58 +0000)
when both mode(server and client) in App is set.
startDiscoveryServer and startListenningServer is calling.
and then both accept thread
and receive thread is running redundantly

Change-Id: I70395ace6115d52d28d2c034ea24c70aafeb671e
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/12799
Reviewed-by: Abhishek Sharma <ce.abhishek@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
resource/csdk/connectivity/src/bt_edr_adapter/android/caedrserver.c

index 36e9b35ebfb47342cf5f5100ff9f736316045ca7..b549ed133e0e8e92c7787c6c924a782a772abb3e 100644 (file)
@@ -45,6 +45,13 @@ static ca_thread_pool_t g_threadPoolHandle = NULL;
 
 static JavaVM *g_jvm;
 
+/**
+ * when Both Mode(server and client) in App is set,
+ * startDiscoveryServer and startListenningServer is calling.
+ * and then both accept thread and receive thread is running redundantly.
+ */
+static bool g_isStartServer = false;
+
 /**
  * Maximum CoAP over TCP header length
  * to know the total data length.
@@ -91,6 +98,11 @@ static ca_mutex g_mutexStateList = NULL;
  */
 static ca_mutex g_mutexObjectList = NULL;
 
+/**
+ * Mutex to synchronize start server state.
+ */
+static ca_mutex g_mutexStartServerState = NULL;
+
 /**
  * Thread context information for unicast, multicast and secured unicast server.
  */
@@ -252,6 +264,15 @@ CAResult_t CAEDRServerStart()
         return CA_STATUS_NOT_INITIALIZED;
     }
 
+    ca_mutex_lock(g_mutexStartServerState);
+    if (g_isStartServer)
+    {
+        OIC_LOG(DEBUG, TAG, "server already started");
+        ca_mutex_unlock(g_mutexStartServerState);
+        return CA_STATUS_OK;
+    }
+    ca_mutex_unlock(g_mutexStartServerState);
+
     CAResult_t res = CAEDRServerStartAcceptThread();
     if (CA_STATUS_OK == res)
     {
@@ -262,6 +283,9 @@ CAResult_t CAEDRServerStart()
             CAEDRServerStop();
             return CA_STATUS_FAILED;
         }
+        ca_mutex_lock(g_mutexStartServerState);
+        g_isStartServer = true;
+        ca_mutex_unlock(g_mutexStartServerState);
     }
 
     return res;
@@ -298,6 +322,9 @@ CAResult_t CAEDRServerStop()
     }
 
     CAEDRNatvieCloseServerTask(env);
+    ca_mutex_lock(g_mutexStartServerState);
+    g_isStartServer = false;
+    ca_mutex_unlock(g_mutexStartServerState);
 
     if (isAttached)
     {
@@ -343,6 +370,12 @@ static void CAEDRServerDestroyMutex()
         ca_mutex_free(g_mutexObjectList);
         g_mutexObjectList = NULL;
     }
+
+    if (g_mutexStartServerState)
+    {
+        ca_mutex_free(g_mutexStartServerState);
+        g_mutexStartServerState = NULL;
+    }
 }
 
 static CAResult_t CAEDRServerCreateMutex()
@@ -390,6 +423,15 @@ static CAResult_t CAEDRServerCreateMutex()
         return CA_STATUS_FAILED;
     }
 
+    g_mutexStartServerState = ca_mutex_new();
+    if (!g_mutexStartServerState)
+    {
+        OIC_LOG(ERROR, TAG, "Failed to created mutex!");
+
+        CAEDRServerDestroyMutex();
+        return CA_STATUS_FAILED;
+    }
+
     return CA_STATUS_OK;
 }