resource_container_env.AppendUnique(
CPPPATH = [
env.get('SRC_DIR')+'/extlibs',
- 'include',
+ 'include',
+ 'include/internal',
+ 'bundle-api/include',
'src',
'../serverBuilder/include',
- '../common/primitiveResource/include'
+ '../common/primitiveResource/include',
+ '/usr/lib/jvm/java-7-openjdk-i386/include',
+ '/usr/lib/jvm/java-7-openjdk-i386/include/linux'
])
if target_os not in ['windows', 'winrt']:
resource_container_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions'])
resource_container_env.PrependUnique(LIBS = ['gnustl_shared', 'compatibility', 'log'])
-#resource_container_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
+resource_container_env.AppendUnique(LIBPATH = ['/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/server/'])
resource_container_env.PrependUnique(LIBS = ['server_builder', 'service_common', 'oc','octbstack', 'oc_logger', 'oc_logger_core', 'connectivity_abstraction'])
-resource_container_env.AppendUnique(LIBS = ['dl'])
+resource_container_env.AppendUnique(LIBS = ['dl', 'jvm'])
+
######################################################################
# Source files and Targets
resource_container_env.InstallTarget([res_container_static,res_container_shared], 'libResContainer')
-#resource_container_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
-
-#containertestapp = resource_container_env.Program('ContainerTest', res_container_src)
-#Alias("containertest", containertestapp)
-#env.AppendTarget('containertest')
-
-#Go to build sample apps
-#SConscript('resource-container/examples/SHPBundle/SConscript')
-
######################################################################
# build Sample Resource Bundle
######################################################################
-#SConscript('resource-container/examples/SampleBundle/SConscript')
sample_resource_bundle_env = resource_container_env.Clone()
sample_resource_bundle_env.AppendUnique(CCFLAGS = ['-fPIC'])
HUE_RESOURCE_BUNDLE_DIR + 'include',
'include/'
])
-
+
hue_resource_bundle_env.PrependUnique(LIBS = ['curl'])
-
+
hue_resource_bundle_src = [ Glob(HUE_RESOURCE_BUNDLE_DIR + 'src/*.cpp'), Glob('src/*.cpp')]
HueBundle = hue_resource_bundle_env.SharedLibrary('HueBundle', hue_resource_bundle_src)
# Build Container Test
######################################################################
containertest_env = resource_container_env.Clone();
-#containertest_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
+containertest_env.AppendUnique(LINKFLAGS=["-rdynamic"])
# Copy test configuration
Command("examples/ResourceContainerConfig.xml","examples/ResourceContainerConfig.xml", Copy("$TARGET", "$SOURCE"))
#ifndef BUNDLERESOURCE_H_
#define BUNDLERESOURCE_H_
+#include <list>
+#include <string>
#include <map>
#include <vector>
-#include <string>
+
+#include "NotificationReceiver.h"
using namespace std;
{
class BundleResource
{
- public:
- BundleResource();
- virtual ~BundleResource();
-
- // TODO use type variant mechanism
- virtual void getAttribute(string attributeName) = 0;
- virtual void setAttribute(string attributeName, string value) = 0;
- virtual void initAttributes() = 0;
-
- public:
- string m_name, m_uri, m_resourceType, m_address;
- map< string, vector< map< string, string > > > m_mapResourceProperty;
- map< string, string > m_mapAttributes;
+ public:
+ BundleResource();
+ virtual ~BundleResource();
+
+ // TODO use type variant mechanism
+ virtual string getAttribute(string attributeName);
+ virtual void setAttribute(string attributeName, string value);
+ std::list<string> getAttributeNames();
+ virtual void initAttributes() = 0;
+
+ void registerObserver(NotificationReceiver *pNotiReceiver);
+
+ public:
+ string m_name, m_uri, m_resourceType, m_address;
+ map< string, vector< map< string, string > > > m_mapResourceProperty;
+
+ private:
+ map< string, string > m_mapAttributes;
+ NotificationReceiver *m_pNotiReceiver;
};
}
}
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#ifndef SOFTSENSORRESOURCE_H_
-#define SOFTSENSORRESOURCE_H_
+#ifndef NOTIFICATIONRECEIVER_H_
+#define NOTIFICATIONRECEIVER_H_
-#include "BundleResource.h"
+#include <string>
namespace OIC
{
namespace Service
{
- class SoftSensorResource: public BundleResource
+ class NotificationReceiver
{
- public:
- struct SensorData
- {
- string sensorName;
- vector< map< string, string > > data;
- };
+ public:
+ NotificationReceiver() {};
+ ~NotificationReceiver() {};
- SoftSensorResource();
- virtual ~SoftSensorResource();
-
- virtual void setInputAttributes(vector< SensorData > inputs) = 0;
-
- int inputCount;
- map< string, SensorData > m_mapStoredInputData;
- SensorData m_outputs;
+ virtual void onNotificationReceived(std::string strResourceUri) = 0;
};
}
}
public:
ProtocolBridgeResource();
virtual ~ProtocolBridgeResource();
- virtual void getAttribute(string attributeName) = 0;
+ virtual string getAttribute(string attributeName) = 0;
virtual void setAttribute(string attributeName, string value) = 0;
};
}
#include "ResourceContainer.h"
#include "BundleInfo.h"
#include "Configuration.h"
+#include "NotificationReceiver.h"
#include "BundleResource.h"
using namespace OIC::Service;
{
namespace Service
{
- class ResourceContainerBundleAPI: public Configuration
+ class ResourceContainerBundleAPI: public Configuration, public NotificationReceiver
{
- public:
- typedef std::map< std::string, std::string > configInfo;
- ResourceContainerBundleAPI();
- virtual ~ResourceContainerBundleAPI();
- virtual void registerResource(BundleResource *resource) = 0;
- virtual void unregisterResource(BundleResource *resource) = 0;
- virtual void getCommonConfiguration(configInfo *configOutput) = 0;
- virtual void getBundleConfiguration(std::string bundleId, configInfo *configOutput) = 0;
- virtual void getResourceConfiguration(std::string bundleId,
- std::vector< resourceInfo > *configOutput) = 0;
+ public:
+ ResourceContainerBundleAPI();
+ virtual ~ResourceContainerBundleAPI();
+ virtual void registerResource(BundleResource *resource) = 0;
+ virtual void unregisterResource(BundleResource *resource) = 0;
+ virtual void getBundleConfiguration(std::string bundleId, configInfo *configOutput) = 0;
+ virtual void getResourceConfiguration(std::string bundleId,
+ std::vector< resourceInfo > *configOutput) = 0;
- static ResourceContainerBundleAPI *getInstance();
+ static ResourceContainerBundleAPI *getInstance();
};
}
}
--- /dev/null
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef SOFTSENSORRESOURCE_H_
+#define SOFTSENSORRESOURCE_H_
+
+#include "BundleResource.h"
+
+namespace OIC
+{
+ namespace Service
+ {
+ class SoftSensorResource: public BundleResource
+ {
+ public:
+ struct SensorData
+ {
+ string sensorName;
+ vector< map< string, string > > data;
+ };
+
+ SoftSensorResource();
+ virtual ~SoftSensorResource();
+
+ virtual string getAttribute(string attributeName) = 0;
+ virtual void setAttribute(string attributeName, string value) = 0;
+
+ virtual void setInputAttribute(SensorData inputs) = 0;
+
+ int inputCount;
+ vector<string> m_vecInputAttributes;
+ map< string, SensorData > m_mapStoredInputData;
+ SensorData m_outputs;
+ };
+ }
+}
+
+#endif
--- /dev/null
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.iotivity.resourcecontainer</groupId>
+ <artifactId>bundle-api</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0</version>
+ <name>resource-container-api</name>
+ <url>http://maven.apache.org</url>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>2.4</version>
+ <type>maven-plugin</type>
+ </dependency>
+ </dependencies>
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-assmembly-plugin</artifactId>
+ <version>2.5.5</version>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.3</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <version>2.5.5</version>
+ <configuration>
+ <descriptorRefs>
+ <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>repo1</id>
+ <name>Repo1</name>
+ <url>http://repo1.maven.rog</url>
+ </repository>
+ </repositories>
+</project>
--- /dev/null
+package org.iotivity.resourcecontainer.bundle.api;
+
+import java.util.List;
+import java.util.Vector;
+
+public class BaseActivator implements BundleActivator
+{
+ private String bundleId;
+
+ public BaseActivator(String bundleId){
+ this.bundleId = bundleId;
+ }
+
+ static{
+ try{
+ System.loadLibrary("ResContainerLib");
+ }
+ catch(Exception e){
+ e.printStackTrace();
+ }
+ }
+
+ public void activateBundle(){
+
+ }
+
+ public void deactivateBundle(){
+
+ }
+
+ public void registerResource(BundleResource resource){
+ registerJavaResource(resource, resource.getAttributeKeys(), bundleId, resource.getURI(), resource.getResourceType(), resource.getName());
+ }
+
+ public List<ResourceConfig> getConfiguredBundleResources(){
+ int configuredResources = getNumberOfConfiguredResources(bundleId);
+
+ Vector<ResourceConfig> configs = new Vector<ResourceConfig>();
+
+ for(int i = 0; i < configuredResources; i++){
+ String[] resourceParams = getConfiguredResourceParams(bundleId, i);
+ ResourceConfig config = new ResourceConfig(resourceParams);
+ configs.add(config);
+
+ }
+ return configs;
+ }
+
+ public void unregisterResource(BundleResource resource){
+
+ }
+
+ public native void registerJavaResource(BundleResource resource, String[] attributes, String bundleId, String uri, String resourceType, String name);
+
+ public native void unregisterJavaResource(BundleResource resource);
+
+ public native int getNumberOfConfiguredResources(String bundleId);
+
+ public native String[] getConfiguredResourceParams(String bundleId, int resId);
+
+}
+
--- /dev/null
+package org.iotivity.resourcecontainer.bundle.api;
+
+
+import java.util.List;
+
+public interface BundleActivator
+{
+ public void activateBundle();
+ public void deactivateBundle();
+ public void registerResource(BundleResource resource);
+ public void unregisterResource(BundleResource resource);
+ public List<ResourceConfig> getConfiguredBundleResources();
+}
--- /dev/null
+package org.iotivity.resourcecontainer.bundle.api;
+
+
+import java.util.HashMap;
+import java.util.Set;
+
+public abstract class BundleResource
+{
+ protected String m_name, m_uri, m_resourceType, m_address;
+
+ protected HashMap<String, String> m_attributes;
+
+ protected abstract void initAttributes();
+ public abstract void setAttribute(String key, String value);
+ public abstract String getAttribute(String key);
+ public String[] getAttributeKeys(){
+ Set<String> keys = m_attributes.keySet();
+ return keys.toArray(new String[keys.size()]);
+ }
+
+ public void setURI(String uri){
+ this.m_uri = uri;
+ }
+
+ public String getURI(){
+ return m_uri;
+ }
+
+ public void setResourceType(String resourceType){
+ this.m_resourceType = resourceType;
+ }
+
+ public String getResourceType(){
+ return m_resourceType;
+ }
+
+ public void setAddress(String address){
+ this.m_address = address;
+ }
+
+ public String getAddress(){
+ return m_address;
+ }
+
+ public void setName(String name){
+ this.m_name = name;
+ }
+
+ public String getName(){
+ return m_name;
+ }
+
+}
--- /dev/null
+package org.iotivity.resourcecontainer.bundle.api;
+
+
+public interface ProtocolBridgeConnector {
+ public void connect();
+ public void disconnect();
+}
--- /dev/null
+package org.iotivity.resourcecontainer.bundle.api;
+
+
+public class ResourceConfig {
+ private String m_name, m_uri, m_resourceType, m_address;
+
+ public ResourceConfig(){
+
+ }
+
+ public ResourceConfig(String[] params){
+ m_name = params[0];
+ m_uri = params[1];
+ m_resourceType = params[2];
+ m_address = params[3];
+ }
+
+ public String getM_name() {
+ return m_name;
+ }
+
+ public void setM_name(String m_name) {
+ this.m_name = m_name;
+ }
+
+ public String getM_uri() {
+ return m_uri;
+ }
+
+ public void setM_uri(String m_uri) {
+ this.m_uri = m_uri;
+ }
+
+ public String getM_resourceType() {
+ return m_resourceType;
+ }
+
+ public void setM_resourceType(String m_resourceType) {
+ this.m_resourceType = m_resourceType;
+ }
+
+ public String getM_address() {
+ return m_address;
+ }
+
+ public void setM_address(String m_address) {
+ this.m_address = m_address;
+ }
+
+ @Override
+ public String toString() {
+ return "ResourceConfig [m_name=" + m_name + ", m_uri=" + m_uri
+ + ", m_resourceType=" + m_resourceType + ", m_address="
+ + m_address + "]";
+ }
+
+
+}
--- /dev/null
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.iotivity.bundles</groupId>
+ <artifactId>hue</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>hue</name>
+ <url>http://maven.apache.org</url>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <version>4.5</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.iotivity.resourcecontainer</groupId>
+ <artifactId>bundle-api</artifactId>
+ <version>1.0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>2.4</version>
+ <type>maven-plugin</type>
+ </dependency>
+ </dependencies>
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-assmembly-plugin</artifactId>
+ <version>2.5.5</version>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.3</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <version>2.5.5</version>
+ <configuration>
+ <descriptorRefs>
+ <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>repo1</id>
+ <name>Repo1</name>
+ <url>http://repo1.maven.rog</url>
+ </repository>
+ </repositories>
+</project>
--- /dev/null
+package org.iotivity.bundle.hue;
+
+import java.util.List;
+
+import org.iotivity.resourcecontainer.bundle.api.BaseActivator;
+import org.iotivity.resourcecontainer.bundle.api.ResourceConfig;
+
+public class HueBundleActivator extends BaseActivator
+{
+ private HueConnector connector;
+
+ public HueBundleActivator(String bundleId){
+ super(bundleId);
+ }
+
+ public void activateBundle(){
+ super.activateBundle();
+ connector = new HueConnector();
+ List<ResourceConfig> resourceConfig = getConfiguredBundleResources();
+
+ for(ResourceConfig config : resourceConfig){
+
+ HueLightResource hueLightResource = new HueLightResource(connector, config.getM_name(), config.getM_uri(), config.getM_resourceType(), config.getM_address());
+ registerResource(hueLightResource);
+ }
+ }
+
+ public void deactivateBundle(){
+ System.out.println("Deactivate bundle called.");
+ }
+
+ // test call
+ public static void main(String[] args){
+ HueBundleActivator activator = new HueBundleActivator("oic.hue.bundle");
+ }
+}
--- /dev/null
+package org.iotivity.bundle.hue;
+
+import java.io.IOException;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.iotivity.resourcecontainer.bundle.api.ProtocolBridgeConnector;
+
+public class HueConnector implements ProtocolBridgeConnector{
+ public void connect(){
+
+ }
+
+ public void disconnect(){
+
+ }
+
+ public void transmit(String target, String payload){
+ try {
+ CloseableHttpClient httpclient = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(target);
+ //httpPost.setHeader("content-type","application/json");
+ StringEntity stringEntity = new StringEntity(payload, ContentType.create("application/json", "UTF-8"));
+ httpPost.setEntity(stringEntity);
+
+ CloseableHttpResponse response1;
+ response1 = httpclient.execute(httpPost);
+ // The underlying HTTP connection is still held by the response object
+ // to allow the response content to be streamed directly from the network socket.
+ // In order to ensure correct deallocation of system resources
+ // the user MUST call CloseableHttpResponse#close() from a finally clause.
+ // Please note that if response content is not fully consumed the underlying
+ // connection cannot be safely re-used and will be shut down and discarded
+ // by the connection manager.
+ try {
+ HttpEntity entity1 = response1.getEntity();
+ // do something useful with the response body
+ // and ensure it is fully consumed
+ EntityUtils.consume(entity1);
+ } finally {
+ response1.close();
+ }
+
+ } catch (ClientProtocolException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public String read(String target){
+ return "";
+ }
+
+}
--- /dev/null
+package org.iotivity.bundle.hue;
+
+import java.util.HashMap;
+
+import org.iotivity.resourcecontainer.bundle.api.BundleResource;
+
+/**
+ * This class maps a Philips Hue light to OIC light resource
+ * @author iotivity
+ */
+public class HueLightResource extends BundleResource
+{
+ private HueConnector m_hueConnector;
+
+ public HueLightResource(){
+ initAttributes();
+ m_resourceType = "oic.light.control";
+ }
+
+ public HueLightResource(HueConnector hueConnector, String name, String uri, String resourceType, String address){
+ this();
+ this.m_hueConnector = hueConnector;
+ m_name = name;
+ m_uri = uri;
+ m_resourceType = resourceType;
+ m_address = address;
+ }
+
+ protected void initAttributes(){
+ m_attributes = new HashMap<String, String>();
+ m_attributes.put("on-off", "true");
+ m_attributes.put("color", "0");
+ m_attributes.put("dim", "0");
+ }
+
+ public void setAttribute(String key, String value){
+ System.out.println("Set attribute called - key: " + key + ", value: " + value + " transmitting now.");
+
+ if("on-off".equals(value)){
+ m_hueConnector.transmit(m_address + "/state", "{\"on\":" + value + "}");
+ }
+
+ if("dim".equals(value)){
+ m_hueConnector.transmit(m_address + "/state", "{\"bri\":" + value + "}");
+ }
+
+ if("color".equals(value)){
+ m_hueConnector.transmit(m_address + "/state", "{\"hue\":" + value + "}");
+ }
+ m_attributes.put(key, value);
+ }
+
+ public String getAttribute(String key){
+ System.out.println("Get attribute called - key: " + key);
+ // map key to hue address
+ // read from Hue gateway, parse resource representation and return attribute
+ m_hueConnector.read(m_address);
+ return "";
+ }
+
+ @Override
+ public String toString() {
+ return "HueLightResource [m_hueConnector=" + m_hueConnector
+ + ", m_name=" + m_name + ", m_uri=" + m_uri
+ + ", m_resourceType=" + m_resourceType + ", m_address="
+ + m_address + ", m_attributes=" + m_attributes + "]";
+ }
+
+
+}
HueLight();
HueLight(HueConnector* connector, std::string address);
virtual ~HueLight();
- void getAttribute(string attributeName);
+ string getAttribute(string attributeName);
void setAttribute(string attributeName, string value);
virtual void initAttributes();
private:
std::string bundleId);
void deactivateBundle();
- void createResource(Configuration::resourceInfo);
+ void createResource(resourceInfo);
void destroyResource(BundleResource *);
std::string m_bundleId;
m_address = address;
m_connector = connector;
initAttributes();
- std::cout << "Number of Attributes: " << m_mapAttributes.size() << "" << endl;
-
}
HueLight::~HueLight(){
}
-void HueLight::getAttribute(string attributeName){
+string HueLight::getAttribute(string attributeName){
}
void HueLight::initAttributes(){
- m_mapAttributes.insert(std::pair<string, string>("on-off", "false"));
+ BundleResource::setAttribute("on-off", "false");
+ BundleResource::setAttribute("dim", "0");
+ BundleResource::setAttribute("color", "0");
+ /*m_mapAttributes.insert(std::pair<string, string>("on-off", "false"));
m_mapAttributes.insert(std::pair<string, string>("dim", "0"));
- m_mapAttributes.insert(std::pair<string, string>("color", "0"));
+ m_mapAttributes.insert(std::pair<string, string>("color", "0"));*/
}
void HueLight::setAttribute(string attributeName, string value){
cout << "HueLight::setAttribute setting " << attributeName << " to " << value << std::endl;
- this->m_mapAttributes[attributeName] = value;
+
if(attributeName == "on-off"){
m_connector->transmit(this->m_address + "/state", "{\"on\":" + value + "}");
m_bundleId = bundleId;
m_connector = new HueConnector();
- vector<Configuration::resourceInfo> resourceConfig;
+ vector<resourceInfo> resourceConfig;
resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
- for (vector<Configuration::resourceInfo>::iterator itor = resourceConfig.begin();
+ for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
itor != resourceConfig.end(); itor++)
{
createResource(*itor);
}
-void HueSampleBundleActivator::createResource(Configuration::resourceInfo resourceInfo)
+void HueSampleBundleActivator::createResource(resourceInfo resourceInfo)
{
std::cout << "HueSampleBundle::createResource called" << std::endl;
</resourceInfo>
</resources>
</bundle>
+ <bundle>
+ <id>oic.bundle.hueJavaSample</id>
+ <path>/home/iotivity/development/iotivity-resource-manipulation/service/resource-manipulation/modules/resourceContainer/examples/HueJavaSampleBundle/hue/target/hue-1.0-SNAPSHOT-jar-with-dependencies.jar</path>
+ <activator>org.iotivity.bundle.hue.HueBundleActivator</activator>
+ <version>1.0.0</version>
+ <resources>
+ <resourceInfo>
+ <name>light</name>
+ <resourceType>oic.light.control</resourceType>
+ <address>http://192.168.0.2/api/newdeveloper/lights/1</address>
+ </resourceInfo>
+ </resources>
+ </bundle>
</container>
{
private:
- static physicalInput s_PHYSICAL_SOFTSENSORs[PHYSICAL_EA];
+ physicalInput s_PHYSICAL_SOFTSENSORs[PHYSICAL_EA];
class InValue
{
InValue m_result;
public:
- DiscomfortIndexSensor();
+ DiscomfortIndexSensor(vector <string> inputs);
int runLogic(std::vector< SoftSensorResource::SensorData > &sensorData);
DIResult getInput(std::vector< SoftSensorResource::SensorData > &contextDataList,
{
public:
DiscomfortIndexSensorResource();
+ DiscomfortIndexSensorResource(vector<string> inputAttributes);
~DiscomfortIndexSensorResource();
- void getAttribute(string attributeName);
+ string getAttribute(string attributeName);
void setAttribute(string attributeName, string value);
virtual void initAttributes();
- void setInputAttributes(vector < SensorData > inputs);
+ void setInputAttribute(SensorData input);
private:
DiscomfortIndexSensor *m_pDiscomfortIndexSensor;
void activateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId);
void deactivateBundle();
- void createResource(Configuration::resourceInfo);
+ void createResource(resourceInfo);
void destroyResource(BundleResource *);
std::string m_bundleId;
char *inputName[2] =
{ (char *)"temperature", (char *)"humidity" };
-physicalInput DiscomfortIndexSensor::s_PHYSICAL_SOFTSENSORs[PHYSICAL_EA] =
-{
- { (char *)"Thing_TempHumSensor", 2, (void *) &inputName },
- { (char *)"Thing_TempHumSensor1", 2, (void *) &inputName }
-};
-
-DiscomfortIndexSensor::DiscomfortIndexSensor()
+DiscomfortIndexSensor::DiscomfortIndexSensor(vector <string> inputs)
{
m_result.m_timestamp = "";
m_result.m_humidity = "";
m_result.m_temperature = "";
m_result.m_discomfortIndex = "";
+
+ for (int i = 0; i < PHYSICAL_EA; i++)
+ {
+ s_PHYSICAL_SOFTSENSORs[i].m_thingName = (char *) inputs.at(i).c_str();
+ s_PHYSICAL_SOFTSENSORs[i].m_inputNum = PHYSICAL_EA;
+ s_PHYSICAL_SOFTSENSORs[i].m_pInputStruct = (void *) &inputName;
+ }
}
-int DiscomfortIndexSensor::runLogic(std::vector< OIC::Service::SoftSensorResource::SensorData > &sensorData)
+int DiscomfortIndexSensor::runLogic(std::vector< SoftSensorResource::SensorData > &sensorData)
{
std::cout << "[DiscomfortIndexSensor] DiscomfortIndexSensor::" << __func__ << " is called."
<< std::endl;
/**
* Get Input data (temperature, humidity) using resource Client of Iotivity base.
*/
-DIResult DiscomfortIndexSensor::getInput(std::vector< OIC::Service::SoftSensorResource::SensorData >
+DIResult DiscomfortIndexSensor::getInput(std::vector< SoftSensorResource::SensorData >
&sensorData, InValue *data)
{
int result_flag = 0;
return SUCCESS;
}
-OIC::Service::SoftSensorResource::SensorData DiscomfortIndexSensor::setOutput(int property_count,
+SoftSensorResource::SensorData DiscomfortIndexSensor::setOutput(int property_count,
InValue *data)
{
- OIC::Service::SoftSensorResource::SensorData out;
+ SoftSensorResource::SensorData out;
std::map < std::string, std::string > output_property;
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include <algorithm>
+
#include "DiscomfortIndexSensorResource.h"
DiscomfortIndexSensorResource::DiscomfortIndexSensorResource()
{
- m_pDiscomfortIndexSensor = new DiscomfortIndexSensor();
+}
+
+DiscomfortIndexSensorResource::DiscomfortIndexSensorResource(vector<string> inputAttributes)
+{
+ m_pDiscomfortIndexSensor = new DiscomfortIndexSensor(inputAttributes);
+ m_vecInputAttributes = inputAttributes;
+ std::cout << "Init attributes" << endl;
initAttributes();
+ std::cout << "Init attributes finished" << endl;
}
DiscomfortIndexSensorResource::~DiscomfortIndexSensorResource()
void DiscomfortIndexSensorResource::initAttributes()
{
- m_mapAttributes.insert(std::pair<string, string>("temperature", "23"));
- m_mapAttributes.insert(std::pair<string, string>("humidity", "40"));
- m_mapAttributes.insert(std::pair<string, string>("discomfortIndex", "5"));
+
+ BundleResource::setAttribute("temperature", "23");
+ BundleResource::setAttribute("humidity", "40");
+ BundleResource::setAttribute("discomfortIndex", "5");
}
-void DiscomfortIndexSensorResource::getAttribute(string attributeName)
+string DiscomfortIndexSensorResource::getAttribute(string attributeName)
{
cout << "DiscomfortIndexSensorResource::getAttribute called !!" << endl;
}
void DiscomfortIndexSensorResource::setAttribute(string attributeName, string value)
{
cout << "DiscomfortIndexSensorResource::setAttribute called !!" << endl;
-
- m_mapAttributes[attributeName] = value;
}
-void DiscomfortIndexSensorResource::setInputAttributes(vector < SensorData > inputs)
+void DiscomfortIndexSensorResource::setInputAttribute(SensorData input)
{
- m_pDiscomfortIndexSensor->runLogic(inputs);
+ vector<string>::iterator itor = std::find(m_vecInputAttributes.begin(), m_vecInputAttributes.end(),
+ input.sensorName);
+ std::vector<SensorData> inData;
+
+ if (itor != m_vecInputAttributes.end())
+ {
+ m_mapStoredInputData[input.sensorName] = input;
+
+ if (inputCount == m_mapStoredInputData.size())
+ {
+ for (map< string, SensorData >::iterator mapItor = m_mapStoredInputData.begin();
+ mapItor != m_mapStoredInputData.end(); mapItor++)
+ {
+ inData.push_back(mapItor->second);
+ }
+
+ m_pDiscomfortIndexSensor->runLogic(inData);
+ m_outputs = m_pDiscomfortIndexSensor->m_output;
+
+ for (int i = 0; i < m_outputs.data.size(); i++)
+ {
+ if (!m_outputs.data.at(i)["name"].compare("temperature"))
+ BundleResource::setAttribute("temperature",m_outputs.data.at(i)["value"]);
+
+ else if (!m_outputs.data.at(i)["name"].compare("humidity"))
+ BundleResource::setAttribute("humidity",m_outputs.data.at(i)["value"]);
+
+ else if (!m_outputs.data.at(i)["name"].compare("discomfortIndex"))
+ BundleResource::setAttribute("discomfortIndex",m_outputs.data.at(i)["value"]);
+ }
+ }
+ }
}
#include "SampleBundle.h"
#include "DiscomfortIndexSensorResource.h"
-using namespace OIC::Service;
-
SampleBundle *bundle;
SampleBundle::SampleBundle()
m_pResourceContainer = resourceContainer;
m_bundleId = bundleId;
- vector<Configuration::resourceInfo> resourceConfig;
+ vector<resourceInfo> resourceConfig;
resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
- for (vector<Configuration::resourceInfo>::iterator itor = resourceConfig.begin();
+ for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
itor != resourceConfig.end(); itor++)
{
createResource(*itor);
}
}
-// TODO : has to be updated for setting the info
-void SampleBundle::createResource(Configuration::resourceInfo resourceInfo)
+void SampleBundle::createResource(resourceInfo resourceInfo)
{
std::cout << "SampleBundle::createResource called" << std::endl;
+
static int discomfortIndexSensorCount = 0;
- DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource();
- if(!resourceInfo.uri.empty()){
- newResource->m_uri = resourceInfo.uri;
- }
- else{
- newResource->m_uri = "sampleBundle/discomfortIndex/" + std::to_string(discomfortIndexSensorCount++);
+
+ std::vector< std::map< std::string, std::string > >::iterator itor_vec;
+ std::map< std::string, std::string >::iterator itor_map;
+ std::vector <std::string> inputs;
+
+ for (itor_vec = resourceInfo.resourceProperty["input"].begin();
+ itor_vec != resourceInfo.resourceProperty["input"].end(); itor_vec++)
+ {
+ for (itor_map = (*itor_vec).begin(); itor_map != (*itor_vec).end(); itor_map++)
+ {
+ inputs.push_back(itor_map->second);
+ }
}
+ std::cout << "SampleBundle::creating new discomfort index sensor " << std::endl;
+ // create DISensor resource
+ DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource(inputs);
+
+ newResource->m_uri = "/sampleBundle/discomfortIndex/" + std::to_string(
+ discomfortIndexSensorCount++);
newResource->m_resourceType = resourceInfo.resourceType;
+ newResource->m_mapResourceProperty = resourceInfo.resourceProperty;
- m_pResourceContainer->registerResource(newResource);
+ // setting input Attributes count
+ newResource->inputCount = newResource->m_mapResourceProperty["input"].size();
+ m_pResourceContainer->registerResource(newResource);
m_vecResources.push_back(newResource);
}
virtual string getPath() = 0;
virtual void setVersion(string version) = 0;
virtual string getVersion() = 0;
- virtual int getId() = 0; // will be set by container
static BundleInfo* createBundleInfo();
protected:
string m_ID, m_path, m_version;
+++ /dev/null
-//******************************************************************
-//
-// Copyright 2015 Samsung Electronics All Rights Reserved.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#ifndef CONFIGURATION_H_
-#define CONFIGURATION_H_
-
-#include <unistd.h>
-#include <string.h>
-#include <fstream>
-
-#include "rapidxml/rapidxml.hpp"
-#include "rapidxml/rapidxml_print.hpp"
-
-#include "ResourceContainer.h"
-#include "BundleInfo.h"
-
-using namespace OIC::Service;
-
-namespace OIC
-{
- namespace Service
- {
- class Configuration
- {
- public:
- typedef vector< std::map< std::string, std::string > > configInfo;
- struct resourceInfo
- {
- std::string name;
- std::string uri;
- std::string resourceType;
- std::string address;
- std::map< std::string, std::vector< std::map< std::string, std::string > > > resourceProperty;
- };
-
- Configuration();
- ~Configuration();
-
- Configuration(string configFile);
-
- void getCommonConfiguration(configInfo *configOutput);
- void getBundleConfiguration(std::string bundleId, configInfo *configOutput);
- void getResourceConfiguration(std::string bundleId,
- std::vector< resourceInfo > *configOutput);
- void getConfiguredBundles(configInfo *configOutput);
-
- private:
- void getConfigDocument(std::string pathConfigFile, std::string *pConfigData);
- void getCurrentPath(std::string *path);
-
- string m_pathConfigFile, m_configFile;
- };
- }
-}
-
-#endif
#include <string>
#include <vector>
#include <map>
+#include <list>
#include "BundleInfo.h"
{
namespace Service
{
- // placeholder
- class Resource
- {
-
- };
-
class ResourceContainer
{
public:
ResourceContainer();
virtual ~ResourceContainer();
- virtual void init() = 0;
- virtual void init(string configFile) = 0;
- virtual void registerBundle(BundleInfo *bundleinfo) = 0;
- virtual void unregisterBundle(BundleInfo *bundleinfo) = 0;
- virtual void unregisterBundle(int id) = 0;
- virtual void activateBundle(int id) = 0;
- virtual void deactivateBundle(int id) = 0;
- virtual void activateBundleByName(string name) = 0;
- virtual void deactivateBundleByName(string id) = 0;
- virtual void activateBundle(BundleInfo *bundleInfo) = 0;
- virtual void deactivateBundle(BundleInfo *bundleInfo) = 0;
- virtual vector< Resource * > listBundleResources(string id) = 0;
+ //virtual void initializeResourceContainer() = 0;
+ virtual void startContainer(string configFile) = 0;
+ virtual void stopContainer() = 0;
+
+ // list of bundle ids
+ virtual std::list<BundleInfo*> listBundles() = 0;
+ virtual void startBundle(string bundleId) = 0;
+ virtual void stopBundle(string bundleId) = 0;
+
+ // dynamic configuration
+ virtual void addBundle(string bundleId, string bundleUri, string bundlePath, std::map<string, string> params) =0;
+ virtual void removeBundle(string bundleId) = 0;
+
+ virtual void addResourceConfig(string bundleId, string resourceUri, std::map<string, string> params) = 0;
+ virtual void removeResourceConfig(string bundleId, string resourceUri) = 0;
+
+ virtual std::list<string> listBundleResources(string bundleId) = 0;
static ResourceContainer *getInstance();
};
#include <string>
#include "BundleInfo.h"
#include "ResourceContainerBundleAPI.h"
+#include "jni.h"
using namespace std;
using namespace OIC::Service;
bool isLoaded();
void setActivated(bool activated);
bool isActivated();
- int getId();
- void setId(int id);
+
+ void setActivatorName(string activatorName);
+ string getActivatorName();
+
+ void setJavaBundleActivatorMethod(jmethodID activator);
+ jmethodID getJavaBundleActivatorMethod();
+ void setJavaBundleDeactivatorMethod(jmethodID deactivator);
+ jmethodID getJavaBundleDeactivatorMethod();
+
+ void setJavaBundleActivatorObject(jobject);
+ jobject getJavaBundleActivatorObject();
void setBundleActivator(activator_t *);
activator_t *getBundleActivator();
void setBundleHandle(void *);
void *getBundleHandle();
+ void setJavaBundle(bool javaBundle);
+ bool getJavaBundle();
+
private:
- bool m_loaded, m_activated;
+ bool m_loaded, m_activated, m_java_bundle;
int m_id;
activator_t *m_activator;
deactivator_t *m_deactivator;
void *m_bundleHandle;
+ string m_activator_name;
+ jmethodID m_java_activator, m_java_deactivator;
+ jobject m_java_activator_object;
};
}
}
--- /dev/null
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef CONFIGURATION_H_
+#define CONFIGURATION_H_
+
+#include <unistd.h>
+#include <string.h>
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+#include <map>
+
+#include "rapidxml/rapidxml.hpp"
+#include "rapidxml/rapidxml_print.hpp"
+
+using namespace std;
+
+namespace OIC
+{
+ namespace Service
+ {
+ typedef vector< map< string, string > > configInfo;
+ struct resourceInfo
+ {
+ string name;
+ string uri;
+ string resourceType;
+ string address;
+ map< string, vector< map< string, string > > > resourceProperty;
+ };
+ class Configuration
+ {
+ public:
+
+
+
+ Configuration();
+ Configuration(string configFile);
+ ~Configuration();
+
+ void getConfiguredBundles(configInfo *configOutput);
+ void getBundleConfiguration(string bundleId, configInfo *configOutput);
+ void getResourceConfiguration(string bundleId, vector< resourceInfo > *configOutput);
+
+ private:
+ void getConfigDocument(string pathConfigFile);
+ void getCurrentPath(string *pPath);
+
+ string m_pathConfigFile;
+ string m_strConfigData;
+ rapidxml::xml_document< char > m_xmlDoc;
+ };
+ }
+}
+
+#endif /* CONFIGURATION_H_ */
--- /dev/null
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#ifndef JAVABUNDLERESOURCE_H_
+#define JAVABUNDLERESOURCE_H_
+
+#include <map>
+#include <vector>
+#include <string>
+#include <jni.h>
+#include "BundleResource.h"
+#include "ResourceContainerImpl.h"
+
+using namespace std;
+
+namespace OIC
+{
+ namespace Service
+ {
+ class JavaBundleResource: public BundleResource
+ {
+ public:
+ JavaBundleResource();
+ JavaBundleResource(JNIEnv *env, jobject obj, jobject bundleResource, string bundleId,
+ jobjectArray attributes);
+ virtual ~JavaBundleResource();
+
+ string getAttribute(string attributeName);
+ void setAttribute(string attributeName, string value);
+ void initAttributes();
+ private:
+ // needs to be a GlobalRef
+ jobject bundleResource;
+ jobjectArray attributes;
+ jclass bundleResourceClass;
+ jmethodID attributeSetter;
+ jmethodID attributeGetter;
+ string m_bundleId;
+ };
+ }
+}
+
+#endif
#include "PrimitiveRequest.h"
#include "PrimitiveResponse.h"
-#include "PrimitiveServerResource.h"
+#include "ResourceObject.h"
+
+#include "jni.h"
+#include <map>
using namespace OIC::Service;
virtual ~ResourceContainerImpl();
// methods from ResourceContainer
- void init();
- void init(string configFile);
- void activateBundle(int id);
- void deactivateBundle(int id);
- void activateBundleByName(string name);
- void deactivateBundleByName(string id);
+ void startContainer(string configFile);
+ void stopContainer();
+ void activateBundle(string bundleId);
+ void deactivateBundle(string bundleId);
void activateBundle(BundleInfo *bundleInfo);
void deactivateBundle(BundleInfo *bundleInfo);
- vector< Resource * > listBundleResources(string id);
// methods from ResourceContainerBundleAPI
void registerBundle(BundleInfo *bundleinfo);
void unregisterBundle(BundleInfo *bundleinfo);
- void unregisterBundle(int id);
+ void unregisterBundle(string id);
void registerResource(BundleResource *resource);
void unregisterResource(BundleResource *resource);
- void getCommonConfiguration(configInfo *configOutput);
void getBundleConfiguration(std::string bundleId, configInfo *configOutput);
void getResourceConfiguration(std::string bundleId,
std::vector< resourceInfo > *configOutput);
PrimitiveSetResponse setRequestHandler(const PrimitiveRequest &request,
const ResourceAttributes &attributes);
-
+
+ void onNotificationReceived(std::string strResourceUri);
+
static ResourceContainerImpl *getImplInstance();
+ void addBundle(string bundleId, string bundleUri, string bundlePath, std::map<string, string> params);
+ void removeBundle(string bundleId);
+
+ std::list<BundleInfo*> listBundles();
+
+ void addResourceConfig(string bundleId, string resourceUri, std::map<string, string> params);
+ void removeResourceConfig(string bundleId, string resourceUri);
+
+ std::list<string> listBundleResources(string bundleId);
+
+ void startBundle(string bundleId);
+ void stopBundle(string bundleId);
+
+ JavaVM* getJavaVM(string bundleId);
+
private:
- vector< BundleInfoInternal * > m_bundles;
- map< std::string, PrimitiveServerResource::Ptr > m_mapServers; //<uri, serverPtr>
+ map< std::string, BundleInfoInternal * > m_bundles; // <bundleID, bundleInfo>
+ map< std::string, ResourceObject::Ptr > m_mapServers; //<uri, serverPtr>
map< std::string, BundleResource * > m_mapResources; //<uri, resourcePtr>
string m_configFile;
- Configuration *m_config;
+ Configuration *m_config = NULL;
+ map<string, JavaVM*> m_bundleVM;
+
+ void registerJavaBundle(BundleInfo *bundleInfo);
+ void registerSoBundle(BundleInfo *bundleInfo);
+ void activateJavaBundle(string bundleId);
+ void activateSoBundle(string bundleId);
+ void deactivateJavaBundle(string bundleId);
+ void deactivateSoBundle(string bundleId);
+
};
}
}
--- /dev/null
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class org_iotivity_resourcecontainer_bundle_api_BaseActivator */
+
+#ifndef _Included_org_iotivity_resourcecontainer_bundle_api_BaseActivator
+#define _Included_org_iotivity_resourcecontainer_bundle_api_BaseActivator
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
+ * Method: registerJavaResource
+ * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_registerJavaResource
+ (JNIEnv *, jobject, jobject, jobjectArray, jstring, jstring, jstring, jstring);
+
+/*
+ * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
+ * Method: unregisterJavaResource
+ * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_unregisterJavaResource
+ (JNIEnv *, jobject, jobject);
+
+/*
+ * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
+ * Method: getNumberOfConfiguredResources
+ * Signature: (Ljava/lang/String;)I
+ */
+JNIEXPORT jint JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getNumberOfConfiguredResources
+ (JNIEnv *, jobject, jstring);
+
+/*
+ * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
+ * Method: getConfiguredResourceParams
+ * Signature: (Ljava/lang/String;I)[Ljava/lang/String;
+ */
+JNIEXPORT jobjectArray JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getConfiguredResourceParams
+ (JNIEnv *, jobject, jstring, jint);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
--- /dev/null
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include <jni.h>
+#include "org_iotivity_resourcecontainer_bundle_api_BaseActivator.h"
+#include "JavaBundleResource.h"
+#include "ResourceContainerImpl.h"
+
+using namespace OIC::Service;
+
+/*
+ * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
+ * Method: registerJavaResource
+ * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_registerJavaResource
+ (JNIEnv *env, jobject obj, jobject bundleResource, jobjectArray attributes, jstring bundleId, jstring uri, jstring resourceType, jstring res_name){
+ //return;
+ const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
+ const char *str_uri = env->GetStringUTFChars(uri, 0);
+ const char *str_resourceType = env->GetStringUTFChars(resourceType, 0);
+ const char *str_res_name = env->GetStringUTFChars(res_name, 0);
+ JavaBundleResource *javaBundleResource = new JavaBundleResource(env, obj, bundleResource, str_bundleId, attributes);
+ ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
+
+ javaBundleResource->m_uri = string(str_uri, strlen(str_uri));
+ javaBundleResource->m_resourceType = string(str_resourceType, strlen(str_resourceType));
+ javaBundleResource->m_name = string(str_res_name, strlen(str_res_name));
+ container->registerResource(javaBundleResource);
+
+}
+
+/*
+ * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
+ * Method: unregisterJavaResource
+ * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;)V
+ */
+JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_unregisterJavaResource
+ (JNIEnv *, jobject, jobject){
+
+}
+
+/*
+ * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
+ * Method: getNumberOfConfiguredResources
+ * Signature: (Ljava/lang/String;)I
+ */
+JNIEXPORT jint JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getNumberOfConfiguredResources
+ (JNIEnv *env, jobject obj, jstring bundleId){
+
+ const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
+
+ ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
+ vector<resourceInfo> resourceConfig;
+ container->getResourceConfiguration(str_bundleId, &resourceConfig);
+
+ return resourceConfig.size();
+}
+
+/*
+ * Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
+ * Method: getConfiguredResourceParams
+ * Signature: (Ljava/lang/String;I)[Ljava/lang/String;
+ */
+JNIEXPORT jobjectArray JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_getConfiguredResourceParams
+ (JNIEnv *env, jobject obj, jstring bundleId, jint resourceId){
+
+ jobjectArray ret;
+ const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
+
+ ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
+ vector<resourceInfo> resourceConfig;
+ container->getResourceConfiguration(str_bundleId, &resourceConfig);
+ resourceInfo conf = resourceConfig[resourceId];
+ ret = (jobjectArray) env->NewObjectArray(4,
+ env->FindClass("java/lang/String"),
+ env->NewStringUTF(""));
+
+ env->SetObjectArrayElement(ret, 0, env->NewStringUTF(conf.name.c_str()));
+ env->SetObjectArrayElement(ret, 1, env->NewStringUTF(conf.uri.c_str()));
+ env->SetObjectArrayElement(ret, 2, env->NewStringUTF(conf.resourceType.c_str()));
+ env->SetObjectArrayElement(ret, 3, env->NewStringUTF(conf.address.c_str()));
+ return ret;
+}
return m_activated;
}
- int BundleInfoInternal::getId()
- {
- return m_id;
- }
-
- void BundleInfoInternal::setId(int id)
- {
- m_id = id;
- }
-
void BundleInfoInternal::setBundleActivator(activator_t *activator)
{
m_activator = activator;
m_bundleHandle = handle;
}
- void *BundleInfoInternal::getBundleHandle()
+ void* BundleInfoInternal::getBundleHandle()
{
return m_bundleHandle;
}
+
+ void BundleInfoInternal::setJavaBundle(bool javaBundle)
+ {
+ m_java_bundle = javaBundle;
+ }
+
+ bool BundleInfoInternal::getJavaBundle()
+ {
+ return m_java_bundle;
+ }
+
+ void BundleInfoInternal::setActivatorName(string activatorName)
+ {
+ m_activator_name = activatorName;
+ }
+
+ string BundleInfoInternal::getActivatorName()
+ {
+ return m_activator_name;
+ }
+
+ void BundleInfoInternal::setJavaBundleActivatorMethod(jmethodID javaBundleActivator)
+ {
+ m_java_activator = javaBundleActivator;
+ }
+
+ jmethodID BundleInfoInternal::getJavaBundleActivatorMethod()
+ {
+ return m_java_activator;
+ }
+
+ void BundleInfoInternal::setJavaBundleDeactivatorMethod(jmethodID javaBundleActivator)
+ {
+ m_java_deactivator = javaBundleActivator;
+ }
+
+ jmethodID BundleInfoInternal::getJavaBundleDeactivatorMethod()
+ {
+ return m_java_deactivator;
+ }
+
+ /*void BundleInfoInternal::setJavaBundleActivatorClass(jclass activator_class)
+ {
+ m_java_activator_class = activator_class;
+ }
+
+ jclass BundleInfoInternal::getJavaBundleActivatorClass()
+ {
+ return m_java_activator_class;
+ }*/
+
+ void BundleInfoInternal::setJavaBundleActivatorObject(jobject activator_object){
+ m_java_activator_object = activator_object;
+ }
+
+ jobject BundleInfoInternal::getJavaBundleActivatorObject(){
+ return m_java_activator_object;
+ }
}
}
#include "BundleResource.h"
#include "Configuration.h"
+#include <list>
+#include <string.h>
-
-namespace OIC{
- namespace Service{
+namespace OIC
+{
+ namespace Service
+ {
BundleResource::BundleResource()
{
- cout << "BundleResource constructor called\n";
+
}
BundleResource::~BundleResource()
{
}
+
+ void BundleResource::registerObserver(NotificationReceiver *pNotiReceiver)
+ {
+ m_pNotiReceiver = pNotiReceiver;
+ }
+
+ void BundleResource::setAttribute(string attributeName, string value)
+ {
+ this->m_mapAttributes[attributeName] = value;
+
+ if (m_pNotiReceiver != NULL && !m_uri.empty())
+ {
+ m_pNotiReceiver->onNotificationReceived(m_uri);
+ }
+
+ }
+
+ std::string BundleResource::getAttribute(string attributeName)
+ {
+ return this->m_mapAttributes[attributeName];
+ }
+
+ std::list< string > BundleResource::getAttributeNames()
+ {
+ std::list< string > ret;
+ for (map< string, string >::iterator it = m_mapAttributes.begin();
+ it != m_mapAttributes.end(); ++it)
+ {
+ ret.push_back(it->first);
+ }
+ return ret;
+ }
+
}
}
Configuration::Configuration()
{
- // TODO: temporary path and config file name
- getCurrentPath(&m_pathConfigFile);
- m_pathConfigFile.append("/");
- m_pathConfigFile.append("ResourceContainerConfig.xml");
- }
-
- Configuration::~Configuration()
- {
}
Configuration::Configuration(string configFile)
{
- m_configFile = configFile;
+
getCurrentPath(&m_pathConfigFile);
m_pathConfigFile.append("/");
- m_pathConfigFile.append(m_configFile);
+ m_pathConfigFile.append(configFile);
+
+ getConfigDocument(m_pathConfigFile);
}
- void Configuration::getCommonConfiguration(configInfo *configOutput)
+ Configuration::~Configuration()
{
- string strConfigData;
-
- rapidxml::xml_document< char > xmlDoc;
-
- rapidxml::xml_node< char > *root;
- rapidxml::xml_node< char > *item;
- rapidxml::xml_node< char > *subItem;
-
- string strKey, strValue;
-
- getConfigDocument(m_pathConfigFile, &strConfigData);
-
- try
- {
- xmlDoc.parse< 0 >((char *) strConfigData.c_str());
-
- // <container>
- root = xmlDoc.first_node();
-
- if (!root)
- {
- throw rapidxml::parse_error("No Root Element", 0);
- }
- std::map< std::string, std::string > bundleMap;
- for (item = root->first_node(); item; item = item->next_sibling())
- {
- strKey = item->name();
- strValue = item->value();
-
- // <config>
- if (!strKey.compare("config"))
- {
- for (subItem = item->first_node(); subItem; subItem =
- subItem->next_sibling())
- {
- strKey = subItem->name();
- strValue = subItem->value();
-
- bundleMap.insert(
- std::make_pair(trim_both(strKey), trim_both(strValue)));
- }
- break;
- }
- }
- configOutput->push_back(bundleMap);
- }
- catch (rapidxml::parse_error &e)
- {
- cout << "xml parsing failed !!" << endl;
- cout << e.what() << endl;
- }
}
void Configuration::getConfiguredBundles(configInfo *configOutput)
{
- string strConfigData;
-
- rapidxml::xml_document< char > xmlDoc;
-
rapidxml::xml_node< char > *bundle;
rapidxml::xml_node< char > *subItem;
string strKey, strValue;
- cout << "Opening: " << m_pathConfigFile << endl;
-
- getConfigDocument(m_pathConfigFile, &strConfigData);
-
- //cout << strConfigData.c_str() << endl;
try
{
-
- xmlDoc.parse< 0 >((char *) strConfigData.c_str());
//cout << "Name of first node is: " << xmlDoc.first_node()->name() << endl;
- for (bundle = xmlDoc.first_node()->first_node("bundle"); bundle;
- bundle = bundle->next_sibling())
+ for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
{
std::map< std::string, std::string > bundleMap;
//cout << "Bundle: " << bundle->name() << endl;
{
strKey = subItem->name();
strValue = subItem->value();
+
if (strlen(subItem->value()) > 0)
{
- bundleMap.insert(
- std::make_pair(trim_both(strKey), trim_both(strValue)));
+ bundleMap.insert(std::make_pair(trim_both(strKey), trim_both(strValue)));
//cout << strKey << " " << strValue << endl;
-
}
}
configOutput->push_back(bundleMap);
void Configuration::getBundleConfiguration(string bundleId, configInfo *configOutput)
{
- string strConfigData;
-
- rapidxml::xml_document< char > xmlDoc;
-
- rapidxml::xml_node< char > *root;
- rapidxml::xml_node< char > *item;
- rapidxml::xml_node< char > *subItem;
-
- string strKey, strValue;
+ rapidxml::xml_node< char > *bundle;
- getConfigDocument(m_pathConfigFile, &strConfigData);
+ string strBundleId, strPath, strVersion;
try
{
- xmlDoc.parse< 0 >((char *) strConfigData.c_str());
+ std::map< std::string, std::string > bundleConfigMap;
- // <container>
- root = xmlDoc.first_node();
-
- if (!root)
- {
- throw rapidxml::parse_error("No Root Element", 0);
- }
- std::map< std::string, std::string > bundleMap;
- for (item = root->first_node(); item; item = item->next_sibling())
+ // <bundle>
+ for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
{
- strKey = item->name();
- strValue = item->value();
+ // <id>
+ strBundleId = bundle->first_node("id")->value();
+ bundleConfigMap.insert(std::make_pair("id", trim_both(strBundleId)));
- // <bundle>
- if (!strKey.compare("bundle"))
+ if (!strBundleId.compare(bundleId))
{
- for (subItem = item->first_node(); subItem; subItem =
- subItem->next_sibling())
- {
- strKey = subItem->name();
- strValue = subItem->value();
+ // <path>
+ strPath = bundle->first_node("path")->value();
+ bundleConfigMap.insert(std::make_pair("id", trim_both(strPath)));
- if (!strKey.compare("bundleID") && strValue.compare(bundleId))
- break;
+ // <version>
+ strVersion = bundle->first_node("version")->value();
+ bundleConfigMap.insert(std::make_pair("id", trim_both(strVersion)));
- // bundle info (except resource data)
- if (strKey.compare("resources"))
- {
- bundleMap.insert(
- std::make_pair(trim_both(strKey), trim_both(strValue)));
- }
- }
+ break;
}
}
- configOutput->push_back(bundleMap);
+ configOutput->push_back(bundleConfigMap);
}
catch (rapidxml::parse_error &e)
{
}
}
- void Configuration::getResourceConfiguration(string bundleId,
- vector< resourceInfo > *configOutput)
+ void Configuration::getResourceConfiguration(std::string bundleId,
+ std::vector< resourceInfo > *configOutput)
{
- string strConfigData;
-
- rapidxml::xml_document< char > xmlDoc;
-
- rapidxml::xml_node< char > *root;
- rapidxml::xml_node< char > *item;
- rapidxml::xml_node< char > *subItem, *subItem2, *subItem3, *subItem4, *subItem5;
+ rapidxml::xml_node< char > *bundle;
+ rapidxml::xml_node< char > *resource;
+ rapidxml::xml_node< char > *item, *subItem, *subItem2;
+ string strBundleId;
string strKey, strValue;
- getConfigDocument(m_pathConfigFile, &strConfigData);
-
try
{
- xmlDoc.parse< 0 >((char *) strConfigData.c_str());
-
- // <container>
- root = xmlDoc.first_node();
-
- if (!root)
- {
- throw rapidxml::parse_error("No Root Element", 0);
- }
-
- for (item = root->first_node(); item; item = item->next_sibling())
+ // <bundle>
+ for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling())
{
- strKey = item->name();
- strValue = item->value();
+ // <id>
+ strBundleId = bundle->first_node("id")->value();
- // <bundle>
- if (!strKey.compare("bundle"))
+ if (!strBundleId.compare(bundleId))
{
- for (subItem = item->first_node(); subItem; subItem =
- subItem->next_sibling())
+ // <resourceInfo>
+ for (resource = bundle->first_node("resources")->first_node("resourceInfo"); resource;
+ resource = resource->next_sibling())
{
- strKey = subItem->name();
- strValue = subItem->value();
-
- if (!strKey.compare("bundleID") && strValue.compare(bundleId))
- break;
+ resourceInfo tempResourceInfo;
- // <resources>
- if (!strKey.compare("resources"))
+ for (item = resource->first_node(); item; item = item->next_sibling())
{
- for (subItem2 = subItem->first_node(); subItem2;
- subItem2 = subItem2->next_sibling())
- {
- strKey = subItem2->name();
- strValue = subItem2->value();
+ strKey = item->name();
+ strValue = item->value();
- // <resourceInfo> : for 1 resource
- if (!strKey.compare("resourceInfo"))
- {
- resourceInfo tempResourceInfo;
-
- for (subItem3 = subItem2->first_node(); subItem3; subItem3 =
- subItem3->next_sibling())
- {
+ if (!strKey.compare("name"))
+ tempResourceInfo.name = trim_both(strValue);
- strKey = subItem3->name();
- strValue = subItem3->value();
+ else if (!strKey.compare("uri"))
+ tempResourceInfo.uri = trim_both(strValue);
- if (!strKey.compare("name"))
- tempResourceInfo.name = trim_both(strValue);
+ else if (!strKey.compare("address"))
+ tempResourceInfo.address = trim_both(strValue);
- else if (!strKey.compare("uri"))
- tempResourceInfo.uri = trim_both(strValue);
+ else if (!strKey.compare("resourceType"))
+ tempResourceInfo.resourceType = trim_both(strValue);
- else if (!strKey.compare("address"))
- tempResourceInfo.address = trim_both(strValue);
-
- else if (!strKey.compare("resourceType"))
- tempResourceInfo.resourceType = trim_both(strValue);
-
- else
- {
- for (subItem4 = subItem3->first_node(); subItem4;
- subItem4 = subItem4->next_sibling())
- {
- map< string, string > propertyMap;
-
- strKey = subItem4->name();
+ else
+ {
+ for (subItem = item->first_node(); subItem; subItem = subItem->next_sibling())
+ {
+ map< string, string > propertyMap;
- for (subItem5 = subItem4->first_node();
- subItem5;
- subItem5 = subItem5->next_sibling())
- {
- string newStrKey = subItem5->name();
- string newStrValue = subItem5->value();
+ strKey = subItem->name();
- propertyMap[trim_both(newStrKey)] =
- trim_both(newStrValue);
- }
+ for (subItem2 = subItem->first_node(); subItem2; subItem2 = subItem2->next_sibling())
+ {
+ string newStrKey = subItem2->name();
+ string newStrValue = subItem2->value();
- tempResourceInfo.resourceProperty[trim_both(
- strKey)].push_back(propertyMap);
- }
- }
+ propertyMap[trim_both(newStrKey)] = trim_both(newStrValue);
}
- configOutput->push_back(tempResourceInfo);
+
+ tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(propertyMap);
}
}
}
+ configOutput->push_back(tempResourceInfo);
}
}
}
}
catch (rapidxml::parse_error &e)
{
- cout << "xml parsing failed !!" << endl;
- cout << e.what() << endl;
+ std::cout << "xml parsing failed !!" << std::endl;
+ std::cout << e.what() << std::endl;
}
+
}
- void Configuration::getConfigDocument(std::string pathConfigFile, std::string *pConfigData)
+ void Configuration::getConfigDocument(std::string pathConfigFile)
{
std::basic_ifstream< char > xmlFile(pathConfigFile.c_str());
xmlFile.read(&xmlData.front(), (std::streamsize) size);
xmlFile.close();
- *pConfigData = std::string(xmlData.data());
+ m_strConfigData = std::string(xmlData.data());
+
+ try
+ {
+ m_xmlDoc.parse< 0 >((char *)m_strConfigData.c_str());
+
+ }
+ catch (rapidxml::parse_error &e)
+ {
+ std::cout << "xml parsing failed !!" << std::endl;
+ std::cout << e.what() << std::endl;
+ }
}
else
{
}
}
- void Configuration::getCurrentPath(std::string *path)
+ void Configuration::getCurrentPath(std::string *pPath)
{
char buffer[2048];
char *strPath = NULL;
*strPath = '\0';
- path->append(buffer);
+ pPath->append(buffer);
}
}
}
using namespace OIC::Service;
using OC::oc_log_stream;
-
/* Annother way to create a context: */
auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
{
return os;
};
+
int main()
{
info_logger()->set_module("ContainerTest");
info_logger() << "Starting container test." << std::flush;
ResourceContainer *container = ResourceContainer::getInstance();
- container->init("examples/ResourceContainerConfig.xml");
+ container->startContainer("examples/ResourceContainerConfig.xml");
+
+ std::list<BundleInfo*> bundles = container->listBundles();
+ std::list<BundleInfo*>::iterator bundleIt;
+
+ for(bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++){
+ BundleInfo* bi = *bundleIt;
+ info_logger() << "Available bundle: " << bi->getID() << endl;
+ }
+
+ /*int menu;
+
+ cout << "press \'1\' to test discomfortIndexSensor" << endl;
+ cin >> menu;
+
+ if (menu == 1)
+ {
+ string testingSoftSensor = "/sampleBundle/discomfortIndex/0";
+
+ SoftSensorResource::SensorData Thing_TempHumSensor;
+ SoftSensorResource::SensorData Thing_TempHumSensor1;
+
+ map < string, string > data;
+ data["name"] = "temperature";
+ data["type"] = "int";
+ data["value"] = "25";
+
+ map < string, string > data1;
+ data1["name"] = "humidity";
+ data1["type"] = "int";
+ data1["value"] = "40";
+
+ Thing_TempHumSensor.sensorName = "Thing_TempHumSensor";
+ Thing_TempHumSensor.data.push_back(data);
+ Thing_TempHumSensor.data.push_back(data1);
+
+ Thing_TempHumSensor1.sensorName = "Thing_TempHumSensor1";
+ Thing_TempHumSensor1.data.push_back(data);
+ Thing_TempHumSensor1.data.push_back(data1);
+
+ container->setInputAttribute(testingSoftSensor, Thing_TempHumSensor);
+ container->setInputAttribute(testingSoftSensor, Thing_TempHumSensor1);
+
+ cout << endl << endl;
+
+ // TEST :: change data (change humidity data of Thing_TempHumSensor1)
+ data["value"] = "33";
+ data1["value"] = "80";
+ Thing_TempHumSensor1.data.clear();
+ Thing_TempHumSensor1.data.push_back(data);
+ Thing_TempHumSensor1.data.push_back(data1);
+ container->setInputAttribute(testingSoftSensor, Thing_TempHumSensor1);
+ }*/
while (1)
{
;
}
}
-
--- /dev/null
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "JavaBundleResource.h"
+#include <jni.h>
+#include <string.h>
+
+#include <iostream>
+
+using namespace OIC::Service;
+using namespace std;
+
+#include "oc_logger.hpp"
+
+using OC::oc_log_stream;
+using namespace OIC::Service;
+
+auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
+{
+ static OC::oc_log_stream ols(oc_make_ostream_logger);
+ static boost::iostreams::stream<OC::oc_log_stream> os(ols);
+ os->set_level(OC_LOG_INFO);
+ os->set_module("JavaBundleResource");
+ return os;
+};
+
+auto error_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
+{
+ static OC::oc_log_stream ols(oc_make_ostream_logger);
+ static boost::iostreams::stream<OC::oc_log_stream> os(ols);
+ os->set_level(OC_LOG_ERROR);
+ os->set_module("JavaBundleResource");
+ return os;
+};
+
+JavaBundleResource::JavaBundleResource()
+{
+
+}
+
+void JavaBundleResource::initAttributes()
+{
+
+}
+
+JavaBundleResource::JavaBundleResource(JNIEnv *env, jobject obj, jobject bundleResource,
+ string bundleId, jobjectArray attributes)
+{
+ int stringCount = env->GetArrayLength(attributes);
+
+ for (int i = 0; i < stringCount; i++)
+ {
+ jstring str = (jstring) env->GetObjectArrayElement(attributes, i);
+ const char *rawString = env->GetStringUTFChars(str, 0);
+ string s(rawString, strlen(rawString));
+ BundleResource::setAttribute(s, "");
+ }
+
+ m_bundleId = bundleId;
+
+ this->bundleResource = env->NewGlobalRef(bundleResource);
+
+ bundleResourceClass = env->GetObjectClass(bundleResource);
+ attributeSetter = env->GetMethodID(bundleResourceClass, "setAttribute",
+ "(Ljava/lang/String;Ljava/lang/String;)V");
+ attributeGetter = env->GetMethodID(bundleResourceClass, "getAttribute",
+ "(Ljava/lang/String;)Ljava/lang/String");
+}
+
+JavaBundleResource::~JavaBundleResource()
+{
+
+}
+
+string JavaBundleResource::getAttribute(string attributeName)
+{
+ JavaVM* vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
+
+ JNIEnv * env;
+ int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
+
+ if (envStat == JNI_EDETACHED)
+ {
+ if (vm->AttachCurrentThread((void**) &env, NULL) != 0)
+ {
+ error_logger() << "Failed to attach " << endl;
+ }
+ }
+ else if (envStat == JNI_EVERSION)
+ {
+ error_logger() << "Env: version not supported " << endl;
+ }
+
+ jstring attrName = env->NewStringUTF(attributeName.c_str());
+
+ jstring returnString = (jstring) env->CallObjectMethod(bundleResource, attributeGetter,
+ attrName);
+ const char *js = env->GetStringUTFChars(returnString, NULL);
+ std::string value(js);
+ env->ReleaseStringUTFChars(returnString, js);
+ BundleResource::setAttribute(attributeName, value);
+ return value;
+}
+
+void JavaBundleResource::setAttribute(string attributeName, string value)
+{
+ BundleResource::setAttribute(attributeName, value);
+
+ JavaVM* vm = ResourceContainerImpl::getImplInstance()->getJavaVM(m_bundleId);
+
+ JNIEnv * env;
+ int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
+
+ if (envStat == JNI_EDETACHED)
+ {
+ if (vm->AttachCurrentThread((void**) &env, NULL) != 0)
+ {
+ error_logger() << "Failed to attach " << endl;
+ }
+ }
+ else if (envStat == JNI_EVERSION)
+ {
+ error_logger() << "Env: version not supported " << endl;
+ }
+
+ jstring attrName = env->NewStringUTF(attributeName.c_str());
+ jstring val = env->NewStringUTF(value.c_str());
+
+ env->CallObjectMethod(bundleResource, attributeSetter, attrName, val);
+}
#include <string.h>
#include <iostream>
#include <fstream>
+#include <jni.h>
#include "oc_logger.hpp"
using OC::oc_log_stream;
using namespace OIC::Service;
-/* Annother way to create a context: */
auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
{
static OC::oc_log_stream ols(oc_make_ostream_logger);
return os;
};
-/* Annother way to create a context: */
auto error_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
{
static OC::oc_log_stream ols(oc_make_ostream_logger);
// TODO Auto-generated destructor stub
}
- void ResourceContainerImpl::init()
+ bool has_suffix(const std::string &str, const std::string &suffix)
{
-
+ return str.size() >= suffix.size()
+ && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
- void ResourceContainerImpl::init(string configFile)
+ void ResourceContainerImpl::startContainer(string configFile)
{
+ info_logger() << "Starting resource container" << endl;
+
m_config = new Configuration(configFile);
- Configuration::configInfo bundles;
+ configInfo bundles;
m_config->getConfiguredBundles(&bundles);
+
for (int i = 0; i < bundles.size(); i++)
{
BundleInfo *bundleInfo = BundleInfo::createBundleInfo();
bundleInfo->setPath(bundles[i]["path"]);
bundleInfo->setVersion(bundles[i]["version"]);
bundleInfo->setID(bundles[i]["id"]);
- cout << "Init Bundle:" << bundles[i]["id"] << ";" << bundles[i]["path"] << endl;
+ if (!bundles[i]["activator"].empty())
+ {
+ string activatorName = bundles[i]["activator"];
+ std::replace(activatorName.begin(), activatorName.end(), '.', '/');
+ ((BundleInfoInternal*) bundleInfo)->setActivatorName(activatorName);
+
+ }
+ info_logger() << "Init Bundle:" << bundles[i]["id"] << ";" << bundles[i]["path"]
+ << endl;
registerBundle(bundleInfo);
activateBundle(bundleInfo);
}
}
- // loads the bundle
- void ResourceContainerImpl::registerBundle(BundleInfo *bundleInfo)
+ void ResourceContainerImpl::registerJavaBundle(BundleInfo *bundleInfo)
{
- info_logger() << "Registering bundle: " << bundleInfo->getPath() << endl;
+ info_logger() << "Registering Java bundle " << bundleInfo->getID() << endl;
+ JavaVM * jvm;
+ JNIEnv * env;
+ JavaVMInitArgs vm_args;
+ JavaVMOption options[3];
+
+ BundleInfoInternal* bundleInfoInternal = (BundleInfoInternal*) bundleInfo;
+
+ options[0].optionString = "-Djava.compiler=NONE";
+ char classpath[1000];
+ strcpy(classpath, "-Djava.class.path=");
+ strcat(classpath, bundleInfo->getPath().c_str());
+
+ options[1].optionString = classpath;
+
+ options[2].optionString = "-Djava.library.path=.";
+
+ vm_args.version = JNI_VERSION_1_4;
+ vm_args.options = options;
+ vm_args.nOptions = 3;
+ vm_args.ignoreUnrecognized = 1;
+
+ JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
+
+ m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
+
+ const char *className = bundleInfoInternal->getActivatorName().c_str();
+
+ jclass bundleActivatorClass = env->FindClass(className);
+
+ if (bundleActivatorClass == NULL)
+ {
+ error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
+ << " bundle activator(" << bundleInfoInternal->getActivatorName()
+ << ") not found " << endl;
+ return;
+ }
+
+ jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
+ "()V");
+
+ if (activateMethod == NULL)
+ {
+ error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
+ << " activate bundle method not found " << endl;
+ return;
+ }
+ bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
+
+ jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
+ "()V");
+
+ if (deactivateMethod == NULL)
+ {
+ error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
+ << " deactivate bundle method not found " << endl;
+ return;
+ }
+
+ bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
+
+ jmethodID constructor;
+
+ constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
+
+ jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
+
+ jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
+
+ bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
- m_bundles.push_back((BundleInfoInternal *) bundleInfo);
- ((BundleInfoInternal *) bundleInfo)->setId(m_bundles.size() - 1);
+ bundleInfoInternal->setLoaded(true);
+ info_logger() << "Bundle registered" << endl;
+ }
+ void ResourceContainerImpl::registerSoBundle(BundleInfo *bundleInfo)
+ {
char *error;
activator_t *bundleActivator = NULL;
}
}
- void ResourceContainerImpl::activateBundle(int id)
+ // loads the bundle
+ void ResourceContainerImpl::registerBundle(BundleInfo *bundleInfo)
+ {
+ info_logger() << "Registering bundle: " << bundleInfo->getPath() << endl;
+
+ m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo);
+
+ if (has_suffix(bundleInfo->getPath(), ".jar"))
+ {
+ ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true);
+ registerJavaBundle(bundleInfo);
+ }
+ else
+ {
+ ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false);
+ registerSoBundle(bundleInfo);
+ }
+ }
+
+ void ResourceContainerImpl::activateJavaBundle(string bundleId)
{
- activator_t *bundleActivator = m_bundles[id]->getBundleActivator();
- info_logger() << "Activating bundle: " << m_bundles[id]->getID() << ", "
- << m_bundles[id]->getId() << endl;
+ info_logger() << "Activating java bundle" << endl;
+ JavaVM* vm = getJavaVM(bundleId);
+ BundleInfoInternal* bundleInfoInternal = (BundleInfoInternal*) m_bundles[bundleId];
+ JNIEnv * env;
+ int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
+
+ if (envStat == JNI_EDETACHED)
+ {
+ if (vm->AttachCurrentThread((void**) &env, NULL) != 0)
+ {
+ error_logger() << "Failed to attach " << endl;
+ }
+ }
+ else if (envStat == JNI_EVERSION)
+ {
+ error_logger() << "Env: version not supported " << endl;
+ }
+
+ env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
+ bundleInfoInternal->getJavaBundleActivatorMethod());
+ }
+
+ void ResourceContainerImpl::activateSoBundle(string bundleId)
+ {
+ activator_t *bundleActivator = m_bundles[bundleId]->getBundleActivator();
if (bundleActivator != NULL)
{
- bundleActivator(this, m_bundles[id]->getID());
- m_bundles[id]->setActivated(true);
+ bundleActivator(this, m_bundles[bundleId]->getID());
+ m_bundles[bundleId]->setActivated(true);
}
else
{
//Unload module and return error
error_logger() << "Activation unsuccessful." << endl;
}
+
+ BundleInfoInternal* bundleInfoInternal = (BundleInfoInternal*) m_bundles[bundleId];
+ bundleInfoInternal->setActivated(true);
+ }
+
+ void ResourceContainerImpl::activateBundle(string id)
+ {
+
+ info_logger() << "Activating bundle: " << m_bundles[id]->getID() << endl;
+
+ if (m_bundles[id]->getJavaBundle())
+ {
+ activateJavaBundle(id);
+ }
+ else
+ {
+ activateSoBundle(id);
+ }
+
+ info_logger() << "Bundle activated: " << m_bundles[id]->getID() << endl;
+
}
void ResourceContainerImpl::activateBundle(BundleInfo *bundleInfo)
{
if (((BundleInfoInternal *) bundleInfo)->isLoaded())
{
- activateBundle(bundleInfo->getId());
+ activateBundle(bundleInfo->getID());
}
}
{
if (((BundleInfoInternal *) bundleInfo)->isActivated())
{
- deactivateBundle(bundleInfo->getId());
+ deactivateBundle(bundleInfo->getID());
+ }
+ }
+
+
+ void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
+ {
+ info_logger() << "Deactivating java bundle" << endl;
+ JavaVM* vm = getJavaVM(bundleId);
+ BundleInfoInternal* bundleInfoInternal = (BundleInfoInternal*) m_bundles[bundleId];
+ JNIEnv * env;
+ int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
+
+ if (envStat == JNI_EDETACHED)
+ {
+ if (vm->AttachCurrentThread((void**) &env, NULL) != 0)
+ {
+ error_logger() << "Failed to attach " << endl;
+ }
}
+ else if (envStat == JNI_EVERSION)
+ {
+ error_logger() << "Env: version not supported " << endl;
+ }
+
+ env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
+ bundleInfoInternal->getJavaBundleDeactivatorMethod());
}
- void ResourceContainerImpl::deactivateBundle(int id)
+ void ResourceContainerImpl::deactivateSoBundle(string id)
{
+
deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator();
- info_logger() << "De-activating bundle: " << m_bundles[id]->getID() << ", "
- << m_bundles[id]->getId() << endl;
+ info_logger() << "De-activating bundle: " << m_bundles[id]->getID() << endl;
if (bundleDeactivator != NULL)
{
}
}
- void ResourceContainerImpl::activateBundleByName(string name)
- {
-
- }
-
- void ResourceContainerImpl::deactivateBundleByName(string id)
+ void ResourceContainerImpl::deactivateBundle(string id)
{
+ if (m_bundles[id]->getJavaBundle())
+ {
+ deactivateJavaBundle(id);
+ }
+ else
+ {
+ deactivateSoBundle(id);
+ }
}
- vector< Resource * > ResourceContainerImpl::listBundleResources(string id)
+ std::list< string > ResourceContainerImpl::listBundleResources(string id)
{
- vector< Resource * > ret;
+ std::list< string > ret;
return ret;
}
void ResourceContainerImpl::registerResource(BundleResource *resource)
{
- cout << "Register resource called. " << resource->m_uri << " "
- << resource->m_resourceType << endl;
string strUri = resource->m_uri;
string strResourceType = resource->m_resourceType;
- PrimitiveServerResource::Ptr server =
- PrimitiveServerResource::Builder(strUri, strResourceType, "DEFAULT_INTERFACE").setObservable(
- false).setDiscoverable(true).create();
+ info_logger() << "Registration of resource " << strUri << "," << strResourceType
+ << endl;
+
+ ResourceObject::Ptr server = ResourceObject::Builder(strUri, strResourceType,
+ "DEFAULT_INTERFACE").setObservable(true).setDiscoverable(true).build();
m_mapServers[strUri] = server;
m_mapResources[strUri] = resource;
- cout << "Registered resource has " << m_mapResources[strUri]->m_mapAttributes.size()
- << " attributes.\n" << endl;
-
+ resource->registerObserver(this);
server->setGetRequestHandler(
std::bind(&ResourceContainerImpl::getRequestHandler, this,
std::placeholders::_1, std::placeholders::_2));
server->setSetRequestHandler(
std::bind(&ResourceContainerImpl::setRequestHandler, this,
std::placeholders::_1, std::placeholders::_2));
+ info_logger() << "Registration finished " << strUri << "," << strResourceType << endl;
}
PrimitiveGetResponse ResourceContainerImpl::getRequestHandler(
if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
&& m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
{
- // temp
- m_mapResources[request.getResourceUri()]->getAttribute("");
-
- for (auto i : m_mapResources[request.getResourceUri()]->m_mapAttributes)
+ for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
{
- cout << "Setting attribute " << i.first.c_str() << ", " << i.second.c_str()
- << endl;
- attr[i.first.c_str()] = i.second.c_str();
+ attr[attrName] = m_mapResources[request.getResourceUri()]->getAttribute(
+ attrName);
}
}
{
ResourceAttributes attr = attributes;
- cout << "Set request handler resource called.\n";
-
if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
&& m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
{
-
- cout << "Setting the attributes of the resource." << request.getResourceUri()
- << " it has "
- << m_mapResources[request.getResourceUri()]->m_mapAttributes.size()
- << " attributes " << std::endl;
-
- for (auto i : m_mapResources[request.getResourceUri()]->m_mapAttributes)
+ for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
{
- if (!attr[i.first].toString().empty())
+ if (!attr[attrName].toString().empty())
{
- cout << "Setting " << i.first << endl;
- m_mapResources[request.getResourceUri()]->setAttribute(i.first,
- attr[i.first].toString());
+ m_mapResources[request.getResourceUri()]->setAttribute(attrName,
+ attr[attrName].toString());
}
}
-
- // set m_mapAttributes to attributes
}
return PrimitiveSetResponse::create(attr);
void ResourceContainerImpl::unregisterResource(BundleResource *resource)
{
-
+ // To be implemented
}
void ResourceContainerImpl::unregisterBundle(BundleInfo *bundleInfo)
if (((BundleInfoInternal *) bundleInfo)->isLoaded()
&& !((BundleInfoInternal *) bundleInfo)->isActivated())
{
- unregisterBundle(bundleInfo->getId());
+ unregisterBundle(bundleInfo->getID());
}
}
- void ResourceContainerImpl::unregisterBundle(int id)
+ void ResourceContainerImpl::unregisterBundle(string id)
{
void *bundleHandle = m_bundles[id]->getBundleHandle();
info_logger() << "Unregister bundle: " << m_bundles[id]->getID() << ", "
- << m_bundles[id]->getId() << endl;
+ << m_bundles[id]->getID() << endl;
char *error;
dlclose(bundleHandle);
if ((error = dlerror()) != NULL)
ResourceContainerImpl *ResourceContainerImpl::getImplInstance()
{
- ResourceContainerImpl *ret = new ResourceContainerImpl();
- return ret;
- }
-
- void ResourceContainerImpl::getCommonConfiguration(configInfo *configOutput)
- {
- m_config->getCommonConfiguration((Configuration::configInfo *) configOutput);
+ static ResourceContainerImpl m_instance;
+ return &m_instance;
}
void ResourceContainerImpl::getBundleConfiguration(std::string bundleId,
configInfo *configOutput)
{
- m_config->getBundleConfiguration(bundleId, (Configuration::configInfo *) configOutput);
+ m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput);
}
void ResourceContainerImpl::getResourceConfiguration(std::string bundleId,
{
m_config->getResourceConfiguration(bundleId, configOutput);
}
+
+ void ResourceContainerImpl::onNotificationReceived(std::string strResourceUri)
+ {
+ info_logger() << "ResourceContainerImpl::onNotificationReceived\n\tnotification from "
+ << strResourceUri << ".\n";
+
+ /*if (m_mapServers.find(strResourceUri) != m_mapServers.end())
+ {
+ //m_mapServers[strResourceUri]->notify();
+ }*/
+ }
+
+ void ResourceContainerImpl::addBundle(string bundleId, string bundleUri, string bundlePath,
+ std::map< string, string > params)
+ {
+ // To be implemented
+ }
+
+ void ResourceContainerImpl::removeBundle(string bundleId)
+ {
+ // To be implemented
+ }
+
+ std::list< BundleInfo* > ResourceContainerImpl::listBundles()
+ {
+ std::list< BundleInfo* > ret;
+ return ret;
+ }
+
+ void ResourceContainerImpl::addResourceConfig(string bundleId, string resourceUri,
+ std::map< string, string > params)
+ {
+ // To be implemented
+ }
+
+ void ResourceContainerImpl::removeResourceConfig(string bundleId, string resourceUri)
+ {
+ // To be implemented
+ }
+
+ void ResourceContainerImpl::startBundle(string bundleId)
+ {
+ activateBundle(m_bundles[bundleId]);
+ }
+
+ void ResourceContainerImpl::stopBundle(string bundleId)
+ {
+ deactivateBundle(m_bundles[bundleId]);
+ }
+
+ void ResourceContainerImpl::stopContainer()
+ {
+ // deactivate all bundles and unload them
+ }
+
+ JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
+ {
+ return m_bundleVM[bundleId];
+ }
}
}