Author: mheath
Date: Sun Jan 14 19:18:14 2007
New Revision: 496213
URL: http://svn.apache.org/viewvc?view=rev&rev=496213
Log:
Removed native implementations for old API.
Modified:
mina/sandbox/mheath/aioj/trunk/src/main/c/Makefile
mina/sandbox/mheath/aioj/trunk/src/main/c/org_apache_aio.cpp
Modified: mina/sandbox/mheath/aioj/trunk/src/main/c/Makefile
URL:
http://svn.apache.org/viewvc/mina/sandbox/mheath/aioj/trunk/src/main/c/Makefile?view=diff&rev=496213&r1=496212&r2=496213
==============================================================================
--- mina/sandbox/mheath/aioj/trunk/src/main/c/Makefile (original)
+++ mina/sandbox/mheath/aioj/trunk/src/main/c/Makefile Sun Jan 14 19:18:14 2007
@@ -25,14 +25,5 @@
all: $(TARGET)
-$(TARGET): org_apache_aio.cpp
$(WORKING_DIR)/org_apache_aio_AsynchronousFileChannel.h
$(WORKING_DIR)/org_apache_aio_posix_PosixAioFutureReadWrite.h
+$(TARGET): org_apache_aio.cpp
g++ -shared -lrt -lstdc++ $(INCLUDES) org_apache_aio.cpp -o $(TARGET)
-
-$(WORKING_DIR)/org_apache_aio_AsynchronousFileChannel.h:
$(TARGET_DIR)/classes/org/apache/aio/AsynchronousFileChannel.class
- mkdir -p $(TARGET_DIR)/jni
- javah -force -classpath $(JAVAH_CLASSPATH) -d $(WORKING_DIR)
org.apache.aio.AsynchronousFileChannel
-
-$(WORKING_DIR)/org_apache_aio_posix_PosixAioFutureReadWrite.h:
$(TARGET_DIR)/classes/org/apache/aio/posix/PosixAioFutureReadWrite.class
- mkdir -p $(TARGET_DIR)/jni
- javah -force -classpath $(JAVAH_CLASSPATH) -d $(WORKING_DIR)
org.apache.aio.posix.PosixAioFutureReadWrite
-
\ No newline at end of file
Modified: mina/sandbox/mheath/aioj/trunk/src/main/c/org_apache_aio.cpp
URL:
http://svn.apache.org/viewvc/mina/sandbox/mheath/aioj/trunk/src/main/c/org_apache_aio.cpp?view=diff&rev=496213&r1=496212&r2=496213
==============================================================================
--- mina/sandbox/mheath/aioj/trunk/src/main/c/org_apache_aio.cpp (original)
+++ mina/sandbox/mheath/aioj/trunk/src/main/c/org_apache_aio.cpp Sun Jan 14
19:18:14 2007
@@ -1,11 +1,9 @@
#include <aio.h>
+#include <jni.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
-#include "org_apache_aio_AsynchronousFileChannel.h"
-#include "org_apache_aio_posix_PosixAioFutureReadWrite.h"
-
#define DEBUG
#ifdef DEBUG
@@ -16,14 +14,7 @@
#define LOG_DEBUG(s)
#endif
-struct aio_request
-{
- struct aiocb aio;
- jobject operation;
- jobject future;
- jint allocated_buffer_size;
- jbyte buffer[];
-};
+#define JNI_VERSION JNI_VERSION_1_4
// --- jvm handler ---
static JavaVM *jvm;
@@ -32,24 +23,6 @@
static jclass ioException;
static jclass nullPointerException;
-// --- Classes and IDs for PosixAioFutureReadWrite ---
-static jclass posixAioFutureReadWrite;
-static jmethodID CID_posixAioFutureReadWrite;
-static jfieldID posixAioFutureReadWrite_aiocbPtrID;
-static jfieldID posixAioFutureReadWrite_bufferID;
-static jmethodID abstractAioFuture_processFutureListenersID;
-static jmethodID abstractAioFuture_handleErrorID;
-
-// --- IDs for file descriptors ---
-static jfieldID fdID; // ID for java.io.FileDescriptor.fd
-static jfieldID fieldDescID; // ID for
org.apache.aio.AsynchronousFileChannel.fd
-
-// --- Operation enums ---
-static jobject operationRead;
-static jobject operationWrite;
-static jobject operationBatchRead;
-static jobject operationBatchWrite;
-
extern "C"
{
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved);
@@ -57,120 +30,11 @@
void aio_read_write_completion_handler(sigval_t sigval);
}
-// --- Utility Functions ----------------------------------------------------
-inline jint getFD(JNIEnv *env, jobject asynchFileChannel)
-{
- jobject fieldDesc = env->GetObjectField(asynchFileChannel, fieldDescID);
- return env->GetIntField(fieldDesc, fdID);
-}
-
-inline jint getBufferPosition(JNIEnv *env, jobject buffer)
-{
- jclass cls = env->GetObjectClass(buffer);
- jmethodID MID_position = env->GetMethodID(cls, "position", "()I");
- env->DeleteLocalRef(cls);
- jint ret = env->CallIntMethod(buffer, MID_position);
- return ret;
-}
-
-inline void setBufferPosition(JNIEnv *env, jobject buffer, jint position)
-{
- jclass cls = env->GetObjectClass(buffer);
- jmethodID MID_position = env->GetMethodID(cls, "position",
"(I)Ljava/nio/Buffer;");
- env->DeleteLocalRef(cls);
- env->CallVoidMethod(buffer, MID_position, position);
-}
-
-inline jint getBufferLimit(JNIEnv *env, jobject buffer)
-{
- jclass cls = env->GetObjectClass(buffer);
- jmethodID MID_limit = env->GetMethodID(cls, "limit", "()I");
- env->DeleteLocalRef(cls);
- return env->CallIntMethod(buffer, MID_limit);
-}
-
-inline void setBufferLimit(JNIEnv *env, jobject buffer, jint limit)
-{
- jclass cls = env->GetObjectClass(buffer);
- jmethodID MID_limit = env->GetMethodID(cls, "limit",
"(I)Ljava/nio/Buffer;");
- env->DeleteLocalRef(cls);
- env->CallVoidMethod(buffer, MID_limit, limit);
-}
-
-inline jobject createPosixAioFutureReadWrite(JNIEnv *env, jobject channel,
jobject operation, void *aiocb, jobject buffer, long position)
-{
- jvalue values[5];
- values[0].l = channel;
- values[1].l = operation;
- values[2].j = (jlong)aiocb;
- values[3].l = buffer;
- values[4].j = position;
- jobject future = env->NewObjectA(posixAioFutureReadWrite,
CID_posixAioFutureReadWrite, values);
- return env->NewGlobalRef(future);
-}
-
-struct aio_request *setupAioRequest(JNIEnv *env, jobject
asynchronousFileChannel, jobject buffer, jlong position, jobject operation)
-{
- // Get address and capacity of buffer
- if (buffer == NULL) {
- env->ThrowNew(nullPointerException, "buffer cannot be null");
- return NULL;
- }
- // Adjust bufferAddress by the position of the buffer
- jint bufferPosition = getBufferPosition(env, buffer);
- jint bufferLimit = getBufferLimit(env, buffer)
- jint bufferSize = bufferLimit - bufferPosition;
-
- jint bufferToAllocate;
- jbyte *bufferAddress = (jbyte *)env->GetDirectBufferAddress(buffer);
- if (bufferAddress == NULL) {
- bufferToAllocate = bufferSize;
- } else {
- bufferAddress += bufferPosition;
- bufferToAllocate = 0;
- }
-
- // Allocate aiocb
- int aio_request_size = sizeof(aio_request) + bufferToAllocate;
- struct aio_request *req = (aio_request *)malloc(aio_request_size);
- bzero(req, aio_request_size);
-
- if (bufferAddress == NULL) {
- req->allocated_buffer_size = bufferToAllocate;
- bufferAddress = req->buffer;
-
- //jbytearray env->NewByteArray(bufferSize);
-
- // TODO: For write, read data from buffer and put into
allocated buffer and forward position
- }
-
- // TODO: For write, increment position
-
- req->future = createPosixAioFutureReadWrite(env,
asynchronousFileChannel, operation, req, buffer, position);
-
- // Setup aiocb
- req->aio.aio_fildes = getFD(env, asynchronousFileChannel);
- req->aio.aio_offset = position;
- req->aio.aio_buf = bufferAddress;
- req->aio.aio_nbytes = bufferSize;
-
- // Set other aio request fields
- req->operation = operation;
-
- req->aio.aio_sigevent.sigev_notify = SIGEV_THREAD;
- req->aio.aio_sigevent.sigev_notify_function =
aio_read_write_completion_handler;
- req->aio.aio_sigevent.sigev_notify_attributes = NULL;
- req->aio.aio_sigevent.sigev_value.sival_ptr = req;
-
- return req;
-}
-
// --- OnLoad and OnUnload Functions ----------------------------------------
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
- jint JNIversion = JNI_VERSION_1_4;
#ifdef DEBUG
- fprintf(stdout, "Initializing native code for JNI version 0x%x\n",
JNIversion);
+ fprintf(stdout, "Initializing native code for JNI version 0x%x\n",
JNI_VERSION);
fflush(stdout);
#endif
@@ -178,7 +42,7 @@
jvm = vm;
JNIEnv *env;
- if (vm->GetEnv((void**)&env, JNIversion)) {
+ if (vm->GetEnv((void**)&env, JNI_VERSION)) {
return JNI_ERR;
}
@@ -189,168 +53,16 @@
cls = env->FindClass("java/lang/NullPointerException");
nullPointerException = (jclass)env->NewWeakGlobalRef(cls);
- cls = env->FindClass("org/apache/aio/posix/PosixAioFutureReadWrite");
- posixAioFutureReadWrite = (jclass)env->NewWeakGlobalRef(cls);
- CID_posixAioFutureReadWrite = env->GetMethodID(posixAioFutureReadWrite,
"<init>",
"(Lorg/apache/aio/AsynchronousFileChannel;Lorg/apache/aio/Operation;JLjava/nio/ByteBuffer;J)V");
- posixAioFutureReadWrite_aiocbPtrID =
env->GetFieldID(posixAioFutureReadWrite, "aiocbPtr", "J");
- posixAioFutureReadWrite_bufferID =
env->GetFieldID(posixAioFutureReadWrite, "buffer", "Ljava/nio/ByteBuffer;");
-
- cls = env->FindClass("org/apache/aio/common/AbstractAioFuture");
- abstractAioFuture_processFutureListenersID = env->GetMethodID(cls,
"processFutureListeners", "()V");
- abstractAioFuture_handleErrorID = env->GetMethodID(cls, "handleError",
"(Ljava/lang/Throwable;)V");
-
- cls = env->FindClass("java/io/FileDescriptor");
- fdID = env->GetFieldID(cls, "fd", "I");
-
- cls = env->FindClass("org/apache/aio/AsynchronousFileChannel");
- fieldDescID = env->GetFieldID(cls, "fd", "Ljava/io/FileDescriptor;");
-
- cls = env->FindClass("org/apache/aio/Operation");
- jfieldID fid;
- fid = env->GetStaticFieldID(cls, "READ", "Lorg/apache/aio/Operation;");
- operationRead = env->NewWeakGlobalRef(env->GetStaticObjectField(cls,
fid));
- fid = env->GetStaticFieldID(cls, "WRITE", "Lorg/apache/aio/Operation;");
- operationWrite = env->NewWeakGlobalRef(env->GetStaticObjectField(cls,
fid));
- fid = env->GetStaticFieldID(cls, "BATCH_READ",
"Lorg/apache/aio/Operation;");
- operationBatchRead =
env->NewWeakGlobalRef(env->GetStaticObjectField(cls, fid));
- fid = env->GetStaticFieldID(cls, "BATCH_WRITE",
"Lorg/apache/aio/Operation;");
- operationBatchWrite =
env->NewWeakGlobalRef(env->GetStaticObjectField(cls, fid));
-
- return JNIversion;
+ return JNI_VERSION;
}
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
{
- jint JNIversion = JNI_VERSION_1_4;
JNIEnv *env;
- if (vm->GetEnv((void**)&env, JNIversion)) {
+ if (vm->GetEnv((void**)&env, JNI_VERSION)) {
return;
}
env->DeleteWeakGlobalRef(ioException);
env->DeleteWeakGlobalRef(nullPointerException);
- env->DeleteWeakGlobalRef(posixAioFutureReadWrite);
- env->DeleteWeakGlobalRef(operationRead);
- env->DeleteWeakGlobalRef(operationWrite);
- env->DeleteWeakGlobalRef(operationBatchRead);
- env->DeleteWeakGlobalRef(operationBatchWrite);
-}
-
-// --- AsynchronousFileChannel method implementations ----------------------
-JNIEXPORT jobject JNICALL Java_org_apache_aio_AsynchronousFileChannel_write
- (JNIEnv *env, jobject obj, jobject buffer, jlong position)
-{
-#ifdef DEBUG
- fprintf(stdout, "aio write at file position %d\n", position);
- fflush(stdout);
-#endif
- struct aio_request *req = setupAioRequest(env, obj, buffer, position,
operationWrite);
-
- int ret = aio_write(&req->aio);
- if (ret) {
- // TODO Handle errors from ret
- // return a null on error.
- return NULL;
- }
- return req->future;
-}
-
-JNIEXPORT jobject JNICALL Java_org_apache_aio_AsynchronousFileChannel_read
- (JNIEnv *env, jobject obj, jobject buffer, jlong position)
-{
-#ifdef DEBUG
- fprintf(stdout, "aio read at file position %d\n", position);
- fflush(stdout);
-#endif
- struct aio_request *req = setupAioRequest(env, obj, buffer, position,
operationRead);
- LOG_DEBUG("Do aio read\n");
- int ret = aio_read(&req->aio);
- if (ret) {
- // TODO Handle errors from ret
- // return a null on error.
- return NULL;
- }
-
- // Return future object
- return req->future;
-}
-
-JNIEXPORT jobject JNICALL Java_org_apache_aio_AsynchronousFileChannel_batchRead
- (JNIEnv *env, jobject obj, jobjectArray batch)
-{
- LOG_DEBUG("batchRead\n")
- return NULL;
-}
-
-JNIEXPORT jobject JNICALL
Java_org_apache_aio_AsynchronousFileChannel_batchWrite
- (JNIEnv *env, jobject obj, jobjectArray batch)
-{
- LOG_DEBUG("batchWrite\n");
- return NULL;
-}
-
-JNIEXPORT void JNICALL Java_org_apache_aio_AsynchronousFileChannel_suspend
- (JNIEnv *env, jobject obj, jobjectArray futures)
-{
- LOG_DEBUG("suspend\n");
}
-
-// --- PosixAioFutureReadWrite methods
---------------------------------------------------
-JNIEXPORT jboolean JNICALL
Java_org_apache_aio_posix_PosixAioFutureReadWrite_cancel
- (JNIEnv *env, jobject obj)
-{
- struct aio_request *req = (struct aio_request *)env->GetLongField(obj,
posixAioFutureReadWrite_aiocbPtrID);
- int ret = aio_cancel(req->aio.aio_fildes, &req->aio);
- if (ret == AIO_CANCELED) {
- free(req);
- return 1;
- }
- return 0;
-}
-
-// --- Completion handlers
----------------------------------------------------------------
-void aio_read_write_completion_handler(sigval_t sigval)
-{
- LOG_DEBUG("In AIO completion handler\n")
-
- JNIEnv *env;
- jint res = jvm->AttachCurrentThread((void**)&env, NULL);
- if (res < 0) {
- fprintf(stderr, "Could not attach JVM to AIO thread");
- fflush(stderr);
- return;
- }
-
- struct aio_request *req = (aio_request *)sigval.sival_ptr;
-
- /* Did the request complete? */
- if (aio_error(&req->aio) == 0) {
- /* Request completed successfully, get number of bytes
processed */
- int ret = aio_return(&req->aio);
-
- if (req->operation == operationRead) {
- jobject buffer = env->GetObjectField(req->future,
posixAioFutureReadWrite_bufferID);
- // Adjust buffer limit
- // TODO: Add support for writing read data to heap byte
buffer
-
- // Get buffer position
- int limit = ret + getBufferPosition(env, buffer);
- setBufferLimit(env, buffer, limit);
- }
-
- // Call aio listeners
- env->CallVoidMethod(req->future,
abstractAioFuture_processFutureListenersID);
- } else {
- // TODO Write a unit test for this
- env->ThrowNew(ioException, "Could not complete AIO request");
- jthrowable e = env->ExceptionOccurred();
- env->CallVoidMethod(req->future,
abstractAioFuture_handleErrorID, e);
- }
-
- // Free resources
- env->DeleteGlobalRef(req->future);
- free(req);
-
- jvm->DetachCurrentThread();
- return;
-}