[IoTConnectivity] Added Base implementation
[platform/core/csapi/iotcon.git] / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / ResourceTypes.cs
1 /// Copyright 2016 by Samsung Electronics, Inc.,
2 ///
3 /// This software is the confidential and proprietary information
4 /// of Samsung Electronics, Inc. ("Confidential Information"). You
5 /// shall not disclose such Confidential Information and shall use
6 /// it only in accordance with the terms of the license agreement
7 /// you entered into with Samsung.
8
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Text.RegularExpressions;
13
14 namespace Tizen.Network.IoTConnectivity
15 {
16     /// <summary>
17     /// Class containing resource types
18     /// </summary>
19     public class ResourceTypes : IEnumerable<string>, IDisposable
20     {
21         internal const int MaxLength = 61;
22         internal IntPtr _resourceTypeHandle = IntPtr.Zero;
23         private readonly HashSet<string> _resourceTypes = new HashSet<string>();
24         private bool _disposed = false;
25
26         /// <summary>
27         /// Constructor
28         /// </summary>
29         public ResourceTypes()
30         {
31             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
32             if (ret != (int)IoTConnectivityError.None)
33             {
34                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
35                 throw IoTConnectivityErrorFactory.GetException(ret);
36             }
37         }
38
39         /// <summary>
40         /// Constructor
41         /// </summary>
42         public ResourceTypes(IEnumerable<string> types)
43         {
44             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
45             if (ret != (int)IoTConnectivityError.None)
46             {
47                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
48                 throw IoTConnectivityErrorFactory.GetException(ret);
49             }
50
51             foreach (string type in types)
52             {
53                 Add(type);
54             }
55         }
56
57         /// <summary>
58         /// Constructor
59         /// </summary>
60         internal ResourceTypes(IntPtr typesHandleToClone)
61         {
62             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
63             if (ret != (int)IoTConnectivityError.None)
64             {
65                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
66                 throw IoTConnectivityErrorFactory.GetException(ret);
67             }
68
69             Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
70             {
71                 _resourceTypes.Add(type);
72                 return true;
73             };
74
75             ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
76             if (ret != (int)IoTConnectivityError.None)
77             {
78                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
79                 throw IoTConnectivityErrorFactory.GetException(ret);
80             }
81         }
82
83         ~ResourceTypes()
84         {
85             Dispose(false);
86         }
87
88         /// <summary>
89         /// Count of resource types in the list
90         /// </summary>
91         public int Count
92         {
93             get
94             {
95                 return _resourceTypes.Count;
96             }
97         }
98
99         /// <summary>
100         ///  Inserts a resource type into the list.
101         /// </summary>
102         /// <param name="item">The resource type to add</param>
103         public void Add(string item)
104         {
105             if (IsValid(item))
106             {
107                 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
108                 if (ret != (int)IoTConnectivityError.None)
109                 {
110                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
111                     throw IoTConnectivityErrorFactory.GetException(ret);
112                 }
113                 _resourceTypes.Add(item);
114             }
115             else
116             {
117                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
118                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
119             }
120         }
121
122         /// <summary>
123         ///  Removes a resource type from the list
124         /// </summary>
125         /// <param name="item">The resource type to remove</param>
126         public void Remove(string item)
127         {
128             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
129             if (ret != (int)IoTConnectivityError.None)
130             {
131                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
132                 throw IoTConnectivityErrorFactory.GetException(ret);
133             }
134
135             _resourceTypes.Remove(item);
136         }
137
138         /// <summary>
139         ///  Return enumerator for the list of types
140         /// </summary>
141         /// <returns>Enumerator of the collection</returns>
142         public IEnumerator<string> GetEnumerator()
143         {
144             return _resourceTypes.GetEnumerator();
145         }
146
147         IEnumerator IEnumerable.GetEnumerator()
148         {
149             return _resourceTypes.GetEnumerator();
150         }
151
152         public void Dispose()
153         {
154             Dispose(true);
155             GC.SuppressFinalize(this);
156         }
157
158         internal static bool IsValid(string type)
159         {
160             Regex r = new Regex("^[a-zA-Z0-9.-]+$");
161             return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
162         }
163
164         protected virtual void Dispose(bool disposing)
165         {
166             if (_disposed)
167                 return;
168
169             if (disposing)
170             {
171                 // Free managed objects
172             }
173
174             Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);
175             _disposed = true;
176         }
177     }
178 }