Skip to content

Commit

Permalink
Merge pull request #30 in XP/xp-v-golden-gate from shawn/fc-5315_nati…
Browse files Browse the repository at this point in the history
…ve_reference_refactoring_part1 to master

* commit '7945c6720216024c80b03792e18d1af258f17b8d':
  FC-5315: add create and free wrapper functions
  FC-5315: add synchronized annotation to setter and getter
  FC-5315: apply new native reference interface to CoapEndpoint and coap filter class
  • Loading branch information
ShawnW858 committed Dec 8, 2020
2 parents 0996351 + 7945c67 commit ce0ac11
Show file tree
Hide file tree
Showing 18 changed files with 322 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ target_sources( # Sets the name of the library.
${CMAKE_CURRENT_LIST_DIR}/util/jni_gg_utils.cpp
${CMAKE_CURRENT_LIST_DIR}/util/jni_gg_memory_data_sink.cpp
${CMAKE_CURRENT_LIST_DIR}/util/jni_gg_memory_data_source.cpp
${CMAKE_CURRENT_LIST_DIR}/util/jni_gg_native_reference.cpp
${CMAKE_CURRENT_LIST_DIR}/remote/jni_gg_remote.cpp
${CMAKE_CURRENT_LIST_DIR}/stackbuilder/jni_gg_stackbuilder.cpp
${CMAKE_CURRENT_LIST_DIR}/services/jni_gg_blast_service.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <jni_gg_loop.h>
#include <string.h>
#include <logging/jni_gg_logging.h>
#include <util/jni_gg_native_reference.h>
#include <util/jni_gg_utils.h>
#include <xp/loop/gg_loop.h>
#include <xp/coap/gg_coap.h>
Expand Down Expand Up @@ -206,15 +207,19 @@ JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_responseFor(
JNIEnv *env,
jobject thiz,
jlong _endpoint,
jlong _endpoint_wrapper,
jobject _request,
jobject _listener
) {
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint *) (intptr_t) _endpoint;
GG_ASSERT(endpoint);
GG_ASSERT(_endpoint_wrapper);
GG_ASSERT(_request);
GG_ASSERT(_listener);

NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
GG_ASSERT(endpoint_wrapper);
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint*)endpoint_wrapper->pointer;
GG_ASSERT(endpoint);

SingleCoapResponseListener *request_for_args = (SingleCoapResponseListener *) GG_AllocateZeroMemory(
sizeof(SingleCoapResponseListener));
if (request_for_args == NULL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <jni_gg_loop.h>
#include <string.h>
#include <logging/jni_gg_logging.h>
#include <util/jni_gg_native_reference.h>
#include <util/jni_gg_utils.h>
#include <xp/loop/gg_loop.h>
#include <xp/coap/gg_coap.h>
Expand Down Expand Up @@ -552,15 +553,20 @@ JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_responseForBlockwise(
JNIEnv *env,
jobject thiz,
jlong _endpoint,
jlong _endpoint_wrapper,
jobject _request,
jobject _listener
) {
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint *) (intptr_t) _endpoint;
GG_ASSERT(endpoint);

GG_ASSERT(_endpoint_wrapper);
GG_ASSERT(_request);
GG_ASSERT(_listener);

NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
GG_ASSERT(endpoint_wrapper);
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint*)endpoint_wrapper->pointer;
GG_ASSERT(endpoint);

