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());
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);
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;
/*
* 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
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);
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
/*
* 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()
/*
* 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;
}
{
DbgPort.E("Failed to start finding resource: " + err.Message);
+ resourceQuery.Dispose();
+
ZarieModel.Instance.IsConnected = false;
return;
}
+
+ resourceQuery.Dispose();
}
public void Dispose()
ZarieModel.Instance.IsConnected = false;
+ ResourceCheckTimer.Dispose();
+
IoTConnectivityClientManager.Deinitialize();
GC.SuppressFinalize(this);
}