Reduce complexity of the code
authorPraveen Naik S <praveen.s@samsung.com>
Mon, 29 Aug 2022 14:00:17 +0000 (19:30 +0530)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 15 Sep 2022 05:38:12 +0000 (14:38 +0900)
android/aitt/src/main/java/com/samsung/android/aitt/Aitt.java
android/aitt/src/main/jni/aitt_jni.cc
android/aitt/src/main/jni/aitt_jni.h

index ef383d7..b2698bd 100644 (file)
@@ -244,13 +244,8 @@ public class Aitt {
      * @param retain Boolean to decide whether or not the message should be retained by the broker
      */
     public void publish(String topic, byte[] message, EnumSet<Protocol> protocols, QoS qos, boolean retain) {
-        if (topic == null || topic.isEmpty()) {
-            throw new IllegalArgumentException(INVALID_TOPIC);
-        }
-        if (protocols.isEmpty()) {
-            throw new IllegalArgumentException("Invalid protocols");
-        }
 
+        checkParams(topic, protocols);
         int jniProtocols = 0;
 
         for (Protocol p : protocols) {
@@ -352,15 +347,12 @@ public class Aitt {
      * @param qos QoS at which the message should be delivered
      */
     public void subscribe(String topic, SubscribeCallback callback, EnumSet<Protocol> protocols, QoS qos) {
-        if (topic == null || topic.isEmpty()) {
-            throw new IllegalArgumentException(INVALID_TOPIC);
-        }
+
+        checkParams(topic, protocols);
+
         if (callback == null) {
             throw new IllegalArgumentException("Invalid callback");
         }
-        if (protocols.isEmpty()) {
-            throw new IllegalArgumentException("Invalid protocols");
-        }
 
         int jniProtocols = 0;
 
@@ -406,6 +398,21 @@ public class Aitt {
     }
 
     /**
+     * Method to verify Aitt pub & sub parameters
+     * @param topic String to which applications can subscribe, to receive data
+     * @param protocols Protocol supported by application, invoking subscribe
+     */
+    private void checkParams(String topic, EnumSet<Protocol> protocols){
+        if (topic == null || topic.isEmpty()) {
+            throw new IllegalArgumentException(INVALID_TOPIC);
+        }
+
+        if (protocols.isEmpty()) {
+            throw new IllegalArgumentException("Invalid protocols");
+        }
+    }
+
+    /**
      * Method to map subscribe callback instance to subscribing topic
      * @param topic String to which application can subscribe
      * @param callback Subscribe callback instance created during subscribe call
index 126cd05..df8c090 100644 (file)
@@ -64,6 +64,17 @@ AittNativeInterface::~AittNativeInterface(void)
     }
 }
 
+
+bool AittNativeInterface::checkParams(JNIEnv *env, jobject jniInterfaceObject){
+    if (env == nullptr || jniInterfaceObject == nullptr) {
+        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Env or Jobject is null");
+        return false;
+    }else{
+        return true;
+    }
+}
+
+
 /**
  * Convert the JNI string to C++ string
  * @param env JNI interface pointer
@@ -97,10 +108,9 @@ std::string AittNativeInterface::GetStringUTF(JNIEnv *env, jstring str)
 void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_connectJNI(JNIEnv *env,
       jobject jniInterfaceObject, jlong handle, jstring host, jint port)
 {
-    if (env == nullptr || jniInterfaceObject == nullptr) {
-        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Env or Jobject is null");
+    if(!checkParams(env, jniInterfaceObject))
         return;
-    }
+
     AittNativeInterface *instance = reinterpret_cast<AittNativeInterface *>(handle);
     std::string brokerIp = GetStringUTF(env, host);
     if (brokerIp.empty()) {
@@ -133,10 +143,9 @@ void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_publishJNI(JNIEnv *
       jobject jniInterfaceObject, jlong handle, jstring topic, jbyteArray data, jlong datalen,
       jint protocol, jint qos, jboolean retain)
 {
-    if (env == nullptr || jniInterfaceObject == nullptr) {
-        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Env or jobject is null");
+    if(!checkParams(env, jniInterfaceObject))
         return;
-    }
+
     AittNativeInterface *instance = reinterpret_cast<AittNativeInterface *>(handle);
     std::string customTopic = GetStringUTF(env, topic);
     if (customTopic.empty()) {
@@ -173,10 +182,9 @@ void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_publishJNI(JNIEnv *
 void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_disconnectJNI(JNIEnv *env,
       jobject jniInterfaceObject, jlong handle)
 {
-    if (env == nullptr || jniInterfaceObject == nullptr) {
-        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Env or Jobject is null");
+    if(!checkParams(env, jniInterfaceObject))
         return;
-    }
+
     AittNativeInterface *instance = reinterpret_cast<AittNativeInterface *>(handle);
     try {
         instance->aitt.Disconnect();
@@ -186,6 +194,20 @@ void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_disconnectJNI(JNIEn
     }
 }
 
+
+bool AittNativeInterface::jniStatusCheck(JNIEnv *env, int JNIStatus){
+    if (JNIStatus == JNI_EDETACHED) {
+        if (cbContext.jvm->AttachCurrentThread(&env, nullptr) != 0) {
+            JNI_LOG(ANDROID_LOG_ERROR, TAG, "Failed to attach current thread");
+            return false;
+        }
+    } else if (JNIStatus == JNI_EVERSION) {
+        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Unsupported version");
+        return false;
+    }
+    return true;
+}
+
 /**
  * JNI API to subscribe to a given topic
  * @param env JNI interface pointer
@@ -198,10 +220,9 @@ void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_disconnectJNI(JNIEn
 jlong AittNativeInterface::Java_com_samsung_android_aitt_Aitt_subscribeJNI(JNIEnv *env,
       jobject jniInterfaceObject, jlong handle, jstring topic, jint protocol, jint qos)
 {
-    if (env == nullptr || jniInterfaceObject == nullptr) {
-        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Env or Jobject is null");
+    if(!checkParams(env, jniInterfaceObject))
         return 0L;
-    }
+
     AittNativeInterface *instance = reinterpret_cast<AittNativeInterface *>(handle);
     std::string customTopic = GetStringUTF(env, topic);
     if (customTopic.empty()) {
@@ -220,15 +241,10 @@ jlong AittNativeInterface::Java_com_samsung_android_aitt_Aitt_subscribeJNI(JNIEn
                   JNIEnv *env;
                   int JNIStatus =
                         cbContext.jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
-                  if (JNIStatus == JNI_EDETACHED) {
-                      if (cbContext.jvm->AttachCurrentThread(&env, nullptr) != 0) {
-                          JNI_LOG(ANDROID_LOG_ERROR, TAG, "Failed to attach current thread");
-                          return;
-                      }
-                  } else if (JNIStatus == JNI_EVERSION) {
-                      JNI_LOG(ANDROID_LOG_ERROR, TAG, "Unsupported version");
+
+                  if(!jniStatusCheck(env, JNIStatus))
                       return;
-                  }
+
                   if (env != nullptr && instance->cbObject != nullptr) {
                       jstring _topic = env->NewStringUTF(handle->GetTopic().c_str());
                       if (env->ExceptionCheck() == true) {
@@ -274,10 +290,9 @@ jlong AittNativeInterface::Java_com_samsung_android_aitt_Aitt_subscribeJNI(JNIEn
 void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_unsubscribeJNI(JNIEnv *env,
       jobject jniInterfaceObject, jlong handle, jlong aittSubId)
 {
-    if (env == nullptr || jniInterfaceObject == nullptr) {
-        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Env or Jobject is null");
+    if(!checkParams(env, jniInterfaceObject))
         return;
-    }
+
     AittNativeInterface *instance = reinterpret_cast<AittNativeInterface *>(handle);
     void *subId = reinterpret_cast<void *>(aittSubId);
     try {
@@ -297,10 +312,9 @@ void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_unsubscribeJNI(JNIE
 void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_setConnectionCallbackJNI(JNIEnv *env,
       jobject jniInterfaceObject, jlong handle)
 {
-    if (env == nullptr || jniInterfaceObject == nullptr) {
-        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Env or Jobject is null");
+    if(!checkParams(env, jniInterfaceObject))
         return;
-    }
+
     AittNativeInterface *instance = reinterpret_cast<AittNativeInterface *>(handle);
 
     try {
@@ -310,15 +324,10 @@ void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_setConnectionCallba
                     JNIEnv *env;
                     int JNIStatus =
                             cbContext.jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
-                    if (JNIStatus == JNI_EDETACHED) {
-                        if (cbContext.jvm->AttachCurrentThread(&env, nullptr) != 0) {
-                            JNI_LOG(ANDROID_LOG_ERROR, TAG, "Failed to attach current thread");
-                            return;
-                        }
-                    } else if (JNIStatus == JNI_EVERSION) {
-                        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Unsupported version");
+
+                    if(!jniStatusCheck(env, JNIStatus))
                         return;
-                    }
+
                     if (env != nullptr && instance->cbObject != nullptr) {
                         env->CallVoidMethod(instance->cbObject, cbContext.connectionCallbackMethodID,
                                             (jint)status);
@@ -349,10 +358,10 @@ void AittNativeInterface::Java_com_samsung_android_aitt_Aitt_setConnectionCallba
 jlong AittNativeInterface::Java_com_samsung_android_aitt_Aitt_initJNI(JNIEnv *env,
       jobject jniInterfaceObject, jstring id, jstring ip, jboolean clearSession)
 {
-    if (env == nullptr || jniInterfaceObject == nullptr) {
-        JNI_LOG(ANDROID_LOG_ERROR, TAG, "Env or Jobject is null");
+
+    if(!checkParams(env, jniInterfaceObject))
         return JNI_ERR;
-    }
+
     std::string mqId = GetStringUTF(env, id);
     if (mqId.empty()) {
         return 0L;
index 69e3262..8a3170f 100644 (file)
@@ -37,6 +37,8 @@ class AittNativeInterface {
         AittNativeInterface(std::string &mqId, std::string &ip, bool clearSession);
         virtual ~AittNativeInterface(void);
         static std::string GetStringUTF(JNIEnv *env, jstring str);
+        static bool checkParams(JNIEnv *env, jobject jniInterfaceObject);
+        static bool jniStatusCheck(JNIEnv *env, int JNIStatus);
 
     public:
         static jlong Java_com_samsung_android_aitt_Aitt_initJNI(JNIEnv *env, jobject jniInterfaceObject,