ResponseListenerBlockwise *request_for_args = (ResponseListenerBlockwise *) GG_AllocateZeroMemory(
sizeof(ResponseListenerBlockwise));
if (request_for_args == NULL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
#include <xp/coap/gg_coap.h>
#include <xp/common/gg_memory.h>
#include <xp/coap/gg_coap_filters.h>
#include <util/jni_gg_native_reference.h>

extern "C" {

// class names
#define COAP_GROUP_REQUEST_FILTER_CLASS_NAME "com/fitbit/goldengate/bindings/coap/CoapGroupRequestFilter"

typedef struct {
GG_CoapGroupRequestFilter *filter;
} CoapGroupRequestFilter_CreateArgs;
Expand All @@ -34,8 +38,19 @@ static int CoapGroupRequestFilter_SetGroup(void *_args) {
}

static void CoapGroupRequestFilter_Destroy(void *_args) {
GG_CoapGroupRequestFilter *filter = (GG_CoapGroupRequestFilter *) _args;
GG_CoapGroupRequestFilter_Destroy(filter);
NativeReferenceWrapper *filterWrapper = (NativeReferenceWrapper *) _args;

//we're running on the GG Loop thread
JNIEnv* env = Loop_GetJNIEnv();

callJavaObjectOnFreeMethod(
env,
COAP_GROUP_REQUEST_FILTER_CLASS_NAME,
filterWrapper->java_object);

GG_CoapGroupRequestFilter_Destroy((GG_CoapGroupRequestFilter*)filterWrapper->pointer);

freeNativeReferenceWrapper(env, filterWrapper);
}

JNIEXPORT jlong JNICALL
Expand All @@ -53,18 +68,23 @@ Java_com_fitbit_goldengate_bindings_coap_CoapGroupRequestFilter_create(
return create_result;
}

return (jlong) (intptr_t) create_args.filter;
return (jlong) (intptr_t) createNativeReferenceWrapper(env, create_args.filter, thiz);
}

JNIEXPORT void JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapGroupRequestFilter_setGroup(
JNIEnv *env,
jobject thiz,
jlong _filter,
jlong _filter_wrapper,
jbyte _group
) {
NativeReferenceWrapper *filter_wrapper = (NativeReferenceWrapper *) (intptr_t) _filter_wrapper;
if (!filter_wrapper || !filter_wrapper->pointer) {
return;
}

CoapGroupRequestFilter_SetGroupArgs setGroup_args = (CoapGroupRequestFilter_SetGroupArgs) {
.filter = (GG_CoapGroupRequestFilter *) (intptr_t)_filter,
.filter = (GG_CoapGroupRequestFilter *)filter_wrapper->pointer,
.group = (uint8_t) _group
};

Expand All @@ -84,12 +104,14 @@ JNIEXPORT void JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapGroupRequestFilter_destroy(
JNIEnv *env,
jobject thiz,
jlong _filter
jlong _filter_wrapper
) {
GG_CoapGroupRequestFilter *filter = (GG_CoapGroupRequestFilter *) (intptr_t) _filter;
GG_ASSERT(filter);
NativeReferenceWrapper *filter_wrapper = (NativeReferenceWrapper *) (intptr_t) _filter_wrapper;
if (!filter_wrapper || !filter_wrapper->pointer) {
return;
}

Loop_InvokeAsync(CoapGroupRequestFilter_Destroy, filter);
Loop_InvokeAsync(CoapGroupRequestFilter_Destroy, filter_wrapper);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <jni_gg_loop.h>
#include <string.h>
#include <logging/jni_gg_logging.h>
#include <util/jni_gg_native_reference.h>
#include <util/jni_gg_utils.h>
#include <xp/loop/gg_loop.h>
#include <xp/coap/gg_coap.h>
Expand Down Expand Up @@ -275,18 +276,20 @@ JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_addResourceHandler(
JNIEnv *env,
jobject thiz,
jlong _endpoint,
jlong _endpoint_wrapper,
jstring _path,
jobject _handler,
jbyte _group
) {
GG_ASSERT(_endpoint);
GG_ASSERT(_endpoint_wrapper);
GG_ASSERT(_path);
GG_ASSERT(_handler);
GG_ASSERT(_group >= 0 &&
_group <= GG_COAP_GROUP_REQUEST_FILTER_MAX_GROUP);

GG_CoapEndpoint *endpoint = (GG_CoapEndpoint *) (intptr_t) _endpoint;
NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
GG_ASSERT(endpoint_wrapper);
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint*)endpoint_wrapper->pointer;
GG_ASSERT(endpoint);

RequestHandler *add_resource_args = (RequestHandler *) GG_AllocateZeroMemory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
#include <xp/coap/gg_coap.h>
#include <xp/common/gg_memory.h>
#include <xp/coap/gg_coap_filters.h>
#include <util/jni_gg_native_reference.h>
#include "jni_gg_coap_common.h"

extern "C" {

// CoapEndpoint class names
#define COAP_ENDPOINT_CLASS_NAME "com/fitbit/goldengate/bindings/coap/CoapEndpoint"

typedef struct {
GG_CoapEndpoint *endpoint;
} CoapEndpoint_CreateArgs;
Expand Down Expand Up @@ -81,8 +86,18 @@ static int CoapEndpoint_AttachFilter(void *_args) {
}

static void CoapEndpoint_Destroy(void *_args) {
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint *) _args;
GG_CoapEndpoint_Destroy(endpoint);
NativeReferenceWrapper *endpointWrapper = (NativeReferenceWrapper *) _args;
//we're running on the GG Loop thread
JNIEnv* env = Loop_GetJNIEnv();

callJavaObjectOnFreeMethod(
env,
COAP_ENDPOINT_CLASS_NAME,
endpointWrapper->java_object);

GG_CoapEndpoint_Destroy((GG_CoapEndpoint*)endpointWrapper->pointer);

freeNativeReferenceWrapper(env, endpointWrapper);
}

JNIEXPORT jlong JNICALL
Expand All @@ -100,24 +115,26 @@ Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_create(
return create_result;
}

return (jlong) (intptr_t) create_args.endpoint;
return (jlong) (intptr_t) createNativeReferenceWrapper(env, create_args.endpoint, thiz);
}

JNIEXPORT void JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_attach(
JNIEnv *env,
jobject thiz,
jlong _endpoint,
jlong _endpoint_wrapper,
jlong _sourcePtr,
jlong _sinkPtr
) {
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint *) (intptr_t) _endpoint;
GG_ASSERT(endpoint);
NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
if (!endpoint_wrapper || !endpoint_wrapper->pointer) {
return;
}
GG_ASSERT(_sourcePtr);
GG_ASSERT(_sinkPtr);

CoapEndpoint_AttachArgs attach_args = {
.endpoint = endpoint,
.endpoint = (GG_CoapEndpoint*)endpoint_wrapper->pointer,
.source = (GG_DataSource *) _sourcePtr,
.sink = (GG_DataSink *) _sinkPtr
};
Expand All @@ -134,14 +151,16 @@ JNIEXPORT void JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_detach(
JNIEnv *env,
jobject thiz,
jlong _endpoint,
jlong _endpoint_wrapper,
jlong _sourcePtr
) {
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint *) (intptr_t) _endpoint;
GG_ASSERT(endpoint);
NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
if (!endpoint_wrapper || !endpoint_wrapper->pointer) {
return;
}

CoapEndpoint_DetachArgs detach_args = {
.endpoint = endpoint,
.endpoint = (GG_CoapEndpoint*)endpoint_wrapper->pointer,
.source = (GG_DataSource *) _sourcePtr
};

Expand All @@ -157,17 +176,20 @@ JNIEXPORT void JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_attachFilter(
JNIEnv *env,
jobject thiz,
jlong _endpoint,
jlong _filter
jlong _endpoint_wrapper,
jlong _filter_wrapper
) {
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint *) (intptr_t) _endpoint;
GG_ASSERT(endpoint);
GG_CoapGroupRequestFilter *filter = (GG_CoapGroupRequestFilter *) (intptr_t) _filter;
GG_ASSERT(filter);
NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
NativeReferenceWrapper *filter_wrapper = (NativeReferenceWrapper *) (intptr_t) _filter_wrapper;
if ((!endpoint_wrapper || !endpoint_wrapper->pointer) ||
(!filter_wrapper || !filter_wrapper->pointer))
{
return;
}

CoapEndpoint_AttachFilterArgs attachFilter_args = {
.endpoint = endpoint,
.filter = filter
.endpoint = (GG_CoapEndpoint*)endpoint_wrapper->pointer,
.filter = (GG_CoapGroupRequestFilter *)filter_wrapper->pointer
};

int create_result = 0;
Expand All @@ -182,34 +204,42 @@ JNIEXPORT jlong JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_asDataSource(
JNIEnv *env,
jobject thiz,
jlong endpoint
jlong _endpoint_wrapper
) {
GG_ASSERT(endpoint);
NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
if (!endpoint_wrapper || !endpoint_wrapper->pointer) {
return 0;
}

return (jlong) (intptr_t) GG_CoapEndpoint_AsDataSource((GG_CoapEndpoint*)endpoint);
return (jlong) (intptr_t) GG_CoapEndpoint_AsDataSource((GG_CoapEndpoint*)endpoint_wrapper->pointer);
}

JNIEXPORT jlong JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_asDataSink(
JNIEnv *env,
jobject thiz,
jlong endpoint
jlong _endpoint_wrapper
) {
GG_ASSERT(endpoint);
NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
if (!endpoint_wrapper || !endpoint_wrapper->pointer) {
return 0;
}

return (jlong) (intptr_t) GG_CoapEndpoint_AsDataSink((GG_CoapEndpoint*)endpoint);
return (jlong) (intptr_t) GG_CoapEndpoint_AsDataSink((GG_CoapEndpoint*)endpoint_wrapper->pointer);
}

JNIEXPORT void JNICALL
Java_com_fitbit_goldengate_bindings_coap_CoapEndpoint_destroy(
JNIEnv *env,
jobject thiz,
jlong _endpoint
jlong _endpoint_wrapper
) {
GG_CoapEndpoint *endpoint = (GG_CoapEndpoint *) (intptr_t) _endpoint;
GG_ASSERT(endpoint);
NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
if (!endpoint_wrapper || !endpoint_wrapper->pointer) {
return;
}

Loop_InvokeAsync(CoapEndpoint_Destroy, endpoint);
Loop_InvokeAsync(CoapEndpoint_Destroy, endpoint_wrapper);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

#include <jni_gg_loop.h>
#include <coap/jni_gg_coap_common.h>
#include <xp/common/gg_common.h>
#include <xp/services/coap_client/gg_coap_client_service.h>
#include <logging/jni_gg_logging.h>
#include <util/jni_gg_native_reference.h>

/**
* Jni Bindings for CoapGeneratorService
Expand All @@ -21,14 +23,20 @@ extern "C" {
Java_com_fitbit_goldengate_bindings_services_CoapGeneratorService_create(
JNIEnv *env,
jobject thiz,
jlong coapEndpointPtr
jlong _endpoint_wrapper
) {
GG_CoapEndpoint* coapEndpoint = (GG_CoapEndpoint*) (intptr_t) coapEndpointPtr;
GG_ASSERT(coapEndpoint);

NativeReferenceWrapper *endpoint_wrapper = (NativeReferenceWrapper *) (intptr_t) _endpoint_wrapper;
if (!endpoint_wrapper || !endpoint_wrapper->pointer) {
return 0;
}

GG_CoapClientService *service = NULL;

GG_Result result = GG_CoapClientService_Create(Loop_GetLoop(), coapEndpoint, &service);
GG_Result result = GG_CoapClientService_Create(
Loop_GetLoop(),
(GG_CoapEndpoint*) endpoint_wrapper->pointer,
&service);

if (GG_FAILED(result)) {
GG_Log_JNI("CoapGeneratorService", "GG_CoapClientService_Create failed with error code %d", result);
Expand Down
Loading

0 comments on commit ce0ac11

Please sign in to comment.