Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / android / OCLib / src / com / oc / OCDiscovery.java
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 package com.oc;
7
8 import java.util.HashMap;
9
10 public class OCDiscovery {
11   static private native void jniFindResource(String host, HashMap<String, String> filter,
12       OCResourceResultHandler handler);
13
14   /**
15    * Internal API for Service and Resource Discovery
16    * 
17    * @param host - Host IP Address of a service to direct resource discovery query. If null or
18    *        empty, performs service discovery OR resource discovery query to ALL group members.
19    * @param filter<String FilterType, String Value> - Filter Type - Can be one of two of the link
20    *        format equivalent attribute types declared in table 7 of the
21    *        OC-Lite-Product-Specification-V1 Document. Either "n" (i.e. Resource Name) or "rt"
22    *        (i.e. Resource Type). Value - Can be any valid String that's defined to work with CoRE
23    *        Link Attributes as defined in the RFC6690.
24    * @param handler - Handles callbacks, success states and failure states.
25    * 
26    *        Four modes of discovery defined as follows: 
27    *        (NULL/Empty, NULL/Empty) - Performs ALL service discovery AND ALL resource discovery. 
28    *        (NULL/Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s) 
29    *                                  from ALL services. 
30    *        (Not Empty, NULL/Empty) - Performs ALL resource discovery on a particular service. 
31    *        (Not Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s) 
32    *                                  from a particular service.
33    * 
34    *        Thread-Safety: TODO Determine if it is going to be safe to call in a UI thread
35    */
36   private void findResource(String host, HashMap<String, String> filter,
37       OCResourceResultHandler handler) {
38     /*** Have to make sure all values are valid so the pass down to JNI runs smoothly. ***/
39     String tempHost = host;
40     HashMap<String, String> tempFilter = filter;
41     if (host == null) {
42       tempHost = "";
43     }
44     if (filter == null) {
45       tempFilter = new HashMap<String, String>();
46       tempFilter.put("", ""); // TODO: NOt sure if putting an empty key is necessary if the hashmap is empty.
47     }
48     // Todo: Throw exception if handler is invalid (i.e. null).
49     /*** Done ensuring all values are valid.  ***/
50     jniFindResource(tempHost, tempFilter, handler);
51   }
52
53   /**
54    * Queries the network for available resources.
55    * 
56    * NOTE: This gets posted to a new thread so it doesn't hold the UI or network thread.
57    * 
58    * @param serviceURL - The URL to the service. NULL for a broadcast/multicast.
59    * @param resourceType - The resource type filter. NULL for match any.
60    * 
61    * @throws This may throw if the service is not started, network is bad, etc.
62    * 
63    *         Thread-Safety: TODO Determine if it is going to be safe to call in a UI thread
64    */
65   public void findResourceByType(String serviceURL, String resourceType,
66       OCResourceResultHandler handler) {
67     HashMap<String, String> filteredMap = new HashMap<String, String>();
68     filteredMap.put("rt", resourceType);
69     findResource(serviceURL, filteredMap, handler);
70   }
71 }