| 1 |
|
| 2 |
#include "protoPipeJni.h" |
| 3 |
#include "protoPipe.h" |
| 4 |
|
| 5 |
#ifdef __cplusplus |
| 6 |
extern "C" { |
| 7 |
#endif |
| 8 |
/* |
| 9 |
* Class: mil_navy_nrl_protolib_ProtoPipe |
| 10 |
* Method: createProtoPipe |
| 11 |
* Signature: ()V |
| 12 |
*/ |
| 13 |
JNIEXPORT void JNICALL Java_mil_navy_nrl_protolib_ProtoPipe_createProtoPipe |
| 14 |
(JNIEnv* env, jobject obj) |
| 15 |
{ |
| 16 |
ProtoPipe* thePipe = new ProtoPipe(ProtoPipe::MESSAGE); |
| 17 |
jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), "handle", "J"); |
| 18 |
env->SetLongField(obj, fid, (jlong)thePipe); |
| 19 |
} |
| 20 |
|
| 21 |
/* |
| 22 |
* Class: mil_navy_nrl_protolib_ProtoPipe |
| 23 |
* Method: listen |
| 24 |
* Signature: (Ljava/lang/String;)Ljava/lang/Boolean; |
| 25 |
*/ |
| 26 |
JNIEXPORT jboolean JNICALL Java_mil_navy_nrl_protolib_ProtoPipe_listen |
| 27 |
(JNIEnv* env, jobject obj, jstring pipeName) |
| 28 |
{ |
| 29 |
jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), "handle", "J"); |
| 30 |
ProtoPipe* thePipe = (ProtoPipe*)env->GetLongField(obj, fid); |
| 31 |
const char* namePtr = env->GetStringUTFChars(pipeName, NULL); |
| 32 |
bool result = thePipe->Listen(namePtr); |
| 33 |
env->ReleaseStringUTFChars(pipeName, namePtr); |
| 34 |
return (jboolean)result; |
| 35 |
} |
| 36 |
|
| 37 |
/* |
| 38 |
* Class: mil_navy_nrl_protolib_ProtoPipe |
| 39 |
* Method: read |
| 40 |
* Signature: ([BII)I |
| 41 |
*/ |
| 42 |
JNIEXPORT jint JNICALL Java_mil_navy_nrl_protolib_ProtoPipe_read |
| 43 |
(JNIEnv* env, jobject obj, jbyteArray buffer, jint offset, jint length) |
| 44 |
{ |
| 45 |
jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), "handle", "J"); |
| 46 |
ProtoPipe* thePipe = (ProtoPipe*)env->GetLongField(obj, fid); |
| 47 |
unsigned int numBytes = (unsigned int) length; |
| 48 |
jbyte* ptr = env->GetByteArrayElements(buffer, NULL); |
| 49 |
if (thePipe->Recv((char*)ptr, numBytes)) |
| 50 |
{ |
| 51 |
// Release the jbyte* to copy them back to the jbyteArray |
| 52 |
env->ReleaseByteArrayElements(buffer, ptr, 0); |
| 53 |
return (jint)numBytes; |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
// Release the jbyte* to copy them back to the jbyteArray |
| 58 |
env->ReleaseByteArrayElements(buffer, ptr, 0); |
| 59 |
return (jint)-1; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* Class: mil_navy_nrl_protolib_ProtoPipe |
| 65 |
* Method: connect |
| 66 |
* Signature: (Ljava/lang/String;)Ljava/lang/Boolean; |
| 67 |
*/ |
| 68 |
JNIEXPORT jboolean JNICALL Java_mil_navy_nrl_protolib_ProtoPipe_connect |
| 69 |
(JNIEnv* env, jobject obj, jstring pipeName) |
| 70 |
{ |
| 71 |
jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), "handle", "J"); |
| 72 |
ProtoPipe* thePipe = (ProtoPipe*)env->GetLongField(obj, fid); |
| 73 |
const char* namePtr = env->GetStringUTFChars(pipeName, NULL); |
| 74 |
bool result = thePipe->Connect(namePtr); |
| 75 |
env->ReleaseStringUTFChars(pipeName, namePtr); |
| 76 |
return (jboolean)result; |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
* Class: mil_navy_nrl_protolib_ProtoPipe |
| 81 |
* Method: write |
| 82 |
* Signature: ([BII)V |
| 83 |
*/ |
| 84 |
JNIEXPORT void JNICALL Java_mil_navy_nrl_protolib_ProtoPipe_write |
| 85 |
(JNIEnv* env, jobject obj, jbyteArray buffer, jint offset, jint length) |
| 86 |
{ |
| 87 |
jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), "handle", "J"); |
| 88 |
ProtoPipe* thePipe = (ProtoPipe*)env->GetLongField(obj, fid); |
| 89 |
unsigned int numBytes = (unsigned int)length; |
| 90 |
jbyte* ptr = env->GetByteArrayElements(buffer, NULL); |
| 91 |
thePipe->Send((char*)ptr, numBytes); |
| 92 |
env->ReleaseByteArrayElements(buffer, ptr, 0); |
| 93 |
} |
| 94 |
|
| 95 |
/* |
| 96 |
* Class: mil_navy_nrl_protolib_ProtoPipe |
| 97 |
* Method: close |
| 98 |
* Signature: ()V |
| 99 |
*/ |
| 100 |
JNIEXPORT void JNICALL Java_mil_navy_nrl_protolib_ProtoPipe_close |
| 101 |
(JNIEnv* env, jobject obj) |
| 102 |
{ |
| 103 |
jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), "handle", "J"); |
| 104 |
ProtoPipe* thePipe = (ProtoPipe*)env->GetLongField(obj, fid); |
| 105 |
thePipe->Close(); |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
* Class: mil_navy_nrl_protolib_ProtoPipe |
| 110 |
* Method: doFinalize |
| 111 |
* Signature: ()V |
| 112 |
*/ |
| 113 |
JNIEXPORT void JNICALL Java_mil_navy_nrl_protolib_ProtoPipe_doFinalize |
| 114 |
(JNIEnv* env, jobject obj) |
| 115 |
{ |
| 116 |
jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), "handle", "J"); |
| 117 |
ProtoPipe* thePipe = (ProtoPipe*)env->GetLongField(obj, fid); |
| 118 |
if (NULL != thePipe) |
| 119 |
{ |
| 120 |
if (thePipe->IsOpen()) thePipe->Close(); |
| 121 |
delete thePipe; |
| 122 |
env->SetLongField(obj, fid, (jlong)0); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#ifdef __cplusplus |
| 127 |
} |
| 128 |
#endif |