Changeset 21
- Timestamp:
- 04/16/08 16:03:15 (8 months ago)
- Files:
-
- trunk/Makefile (added)
- trunk/libsipc.spec (modified) (2 diffs)
- trunk/libsipc/bindings/java/Makefile (modified) (1 diff)
- trunk/libsipc/bindings/java/com/tresys/sipc/Sipc.java (modified) (8 diffs)
- trunk/libsipc/bindings/java/com/tresys/sipc/libsipc.java (modified) (2 diffs)
- trunk/libsipc/bindings/java/examples/MQ_Binary_Reader.java (added)
- trunk/libsipc/bindings/java/sipc_wrap.c (modified) (10 diffs)
- trunk/libsipc/examples/Makefile (modified) (1 diff)
- trunk/libsipc/examples/README (modified) (3 diffs)
- trunk/libsipc/examples/mq_bin_reader.c (added)
- trunk/libsipc/examples/mq_bin_sender.c (added)
- trunk/libsipc/examples/mq_creator.c (modified) (1 diff)
- trunk/libsipc/examples/mq_destroyer.c (modified) (1 diff)
- trunk/libsipc/examples/mq_reader.c (modified) (5 diffs)
- trunk/libsipc/examples/mq_sender.c (modified) (4 diffs)
- trunk/libsipc/examples/shm_creator.c (modified) (1 diff)
- trunk/libsipc/examples/shm_destroyer.c (modified) (1 diff)
- trunk/libsipc/examples/shm_reader.c (modified) (3 diffs)
- trunk/libsipc/examples/shm_sender.c (modified) (4 diffs)
- trunk/libsipc/include/sipc/sipc.h (modified) (3 diffs)
- trunk/libsipc/man/man3/sipc_close.3 (added)
- trunk/libsipc/man/man3/sipc_connect.3 (deleted)
- trunk/libsipc/man/man3/sipc_create.3 (deleted)
- trunk/libsipc/man/man3/sipc_destroy_handle.3 (deleted)
- trunk/libsipc/man/man3/sipc_destroy_resource.3 (deleted)
- trunk/libsipc/man/man3/sipc_disconnect.3 (deleted)
- trunk/libsipc/man/man3/sipc_error.3 (modified) (2 diffs)
- trunk/libsipc/man/man3/sipc_get_data_ptr.3 (modified) (2 diffs)
- trunk/libsipc/man/man3/sipc_init.3 (deleted)
- trunk/libsipc/man/man3/sipc_open.3 (added)
- trunk/libsipc/man/man3/sipc_recv_data.3 (modified) (3 diffs)
- trunk/libsipc/man/man3/sipc_send_data.3 (modified) (2 diffs)
- trunk/libsipc/man/man3/sipc_shm_recv_done.3 (modified) (3 diffs)
- trunk/libsipc/man/man3/sipc_unlink.3 (added)
- trunk/libsipc/src/libsipc.map (modified) (1 diff)
- trunk/libsipc/src/mqueue_internal.c (modified) (2 diffs)
- trunk/libsipc/src/sipc.c (modified) (8 diffs)
- trunk/libsipc/src/sipc_internal.h (modified) (4 diffs)
- trunk/libsipc/src/sipc_mqueue.c (modified) (17 diffs)
- trunk/libsipc/src/sipc_mqueue.h (modified) (1 diff)
- trunk/libsipc/src/sipc_shm.c (modified) (9 diffs)
- trunk/libsipc/src/sipc_shm.h (modified) (1 diff)
- trunk/libsipc/tests/ipc_creator.c (modified) (1 diff)
- trunk/libsipc/tests/ipc_destroyer.c (modified) (1 diff)
- trunk/libsipc/tests/mqueue.c (modified) (4 diffs)
- trunk/libsipc/tests/shm.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/libsipc.spec
r20 r21 7 7 Summary: Secure IPC Library 8 8 Group: Development/Libraries 9 BuildRequires: jdk >= 1.5 9 BuildRequires: jdk >= 1.5 glibc-devel gcc 10 10 Requires: jdk >= 1.5 11 11 … … 38 38 39 39 %files 40 %defattr(-,root,root,-) 40 41 %doc LICENSE bindings/java/README 41 42 %{_libdir}/libsipc.so.* 42 43 %{_includedir}/sipc/ 43 44 %{_mandir}/man3/* 44 %{_libdir}/libsipcwrapper.so.* 45 # The following regexp captures both the library and its symlink; 46 # this is needed because Java will dynamically load libsipcwrapper.so, 47 # not its full soname. 48 %{_libdir}/libsipcwrapper.so* 45 49 %{_datadir}/java/* 46 50 %{_datadir}/sipc/bindings/java/ trunk/libsipc/bindings/java/Makefile
r20 r21 54 54 55 55 %.o: %.c 56 $(CC) $(CFLAGS) $(INCLUDEDIRS) $(JNI_CFLAGS) - c -o $@ $<56 $(CC) $(CFLAGS) $(INCLUDEDIRS) $(JNI_CFLAGS) -fPIC -c -o $@ $< 57 57 58 58 install: trunk/libsipc/bindings/java/com/tresys/sipc/Sipc.java
r1 r21 25 25 * @author David Windsor <dwindsor@tresys.com> 26 26 * @author Norman Patrick <npatrick@tresys.com> 27 * @author L. Ross Raszewski <lraszewski@tresys.com> 27 28 */ 28 29 public abstract class Sipc { … … 41 42 protected long handle; /* internal handle */ 42 43 44 protected long size_handle; /* internal message size handle */ 43 45 private java.nio.ByteBuffer dataPtr = null; /* internal pointer to data */ 44 46 … … 77 79 public void Init(boolean write) { 78 80 handle = libsipc.sipc_init(Key, GetIPCType(), DATA_LEN, write ? 1 : 0); 81 isConnected = handle!=0; 82 size_handle=libsipc.sipc_create_length(); 79 83 } 80 84 … … 86 90 if(handle != 0) { 87 91 libsipc.sipc_destroy_handle(handle); 92 libsipc.sipc_destroy_length(size_handle); 93 size_handle=0; 88 94 handle = 0; 89 95 dataPtr = null; … … 100 106 public boolean Connect() throws ConnectionException { 101 107 if(!isConnected) { 102 if(libsipc.sipc_connect(handle) < 0) {103 isConnected = false;104 108 throw new ConnectionException("Attempt to connect failed"); 105 } 106 isConnected = true; 107 } 108 109 } 109 110 return isConnected; 110 111 } … … 146 147 */ 147 148 public java.nio.ByteBuffer ReadData() { 148 return libsipc.sipc_recv_data(handle, GetIPCType()); 149 } 150 149 return libsipc.sipc_recv_data(handle, GetIPCType(), size_handle); 150 } 151 152 /** 153 * Returns the size of the last successful ReadData() operation. 154 * This should be identical to the capacity of the buffer returned by 155 * ReadData(). Return value is undefined if ReadData has not been called. 156 * 157 * @return The size of the buffer returned by the most recent call to ReadData() 158 */ 159 public int GetReadSize() { 160 return libsipc.sipc_get_length(size_handle); 161 } 151 162 /** 152 163 * Get a reference to the IPC handle's internal data buffer. … … 159 170 public java.nio.ByteBuffer GetDataPtr() { 160 171 if(isConnected && dataPtr == null) { 161 dataPtr = libsipc.sipc_get_data_ptr(handle, DATA_LEN); 162 } 172 dataPtr = libsipc.sipc_get_data_ptr(handle); 173 } 174 163 175 164 176 return dataPtr; … … 169 181 * connected state for this to succeed. 170 182 * 183 * @param len Length of the message to be sent 171 184 * @return 0 on success, <0 on error. 172 185 * @see #Connect() Connect 173 186 */ 187 public int SendData(int len) { 188 return libsipc.sipc_send_data(handle, len); 189 } 190 191 /** 192 * Send data to the communications channel. The amount of data to 193 * send is determined by the current limit of the data buffer. 194 * 195 * @return 0 on success, <0 on error 196 * @see #SendData(int) SendData(int) 197 */ 174 198 public int SendData() { 175 return libsipc.sipc_send_data(handle);199 return SendData(dataPtr.limit()); 176 200 } 177 201 } trunk/libsipc/bindings/java/com/tresys/sipc/libsipc.java
r1 r21 1 /* This file has been hand-modified -- do not regenerate */ 2 1 3 /* ---------------------------------------------------------------------------- 2 4 * This file was automatically generated by SWIG (http://www.swig.org). … … 9 11 package com.tresys.sipc; 10 12 11 final class libsipc { 13 class libsipc { 14 12 15 static { 13 16 System.loadLibrary("sipcwrapper"); 14 17 } 15 16 public final static int SIPC_SYSV_SHM = 0; 17 18 public final static int SIPC_SYSV_MQUEUES = 1; 19 20 public final static int SIPC_NUM_TYPES = 2; 21 22 public final static native long sipc_init(String key, int ipc_type, 23 long size, int sender); 24 25 public final static native void sipc_destroy_handle(long handle); 26 27 public final static native void sipc_destroy_resource(String key, 28 int ipc_type); 29 30 public final static native int sipc_create(String key, int ipc_type); 31 32 public final static native int sipc_connect(long handle); 33 34 public final static native void sipc_disconnect(long handle); 35 36 public final static native int sipc_send_data(long handle); 37 38 public final static native java.nio.ByteBuffer sipc_recv_data(long handle, 39 int ipc_type); 40 41 public final static native int sipc_shm_recv_done(long handle); 42 43 public final static native java.nio.ByteBuffer sipc_get_data_ptr( 44 long jarg1, 45 long size); 18 public final static int SIPC_SYSV_SHM=0; 19 public final static int SIPC_SYSV_MQUEUES=1; 20 public final static int SIPC_NUM_TYPES=2; 21 public final static native long sipc_init(String jarg1, int jarg2, long jarg3, int jarg4); 22 public final static native void sipc_destroy_handle(long jarg1); 23 public final static native void sipc_destroy_resource(String jarg1, int jarg2); 24 public final static native int sipc_create(String jarg1, int jarg2); 25 public final static native int sipc_connect(long jarg1); 26 public final static native void sipc_disconnect(long jarg1); 27 public final static native int sipc_send_data(long jarg1, int jarg2); 28 /* This method is for internal use by sipc_recv_data */ 29 private final static native byte[] sipc_recv_datad(long handle, 30 int ipc_type, 31 long size_handle); 32 /* Wrap the result of the native call in a ByteBuffer */ 33 public final static java.nio.ByteBuffer sipc_recv_data(long handle, 34 int ipc_type, 35 long size_handle) 36 { 37 byte [] buf=sipc_recv_datad(handle,ipc_type,size_handle); 38 return (buf==null? null: 39 java.nio.ByteBuffer.wrap(buf,0,sipc_get_length(size_handle))); 40 } 41 public final static native java.nio.ByteBuffer sipc_get_data_ptr(long jarg1); 42 public final static native void sipc_error(long jarg1, String jarg2); 43 public final static native int sipc_shm_recv_done(long jarg1); 44 public final static native long sipc_create_length(); 45 public final static native int sipc_get_length(long jarg1); 46 public final static native void sipc_destroy_length(long jarg1); 46 47 } trunk/libsipc/bindings/java/sipc_wrap.c
r20 r21 1 /* This file has been modified by hand. Do not regenerate it. */ 2 3 1 4 /* ---------------------------------------------------------------------------- 2 5 * This file was automatically generated by SWIG (http://www.swig.org). … … 14 17 * ----------------------------------------------------------------------------- */ 15 18 16 /* template workaround for compilers that cannot correctly implement the C++ standard */ 19 /* template workaround for compilers that cannot correctly implement the 20 C++ standard */ 17 21 #ifndef SWIGTEMPLATEDISAMBIGUATOR 18 22 # if defined(__SUNPRO_CC) … … 172 176 #define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, msg); return nullreturn; } else 173 177 174 175 /* Includes the header in the wrapper code */ 176 #include <sipc/sipc.h> 177 178 /* Includes the header in the wrapper code */ 179 #include <sipc/sipc.h> 180 181 /* Need the internal definition of the sipc structure to generate the output 182 bytebuffer */ 183 struct sipc { 184 key_t key; 185 int ipc_type; 186 int sender; 187 union { 188 int fd; /* file desciptor for socket connections */ 189 struct shm { /* shm data */ 190 int shmid; /* ID of segment */ 191 int msqid; /* notify channel */ 192 /* sipc_t *mqueue; handle to notify channel */ 193 } s; 194 int msqid; /* message queue */ 195 }; 196 197 char *data; 198 size_t len; 199 size_t msg_len; /* Length in bytes of the next message */ 200 size_t copied; /* Number of bytes xmitted so far */ 201 struct sipc_func_table *funcs; 202 }; 203 204 205 178 206 179 207 #ifdef __cplusplus … … 181 209 #endif 182 210 183 // sipc_init 184 JNIEXPORT jlong JNICALL Java_com_tresys_sipc_libsipc_sipc_1init(JNIEnv *jenv, jclass jcls, jstring key, jint ipc_type, jlong size, jint sender) { 211 JNIEXPORT jlong JNICALL Java_com_tresys_sipc_libsipc_sipc_1init(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2, jlong jarg3, jint jarg4) { 185 212 jlong jresult = 0 ; 186 213 char *arg1 = (char *) 0 ; … … 193 220 (void)jcls; 194 221 arg1 = 0; 195 if ( key) {196 arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0);222 if (jarg1) { 223 arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 197 224 if (!arg1) return 0; 198 225 } 199 arg2 = (int) ipc_type;200 arg3 = (size_t) size;201 arg4 = ( int)sender;202 result = (sipc_t *)sipc_ init(arg1,arg2,arg3,arg4);226 arg2 = (int)jarg2; 227 arg3 = (size_t)jarg3; 228 arg4 = ((int)jarg4) ? SIPC_SENDER:SIPC_RECEIVER; 229 result = (sipc_t *)sipc_open(arg1,arg4,arg2,arg3); 203 230 *(sipc_t **)&jresult = result; 204 if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, key, arg1);205 return jresult; 206 } 207 208 // sipc_destroy_handle 209 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1handle(JNIEnv *jenv, jclass jcls, jlong handle) {210 sipc_t *arg1 = (sipc_t *) 0 ; 211 212 (void)jenv; 213 (void)jcls; 214 arg1 = *(sipc_t **)& handle;215 sipc_ destroy_handle(arg1);216 } 217 218 // sipc_destroy_resource 219 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1resource(JNIEnv *jenv, jclass jcls, jstring key, jint ipc_type) {231 if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, arg1); 232 return jresult; 233 } 234 235 236 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1handle(JNIEnv *jenv, jclass jcls, jlong jarg1) { 237 sipc_t *arg1 = (sipc_t *) 0 ; 238 239 (void)jenv; 240 (void)jcls; 241 arg1 = *(sipc_t **)&jarg1; 242 sipc_close(arg1); 243 } 244 245 246 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1resource(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2) { 220 247 char *arg1 = (char *) 0 ; 221 248 int arg2 ; … … 224 251 (void)jcls; 225 252 arg1 = 0; 226 if ( key) {227 arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0);253 if (jarg1) { 254 arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 228 255 if (!arg1) return ; 229 256 } 230 arg2 = (int) ipc_type;231 sipc_ destroy_resource(arg1,arg2);232 if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, key, arg1);233 } 234 235 // sipc_create 236 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1create(JNIEnv *jenv, jclass jcls, jstring key, jint ipc_type) {257 arg2 = (int)jarg2; 258 sipc_unlink(arg1,arg2); 259 if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, arg1); 260 } 261 262 263 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1create(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2) { 237 264 jint jresult = 0 ; 238 265 char *arg1 = (char *) 0 ; … … 243 270 (void)jcls; 244 271 arg1 = 0; 245 if ( key) {246 arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0);272 if (jarg1) { 273 arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 247 274 if (!arg1) return 0; 248 275 } 249 arg2 = (int)ipc_type; 250 result = (int)sipc_create(arg1,arg2); 276 arg2 = (int)jarg2; 277 // result = (int)sipc_create(arg1,arg2); 278 result=sipc_open(arg1, SIPC_CREATOR, arg2, 0)!=NULL; 251 279 jresult = (jint)result; 252 if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, key, arg1); 253 return jresult; 254 } 255 256 // sipc_connect 257 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1connect(JNIEnv *jenv, jclass jcls, jlong handle) { 258 jint jresult = 0 ; 259 sipc_t *arg1 = (sipc_t *) 0 ; 260 int result; 261 262 (void)jenv; 263 (void)jcls; 264 arg1 = *(sipc_t **)&handle; 280 if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, arg1); 281 return jresult; 282 } 283 284 285 /* No longer needed in new API 286 */ 287 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1connect(JNIEnv *jenv, jclass jcls, jlong jarg1) { 288 /* jint jresult = 0 ; 289 sipc_t *arg1 = (sipc_t *) 0 ; 290 int result; 291 292 (void)jenv; 293 (void)jcls; 294 arg1 = *(sipc_t **)&jarg1; 265 295 result = (int)sipc_connect(arg1); 266 296 jresult = (jint)result; 267 297 return jresult; 268 } 269 270 // sipc_disconnect 271 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1disconnect(JNIEnv *jenv, jclass jcls, jlong handle) { 272 sipc_t *arg1 = (sipc_t *) 0 ; 273 274 (void)jenv; 275 (void)jcls; 276 arg1 = *(sipc_t **)&handle; 277 sipc_disconnect(arg1); 278 } 279 280 // sipc_send_data 281 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1send_1data(JNIEnv *jenv, jclass jcls, jlong handle) { 298 */ 299 sipc_t *arg1 = (sipc_t *) 0 ; 300 arg1 = *(sipc_t **)&jarg1; 301 return (jint) (arg1!=NULL); 302 } 303 304 305 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1disconnect(JNIEnv *jenv, jclass jcls, jlong jarg1) { 306 sipc_t *arg1 = (sipc_t *) 0 ; 307 308 (void)jenv; 309 (void)jcls; 310 arg1 = *(sipc_t **)&jarg1; 311 sipc_close(arg1); 312 } 313 314 315 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1send_1data(JNIEnv *jenv, jclass jcls, jlong jarg1, jint jarg2) { 282 316 jint jresult = 0 ; 283 317 sipc_t *arg1 = (sipc_t *) 0 ; 284 int result; 285 286 (void)jenv; 287 (void)jcls; 288 arg1 = *(sipc_t **)&handle; 289 result = (int)sipc_send_data(arg1); 318 int arg2 ; 319 int result; 320 321 (void)jenv; 322 (void)jcls; 323 arg1 = *(sipc_t **)&jarg1; 324 arg2 = (int)jarg2; 325 result = (int)sipc_send_data(arg1,arg2); 290 326 jresult = (jint)result; 291 327 return jresult; … … 293 329 294 330 295 // sipc_recv_data 296 JNIEXPORT jobject JNICALL Java_com_tresys_sipc_libsipc_sipc_1recv_1data(JNIEnv *jenv, jclass jcls, jlong handle, jint ipc_type) { 297 j object jresult = 0;331 JNIEXPORT jbyteArray JNICALL Java_com_tresys_sipc_libsipc_sipc_1recv_1datad(JNIEnv *jenv, jclass jcls, jlong handle, jint ipc_type, jlong size_handle) { 332 333 jbyteArray jresult = (jbyteArray) 0; 298 334 sipc_t *arg1 = (sipc_t *) 0 ; 299 335 char *arg2 = NULL; 300 336 int arg3 = 0; 301 337 int result; 302 303 (void)jenv; 304 (void)jcls; 305 arg1 = *(sipc_t **)&handle; 338 339 (void)jenv; 340 (void)jcls; 341 arg1 = *(sipc_t **)&handle; 306 342 result = (int)sipc_recv_data(arg1, &arg2, &arg3); 307 343 344 *(*(int **)&size_handle)=arg3; 345 308 346 if (result == 0) 309 347 { 310 jresult = (*jenv)->NewDirectByteBuffer(jenv, arg2, arg3); 348 /* 349 jresult = (*jenv)->NewDirectByteBuffer(jenv, arg2, arg3); 350 LRR: The original form above 351 leaks memory like a sieve. Instead, we ask java for a new 352 array and copy the output there. This incurs a performance 353 penalty, but it's better than leaking memory. 354 */ 355 jbyteArray jba=(*jenv)->NewByteArray(jenv,arg3); 356 (*jenv)->SetByteArrayRegion(jenv,jba,0,arg3,(signed char *) arg2); 357 jresult=jba; 358 359 311 360 } 312 313 if (ipc_type == (jint)SIPC_SYSV_MQUEUES) 314 free(arg2); 315 316 return jresult; 317 } 318 319 //sipc_get_data_ptr 320 JNIEXPORT jobject JNICALL Java_com_tresys_sipc_libsipc_sipc_1get_1data_1ptr(JNIEnv *jenv, jclass jcls, jlong handle, jlong size) { 321 jobject jresult = 0 ; 322 sipc_t *arg1 = (sipc_t *) 0 ; 323 char *result = NULL; 324 325 (void)jenv; 326 (void)jcls; 327 arg1 = *(sipc_t **)&handle; 328 result = sipc_get_data_ptr(arg1); 329 if(result) 330 { 331 jresult = (*jenv)->NewDirectByteBuffer(jenv, result, size); 361 if (ipc_type==(jint) SIPC_SYSV_MQUEUES) 362 { 363 free(arg2); 364 } 365 366 return jresult; 367 368 } 369 370 371 372 JNIEXPORT jobject JNICALL Java_com_tresys_sipc_libsipc_sipc_1get_1data_1ptr(JNIEnv *jenv, jclass jcls, jlong jarg1) { 373 jstring jresult = 0 ; 374 sipc_t *arg1 = (sipc_t *) 0 ; 375 char *result = 0 ; 376 377 (void)jenv; 378 (void)jcls; 379 arg1 = *(sipc_t **)&jarg1; 380 result = (char *)sipc_get_data_ptr(arg1); 381 if (result) jresult = (*jenv)->NewDirectByteBuffer(jenv, result, arg1->len); 382 return jresult; 383 } 384 385 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1error(JNIEnv *jenv, jclass jcls, jlong jarg1, jstring jarg2) { 386 sipc_t *arg1 = (sipc_t *) 0 ; 387 char *arg2 = (char *) 0 ; 388 void *arg3 = 0 ; 389 390 (void)jenv; 391 (void)jcls; 392 arg1 = *(sipc_t **)&jarg1; 393 arg2 = 0; 394 if (jarg2) { 395 arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); 396 if (!arg2) return ; 332 397 } 333 return jresult; 334 } 335 336 // sipc_shm_recv_done 337 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1shm_1recv_1done(JNIEnv *jenv, jclass jcls, jlong handle) { 398 sipc_error(arg1,(char const *)arg2,arg3); 399 if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, arg2); 400 } 401 402 403 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1shm_1recv_1done(JNIEnv *jenv, jclass jcls, jlong jarg1) { 338 404 jint jresult = 0 ; 339 405 sipc_t *arg1 = (sipc_t *) 0 ; … … 342 408 (void)jenv; 343 409 (void)jcls; 344 arg1 = *(sipc_t **)& handle;410 arg1 = *(sipc_t **)&jarg1; 345 411 result = (int)sipc_shm_recv_done(arg1); 346 412 jresult = (jint)result; … … 349 415 350 416 417 /* Methods to allocate and unbox the int pointer used for returning 418 message size */ 419 static int *sipc_create_length() 420 { 421 return (int *)malloc(sizeof(int)); 422 } 423 424 static int sipc_get_length(int *arg) 425 { 426 if (arg) return *arg; else return -1; 427 } 428 429 static void sipc_destroy_length(int *arg) 430 { 431 if (arg) free(arg); 432 } 433 434 435 JNIEXPORT jlong JNICALL Java_com_tresys_sipc_libsipc_sipc_1create_1length(JNIEnv *jenv, jclass jcls) { 436 jlong jresult = 0 ; 437 int *result = 0 ; 438 439 (void)jenv; 440 (void)jcls; 441 result = (int *)sipc_create_length(); 442 *(int **)&jresult = result; 443 return jresult; 444 } 445 446 JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1get_1length(JNIEnv *jenv, jclass jcls, jlong jarg1) { 447 jint jresult = 0 ; 448 int *arg1 = (int *) 0 ; 449 int result; 450 451 (void)jenv; 452 (void)jcls; 453 arg1 = *(int **)&jarg1; 454 result = (int)sipc_get_length(arg1); 455 jresult = (jint)result; 456 return jresult; 457 } 458 459 JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1length(JNIEnv *jenv, jclass jcls, jlong jarg1) { 460 int *arg1 = (int *) 0 ; 461 462 (void)jenv; 463 (void)jcls; 464 arg1 = *(int **)&jarg1; 465 sipc_destroy_length(arg1); 466 } 467 468 351 469 #ifdef __cplusplus 352 470 } trunk/libsipc/examples/Makefile
r20 r21 33 33 (cd $$subdir && $(MAKE) $@) || exit 1;\ 34 34 done 35 36 mq_bin_reader: mq_bin_reader.o 37 $(CC) $(LDFLAGS) -lsipc $(LIBDIRS) -o $@ $^ 38 39 mq_bin_sender: mq_bin_sender.o 40 $(CC) $(LDFLAGS) -lsipc $(LIBDIRS) -o $@ $^ 35 41 36 42 mq_creator: mq_creator.o trunk/libsipc/examples/README
r1 r21 3 3 Creating an IPC Resource 4 4 ======================== 5 Libsipc was designed with the intention of minimizing backchannels between reader and 6 writer processes. Therefore, creation and destruction of IPC resources must be controlled 7 by applications outside of the communicating processes. 8 The shm_creator and mq_creator targets in this directory are examples of such helper applications; they simply call sipc_create(3). 9 Likewise, the shm_destroyer and mq_destroyer targets in this directory are also helper applications; they call sipc_destroy_resource(3). 5 Libsipc was designed with the intention of minimizing backchannels 6 between reader and writer processes. Therefore, creation and destruction 7 of IPC resources must be controlled by applications outside of the 8 communicating processes. The shm_creator and mq_creator targets in this 9 directory are examples of such helper applications; they simply call 10 sipc_open(3) with the SIPC_CREATOR flag. Likewise, the shm_destroyer 11 and mq_destroyer targets in this directory are also helper applications; 12 they call sipc_unlink(3). 10 13 11 14 Connecting 12 15 ========== 13 Connecting a process to an IPC resource created through libsipc involves calling sipc_init(3). 14 This function will return an IPC handle, which contains an internal buffer and state information relevant 15 to the communications process. A call to sipc_init(3) will allocate the IPC handle's data member. 16 After the handle has been initialized, a call to calling sipc_connect(3) will actually connect the handle to the IPC resource: 16 Connecting a process to an IPC resource created through libsipc involves 17 calling sipc_open(3) with the SIPC_SENDER or SIPC_RECEIVER flag. This 18 function will return an IPC handle, which contains an internal buffer 19 and state information relevant to the communications process. When the 20 SIPC_SENDER or SIPC_RECEIVER flag is specified, a call to sipc_open(3) 21 will allocate the IPC handle's data member and connect the handle to 22 the IPC resource: 17 23 18 ipc = sipc_init("ipc_key", size, ipc_type); 19 sipc_connect(ipc); 24 ipc = sipc_open("ipc_key", role, ipc_type, size); 20 25 21 Note the distinction between an IPC handle and an IPC resource: an IPC handle has an underlying IPC mechanism, 22 such as message queues or shared memory, as specified by the ipc_type parameter to sipc_init(3). 23 This underlying mechanism and its associated system resources shall be known as an "IPC resource." Processes communicating 24 using libsipc do not directly access IPC resources, rather they operate entirely on IPC handles. 26 where "role" is either SIPC_SENDER or SIPC_RECEIVER. Note the distinction 27 between an IPC handle and an IPC resource: an IPC handle has an underlying 28 IPC mechanism, such as message queues or shared memory, as specified by 29 the ipc_type parameter to sipc_open(3). This underlying mechanism and its 30 associated system resources shall be known as an "IPC resource." Processes 31 communicating using libsipc do not directly access IPC resources, rather 32 they operate entirely on IPC handles. 25 33 26 34 Sending Data 27 35 ============ 28 To send data to a connected IPC handle, the data must first be copied into the handle's internal data member. 29 To do this, a pointer to the handle's internal data member can be obtained via sipc_get_data_ptr(3): 36 To send data to a connected IPC handle, the data must first be copied 37 into the handle's internal data member. To do this, a pointer to the 38 handle's internal data member can be obtained via sipc_get_data_ptr(3): 30 39 31 40 char my_data[] = "Hello, world."; … … 36 45 Receiving Data 37 46 ============== 38 To receive data from a connected IPC handle, you must call sipc_recv_data(3), passing it a buffer39 for data storage:47 To receive data from a connected IPC handle, you must call 48 sipc_recv_data(3), passing it a buffer for data storage: 40 49 41 50 char *data = sipc_get_data_ptr(ipc); … … 44 53 sipc_shm_recv_done(ipc); /* Only needed when using shm */ 45 54 46 When using shared memory, a call to sipc_shm_recv_done(3) is needed to unblock the sending process,47 allowing it to send more data. 55 When using shared memory, a call to sipc_shm_recv_done(3) is needed to 56 unblock the sending process, allowing it to send more data. 48 57 49 58 Cleaning Up 50 59 =========== 51 Disconnecting an IPC handle from an IPC resource can be done with sipc_disconnect(3). 52 To eliminate backchannels between sending and receiving processes, freeing IPC resources acquired 53 during the communications process should be done in a separate process (see "Creating an IPC Resource"). 54 This destroyer process should call sipc_destroy_resource(3) to free IPC resources. Note that 55 the destroyer process should be run only when all communicating processes have disconnected from 56 their respective IPC resource. 60 Closing an IPC handle from an IPC resource can be done with sipc_close(3). 61 To eliminate backchannels between sending and receiving processes, 62 freeing IPC resources acquired during the communications process 63 should be done in a separate process (see "Creating an IPC Resource"). 64 This destroyer process should call sipc_unlink(3) to free IPC resources. 65 Note that the destroyer process should be run only when all communicating 66 processes have closed their respective IPC handles. 57 67 58 68 Handling Errors 59 69 =============== 60 When using libsipc, various errors may occur in the communication process. 61 The sipc_error(3) function has been provided to handle the displaying of error messages62 when such conditions arise:70 When using libsipc, various errors may occur in the communication process. 71 The sipc_error(3) function has been provided to handle the displaying 72 of error messages when such conditions arise: 63 73 64 74 if (sipc_send_data(ipc) < 0) 65 75 sipc_error(ipc, "Error sending data\n"); 66 67 trunk/libsipc/examples/mq_creator.c
r1 r21 33 33 int main() 34 34 { 35 if (sipc_create(SIPC_KEY, SIPC_SYSV_MQUEUES) < 0) { 35 sipc_t *sipc; 36 37 /* Do not need to specify a max size when creating an mqueue. */ 38 sipc = sipc_open(SIPC_KEY, SIPC_CREATOR, SIPC_SYSV_MQUEUES, 0); 39 if (sipc == NULL) { 36 40 fprintf(stderr, "Unable to create message queue.\n"); 37 41 return -1; 38 42 } 43 sipc_close(sipc); 39 44 40 45 return 0; trunk/libsipc/examples/mq_destroyer.c
r1 r21 36 36 int main() 37 37 { 38 sipc_ destroy_resource(SIPC_KEY, SIPC_SYSV_MQUEUES);38 sipc_unlink(SIPC_KEY, SIPC_SYSV_MQUEUES); 39 39 return 0; 40 40 } trunk/libsipc/examples/mq_reader.c
r1 r21 33 33 34 34 /* Amount of data to allocate inside the IPC handle */ 35 #define DATA_LEN 409635 #define DATA_LEN 8192 36 36 37 37 /* End of message marker which sender and receiver have agreed upon */ … … 47 47 48 48 /* Initialize the IPC handle */ 49 sipc_t *ipc = sipc_ init(SIPC_KEY, SIPC_SYSV_MQUEUES, DATA_LEN, 0);49 sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_RECEIVER, SIPC_SYSV_MQUEUES, DATA_LEN); 50 50 if (!ipc) { 51 51 fprintf(stderr, "Error: unable to create IPC resource\n"); … … 53 53 } 54 54 55 /* Connect to the IPC resource */56 if (sipc_connect(ipc)) {57 fprintf(stderr, "Error: unable to connect IPC resource\n");58 return 1;59 }60 61 55 /* Receive data from shared memory until the end of transmission 62 56 * marker has been received. */ … … 71 65 72 66 /* Cleanup */ 73 sipc_disconnect(ipc); 74 sipc_destroy_handle(ipc); 67 sipc_close(ipc); 75 68 return 0; 76 69 } … … 78 71 static int END_XMIT(char *data) 79 72 { 73 if (!data) 74 return 0; 75 80 76 return !strcmp(data, DATA_END); 81 77 } trunk/libsipc/examples/mq_sender.c
r1 r21 58 58 59 59 /* Initialize and connect IPC handle */ 60 ipc = sipc_ init(SIPC_KEY, SIPC_SYSV_MQUEUES, IPC_LEN, 1);60 ipc = sipc_open(SIPC_KEY, SIPC_SENDER, SIPC_SYSV_MQUEUES, IPC_LEN); 61 61 if (!ipc) { 62 62 fprintf(stderr, "Unable to create IPC resource\n"); 63 63 goto out; 64 64 } 65 if (sipc_connect(ipc)) {66 fprintf(stderr, "Unable to connect IPC resource\n");67 goto out;68 }69 65 70 66 /* Get pointer to the handle's internal buffer */ … … 85 81 while ((rbytes = fread(data, sizeof(char), READ_LEN, ifile)) > 0) { 86 82 /* Send this chunk of data */ 87 if (sipc_send_data(ipc ) < 0)83 if (sipc_send_data(ipc, IPC_LEN) < 0) 88 84 sipc_error(ipc, "Unable to send IPC message\n"); 89 85 … … 100 96 out: 101 97 /* Cleanup */ 102 sipc_ destroy_handle(ipc);98 sipc_close(ipc); 103 99 return retv; 104 100 } … … 117 113 bzero(data, IPC_LEN); 118 114 strncpy(data, DATA_END, IPC_LEN-1); 119 if (sipc_send_data(ipc ) < 0) {115 if (sipc_send_data(ipc, IPC_LEN) < 0) { 120 116 sipc_error(ipc, "Unable to send end of transmission marker\n"); 121 117 return -1; 122 118 } 123 119 120 printf("Send end of transmission marker: %s\n:", data); 124 121 return 0; 125 122 } trunk/libsipc/examples/shm_creator.c
r1 r21 33 33 * by both the sender and receiver. The file must already exist */ 34 34 #define SIPC_KEY "sipc_shm_test" 35 36 /* Amount of data to allocate inside the IPC handle */ 37 #define READ_LEN 4096 35 38 36 39 int main() 37 40 { 38 if (sipc_create(SIPC_KEY, SIPC_SYSV_SHM) < 0) { 41 sipc_t *sipc; 42 43 sipc = sipc_open(SIPC_KEY, SIPC_CREATOR, SIPC_SYSV_SHM, READ_LEN); 44 if (sipc == NULL) { 39 45 fprintf(stderr, "Unable to create message queue.\n"); 40 46 return -1; 41 47 } 48 sipc_close(sipc); 42 49 43 50 return 0; trunk/libsipc/examples/shm_destroyer.c
r1 r21 36 36 int main() 37 37 { 38 sipc_ destroy_resource(SIPC_KEY, SIPC_SYSV_SHM);38 sipc_unlink(SIPC_KEY, SIPC_SYSV_SHM); 39 39 return 0; 40 40 } trunk/libsipc/examples/shm_reader.c
r1 r21 47 47 48 48 /* Initialize the IPC handle */ 49 sipc_t *ipc = sipc_ init(SIPC_KEY, SIPC_SYSV_SHM, READ_LEN, 0);49 sipc_t *ipc = sipc_open(SIPC_KEY, SIPC_RECEIVER, SIPC_SYSV_SHM, READ_LEN); 50 50 if (!ipc) { 51 51 fprintf(stderr, "Could not create IPC resource\n"); … … 53 53 } 54 54 55 /* Connect shm */56 if (sipc_connect(ipc) < 0) {57 sipc_error(ipc, "Unable to connect to IPC resource\n");58 &
