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()"/>
41 /// ResourceQuery query = new ResourceQuery();
43 public ResourceQuery()
45 int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle);
46 if (ret != (int)IoTConnectivityError.None)
48 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create query");
49 throw IoTConnectivityErrorFactory.GetException(ret);
53 internal ResourceQuery(IntPtr resourceQueryHandleToClone)
55 int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle);
56 if (ret != (int)IoTConnectivityError.None)
58 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create query");
59 throw IoTConnectivityErrorFactory.GetException(ret);
62 Interop.IoTConnectivity.Common.Query.QueryCallback forEachCallback = (string key, string value, IntPtr userData) =>
68 ret = Interop.IoTConnectivity.Common.Query.Foreach(resourceQueryHandleToClone, forEachCallback, IntPtr.Zero);
69 if (ret != (int)IoTConnectivityError.None)
71 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to iterate query");
72 throw IoTConnectivityErrorFactory.GetException(ret);
77 /// Destructor of the ResourceQuery class.
85 /// Gets and sets the resource type of the query
88 /// ResourceQuery query = new ResourceQuery();
89 /// query.Type = "org.tizen.light";
90 /// Console.WriteLine("Type of query : {0}", query.Type);
97 int ret = Interop.IoTConnectivity.Common.Query.GetResourceType(_resourceQueryHandle, out type);
98 if (ret != (int)IoTConnectivityError.None)
100 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
101 throw IoTConnectivityErrorFactory.GetException(ret);
103 return Marshal.PtrToStringAnsi(type);
107 int ret = (int)IoTConnectivityError.InvalidParameter;
108 if (ResourceTypes.IsValid(value))
109 ret = Interop.IoTConnectivity.Common.Query.SetResourceType(_resourceQueryHandle, value);
111 if (ret != (int)IoTConnectivityError.None)
113 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set type");
114 throw IoTConnectivityErrorFactory.GetException(ret);
120 /// Gets and sets the resource interface of the query
123 /// Setter value could be a value such as <see cref="ResourceInterfaces.DefaultInterface"/>
126 /// ResourceQuery query = new ResourceQuery();
127 /// query.Interface = ResourceInterfaces.LinkInterface;
129 public string Interface
134 int ret = Interop.IoTConnectivity.Common.Query.GetInterface(_resourceQueryHandle, out iface);
135 if (ret != (int)IoTConnectivityError.None)
137 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
138 throw IoTConnectivityErrorFactory.GetException(ret);
140 return Marshal.PtrToStringAnsi(iface);
144 int ret = (int)IoTConnectivityError.InvalidParameter;
145 if (ResourceInterfaces.IsValid(value))
146 ret = Interop.IoTConnectivity.Common.Query.SetInterface(_resourceQueryHandle, value);
148 if (ret != (int)IoTConnectivityError.None)
150 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to set interface");
151 throw IoTConnectivityErrorFactory.GetException(ret);
157 /// Contains all the query keys
160 /// ResourceQuery query = new ResourceQuery();
161 /// query.Add("key", "value");
162 /// query.Add("newKey", "sample value");
163 /// var keys = query.Keys;
164 /// Console.WriteLine("Resource query contains keys {0} and {1}", keys.ElementAt(0), keys.ElementAt(1));
166 public ICollection<string> Keys
175 /// Contains all the query values
178 /// ResourceQuery query = new ResourceQuery();
179 /// query.Add("key", "value");
180 /// query.Add("newKey", "sample value");
181 /// var values = query.Values;
182 /// Console.WriteLine("Resource query contains values {0} and {1}", values.ElementAt(0), values.ElementAt(1));
184 public ICollection<string> Values
188 return _query.Values;
193 /// Gets the number of query elements
196 /// ResourceQuery query = new ResourceQuery();
197 /// query.Add("key", "value");
198 /// query.Add("newKey", "sample value");
199 /// var count = query.Count;
200 /// Console.WriteLine("There are {0} keys in the query object", count);
211 /// Represents whether the collection is readonly
214 /// ResourceQuery query = new ResourceQuery();
215 /// if (query.IsReadOnly)
216 /// Console.WriteLine("Read only query");
218 public bool IsReadOnly
222 return _query.IsReadOnly;
227 /// Gets or sets the query data
229 /// <param name="key">The query key to get or set.</param>
230 /// <returns>The query with the specified key.</returns>
232 /// ResourceQuery query = new ResourceQuery();
233 /// query["key1"] = "sample-data";
234 /// Console.WriteLine("query has : {0}", query["key1"]);
236 public string this[string key]
250 /// Checks whether the given key exists in Query collection
252 /// <param name="key">The key to look for</param>
253 /// <returns>true if exists. Otherwise, false</returns>
255 /// ResourceQuery query = new ResourceQuery();
256 /// query.Add("key1", "value1");
257 /// if (query.ContainsKey("key1"))
258 /// Console.WriteLine("query conatins key : key1");
260 public bool ContainsKey(string key)
262 return _query.ContainsKey(key);
266 /// Adds a new key and correspoding value into the query.
269 /// The full length of query should be less than or equal to 64.
271 /// <param name="key">The key of the query to insert</param>
272 /// <param name="value">The string data to insert into the query</param>
273 /// <seealso cref="Remove()"/>
275 /// ResourceQuery query = new ResourceQuery();
276 /// query.Add("key1", "value1");
278 public void Add(string key, string value)
280 if (CanAddQuery(key, value))
282 int ret = Interop.IoTConnectivity.Common.Query.Add(_resourceQueryHandle, key, value);
283 if (ret != (int)IoTConnectivityError.None)
285 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add query");
286 throw IoTConnectivityErrorFactory.GetException(ret);
288 _query.Add(key, value);
292 Log.Error(IoTConnectivityErrorFactory.LogTag, "Query cannot be added");
293 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
298 /// Removes the key and its associated value from the query.
300 /// <param name="key">The id of the query to delete</param>
301 /// <returns>True if operation is successful. Otherwise, false</returns>
302 /// <seealso cref="Add()"/>
304 /// ResourceQuery query = new ResourceQuery();
305 /// query.Add("key1", "value1");
306 /// var result = query.Remove("key1");
308 public bool Remove(string key)
310 bool isRemoved = _query.Remove(key);
313 int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key);
314 if (ret != (int)IoTConnectivityError.None)
316 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove query");
317 throw IoTConnectivityErrorFactory.GetException(ret);
324 /// Gets the value associated with the specified key.
326 /// <param name="key">The query key</param>
327 /// <param name="value">Value corresponding to query key</param>
328 /// <returns>True if the key exists, false otherwise</returns>
330 /// ResourceQuery query = new ResourceQuery();
331 /// query.Add("key1", "value1");
333 /// var isPresent = query.TryGetValue("key1", out value);
335 /// Console.WriteLine("value : {0}", value);
337 public bool TryGetValue(string key, out string value)
339 return _query.TryGetValue(key, out value);
343 /// Adds query key and value as a key value pair
345 /// <param name="item">The key value pair</param>
346 /// <seealso cref="Remove()"/>
348 /// ResourceQuery query = new ResourceQuery();
349 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
351 public void Add(KeyValuePair<string, string> item)
353 Add(item.Key, item.Value);
357 /// Clears the Query collection
360 /// ResourceQuery query = new ResourceQuery();
361 /// query.Add("key1", "value1");
362 /// query.Add("key2", "value2");
367 foreach (string key in _query.Keys)
369 int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key);
370 if (ret != (int)IoTConnectivityError.None)
372 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to clear query");
373 throw IoTConnectivityErrorFactory.GetException(ret);
380 /// Checks if the given query pair exists
382 /// <param name="item">The key value pair</param>
383 /// <returns>True if exists. Otherwise, false</returns>
385 /// ResourceQuery query = new ResourceQuery();
386 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
387 /// var isPresent = query.Contains(new KeyValuePair<string, string>("key1", "value1"));
389 /// Console.WriteLine("Key value pair is present");
391 public bool Contains(KeyValuePair<string, string> item)
393 return _query.Contains(item);
397 /// Copies the elements of the query collection to an Array, starting at a particular index.
399 /// <param name="array">The destination array</param>
400 /// <param name="arrayIndex">Index parameter</param>
402 /// ResourceQuery query = new ResourceQuery();
403 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
404 /// KeyValuePair<string, string>[] dest = new KeyValuePair<string, string>[query.Count];
405 /// query.CopyTo(dest, 0);
406 /// Console.WriteLine("Dest conatins ({0}, {1})", dest[0].Key, dest[0].Value);
408 public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
410 _query.CopyTo(array, arrayIndex);
414 /// Remove the given key value pair from the query
416 /// <param name="item">The key value pair to remove</param>
417 /// <returns>True if operation is successful. Otherwise, false</returns>
418 /// <seealso cref="Add()"/>
420 /// ResourceQuery query = new ResourceQuery();
421 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
422 /// var result = query.Remove(new KeyValuePair<string, string>("key1", "value1"));
424 public bool Remove(KeyValuePair<string, string> item)
426 return Remove(item.Key);
430 /// Get the enumerator to query collection
432 /// <returns>Enumerator to query pairs</returns>
434 /// ResourceQuery query = new ResourceQuery();
435 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
436 /// query.Add(new KeyValuePair<string, string>("key2", "value2"));
437 /// foreach (KeyValuePair<string, string> pair in query)
439 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
442 public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
444 return _query.GetEnumerator();
448 /// Get the enumerator to query collection
450 /// <returns>Enumerator to query pairs</returns>
452 /// ResourceQuery query = new ResourceQuery();
453 /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
454 /// query.Add(new KeyValuePair<string, string>("key2", "value2"));
455 /// foreach (KeyValuePair<string, string> pair in query)
457 /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
460 IEnumerator IEnumerable.GetEnumerator()
462 return _query.GetEnumerator();
466 /// Releases any unmanaged resources used by this object.
468 public void Dispose()
471 GC.SuppressFinalize(this);
475 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
477 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
478 protected virtual void Dispose(bool disposing)
485 // Free managed objects
488 Interop.IoTConnectivity.Common.Query.Destroy(_resourceQueryHandle);
492 private bool CanAddQuery(string newKey, string newValue)
495 foreach (string key in Keys)
497 queryLenth += key.Length + 2;
499 foreach (string value in Values)
501 queryLenth += value.Length;
504 if ((newKey.Length + newValue.Length + queryLenth + 2) < QueryMaxLenth)