1 /// Copyright 2016 by Samsung Electronics, Inc.,
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.
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Text.RegularExpressions;
14 namespace Tizen.Network.IoTConnectivity
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.
20 public class ResourceTypes : IEnumerable<string>, IDisposable
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;
28 /// Constructor of ResourceTypes
30 /// <seealso cref="Add()"/>
31 /// <seealso cref="Remove()"/>
33 /// ResourceTypes types = new ResourceTypes();
35 public ResourceTypes()
37 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
38 if (ret != (int)IoTConnectivityError.None)
40 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
41 throw IoTConnectivityErrorFactory.GetException(ret);
46 /// Constructor of ResourceTypes using list of types
48 /// <param name="types">List of resource types</param>
50 /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
52 public ResourceTypes(IEnumerable<string> types)
54 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
55 if (ret != (int)IoTConnectivityError.None)
57 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
58 throw IoTConnectivityErrorFactory.GetException(ret);
61 foreach (string type in types)
67 internal ResourceTypes(IntPtr typesHandleToClone)
69 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
70 if (ret != (int)IoTConnectivityError.None)
72 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
73 throw IoTConnectivityErrorFactory.GetException(ret);
76 Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
78 _resourceTypes.Add(type);
82 ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
83 if (ret != (int)IoTConnectivityError.None)
85 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
86 throw IoTConnectivityErrorFactory.GetException(ret);
91 /// Destructor of the ResourceTypes class.
99 /// Indicates count of types in the list
102 /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
103 /// Console.WriteLine("There are {0} items", types.Count);
109 return _resourceTypes.Count;
114 /// Adds a resource type into the list.
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.
122 /// <param name="item">The string data to insert into the resource types</param>
123 /// <seealso cref="Remove()"/>
125 /// ResourceTypes resourceTypes = new ResourceTypes();
126 /// resourceTypes.Add("org.tizen.light");
128 public void Add(string item)
132 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
133 if (ret != (int)IoTConnectivityError.None)
135 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
136 throw IoTConnectivityErrorFactory.GetException(ret);
138 _resourceTypes.Add(item);
142 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
143 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
148 /// Removes a resource type from the list
150 /// <param name="item">The string data to delete from the resource types</param>
151 /// <seealso cref="Add()"/>
153 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
154 /// resourceTypes.Remove("oic.if.room");
156 public void Remove(string item)
158 bool isRemoved = _resourceTypes.Remove(item);
161 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
162 if (ret != (int)IoTConnectivityError.None)
164 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
165 throw IoTConnectivityErrorFactory.GetException(ret);
169 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
173 /// Return enumerator for the list of types
175 /// <returns>The enumerator</returns>
177 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
178 /// foreach(string item in resourceTypes)
180 /// Console.WriteLine("Type : {0}", item);
183 public IEnumerator<string> GetEnumerator()
185 return _resourceTypes.GetEnumerator();
189 /// Return enumerator for the list of types
191 /// <returns>The enumerator</returns>
193 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
194 /// foreach(string item in resourceTypes)
196 /// Console.WriteLine("Type : {0}", item);
199 IEnumerator IEnumerable.GetEnumerator()
201 return _resourceTypes.GetEnumerator();
205 /// Releases any unmanaged resources used by this object.
207 public void Dispose()
210 GC.SuppressFinalize(this);
213 internal static bool IsValid(string type)
215 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
216 return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
220 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
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)
230 // Free managed objects
233 Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);