Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / SimulatorManager.java
index a16ce67..a1d93d7 100644 (file)
 
 package org.oic.simulator;
 
-import org.oic.simulator.clientcontroller.IFindResourceListener;
-import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
-import org.oic.simulator.serviceprovider.SimulatorResourceServer;
+import java.util.Vector;
+
+import org.oic.simulator.client.FindResourceListener;
+import org.oic.simulator.server.SimulatorResource;
 
 /**
  * This class provides a set of methods for creation, discovery and deletion of
@@ -26,17 +27,21 @@ import org.oic.simulator.serviceprovider.SimulatorResourceServer;
  */
 public class SimulatorManager {
 
+    static {
+        System.loadLibrary("oc_logger");
+        System.loadLibrary("octbstack");
+        System.loadLibrary("oc");
+        System.loadLibrary("RamlParser");
+        System.loadLibrary("SimulatorManager");
+    }
+
     /**
-     * API for creating a resource from a RAML configuration file whose path is
-     * given as a parameter.
+     * API for creating a resource from a RAML configuration file.
      *
      * @param configPath
      *            Path to RAML configuration file.
-     * @param listener
-     *            Listener for receiving notifications whenever there is a
-     *            change in the resource model.
      *
-     * @return {@link SimulatorResourceServer} - Created resource on success,
+     * @return {@link SimulatorResource} - Created resource on success,
      *         otherwise null.
      *
      * @throws InvalidArgsException
@@ -44,84 +49,61 @@ public class SimulatorManager {
      * @throws SimulatorException
      *             Thrown for other errors.
      */
-    public static SimulatorResourceServer createResource(String configPath,
-            IResourceModelChangedListener listener)
+    public static SimulatorResource createResource(String configPath)
             throws InvalidArgsException, SimulatorException {
-        if (configPath.isEmpty() || null == listener)
-            throw new InvalidArgsException(
-                    SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
-                    "Parameter passed in invalid");
-        SimulatorResourceServer simulatorResourceServerObj;
-        simulatorResourceServerObj = SimulatorManagerNativeInterface
-                .createResource(configPath, listener);
-        return simulatorResourceServerObj;
+        return nativeCreateResource(configPath);
     }
 
     /**
-     * API for creating a set of resources from a RAML configuration file whose
-     * path is given as a parameter.
+     * API for creating a set of resources from a RAML configuration file.
      *
      * @param configPath
      *            Path to RAML configuration file.
      * @param count
      *            Number of resources to be created.
-     * @param listener
-     *            Listener for receiving notifications whenever there is a
-     *            change in the resource model.
      *
-     * @return Returns an array of {@link SimulatorResourceServer} objects one
-     *         for each created resource on success, otherwise null.
+     * @return Returns an array of {@link SimulatorResource} objects one for
+     *         each created resource on success, otherwise null.
      *
      * @throws InvalidArgsException
      *             Thrown if the input parameters are empty.
      * @throws SimulatorException
      *             Thrown for other errors.
      */
-    public static SimulatorResourceServer[] createResource(String configPath,
-            int count, IResourceModelChangedListener listener)
-            throws InvalidArgsException, SimulatorException {
-        if (configPath.isEmpty() || count < 0 || null == listener)
-            throw new InvalidArgsException(
-                    SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
-                    "Parameter passed in invalid");
-        SimulatorResourceServer[] simulatorResourceServers;
-        simulatorResourceServers = SimulatorManagerNativeInterface
-                .createResources(configPath, count, listener);
-        return simulatorResourceServers;
-    }
+    public static Vector<SimulatorResource> createResource(String configPath,
+            int count) throws InvalidArgsException, SimulatorException {
+        return nativeCreateResources(configPath, count);
+    };
 
     /**
-     * API for deleting a specific resource.
+     * API for creating a resource either single or collection type.
      *
-     * @param resource
-     *            {@link SimulatorResourceServer} object of the resource to be
-     *            deleted.
+     * @param type
+     *            Indicates whether single or collection type.
+     * @param name
+     *            Resource Name.
+     * @param uri
+     *            Resource URI.
+     * @param resourceType
+     *            Resource Type.
+     *
+     * @return {@link SimulatorResource} - Created resource on success,
+     *         otherwise null.
      *
      * @throws InvalidArgsException
-     *             Thrown if the input parameter is empty.
+     *             Thrown if the input parameters are empty.
      * @throws SimulatorException
      *             Thrown for other errors.
      */
-    public static void deleteResource(SimulatorResourceServer resource)
+    public static SimulatorResource createResource(SimulatorResource.Type type,
+            String name, String uri, String resourceType)
             throws InvalidArgsException, SimulatorException {
-        SimulatorManagerNativeInterface.deleteResource(resource);
-    }
-
-    /**
-     * API for deleting either all the resources or resources of a specific
-     * type. Ex: If resourceType is oic.light, all resources of oic.light type
-     * will be deleted. If resourceType is null, then all of the resources will
-     * be deleted.
-     *
-     * @param resourceType
-     *            Type of resource to be deleted.
-     *
-     * @throws SimulatorException
-     *             Thrown for other errors.
-     */
-    public static void deleteResources(String resourceType)
-            throws SimulatorException {
-        SimulatorManagerNativeInterface.deleteResources(resourceType);
+        SimulatorResource resource;
+        if (type == SimulatorResource.Type.SINGLE)
+            resource = nativeCreateSingleResource(name, uri, resourceType);
+        else
+            resource = nativeCreateCollectionResource(name, uri, resourceType);
+        return resource;
     }
 
     /**
@@ -136,9 +118,9 @@ public class SimulatorManager {
      * @throws SimulatorException
      *             Thrown for other errors.
      */
-    public static void findResource(IFindResourceListener listener)
+    public static void findResource(FindResourceListener listener)
             throws InvalidArgsException, SimulatorException {
-        SimulatorManagerNativeInterface.findResource(null, listener);
+        nativeSearchResource(null, listener);
     }
 
     /**
@@ -146,7 +128,7 @@ public class SimulatorManager {
      * is called when a resource is discovered in the network.
      *
      * @param resourceType
-     *            Required resource type
+     *            Required resource type.
      * @param listener
      *            Interface to receive the discovered remote resources.
      *
@@ -156,24 +138,15 @@ public class SimulatorManager {
      *             Thrown for other errors.
      */
     public static void findResource(String resourceType,
-            IFindResourceListener listener) throws InvalidArgsException,
+            FindResourceListener listener) throws InvalidArgsException,
             SimulatorException {
         if (null == resourceType || resourceType.isEmpty()) {
             throw new InvalidArgsException(
-                    SimulatorResult.SIMULATOR_INVALID_PARAM.ordinal(),
-                    "Resource type is empty");
+                    SimulatorResult.SIMULATOR_INVALID_PARAM,
+                    "Invalid resource type!");
         }
