Start working on JNI implementation of UUID factory.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 30 Jan 2022 08:39:54 +0000 (09:39 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 30 Jan 2022 08:39:54 +0000 (09:39 +0100)
org.argeo.api.uuid/jni/.cproject
org.argeo.api.uuid/jni/org_argeo_api_uuid/libuuid_NativeUuidFactory.cpp [deleted file]
org.argeo.api.uuid/jni/org_argeo_api_uuid/org_argeo_api_uuid_NativeUuidFactory.c [new file with mode: 0644]
sdk/jni.mk

index 4bb1d0cc8497fab6167efde49a67f1f6bb90db9c..8ebb27348dbc15bdddc2a260f1a1adef96571774 100644 (file)
@@ -55,7 +55,9 @@
                                                                                                        
                                     <listOptionValue builtIn="false" value="/usr/lib/jvm/java-11-openjdk/include/"/>
                                                                                                        
-                                    <listOptionValue builtIn="false" value="/usr/lib/jvm/java-11-openjdk/include/linux"/>
+                                    <listOptionValue builtIn="false" value="/usr/include/uuid/"/>
+                                                                                                       
+                                    <listOptionValue builtIn="false" value="/usr/lib/jvm/java-11-openjdk/include/linux/"/>
                                                                                                
                                 </option>
                                                                                                
diff --git a/org.argeo.api.uuid/jni/org_argeo_api_uuid/libuuid_NativeUuidFactory.cpp b/org.argeo.api.uuid/jni/org_argeo_api_uuid/libuuid_NativeUuidFactory.cpp
deleted file mode 100644 (file)
index 2930397..0000000
+++ /dev/null
@@ -1 +0,0 @@
-#include "../../jni/org_argeo_api_uuid/org_argeo_api_uuid_NativeUuidFactory.h"
diff --git a/org.argeo.api.uuid/jni/org_argeo_api_uuid/org_argeo_api_uuid_NativeUuidFactory.c b/org.argeo.api.uuid/jni/org_argeo_api_uuid/org_argeo_api_uuid_NativeUuidFactory.c
new file mode 100644 (file)
index 0000000..5e32548
--- /dev/null
@@ -0,0 +1,130 @@
+#include <jni.h>
+#include <uuid.h>
+#include "org_argeo_api_uuid_NativeUuidFactory.h"
+/* Header for class org_argeo_api_uuid_NativeUuidFactory */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+       /*
+        * Class:     org_argeo_api_uuid_NativeUuidFactory
+        * Method:    timeUUID
+        * Signature: ()Ljava/util/UUID;
+        */
+       JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_NativeUuidFactory_timeUUID(
+                       JNIEnv *env, jobject) {
+               uuid_t out;
+               jlong msb = 0;
+               jlong lsb = 0;
+
+               uuid_generate_time(out);
+
+               for (int i = 0; i < 8; i++)
+                       msb = (msb << 8) | (out[i] & 0xff);
+               for (int i = 8; i < 16; i++)
+                       lsb = (lsb << 8) | (out[i] & 0xff);
+
+               jclass uuidClass = (*env)->FindClass(env, "java/util/UUID");
+               jmethodID uuidConstructor = (*env)->GetMethodID(env, uuidClass,
+                               "<init>", "(JJ)V");
+
+               jobject jUUID = (*env)->AllocObject(env, uuidClass);
+               (*env)->CallVoidMethod(env, jUUID, uuidConstructor, msb, lsb);
+
+               return jUUID;
+       }
+
+       /*
+        * Class:     org_argeo_api_uuid_NativeUuidFactory
+        * Method:    nameUUIDv5
+        * Signature: (Ljava/util/UUID;[B)Ljava/util/UUID;
+        */
+       JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_NativeUuidFactory_nameUUIDv5(
+                       JNIEnv *env, jobject, jobject, jbyteArray name) {
+               size_t length = (*env)->GetArrayLength(env, name);
+               jbyte *bytes = (*env)->GetByteArrayElements(env, name, 0);
+               return NULL;
+       }
+
+       /*
+        * Class:     org_argeo_api_uuid_NativeUuidFactory
+        * Method:    nameUUIDv3
+        * Signature: (Ljava/util/UUID;[B)Ljava/util/UUID;
+        */
+       JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_NativeUuidFactory_nameUUIDv3(
+                       JNIEnv *env, jobject, jobject, jbyteArray) {
+               return NULL;
+       }
+
+       /*
+        * Class:     org_argeo_api_uuid_NativeUuidFactory
+        * Method:    randomUUIDStrong
+        * Signature: ()Ljava/util/UUID;
+        */
+       JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_NativeUuidFactory_randomUUIDStrong(
+                       JNIEnv *env, jobject) {
+               uuid_t out;
+
+               uuid_generate_random(out);
+               return NULL;
+       }
+
+//     void fromBytes(JNIEnv *env, jobject jUUID, jmethodID uuidConstructor, uuid_t out){
+//             jlong msb = 0;
+//             jlong lsb = 0;
+//
+//             for (int i = 0; i < 8; i++)
+//                     msb = (msb << 8) | (out[i] & 0xff);
+//             for (int i = 8; i < 16; i++)
+//                     lsb = (lsb << 8) | (out[i] & 0xff);
+//
+//             (*env)->CallVoidMethod(env, jUUID, uuidConstructor, msb, lsb);
+//
+//     }
+
+//     void getMostSignificantBits(uuid_t out) {
+//             jlong msb = 0;
+//             for (int i = 0; i < 8; i++)
+//                     msb = (msb << 8) | (out[i] & 0xff);
+//             return msb;
+//     }
+//
+//     jlong getLeastSignificantBits(uuid_t out) {
+//             jlong lsb = 0;
+//             for (int i = 8; i < 16; i++)
+//                     lsb = (lsb << 8) | (out[i] & 0xff);
+//             return lsb;
+//     }
+//
+//     jobject fromBits(JNIEnv *env, jlong msb, jlong lsb) {
+//             jclass uuidClass = (*env)->FindClass(env, "java/util/UUID");
+//             jmethodID uuidConstructor = (*env)->GetMethodID(env, uuidClass,
+//                             "<init>", "(JJ)V");
+//
+//             jobject jUUID = (*env)->AllocObject(env, uuidClass);
+//             (*env)->CallVoidMethod(env, jUUID, uuidConstructor, msb, lsb);
+//             return jUUID;
+//     }
+
+//     uuid_t toBytes(JNIEnv *env, jobject jUUID) {
+//             uuid_t result;
+//
+//             jclass uuidClass = (*env)->FindClass(env, "java/util/UUID");
+//             jmethodID getMostSignificantBits = (*env)->GetMethodID(env, uuidClass,
+//                             "getMostSignificantBits", "J");
+//             jmethodID getLeastSignificantBits = (*env)->GetMethodID(env, uuidClass,
+//                             "getLeastSignificantBits", "J");
+//
+//             jlong msb = (*env)->CallLongMethod(env, jUUID, getMostSignificantBits);
+//             jlong lsb = (*env)->CallLongMethod(env, jUUID, getMostSignificantBits);
+//
+//             for (int i = 0; i < 8; i++)
+//                     result[i] = (unsigned char) ((msb >> ((7 - i) * 8)) & 0xff);
+//             for (int i = 8; i < 16; i++)
+//                     result[i] = (unsigned char) ((lsb >> ((15 - i) * 8)) & 0xff);
+//             return result;
+//     }
+
+#ifdef __cplusplus
+}
+#endif
index 6f5c31fb4edac095183707c9f426e5376c6225df..bf2b872375410a0bc7710a455ab514a4bfe0dd44 100644 (file)
@@ -1,6 +1,7 @@
 TARGET_EXEC := libJava_$(NATIVE_PACKAGE).so
 
 LDFLAGS = -shared -fPIC -Wl,-soname,$(TARGET_EXEC).$(MAJOR).$(MINOR)
+CFLAGS = -fPIC
 
 SRC_DIRS := . 
 %:
@@ -20,7 +21,8 @@ BUILD_DIR := $(SDK_BUILD_BASE)/$(NATIVE_PACKAGE)
 #ARCH := $(shell uname -p)
 
 # Every folder in ./src will need to be passed to GCC so that it can find header files
-INC_DIRS := $(shell find $(SRC_DIRS) -type d) /usr/lib/jvm/java/include /usr/lib/jvm/java/include/linux
+INC_DIRS := $(shell find $(SRC_DIRS) -type d) /usr/lib/jvm/java/include /usr/lib/jvm/java/include/linux /usr/include/uuid
+
 
 .PHONY: clean all ide
 all: $(SDK_BUILD_BASE)/$(TARGET_EXEC)