2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Text.RegularExpressions;
23 namespace Tizen.Network.IoTConnectivity
26 /// This class contains resource types and provides APIs to manage, add, remove those types.
27 /// A resource type indicates a class or category of resources.
29 public class ResourceTypes : IEnumerable<string>, IDisposable
31 internal const int MaxLength = 61;
32 internal IntPtr _resourceTypeHandle = IntPtr.Zero;
33 private readonly HashSet<string> _resourceTypes = new HashSet<string>();
34 private bool _disposed = false;
37 /// Constructor of ResourceTypes
39 /// <seealso cref="Add()"/>
40 /// <seealso cref="Remove()"/>
41 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
42 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
43 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
45 /// ResourceTypes types = new ResourceTypes();
47 public ResourceTypes()
49 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
50 if (ret != (int)IoTConnectivityError.None)
52 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
53 throw IoTConnectivityErrorFactory.GetException(ret);
58 /// Constructor of ResourceTypes using list of types
60 /// <param name="types">List of resource types</param>
62 /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
64 public ResourceTypes(IEnumerable<string> types)
66 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
67 if (ret != (int)IoTConnectivityError.None)
69 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
70 throw IoTConnectivityErrorFactory.GetException(ret);
73 foreach (string type in types)
79 internal ResourceTypes(IntPtr typesHandleToClone)
81 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
82 if (ret != (int)IoTConnectivityError.None)
84 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
85 throw IoTConnectivityErrorFactory.GetException(ret);
88 Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
90 _resourceTypes.Add(type);
94 ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
95 if (ret != (int)IoTConnectivityError.None)
97 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
98 throw IoTConnectivityErrorFactory.GetException(ret);
103 /// Destructor of the ResourceTypes class.
111 /// Indicates count of types in the list
114 /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
115 /// Console.WriteLine("There are {0} items", types.Count);
121 return _resourceTypes.Count;
126 /// Adds a resource type into the list.
129 /// The length of @a item should be less than or equal to 61.\n
130 /// The @a item must start with a lowercase alphabetic character, followed by a sequence
131 /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.\n
132 /// Duplicate strings are not allowed.
134 /// <param name="item">The string data to insert into the resource types</param>
135 /// <seealso cref="Remove()"/>
136 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
137 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
138 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
140 /// ResourceTypes resourceTypes = new ResourceTypes();
141 /// resourceTypes.Add("org.tizen.light");
143 public void Add(string item)
147 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
148 if (ret != (int)IoTConnectivityError.None)
150 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
151 throw IoTConnectivityErrorFactory.GetException(ret);
153 _resourceTypes.Add(item);
157 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
158 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
163 /// Removes a resource type from the list
165 /// <param name="item">The string data to delete from the resource types</param>
166 /// <seealso cref="Add()"/>
167 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
168 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
169 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
171 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
172 /// resourceTypes.Remove("oic.if.room");
174 public void Remove(string item)
176 bool isRemoved = _resourceTypes.Remove(item);
179 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
180 if (ret != (int)IoTConnectivityError.None)
182 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
183 throw IoTConnectivityErrorFactory.GetException(ret);
187 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
191 /// Return enumerator for the list of types
193 /// <returns>The enumerator</returns>
195 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
196 /// foreach(string item in resourceTypes)
198 /// Console.WriteLine("Type : {0}", item);
201 public IEnumerator<string> GetEnumerator()
203 return _resourceTypes.GetEnumerator();
207 /// Return enumerator for the list of types
209 /// <returns>The enumerator</returns>
211 /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
212 /// foreach(string item in resourceTypes)
214 /// Console.WriteLine("Type : {0}", item);
217 IEnumerator IEnumerable.GetEnumerator()
219 return _resourceTypes.GetEnumerator();
223 /// Releases any unmanaged resources used by this object.
225 public void Dispose()
228 GC.SuppressFinalize(this);
231 internal static bool IsValid(string type)
233 Regex r = new Regex("^[a-zA-Z0-9.-]+$");
234 return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
238 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
240 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
241 protected virtual void Dispose(bool disposing)
248 // Free managed objects
251 Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);