[IOT-828] OCRepresentation to provide host info
authorhekra <hekra@cisco.com>
Sun, 22 Nov 2015 23:27:15 +0000 (15:27 -0800)
committerJon A. Cruz <jonc@osg.samsung.com>
Tue, 24 Nov 2015 21:25:50 +0000 (21:25 +0000)
Change-Id: Iaf18ef8119d592a33eac69bd6d9e6a609aaf0d87
Signed-off-by: Hugues Ekra <hekra@cisco.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4307
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Tim Kourt <tim.a.kourt@intel.com>
Reviewed-by: Jon A. Cruz <jonc@osg.samsung.com>
android/android_api/base/jni/JniOcRepresentation.cpp
android/android_api/base/jni/JniOcRepresentation.h
android/android_api/base/src/main/java/org/iotivity/base/OcRepresentation.java
resource/include/OCRepresentation.h
resource/src/InProcClientWrapper.cpp
resource/src/OCRepresentation.cpp

index 3c69be6..c3c37ad 100644 (file)
@@ -793,6 +793,22 @@ JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcRepresentation_getUri
 
 /*
 * Class:     org_iotivity_base_OcRepresentation
+* Method:    getHost
+* Signature: ()Ljava/lang/String;
+*/
+JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcRepresentation_getHost
+(JNIEnv *env, jobject thiz)
+{
+    LOGD("OcRepresentation_getHost");
+    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
+    if (!rep) return nullptr;
+
+    std::string uri(rep->getHost());
+    return env->NewStringUTF(uri.c_str());
+}
+
+/*
+* Class:     org_iotivity_base_OcRepresentation
 * Method:    setUri
 * Signature: (Ljava/lang/String;)V
 */
index 2ac86ff..ee8f222 100644 (file)
@@ -653,6 +653,14 @@ extern "C" {
 
     /*
     * Class:     org_iotivity_base_OcRepresentation
+    * Method:    getHost
+    * Signature: ()Ljava/lang/String;
+    */
+    JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcRepresentation_getHost
+        (JNIEnv *, jobject);
+
+    /*
+    * Class:     org_iotivity_base_OcRepresentation
     * Method:    setUri
     * Signature: (Ljava/lang/String;)V
     */
index eba1001..309ab70 100644 (file)
@@ -278,6 +278,8 @@ public class OcRepresentation {
 
     public native String getUri();
 
+    public native String getHost();
+
     public native void setUri(String uri);
 
     /**
index 22fdb79..687b64c 100644 (file)
@@ -109,6 +109,10 @@ namespace OC
 
             virtual ~OCRepresentation(){}
 
+            void setDevAddr(const OCDevAddr addr);
+
+            const std::string getHost() const;
+
             OCRepPayload* getPayload() const;
 
             void addChild(const OCRepresentation&);
@@ -224,6 +228,9 @@ namespace OC
 
             bool isNULL(const std::string& str) const;
 
+        private:
+            std::string m_host;
+
             // STL Container stuff
         public:
             class iterator;
index 4f1dd62..f441ca9 100644 (file)
@@ -123,6 +123,7 @@ namespace OC
 
         // first one is considered the root, everything else is considered a child of this one.
         OCRepresentation root = *it;
+        root.setDevAddr(clientResponse->devAddr);
         ++it;
 
         std::for_each(it, oc.representations().end(),
index 315f83b..ac6654d 100644 (file)
@@ -37,6 +37,8 @@
 
 namespace OC
 {
+    static const char COAP[] = "coap://";
+    static const char COAPS[] = "coaps://";
 
     void MessageContainer::setPayload(const OCPayload* rep)
     {
@@ -641,6 +643,44 @@ namespace OC
     {
         m_children = children;
     }
+
+    void OCRepresentation::setDevAddr(const OCDevAddr m_devAddr)
+    {
+        std::ostringstream ss;
+        if (m_devAddr.flags & OC_SECURE)
+        {
+            ss << COAPS;
+        }
+    #ifdef TCP_ADAPTER
+        else if (m_devAddr.adapter & OC_ADAPTER_TCP)
+        {
+            ss << COAP_TCP;
+        }
+    #endif
+        else
+        {
+            ss << COAP;
+        }
+        if (m_devAddr.flags & OC_IP_USE_V6)
+        {
+            ss << '[' << m_devAddr.addr << ']';
+        }
+        else
+        {
+            ss << m_devAddr.addr;
+        }
+        if (m_devAddr.port)
+        {
+            ss << ':' << m_devAddr.port;
+        }
+        m_host = ss.str();
+    }
+
+    const std::string OCRepresentation::getHost() const
+    {
+        return m_host;
+    }
+
     void OCRepresentation::setUri(const char* uri)
     {
         m_uri = uri ? uri : "";