* @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) {
* @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;
}
/**
+ * 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
}
}
+
+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
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()) {
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()) {
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();
}
}
+
+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
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()) {
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) {
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 {
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 {
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);
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;