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.Runtime.InteropServices;
14 namespace Tizen.Network.IoTConnectivity
17 /// This class provides APIs to manage query of request.
19 public class ResourceQuery : IDictionary<string, string>, IDisposable
21 internal const int QueryMaxLenth = 64;
22 internal IntPtr _resourceQueryHandle = IntPtr.Zero;
23 private readonly IDictionary<string, string> _query = new Dictionary<string, string>();
24 private bool _disposed = false;
27 /// The resource query constructor
29 /// <seealso cref="Add()"/>
30 /// <seealso cref="Remove()"/>
32 /// ResourceQuery query = new ResourceQuery();
34 public ResourceQuery()
36 int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle);
37 if (ret != (int)IoTConnectivityError.None)
39 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create query");
40 throw IoTConnectivityErrorFactory.GetException(ret);
44 internal ResourceQuery(IntPtr resourceQueryHandleToClone)
46 int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle);
47 if (ret != (int)IoTConnectivityError.None)
49 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create query");
50 throw IoTConnectivityErrorFactory.GetException(ret);
53 Interop.IoTConnectivity.Common.Query.QueryCallback forEachCallback = (string key, string value, IntPtr userData) =>
59 ret = Interop.IoTConnectivity.Common.Query.Foreach(resourceQueryHandleToClone, forEachCallback, IntPtr.Zero);
60 if (ret != (int)IoTConnectivityError.None)
62 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to iterate query");
63 throw IoTConnectivityErrorFactory.GetException(ret);
68 /// Destructor of the ResourceQuery class.
76 /// Gets and sets the resource type of the query
79 /// ResourceQuery query = new ResourceQuery();
80 /// query.Type = "org.tizen.light";
81 /// Console.WriteLine("Type of query : {0}", query.Type);
88 int ret = Interop.IoTConnectivity.Common.Query.GetResourceType(_resourceQueryHandle, out type);
89 if (ret != (int)IoTConnectivityError.None)
91 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
92 throw IoTConnectivityErrorFactory.GetException(ret);
94 return Marshal.PtrToStringAnsi(type);
98 int ret = (int)IoTConnectivityError.InvalidParameter;
99 if (ResourceTypes.IsValid(value))
100 ret = Interop.IoTConnectivity.Common.Query.SetResourceType(_resourceQueryHandle, value);
102 if (ret != (int)IoTConnectivityError.None)
104 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
105 throw IoTConnectivityErrorFactory.GetException(ret);
111 /// Gets and sets the resource interface of the query
114 /// Setter value could be a value such as <see cref="ResourceInterfaces.DefaultInterface"/>
117 /// ResourceQuery query = new ResourceQuery();
118 /// query.Interface = ResourceInterfaces.LinkInterface;
120 public string Interface
125 int ret = Interop.IoTConnectivity.Common.Query.GetInterface(_resourceQueryHandle, out iface);
126 if (ret != (int)IoTConnectivityError.None)
128 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
129 throw IoTConnectivityErrorFactory.GetException(ret);
131 return Marshal.PtrToStringAnsi(iface);
135 int ret = (int)IoTConnectivityError.InvalidParameter;
136 if (ResourceInterfaces.IsValid(value))
137 ret = Interop.IoTConnectivity.Common.Query.SetInterface(_resourceQueryHandle, value);
139 if (ret != (int)IoTConnectivityError.None)
141 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
142 throw IoTConnectivityErrorFactory.GetException(ret);
148 /// Contains all the query keys
151 /// ResourceQuery query = new ResourceQuery();
152 /// query.Add("key", "value");
153 /// query.Add("newKey", "sample value");
154 /// var keys = query.Keys;
155 /// Console.WriteLine("Resource query contains keys {0} and {1}", keys.ElementAt(0), keys.ElementAt(1));
157 public ICollection<string> Keys
166 /// Contains all the query values
169 /// ResourceQuery query = new ResourceQuery();
170 /// query.Add("key", "value");
171 /// query.Add("newKey", "sample value");
172 /// var values = query.Values;
173 /// Console.WriteLine("Resource query contains values {0} and {1}", values.ElementAt(0), values.ElementAt(1));
175 public ICollection<string> Values
179 return _query.Values;
184 /// Gets the number of query elements
187 /// ResourceQuery query = new ResourceQuery();
188 /// query.Add("key", "value");
189 /// query.Add("newKey", "sample value");
190 /// var count = query.Count;
191 /// Console.WriteLine("There are {0} keys in the query object", count);
202 /// Represents whether the collection is readonly
205 /// ResourceQuery query = new ResourceQuery();
206 /// if (query.IsReadOnly)
207 /// Console.WriteLine("Read only query");
209 public bool IsReadOnly
213 return _query.IsReadOnly;
218 /// Gets or sets the query data
220 /// <param name="key">The query key to get or set.</param>
221 /// <returns>The query with the specified key.</returns>
223 /// ResourceQuery query = new ResourceQuery();
224 /// query["key1"] = "sample-data";
225 /// Console.WriteLine("query has : {0}", query["key1"]);
227 public string this[string key]
241 /// Checks whether the given key exists in Query collection
243 /// <param name="key">The key to look for</param>
244 /// <returns>true if exists. Otherwise, false</returns>
246 /// ResourceQuery query = new ResourceQuery();
247 /// query.Add("key1", "value1");
248 /// if (query.ContainsKey("key1"))
249 /// Console.WriteLine("query conatins key : key1");
251 public bool ContainsKey(string key)
253 return _query.ContainsKey(key);
257 /// Adds a new key and correspoding value into the query.
260 /// The full length of query should be less than or equal to 64.
262 /// <param name="key">The key of the query to insert</param>
263 /// <param name="value">The string data to insert into the query</param>
264 /// <seealso cref="Remove()"/>
266 /// ResourceQuery query = new ResourceQuery();
267 /// query.Add("key1", "value1");
269 public void Add(string key, string value)
271 if (CanAddQuery(key, value))
273 int ret = Interop.IoTConnectivity.Common.Query.Add(_resourceQueryHandle, key, value);
274 if (ret != (int)IoTConnectivityError.None)
276 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add query");
277 throw IoTConnectivityErrorFactory.GetException(ret);
279 _query.Add(key, value);
283 Log.Error(IoTConnectivityErrorFactory.LogTag, "Query cannot be added");
284 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
289 /// Removes the key and its associated value from the query.
291 /// <param name="key">The id of the query to delete</param>
292 /// <returns>True if operation is successful. Otherwise, false</returns>
293 /// <seealso cref="Add()"/>
295 /// ResourceQuery query = new ResourceQuery();
296 /// query.Add("key1", "value1");
297 /// var result = query.Remove("key1");
299 public bool Remove(string key)
301 bool isRemoved = _query.Remove(key);
304 int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key);
305 if (ret != (int)IoTConnectivityError.None)
307 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove query");
308 throw IoTConnectivityErrorFactory.GetException(ret);
315 /// Gets the value associated with the specified key.
317 /// <param name="key">The query key</param>
318 /// <param name="value">Value corresponding to query key</param>
319 /// <returns>True if the key exists, false otherwise</returns>
321 /// ResourceQuery query = new ResourceQuery();
322 /// query.Add("key1", "value1");
324 /// var isPresent = query.TryGetValue("key1", out value);
326 /// Console.WriteLine("value : {0}", value);
328 public bool TryGetValue(string key, out string value)
330 return _query.TryGetValue(key, out value);
334 /// Adds query key and value as a key value pair
336 /// <param name="item">The key value pair</param>
337 /// <seealso cref="Remove()"/>
339 /// ResourceQuery query = new ResourceQuery();
340 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
342 public void Add(KeyValuePair<string, string> item)
344 Add(item.Key, item.Value);
348 /// Clears the Query collection
351 /// ResourceQuery query = new ResourceQuery();
352 /// query.Add("key1", "value1");
353 /// query.Add("key2", "value2");
358 foreach (string key in _query.Keys)
360 int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key);
361 if (ret != (int)IoTConnectivityError.None)
363 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to clear query");
364 throw IoTConnectivityErrorFactory.GetException(ret);
371 /// Checks if the given query pair exists
373 /// <param name="item">The key value pair</param>
374 /// <returns>True if exists. Otherwise, false</returns>
376 /// ResourceQuery query = new ResourceQuery();
377 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
378 /// var isPresent = query.Contains(new KeyValuePair<string, string>("key1", "value1"));
380 /// Console.WriteLine("Key value pair is present");
382 public bool Contains(KeyValuePair<string, string> item)
384 return _query.Contains(item);
388 /// Copies the elements of the query collection to an Array, starting at a particular index.
390 /// <param name="array">The destination array</param>
391 /// <param name="arrayIndex">Index parameter</param>
393 /// ResourceQuery query = new ResourceQuery();
394 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
395 /// KeyValuePair<string, string>[] dest = new KeyValuePair<string, string>[query.Count];
396 /// query.CopyTo(dest, 0);
397 /// Console.WriteLine("Dest conatins ({0}, {1})", dest[0].Key, dest[0].Value);
399 public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
401 _query.CopyTo(array, arrayIndex);
405 /// Remove the given key value pair from the query
407 /// <param name="item">The key value pair to remove</param>
408 /// <returns>True if operation is successful. Otherwise, false</returns>
409 /// <seealso cref="Add()"/>
411 /// ResourceQuery query = new ResourceQuery();
412 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
413 /// var result = query.Remove(new KeyValuePair<string, string>("key1", "value1"));
415 public bool Remove(KeyValuePair<string, string> item)
417 return Remove(item.Key);
421 /// Get the enumerator to query collection
423 /// <returns>Enumerator to query pairs</returns>
425 /// ResourceQuery query = new ResourceQuery();
426 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
427 /// query.Add(new KeyValuePair<string, string>("key2", "value2"));
428 /// foreach (KeyValuePair<string, string> pair in query)
430 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
433 public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
435 return _query.GetEnumerator();
439 /// Get the enumerator to query collection
441 /// <returns>Enumerator to query pairs</returns>
443 /// ResourceQuery query = new ResourceQuery();
444 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
445 /// query.Add(new KeyValuePair<string, string>("key2", "value2"));
446 /// foreach (KeyValuePair<string, string> pair in query)
448 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
451 IEnumerator IEnumerable.GetEnumerator()
453 return _query.GetEnumerator();
457 /// Releases any unmanaged resources used by this object.
459 public void Dispose()
462 GC.SuppressFinalize(this);
466 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
468 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
469 protected virtual void Dispose(bool disposing)
476 // Free managed objects
479 Interop.IoTConnectivity.Common.Query.Destroy(_resourceQueryHandle);
483 private bool CanAddQuery(string newKey, string newValue)
486 foreach (string key in Keys)
488 queryLenth += key.Length + 2;
490 foreach (string value in Values)
492 queryLenth += value.Length;
495 if ((newKey.Length + newValue.Length + queryLenth + 2) < QueryMaxLenth)