#include "JniOcAccountManager.h"
#endif
+#define CA_OBSERVE_MAX_SEQUENCE_NUMBER 0xFFFFFF
+
JniOnObserveListener::JniOnObserveListener(JNIEnv *env, jobject jListener, JniOcResource* owner)
: m_ownerResource(owner)
{
return;
}
+ if (nullptr == m_jwListener)
+ {
+ LOGE("listener is not available");
+ if (JNI_EDETACHED == envRet)
+ {
+ g_jvm->DetachCurrentThread();
+ }
+ return;
+ }
+
jobject jListener = env->NewLocalRef(m_jwListener);
if (!jListener)
{
}
return;
}
+
jclass clsL = env->GetObjectClass(jListener);
if (!clsL)
{
+ env->DeleteLocalRef(jListener);
checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet)
{
jobject ex = GetOcException(eCode, "stack error in onObserveCallback");
if (!ex)
{
- checkExAndRemoveListener(env);
- if (JNI_EDETACHED == envRet)
- {
- g_jvm->DetachCurrentThread();
- }
- return;
+ goto JNI_EXIT;
}
+
jmethodID midL = env->GetMethodID(clsL, "onObserveFailed", "(Ljava/lang/Throwable;)V");
if (!midL)
{
- checkExAndRemoveListener(env);
- if (JNI_EDETACHED == envRet)
- {
- g_jvm->DetachCurrentThread();
- }
- return;
+ env->DeleteLocalRef(ex);
+ goto JNI_EXIT;
}
env->CallVoidMethod(jListener, midL, ex);
}
jobject jHeaderOptionList = JniUtils::convertHeaderOptionsVectorToJavaList(env, headerOptions);
if (!jHeaderOptionList)
{
- checkExAndRemoveListener(env);
- if (JNI_EDETACHED == envRet)
- {
- g_jvm->DetachCurrentThread();
- }
- return;
+ goto JNI_EXIT;
}
OCRepresentation * rep = new OCRepresentation(ocRepresentation);
if (!jRepresentation)
{
delete rep;
- checkExAndRemoveListener(env);
- if (JNI_EDETACHED == envRet)
- {
- g_jvm->DetachCurrentThread();
- }
- return;
+ env->DeleteLocalRef(jHeaderOptionList);
+ goto JNI_EXIT;
}
jmethodID midL = env->GetMethodID(clsL, "onObserveCompleted",
"(Ljava/util/List;Lorg/iotivity/base/OcRepresentation;I)V");
if (!midL)
{
- checkExAndRemoveListener(env);
- if (JNI_EDETACHED == envRet)
- {
- g_jvm->DetachCurrentThread();
- }
- return;
+ env->DeleteLocalRef(jRepresentation);
+ env->DeleteLocalRef(jHeaderOptionList);
+ goto JNI_EXIT;
}
env->CallVoidMethod(jListener, midL, jHeaderOptionList, jRepresentation,
{
LOGE("Java exception is thrown");
delete rep;
+ env->DeleteLocalRef(jRepresentation);
+ env->DeleteLocalRef(jHeaderOptionList);
jthrowable ex = env->ExceptionOccurred();
env->ExceptionClear();
m_ownerResource->removeOnObserveListener(env, m_jwListener);
env->Throw((jthrowable)ex);
}
- if (OC_OBSERVE_DEREGISTER == sequenceNumber)
+ if (CA_OBSERVE_MAX_SEQUENCE_NUMBER + 1 == sequenceNumber)
{
- checkExAndRemoveListener(env);
+ LOGI("Observe De-registration action is successful");
+ goto JNI_EXIT;
}
}
+ env->DeleteLocalRef(clsL);
+ env->DeleteLocalRef(jListener);
+ if (JNI_EDETACHED == envRet)
+ {
+ g_jvm->DetachCurrentThread();
+ }
+ return;
+
+JNI_EXIT:
+ env->DeleteLocalRef(clsL);
+ env->DeleteLocalRef(jListener);
+ checkExAndRemoveListener(env);
if (JNI_EDETACHED == envRet)
{
g_jvm->DetachCurrentThread();
void JniOnObserveListener::checkExAndRemoveListener(JNIEnv* env)
{
+ LOGI("checkExAndRemoveListener");
if (env->ExceptionCheck())
{
jthrowable ex = env->ExceptionOccurred();
private OcResource mFoundLightResource = null;
//local representation of a server's light resource
private Light mLight = new Light();
+ //variables related observer
+ private int maxSequenceNumber = 0xFFFFFF;
+ private OcConnectivityType adapterFlag = OcConnectivityType.CT_ADAPTER_IP;
+ //flags related TCP transport test
+ private boolean isRequestFlag = false;
+ private boolean isTCPContained = false;
/**
* A local method to configure and initialize platform, and then search for the light resources.
*/
- private void startSimpleClient() {
+ private void startSimpleClient(OcConnectivityType type) {
Context context = this;
+ adapterFlag = type;
PlatformConfig platformConfig = new PlatformConfig(
this,
}
if (null != mFoundLightResource) {
+ if (ocResource.getUri().equals("/a/light")) {
+ if (ocResource.getConnectivityTypeSet().contains(OcConnectivityType.CT_ADAPTER_TCP)) {
+ msg("Found resource which has TCP transport");
+ if (isTCPContained == false)
+ {
+ isTCPContained = true;
+ return;
+ }
+ }
+ }
msg("Found another resource, ignoring");
return;
+
}
// Get the resource URI
String resourceUri = ocResource.getUri();
if (resourceUri.equals("/a/light")) {
//Assign resource reference to a global variable to keep it from being
//destroyed by the GC when it is out of scope.
- mFoundLightResource = ocResource;
-
- // Call a local method which will internally invoke "get" API on the foundLightResource
- getLightResourceRepresentation();
+ if (OcConnectivityType.CT_ADAPTER_TCP == adapterFlag)
+ {
+ if (ocResource.getConnectivityTypeSet().contains(OcConnectivityType.CT_ADAPTER_TCP))
+ {
+ msg("set mFoundLightResource which has TCP transport");
+ mFoundLightResource = ocResource;
+ // Call a local method which will internally invoke "get" API
+ getLightResourceRepresentation();
+ return;
+ }
+ }
+ else
+ {
+ msg("set mFoundLightResource which has UDP transport");
+ mFoundLightResource = ocResource;
+ // Call a local method which will internally invoke "get" API on the foundLightResource
+ getLightResourceRepresentation();
+ }
}
}
public synchronized void onObserveCompleted(List<OcHeaderOption> list,
OcRepresentation ocRepresentation,
int sequenceNumber) {
- if (OcResource.OnObserveListener.REGISTER == sequenceNumber) {
- msg("Observe registration action is successful:");
- } else if (OcResource.OnObserveListener.DEREGISTER == sequenceNumber) {
- msg("Observe De-registration action is successful");
- } else if (OcResource.OnObserveListener.NO_OPTION == sequenceNumber) {
- msg("Observe registration or de-registration action is failed");
- }
-
- msg("OBSERVE Result:");
- msg("\tSequenceNumber:" + sequenceNumber);
- try {
- mLight.setOcRepresentation(ocRepresentation);
- } catch (OcException e) {
- Log.e(TAG, e.toString());
- msg("Failed to get the attribute values");
- }
- msg(mLight.toString());
- if ((++mObserveCount) == 11) {
- msg("Cancelling Observe...");
+ if (sequenceNumber != maxSequenceNumber + 1)
+ {
+ msg("OBSERVE Result:");
+ msg("\tSequenceNumber:" + sequenceNumber);
try {
- mFoundLightResource.cancelObserve();
+ mLight.setOcRepresentation(ocRepresentation);
} catch (OcException e) {
Log.e(TAG, e.toString());
- msg("Error occurred while invoking \"cancelObserve\" API");
+ msg("Failed to get the attribute values");
}
- msg("DONE");
+ msg(mLight.toString());
+
+ if ((++mObserveCount) == 11) {
+ msg("Cancelling Observe...");
+ try {
+ mFoundLightResource.cancelObserve(QualityOfService.HIGH);
+ } catch (OcException e) {
+ Log.e(TAG, e.toString());
+ msg("Error occurred while invoking \"cancelObserve\" API");
+ }
- //prepare for the next restart of the SimpleClient
- resetGlobals();
- enableStartButton();
+ sleep(10);
+ resetGlobals();
+ if (true == isTCPContained && false == isRequestFlag)
+ {
+ msg("Start TCP test...");
+ startSimpleClient(OcConnectivityType.CT_ADAPTER_TCP);
+ isRequestFlag = true;
+ return;
+ } else if (true == isRequestFlag)
+ {
+ msg("End TCP test...");
+ isRequestFlag = false;
+ }
+
+ msg("DONE");
+ //prepare for the next restart of the SimpleClient
+ enableStartButton();
+ }
}
}
button.setEnabled(false);
new Thread(new Runnable() {
public void run() {
- startSimpleClient();
+ isTCPContained = false;
+ startSimpleClient(OcConnectivityType.CT_ADAPTER_IP);
}
}).start();
}