1fc9faccd129d2d075c0ec3f850e054149d8f330
[platform/core/csapi/tizenfx.git] / src / 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     /// This class contains resource types and provides APIs to manage, add, remove those types.
18     /// A resource type indicates a class or category of resources.
19     /// </summary>
20     public class ResourceTypes : IEnumerable<string>, IDisposable
21     {
22         internal const int MaxLength = 61;
23         internal IntPtr _resourceTypeHandle = IntPtr.Zero;
24         private readonly HashSet<string> _resourceTypes = new HashSet<string>();
25         private bool _disposed = false;
26
27         /// <summary>
28         /// Constructor of ResourceTypes
29         /// </summary>
30         /// <seealso cref="Add()"/>
31         /// <seealso cref="Remove()"/>
32         /// <code>
33         /// ResourceTypes types = new ResourceTypes();
34         /// </code>
35         public ResourceTypes()
36         {
37             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
38             if (ret != (int)IoTConnectivityError.None)
39             {
40                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
41                 throw IoTConnectivityErrorFactory.GetException(ret);
42             }
43         }
44
45         /// <summary>
46         /// Constructor of ResourceTypes using list of types
47         /// </summary>
48         /// <param name="types">List of resource types</param>
49         /// <code>
50         /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
51         /// </code>
52         public ResourceTypes(IEnumerable<string> types)
53         {
54             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
55             if (ret != (int)IoTConnectivityError.None)
56             {
57                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
58                 throw IoTConnectivityErrorFactory.GetException(ret);
59             }
60
61             foreach (string type in types)
62             {
63                 Add(type);
64             }
65         }
66
67         internal ResourceTypes(IntPtr typesHandleToClone)
68         {
69             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
70             if (ret != (int)IoTConnectivityError.None)
71             {
72                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
73                 throw IoTConnectivityErrorFactory.GetException(ret);
74             }
75
76             Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
77             {
78                 _resourceTypes.Add(type);
79                 return true;
80             };
81
82             ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
83             if (ret != (int)IoTConnectivityError.None)
84             {
85                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
86                 throw IoTConnectivityErrorFactory.GetException(ret);
87             }
88         }
89
90         /// <summary>
91         /// Destructor of the ResourceTypes class.
92         /// </summary>
93         ~ResourceTypes()
94         {
95             Dispose(false);
96         }
97
98         /// <summary>
99         /// Indicates count of types in the list
100         /// </summary>
101         /// <code>
102         /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
103         /// Console.WriteLine("There are {0} items", types.Count);
104         /// </code>
105         public int Count
106         {
107             get
108             {
109                 return _resourceTypes.Count;
110             }
111         }
112
113         /// <summary>
114         /// Adds a resource type into the list.
115         /// </summary>
116         /// <remarks>
117         /// The length of @a item should be less than or equal to 61.\n
118         /// The @a item must start with a lowercase alphabetic character, followed by a sequence
119         /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.\n
120         /// Duplicate strings are not allowed.
121         /// </remarks>
122         /// <param name="item">The string data to insert into the resource types</param>
123         /// <seealso cref="Remove()"/>
124         /// <code>
125         /// ResourceTypes resourceTypes = new ResourceTypes();
126         /// resourceTypes.Add("org.tizen.light");
127         /// </code>
128         public void Add(string item)
129         {
130             if (IsValid(item))
131             {
132                 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
133                 if (ret != (int)IoTConnectivityError.None)
134                 {
135                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
136                     throw IoTConnectivityErrorFactory.GetException(ret);
137                 }
138                 _resourceTypes.Add(item);
139             }
140             else
141             {
142                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
143                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
144             }
145         }
146
147         /// <summary>
148         /// Removes a resource type from the list
149         /// </summary>
150         /// <param name="item">The string data to delete from the resource types</param>
151         /// <seealso cref="Add()"/>
152         /// <code>
153         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
154         /// resourceTypes.Remove("oic.if.room");
155         /// </code>
156         public void Remove(string item)
157         {
158             bool isRemoved = _resourceTypes.Remove(item);
159             if (isRemoved)
160             {
161                 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
162                 if (ret != (int)IoTConnectivityError.None)
163                 {
164                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
165                     throw IoTConnectivityErrorFactory.GetException(ret);
166                 }
167             }
168             else
169                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
170         }
171
172         /// <summary>
173         /// Return enumerator for the list of types
174         /// </summary>
175         /// <returns>The enumerator</returns>
176         /// <code>
177         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
178         /// foreach(string item in resourceTypes)
179         /// {
180         ///     Console.WriteLine("Type : {0}", item);
181         /// }
182         /// </code>
183         public IEnumerator<string> GetEnumerator()
184         {
185             return _resourceTypes.GetEnumerator();
186         }
187
188         /// <summary>
189         /// Return enumerator for the list of types
190         /// </summary>
191         /// <returns>The enumerator</returns>
192         /// <code>
193         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
194         /// foreach(string item in resourceTypes)
195         /// {
196         ///     Console.WriteLine("Type : {0}", item);
197         /// }
198         /// </code>
199         IEnumerator IEnumerable.GetEnumerator()
200         {
201             return _resourceTypes.GetEnumerator();
202         }
203
204         /// <summary>
205         /// Releases any unmanaged resources used by this object.
206         /// </summary>
207         public void Dispose()
208         {
209             Dispose(true);
210             GC.SuppressFinalize(this);
211         }
212
213         internal static bool IsValid(string type)
214         {
215             Regex r = new Regex("^[a-zA-Z0-9.-]+$");
216             return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
217         }
218
219         /// <summary>
220         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
221         /// </summary>
222         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
223         protected virtual void Dispose(bool disposing)
224         {
225             if (_disposed)
226                 return;
227
228             if (disposing)
229             {
230                 // Free managed objects
231             }
232
233             Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);
234             _disposed = true;
235         }
236     }
237 }