Update remote resources management logic 18/142218/5
authorGeunsun, Lee <gs86.lee@samsung.com>
Thu, 3 Aug 2017 05:47:13 +0000 (14:47 +0900)
committerGeunsun, Lee <gs86.lee@samsung.com>
Fri, 4 Aug 2017 01:19:04 +0000 (10:19 +0900)
Change-Id: I4eb09ee7077678c8df9563b163d552cdceb167cf

PositionFinderClient/PositionFinderClient.Tizen/Model/ConnectivityModel.cs

index 0e4794d..bc069f9 100644 (file)
@@ -23,16 +23,25 @@ using PositionFinderClient.Tizen.Utils;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Threading;
 using Tizen.Network.IoTConnectivity;
 
 namespace PositionFinderClient.Tizen.Model
 {
+    public class ConnectionInformation
+    {
+        public RemoteResource RemoteResource { get; set; }
+
+        public DateTime ReceivedTime { get; set; }
+    }
+
     public sealed class ConnectivityModel : IDisposable
     {
-        private IList<string> RemoteResourceList = new List<string>();
-        private bool IsDispose = false;
-        private static int MaxObserveQueueElements = 20;
+        private static bool IsDispose = false;
+        private static List<ConnectionInformation> RemoteResourceList = new List<ConnectionInformation>();
         private static Queue<bool> observeQueue = new Queue<bool>();
+        private static Timer ResourceCheckTimer = null;
+        private const int MaxObserveQueueElements = 20;
 
         private static readonly Lazy<ConnectivityModel> instance = new Lazy<ConnectivityModel>(() => new ConnectivityModel());
 
@@ -46,8 +55,28 @@ namespace PositionFinderClient.Tizen.Model
             Dispose();
         }
 
+        private void ResourceCheckTimerCallBack(object state)
+        {
+            for (int i = 0; i < RemoteResourceList.Count; i++)
+            {
+                var connectionInfo = RemoteResourceList[i];
+                if (DateTime.Now > connectionInfo.ReceivedTime.Add(new TimeSpan(0, 0, 10)))
+                {
+                    connectionInfo.RemoteResource.StopObserving();
+                    connectionInfo.RemoteResource.Dispose();
+                    RemoteResourceList.Remove(connectionInfo);
+                }
+            }
+        }
+
         private void ObserverNotifiedEventHandler(object sender, ObserverNotifiedEventArgs e)
         {
+            var connectionInfo = RemoteResourceList.Find(obj => obj.RemoteResource.DeviceId.Equals((sender as RemoteResource).DeviceId));
+            if (connectionInfo != null)
+            {
+                connectionInfo.ReceivedTime = DateTime.Now;
+            }
+
             var representation = e.Representation;
 
             DbgPort.D("Observe Result: " + e.Result);
@@ -95,11 +124,9 @@ namespace PositionFinderClient.Tizen.Model
         private void StateChangedEventHandler(object sender, StateChangedEventArgs e)
         {
             DbgPort.D("Resource state: " + Enum.GetName(typeof(ResourceState), e.State));
-
-            ZarieModel.Instance.IsConnected = (e.State == ResourceState.Alive) ? true: false;
         }
 
-        private void resourceFoundEventHandler(object sender, ResourceFoundEventArgs e)
+        private void ResourceFoundEventHandler(object sender, ResourceFoundEventArgs e)
         {
             var remoteResource = e.Resource;
 
@@ -108,12 +135,19 @@ namespace PositionFinderClient.Tizen.Model
             /*
              * 1. Checks that the resource is already exist
              */
-            if (RemoteResourceList.Contains(remoteResource.DeviceId))
+            var info = RemoteResourceList.Find(obj => obj.RemoteResource.DeviceId.Equals(remoteResource.DeviceId));
+            if (info != null)
             {
                 DbgPort.D("Already found. skip!(" + remoteResource.DeviceId + ")");
                 return;
             }
-            RemoteResourceList.Add(remoteResource.DeviceId);
+
+            info = new ConnectionInformation()
+            {
+                RemoteResource = remoteResource,
+                ReceivedTime = DateTime.Now,
+            };
+            RemoteResourceList.Add(info);
 
             /*
              * 2. Gets the information of the remote resource
@@ -169,7 +203,7 @@ namespace PositionFinderClient.Tizen.Model
             ZarieModel.Instance.IsConnected = true;
         }
 
-        private void findingErrorOccurredventHandler(object sender, FindingErrorOccurredEventArgs e)
+        private void FindingErrorOccurredventHandler(object sender, FindingErrorOccurredEventArgs e)
         {
             DbgPort.E("Error(" + e.RequestId + "): " + e.Error.Message);
 
@@ -177,27 +211,6 @@ namespace PositionFinderClient.Tizen.Model
             ZarieModel.Instance.IsConnected = false;
         }
 
-        private ResourceQuery CreateResourceQuery()
-        {
-            ResourceQuery query = null;
-
-            try
-            {
-                query = new ResourceQuery()
-                {
-                    Type = "org.tizen.door",
-                };
-            }
-            catch (Exception err)
-            {
-                DbgPort.E("Failed to create ResouceQuery: " + err.Message);
-
-                return null;
-            }
-
-            return query;
-        }
-
         public static ConnectivityModel Instance
         {
             get
@@ -239,13 +252,15 @@ namespace PositionFinderClient.Tizen.Model
             /*
              * 2. Registers EventHandlers
              */
-            IoTConnectivityClientManager.FindingErrorOccurred += findingErrorOccurredventHandler;
-            IoTConnectivityClientManager.ResourceFound += resourceFoundEventHandler;
+            IoTConnectivityClientManager.FindingErrorOccurred += FindingErrorOccurredventHandler;
+            IoTConnectivityClientManager.ResourceFound += ResourceFoundEventHandler;
 
             /*
              * 3. Tries to connect to the server
              */
             TryConnection();
+
+            ResourceCheckTimer = new Timer(ResourceCheckTimerCallBack, null, 0, 5000);
         }
 
         public void TryConnection()
@@ -253,12 +268,17 @@ namespace PositionFinderClient.Tizen.Model
             /*
              * 1. Creates the query to find resource
              */
-            ResourceQuery resourceQuery = CreateResourceQuery();
-            if (resourceQuery == null)
+            ResourceQuery resourceQuery = null;
+            try
             {
-                DbgPort.E("Failed to create Query");
-
-                ZarieModel.Instance.IsConnected = false;
+                resourceQuery = new ResourceQuery()
+                {
+                    Type = "org.tizen.door",
+                };
+            }
+            catch (Exception err)
+            {
+                DbgPort.E("Failed to create ResouceQuery: " + err.Message);
 
                 return;
             }
@@ -274,10 +294,14 @@ namespace PositionFinderClient.Tizen.Model
             {
                 DbgPort.E("Failed to start finding resource: " + err.Message);
 
+                resourceQuery.Dispose();
+
                 ZarieModel.Instance.IsConnected = false;
 
                 return;
             }
+
+            resourceQuery.Dispose();
         }
 
         public void Dispose()
@@ -290,6 +314,8 @@ namespace PositionFinderClient.Tizen.Model
 
             ZarieModel.Instance.IsConnected = false;
 
+            ResourceCheckTimer.Dispose();
+
             IoTConnectivityClientManager.Deinitialize();
             GC.SuppressFinalize(this);
         }