-        SimulatorManagerNativeInterface.findResource(resourceType, listener);
-    }
 
-    /**
-     * API to set the listener for receiving log messages.
-     *
-     * @param logger
-     *            {@link ILogger} to receive the log messages.
-     */
-    public static void setLogger(ILogger logger) {
-        SimulatorManagerNativeInterface.setLogger(logger);
+        nativeSearchResource(resourceType, listener);
     }
 
     /**
@@ -181,20 +154,33 @@ public class SimulatorManager {
      *
      * @param deviceInfo
      *            Device information.
+     *
+     * @throws InvalidArgsException
+     *             Thrown if the input parameter is empty.
+     * @throws SimulatorException
+     *             Thrown for other errors.
      */
-    public static void setDeviceInfo(String deviceInfo) {
-        SimulatorManagerNativeInterface.setDeviceInfo(deviceInfo);
+    public static void setDeviceInfo(String deviceInfo)
+            throws InvalidArgsException, SimulatorException {
+        nativeSetDeviceInfo(deviceInfo);
     }
 
     /**
-     * API to get the device information asynchronously via callback
-     * using {@link IDeviceInfo}.
+     * API to search for devices on the given host in the network.
      *
+     * @param hostUri
+     *            URI of the host device.
      * @param listener
-     *            Interface for receiving the device information.
+     *            Listener for receiving the device information.
+     *
+     * @throws InvalidArgsException
+     *             Thrown if the input parameter is empty.
+     * @throws SimulatorException
+     *             Thrown for other errors.
      */
-    public static void getDeviceInfo(IDeviceInfo listener) {
-        SimulatorManagerNativeInterface.getDeviceInfo(listener);
+    public static void findDevices(String hostUri, DeviceListener listener)
+            throws InvalidArgsException, SimulatorException {
+        nativeFindDevices(hostUri, listener);
     }
 
     /**
@@ -202,19 +188,73 @@ public class SimulatorManager {
      *
      * @param platformInfo
      *            {@link PlatformInfo} - Platform information.
+     *
+     * @throws InvalidArgsException
+     *             Thrown if the input parameter is empty.
+     * @throws SimulatorException
+     *             Thrown for other errors.
      */
-    public static void setPlatformInfo(PlatformInfo platformInfo) {
-        SimulatorManagerNativeInterface.setPlatformInfo(platformInfo);
+    public static void setPlatformInfo(PlatformInfo platformInfo)
+            throws InvalidArgsException, SimulatorException {
+        nativeSetPlatformInfo(platformInfo);
     }
 
     /**
-     * API to get the platform information asynchronously via callback
-     * using {@link IPlatformInfo}..
+     * API to find the platform information of the given host in the network.
      *
+     * @param hostUri
+     *            URI of the host device.
      * @param listener
-     *            Interface for receiving the platform information.
+     *            Listener for receiving the platform information.
+     *
+     * @throws InvalidArgsException
+     *             Thrown if the input parameter is empty.
+     * @throws SimulatorException
+     *             Thrown for other errors.
+     */
+    public static void getPlatformInformation(String hostUri,
+            PlatformListener listener) throws InvalidArgsException,
+            SimulatorException {
+        nativeGetPlatformInformation(hostUri, listener);
+    }
+
+    /**
+     * API to set the listener for receiving log messages.
+     *
+     * @param logger
+     *            {@link ILogger} to receive the log messages.
      */
-    public static void getPlatformInfo(IPlatformInfo listener) {
-        SimulatorManagerNativeInterface.getPlatformInfo(listener);
+    public static void setLogger(ILogger logger) {
+        nativeSetLogger(logger);
     }
+
+    private SimulatorManager() {
+    }
+
+    private static native SimulatorResource nativeCreateResource(
+            String configPath);
+
+    private static native Vector<SimulatorResource> nativeCreateResources(
+            String configPath, int count);
+
+    private static native SimulatorResource nativeCreateSingleResource(
+            String name, String uri, String resourceType);
+
+    private static native SimulatorResource nativeCreateCollectionResource(
+            String name, String uri, String resourceType);
+
+    private static native void nativeSearchResource(String resourceType,
+            FindResourceListener listener);
+
+    private static native void nativeSetDeviceInfo(String deviceInfo);
+
+    private static native void nativeFindDevices(String hostUri,
+            DeviceListener listener);
+
+    private static native void nativeSetPlatformInfo(PlatformInfo platformInfo);
+
+    private static native void nativeGetPlatformInformation(String hostUri,
+            PlatformListener listener);
+
+    private static native void nativeSetLogger(ILogger logger);
 }
\ No newline at end of file