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.Runtime.InteropServices;
23 namespace Tizen.Network.IoTConnectivity
26 /// This class provides APIs to manage query of request.
28 public class ResourceQuery : IDictionary<string, string>, IDisposable
30 internal const int QueryMaxLenth = 64;
31 internal IntPtr _resourceQueryHandle = IntPtr.Zero;
32 private readonly IDictionary<string, string> _query = new Dictionary<string, string>();
33 private bool _disposed = false;
36 /// The resource query constructor
38 /// <seealso cref="Add()"/>
39 /// <seealso cref="Remove()"/>
40 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
41 /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
42 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
44 /// ResourceQuery query = new ResourceQuery();
46 public ResourceQuery()
48 int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle);
49 if (ret != (int)IoTConnectivityError.None)
51 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create query");
52 throw IoTConnectivityErrorFactory.GetException(ret);
56 internal ResourceQuery(IntPtr resourceQueryHandleToClone)
58 int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle);
59 if (ret != (int)IoTConnectivityError.None)
61 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create query");
62 throw IoTConnectivityErrorFactory.GetException(ret);
65 Interop.IoTConnectivity.Common.Query.QueryCallback forEachCallback = (string key, string value, IntPtr userData) =>
71 ret = Interop.IoTConnectivity.Common.Query.Foreach(resourceQueryHandleToClone, forEachCallback, IntPtr.Zero);
72 if (ret != (int)IoTConnectivityError.None)
74 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to iterate query");
75 throw IoTConnectivityErrorFactory.GetException(ret);
80 /// Destructor of the ResourceQuery class.
88 /// Gets and sets the resource type of the query
90 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
91 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
92 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
94 /// ResourceQuery query = new ResourceQuery();
95 /// query.Type = "org.tizen.light";
96 /// Console.WriteLine("Type of query : {0}", query.Type);
103 int ret = Interop.IoTConnectivity.Common.Query.GetResourceType(_resourceQueryHandle, out type);
104 if (ret != (int)IoTConnectivityError.None)
106 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
109 return Marshal.PtrToStringAnsi(type);
113 int ret = (int)IoTConnectivityError.InvalidParameter;
114 if (ResourceTypes.IsValid(value))
115 ret = Interop.IoTConnectivity.Common.Query.SetResourceType(_resourceQueryHandle, value);
117 if (ret != (int)IoTConnectivityError.None)
119 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
120 throw IoTConnectivityErrorFactory.GetException(ret);
126 /// Gets and sets the resource interface of the query
129 /// Setter value could be a value such as <see cref="ResourceInterfaces.DefaultInterface"/>
131 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
132 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
133 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
135 /// ResourceQuery query = new ResourceQuery();
136 /// query.Interface = ResourceInterfaces.LinkInterface;
138 public string Interface
143 int ret = Interop.IoTConnectivity.Common.Query.GetInterface(_resourceQueryHandle, out iface);
144 if (ret != (int)IoTConnectivityError.None)
146 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
149 return Marshal.PtrToStringAnsi(iface);
153 int ret = (int)IoTConnectivityError.InvalidParameter;
154 if (ResourceInterfaces.IsValid(value))
155 ret = Interop.IoTConnectivity.Common.Query.SetInterface(_resourceQueryHandle, value);
157 if (ret != (int)IoTConnectivityError.None)
159 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
160 throw IoTConnectivityErrorFactory.GetException(ret);
166 /// Contains all the query keys
169 /// ResourceQuery query = new ResourceQuery();
170 /// query.Add("key", "value");
171 /// query.Add("newKey", "sample value");
172 /// var keys = query.Keys;
173 /// Console.WriteLine("Resource query contains keys {0} and {1}", keys.ElementAt(0), keys.ElementAt(1));
175 public ICollection<string> Keys
184 /// Contains all the query values
187 /// ResourceQuery query = new ResourceQuery();
188 /// query.Add("key", "value");
189 /// query.Add("newKey", "sample value");
190 /// var values = query.Values;
191 /// Console.WriteLine("Resource query contains values {0} and {1}", values.ElementAt(0), values.ElementAt(1));
193 public ICollection<string> Values
197 return _query.Values;
202 /// Gets the number of query elements
205 /// ResourceQuery query = new ResourceQuery();
206 /// query.Add("key", "value");
207 /// query.Add("newKey", "sample value");
208 /// var count = query.Count;
209 /// Console.WriteLine("There are {0} keys in the query object", count);
220 /// Gets or sets the query data
222 /// <param name="key">The query key to get or set.</param>
223 /// <returns>The query with the specified key.</returns>
225 /// ResourceQuery query = new ResourceQuery();
226 /// query["key1"] = "sample-data";
227 /// Console.WriteLine("query has : {0}", query["key1"]);
229 public string this[string key]
243 /// Checks whether the given key exists in Query collection
245 /// <param name="key">The key to look for</param>
246 /// <returns>true if exists. Otherwise, false</returns>
248 /// ResourceQuery query = new ResourceQuery();
249 /// query.Add("key1", "value1");
250 /// if (query.ContainsKey("key1"))
251 /// Console.WriteLine("query conatins key : key1");
253 public bool ContainsKey(string key)
255 return _query.ContainsKey(key);
259 /// Adds a new key and correspoding value into the query.
262 /// The full length of query should be less than or equal to 64.
264 /// <param name="key">The key of the query to insert</param>
265 /// <param name="value">The string data to insert into the query</param>
266 /// <seealso cref="Remove()"/>
267 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
268 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
269 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
271 /// ResourceQuery query = new ResourceQuery();
272 /// query.Add("key1", "value1");
274 public void Add(string key, string value)
276 if (CanAddQuery(key, value))
278 int ret = Interop.IoTConnectivity.Common.Query.Add(_resourceQueryHandle, key, value);
279 if (ret != (int)IoTConnectivityError.None)
281 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add query");
282 throw IoTConnectivityErrorFactory.GetException(ret);
284 _query.Add(key, value);
288 Log.Error(IoTConnectivityErrorFactory.LogTag, "Query cannot be added");
289 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
294 /// Removes the key and its associated value from the query.
296 /// <param name="key">The id of the query to delete</param>
297 /// <returns>True if operation is successful. Otherwise, false</returns>
298 /// <seealso cref="Add()"/>
299 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
300 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
301 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
303 /// ResourceQuery query = new ResourceQuery();
304 /// query.Add("key1", "value1");
305 /// var result = query.Remove("key1");
307 public bool Remove(string key)
309 bool isRemoved = _query.Remove(key);
312 int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key);
313 if (ret != (int)IoTConnectivityError.None)
315 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove query");
316 throw IoTConnectivityErrorFactory.GetException(ret);
323 /// Gets the value associated with the specified key.
325 /// <param name="key">The query key</param>
326 /// <param name="value">Value corresponding to query key</param>
327 /// <returns>True if the key exists, false otherwise</returns>
329 /// ResourceQuery query = new ResourceQuery();
330 /// query.Add("key1", "value1");
332 /// var isPresent = query.TryGetValue("key1", out value);
334 /// Console.WriteLine("value : {0}", value);
336 public bool TryGetValue(string key, out string value)
338 return _query.TryGetValue(key, out value);
342 /// Adds query key and value as a key value pair
344 /// <param name="item">The key value pair</param>
345 /// <seealso cref="Remove()"/>
347 /// ResourceQuery query = new ResourceQuery();
348 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
350 public void Add(KeyValuePair<string, string> item)
352 Add(item.Key, item.Value);
356 /// Clears the Query collection
358 /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
359 /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
360 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
362 /// ResourceQuery query = new ResourceQuery();
363 /// query.Add("key1", "value1");
364 /// query.Add("key2", "value2");
369 foreach (string key in _query.Keys)
371 int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key);
372 if (ret != (int)IoTConnectivityError.None)
374 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to clear query");
375 throw IoTConnectivityErrorFactory.GetException(ret);
382 /// Checks if the given query pair exists
384 /// <param name="item">The key value pair</param>
385 /// <returns>True if exists. Otherwise, false</returns>
387 /// ResourceQuery query = new ResourceQuery();
388 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
389 /// var isPresent = query.Contains(new KeyValuePair<string, string>("key1", "value1"));
391 /// Console.WriteLine("Key value pair is present");
393 public bool Contains(KeyValuePair<string, string> item)
395 return _query.Contains(item);
399 /// Copies the elements of the query collection to an Array, starting at a particular index.
401 /// <param name="array">The destination array</param>
402 /// <param name="arrayIndex">Index parameter</param>
404 /// ResourceQuery query = new ResourceQuery();
405 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
406 /// KeyValuePair<string, string>[] dest = new KeyValuePair<string, string>[query.Count];
407 /// query.CopyTo(dest, 0);
408 /// Console.WriteLine("Dest conatins ({0}, {1})", dest[0].Key, dest[0].Value);
410 public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
412 _query.CopyTo(array, arrayIndex);
416 /// Remove the given key value pair from the query
418 /// <param name="item">The key value pair to remove</param>
419 /// <returns>True if operation is successful. Otherwise, false</returns>
420 /// <seealso cref="Add()"/>
422 /// ResourceQuery query = new ResourceQuery();
423 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
424 /// var result = query.Remove(new KeyValuePair<string, string>("key1", "value1"));
426 public bool Remove(KeyValuePair<string, string> item)
428 return Remove(item.Key);
432 /// Get the enumerator to query collection
434 /// <returns>Enumerator to query pairs</returns>
436 /// ResourceQuery query = new ResourceQuery();
437 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
438 /// query.Add(new KeyValuePair<string, string>("key2", "value2"));
439 /// foreach (KeyValuePair<string, string> pair in query)
441 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
444 public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
446 return _query.GetEnumerator();
450 /// Get the enumerator to query collection
452 /// <returns>Enumerator to query pairs</returns>
454 /// ResourceQuery query = new ResourceQuery();
455 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
456 /// query.Add(new KeyValuePair<string, string>("key2", "value2"));
457 /// foreach (KeyValuePair<string, string> pair in query)
459 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
462 IEnumerator IEnumerable.GetEnumerator()
464 return _query.GetEnumerator();
468 /// Releases any unmanaged resources used by this object.
470 public void Dispose()
473 GC.SuppressFinalize(this);
477 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
479 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
480 protected virtual void Dispose(bool disposing)
487 // Free managed objects
490 Interop.IoTConnectivity.Common.Query.Destroy(_resourceQueryHandle);
494 private bool CanAddQuery(string newKey, string newValue)
497 foreach (string key in Keys)
499 queryLenth += key.Length + 2;
501 foreach (string value in Values)
503 queryLenth += value.Length;
506 if ((newKey.Length + newValue.Length + queryLenth + 2) < QueryMaxLenth)