Changeset 21

Show
Ignore:
Timestamp:
04/16/08 16:03:15 (8 months ago)
Author:
jtang
Message:

Merged binary-mq branch into trunk, with additional fixes by Ross.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libsipc.spec

    r20 r21  
    77Summary: Secure IPC Library 
    88Group: Development/Libraries 
    9 BuildRequires: jdk >= 1.5 
     9BuildRequires: jdk >= 1.5 glibc-devel gcc 
    1010Requires: jdk >= 1.5 
    1111 
     
    3838 
    3939%files 
     40%defattr(-,root,root,-) 
    4041%doc LICENSE bindings/java/README 
    4142%{_libdir}/libsipc.so.* 
    4243%{_includedir}/sipc/ 
    4344%{_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* 
    4549%{_datadir}/java/* 
    4650%{_datadir}/sipc/bindings/java/ 
  • trunk/libsipc/bindings/java/Makefile

    r20 r21  
    5454 
    5555%.o: %.c 
    56         $(CC) $(CFLAGS) $(INCLUDEDIRS) $(JNI_CFLAGS) -c -o $@ $< 
     56        $(CC) $(CFLAGS) $(INCLUDEDIRS) $(JNI_CFLAGS) -fPIC -c -o $@ $< 
    5757 
    5858install: 
  • trunk/libsipc/bindings/java/com/tresys/sipc/Sipc.java

    r1 r21  
    2525 * @author David Windsor <dwindsor@tresys.com> 
    2626 * @author Norman Patrick <npatrick@tresys.com> 
     27 * @author L. Ross Raszewski <lraszewski@tresys.com> 
    2728 */ 
    2829public abstract class Sipc { 
     
    4142        protected long handle; /* internal handle */ 
    4243 
     44        protected long size_handle; /* internal message size handle */ 
    4345        private java.nio.ByteBuffer dataPtr = null; /* internal pointer to data */ 
    4446 
     
    7779        public void Init(boolean write) { 
    7880                handle = libsipc.sipc_init(Key, GetIPCType(), DATA_LEN, write ? 1 : 0); 
     81                isConnected = handle!=0; 
     82                size_handle=libsipc.sipc_create_length(); 
    7983        } 
    8084 
     
    8690                if(handle != 0) { 
    8791                        libsipc.sipc_destroy_handle(handle); 
     92                        libsipc.sipc_destroy_length(size_handle); 
     93                        size_handle=0; 
    8894                        handle = 0; 
    8995                        dataPtr = null; 
     
    100106        public boolean Connect() throws ConnectionException { 
    101107                if(!isConnected) { 
    102                         if(libsipc.sipc_connect(handle) < 0) { 
    103                                 isConnected = false; 
    104108                                throw new ConnectionException("Attempt to connect failed"); 
    105                         } 
    106                         isConnected = true; 
    107                 } 
    108  
     109                } 
    109110                return isConnected; 
    110111        } 
     
    146147         */ 
    147148        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        } 
    151162        /** 
    152163         * Get a reference to the IPC handle's internal data buffer. 
     
    159170        public java.nio.ByteBuffer GetDataPtr() { 
    160171                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                 
    163175 
    164176                return dataPtr; 
     
    169181         * connected state for this to succeed. 
    170182         * 
     183         * @param len Length of the message to be sent 
    171184         * @return  0 on success, <0 on error. 
    172185         * @see     #Connect() Connect 
    173186         */ 
     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         */ 
    174198        public int SendData() { 
    175                 return libsipc.sipc_send_data(handle); 
     199                return SendData(dataPtr.limit()); 
    176200        } 
    177201} 
  • trunk/libsipc/bindings/java/com/tresys/sipc/libsipc.java

    r1 r21  
     1/* This file has been hand-modified -- do not regenerate */ 
     2 
    13/* ---------------------------------------------------------------------------- 
    24 * This file was automatically generated by SWIG (http://www.swig.org). 
     
    911package com.tresys.sipc; 
    1012 
    11 final class libsipc { 
     13class libsipc { 
     14 
    1215        static { 
    1316                System.loadLibrary("sipcwrapper"); 
    1417        } 
    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); 
    4647} 
  • trunk/libsipc/bindings/java/sipc_wrap.c

    r20 r21  
     1/* This file has been modified by hand. Do not regenerate it. */ 
     2 
     3 
    14/* ---------------------------------------------------------------------------- 
    25 * This file was automatically generated by SWIG (http://www.swig.org). 
     
    1417 * ----------------------------------------------------------------------------- */ 
    1518 
    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 */ 
    1721#ifndef SWIGTEMPLATEDISAMBIGUATOR 
    1822# if defined(__SUNPRO_CC) 
     
    172176#define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, msg); return nullreturn; } else 
    173177 
    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 */ 
     183struct 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 
    178206 
    179207#ifdef __cplusplus 
     
    181209#endif 
    182210 
    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) { 
     211JNIEXPORT jlong JNICALL Java_com_tresys_sipc_libsipc_sipc_1init(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2, jlong jarg3, jint jarg4) { 
    185212  jlong jresult = 0 ; 
    186213  char *arg1 = (char *) 0 ; 
     
    193220  (void)jcls; 
    194221  arg1 = 0; 
    195   if (key) { 
    196     arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0); 
     222  if (jarg1) { 
     223    arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 
    197224    if (!arg1) return 0; 
    198225  } 
    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); 
    203230  *(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 
     236JNIEXPORT 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 
     246JNIEXPORT void JNICALL Java_com_tresys_sipc_libsipc_sipc_1destroy_1resource(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2) { 
    220247  char *arg1 = (char *) 0 ; 
    221248  int arg2 ; 
     
    224251  (void)jcls; 
    225252  arg1 = 0; 
    226   if (key) { 
    227     arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0); 
     253  if (jarg1) { 
     254    arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 
    228255    if (!arg1) return ; 
    229256  } 
    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 
     263JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1create(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2) { 
    237264  jint jresult = 0 ; 
    238265  char *arg1 = (char *) 0 ; 
     
    243270  (void)jcls; 
    244271  arg1 = 0; 
    245   if (key) { 
    246     arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, key, 0); 
     272  if (jarg1) { 
     273    arg1 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0); 
    247274    if (!arg1) return 0; 
    248275  } 
    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; 
    251279  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   */ 
     287JNIEXPORT 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;  
    265295  result = (int)sipc_connect(arg1); 
    266296  jresult = (jint)result;  
    267297  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 
     305JNIEXPORT 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 
     315JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1send_1data(JNIEnv *jenv, jclass jcls, jlong jarg1, jint jarg2) { 
    282316  jint jresult = 0 ; 
    283317  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); 
    290326  jresult = (jint)result;  
    291327  return jresult; 
     
    293329 
    294330 
    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   jobject 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
    298334  sipc_t *arg1 = (sipc_t *) 0 ; 
    299335  char *arg2 = NULL; 
    300336  int arg3 = 0; 
    301337  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; 
    306342  result = (int)sipc_recv_data(arg1, &arg2, &arg3); 
    307    
     343 
     344  *(*(int **)&size_handle)=arg3; 
     345 
    308346  if (result == 0) 
    309347  { 
    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 
    311360  } 
    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 
     372JNIEXPORT 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 
     385JNIEXPORT 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 ; 
    332397  } 
    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 
     403JNIEXPORT jint JNICALL Java_com_tresys_sipc_libsipc_sipc_1shm_1recv_1done(JNIEnv *jenv, jclass jcls, jlong jarg1) { 
    338404  jint jresult = 0 ; 
    339405  sipc_t *arg1 = (sipc_t *) 0 ; 
     
    342408  (void)jenv; 
    343409  (void)jcls; 
    344   arg1 = *(sipc_t **)&handle;  
     410  arg1 = *(sipc_t **)&jarg1;  
    345411  result = (int)sipc_shm_recv_done(arg1); 
    346412  jresult = (jint)result;  
     
    349415 
    350416 
     417  /* Methods to allocate and unbox the int pointer used for returning  
     418     message size */ 
     419static int *sipc_create_length() 
     420{ 
     421  return (int *)malloc(sizeof(int)); 
     422} 
     423 
     424static int sipc_get_length(int *arg) 
     425{ 
     426  if (arg) return *arg; else return -1; 
     427} 
     428 
     429static void sipc_destroy_length(int *arg) 
     430{  
     431  if (arg) free(arg); 
     432} 
     433 
     434 
     435JNIEXPORT 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 
     446JNIEXPORT 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 
     459JNIEXPORT 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 
    351469#ifdef __cplusplus 
    352470} 
  • trunk/libsipc/examples/Makefile

    r20 r21  
    3333                (cd $$subdir && $(MAKE) $@) || exit 1;\ 
    3434        done 
     35 
     36mq_bin_reader: mq_bin_reader.o 
     37        $(CC) $(LDFLAGS) -lsipc $(LIBDIRS) -o $@ $^ 
     38 
     39mq_bin_sender: mq_bin_sender.o 
     40        $(CC) $(LDFLAGS) -lsipc $(LIBDIRS) -o $@ $^ 
    3541 
    3642mq_creator: mq_creator.o 
  • trunk/libsipc/examples/README

    r1 r21  
    33Creating an IPC Resource 
    44======================== 
    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). 
     5Libsipc was designed with the intention of minimizing backchannels 
     6between reader and writer processes. Therefore, creation and destruction 
     7of IPC resources must be controlled by applications outside of the 
     8communicating processes.  The shm_creator and mq_creator targets in this 
     9directory are examples of such helper applications; they simply call 
     10sipc_open(3) with the SIPC_CREATOR flag.  Likewise, the shm_destroyer 
     11and mq_destroyer targets in this directory are also helper applications; 
     12they call sipc_unlink(3). 
    1013 
    1114Connecting 
    1215========== 
    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: 
     16Connecting a process to an IPC resource created through libsipc involves 
     17calling sipc_open(3) with the SIPC_SENDER or SIPC_RECEIVER flag.  This 
     18function will return an IPC handle, which contains an internal buffer 
     19and state information relevant to the communications process. When the 
     20SIPC_SENDER or SIPC_RECEIVER flag is specified, a call to sipc_open(3) 
     21will allocate the IPC handle's data member and connect the handle to 
     22the IPC resource: 
    1723 
    18         ipc = sipc_init("ipc_key", size, ipc_type);      
    19         sipc_connect(ipc); 
     24        ipc = sipc_open("ipc_key", role, ipc_type, size);        
    2025 
    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. 
     26where "role" is either SIPC_SENDER or SIPC_RECEIVER.  Note the distinction 
     27between an IPC handle and an IPC resource: an IPC handle has an underlying 
     28IPC mechanism, such as message queues or shared memory, as specified by 
     29the ipc_type parameter to sipc_open(3).  This underlying mechanism and its 
     30associated system resources shall be known as an "IPC resource." Processes 
     31communicating using libsipc do not directly access IPC resources, rather 
     32they operate entirely on IPC handles. 
    2533 
    2634Sending Data 
    2735============ 
    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): 
     36To send data to a connected IPC handle, the data must first be copied 
     37into the handle's internal data member.  To do this, a pointer to the 
     38handle's internal data member can be obtained via sipc_get_data_ptr(3): 
    3039 
    3140        char my_data[] = "Hello, world."; 
     
    3645Receiving Data 
    3746============== 
    38 To receive data from a connected IPC handle, you must call sipc_recv_data(3), passing it a buffer  
    39 for data storage: 
     47To receive data from a connected IPC handle, you must call 
     48sipc_recv_data(3), passing it a buffer for data storage: 
    4049 
    4150        char *data = sipc_get_data_ptr(ipc); 
     
    4453        sipc_shm_recv_done(ipc);  /* Only needed when using shm */ 
    4554 
    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.  
     55When using shared memory, a call to sipc_shm_recv_done(3) is needed to 
     56unblock the sending process, allowing it to send more data. 
    4857 
    4958Cleaning Up 
    5059=========== 
    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. 
     60Closing an IPC handle from an IPC resource can be done with sipc_close(3). 
     61To eliminate backchannels between sending and receiving processes, 
     62freeing IPC resources acquired during the communications process 
     63should be done in a separate process (see "Creating an IPC Resource"). 
     64This destroyer process should call sipc_unlink(3) to free IPC resources. 
     65Note that the destroyer process should be run only when all communicating 
     66processes have closed their respective IPC handles. 
    5767 
    5868Handling Errors 
    5969=============== 
    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 messages  
    62 when such conditions arise: 
     70When using libsipc, various errors may occur in the communication process. 
     71The sipc_error(3) function has been provided to handle the displaying 
     72of error messages when such conditions arise: 
    6373 
    6474        if (sipc_send_data(ipc) < 0)  
    6575                sipc_error(ipc, "Error sending data\n");         
    66  
    67  
  • trunk/libsipc/examples/mq_creator.c

    r1 r21  
    3333int main() 
    3434{ 
    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) { 
    3640                fprintf(stderr, "Unable to create message queue.\n"); 
    3741                return -1; 
    3842        } 
     43        sipc_close(sipc); 
    3944 
    4045        return 0; 
  • trunk/libsipc/examples/mq_destroyer.c

    r1 r21  
    3636int main() 
    3737{ 
    38         sipc_destroy_resource(SIPC_KEY, SIPC_SYSV_MQUEUES); 
     38        sipc_unlink(SIPC_KEY, SIPC_SYSV_MQUEUES); 
    3939        return 0; 
    4040} 
  • trunk/libsipc/examples/mq_reader.c

    r1 r21  
    3333 
    3434/* Amount of data to allocate inside the IPC handle */    
    35 #define DATA_LEN 4096           
     35#define DATA_LEN 8192 
    3636 
    3737/* End of message marker which sender and receiver have agreed upon */ 
     
    4747 
    4848        /* 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); 
    5050        if (!ipc) { 
    5151                fprintf(stderr, "Error: unable to create IPC resource\n"); 
     
    5353        } 
    5454 
    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          
    6155        /* Receive data from shared memory until the end of transmission 
    6256         * marker has been received. */ 
     
    7165 
    7266        /* Cleanup */ 
    73         sipc_disconnect(ipc); 
    74         sipc_destroy_handle(ipc); 
     67        sipc_close(ipc); 
    7568        return 0; 
    7669} 
     
    7871static int END_XMIT(char *data) 
    7972{ 
     73        if (!data) 
     74                return 0; 
     75 
    8076        return !strcmp(data, DATA_END); 
    8177} 
  • trunk/libsipc/examples/mq_sender.c

    r1 r21  
    5858 
    5959        /* 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); 
    6161        if (!ipc) { 
    6262                fprintf(stderr, "Unable to create IPC resource\n"); 
    6363                goto out; 
    6464        } 
    65         if (sipc_connect(ipc)) { 
    66                 fprintf(stderr, "Unable to connect IPC resource\n"); 
    67                 goto out; 
    68         }        
    6965 
    7066        /* Get pointer to the handle's internal buffer */ 
     
    8581        while ((rbytes = fread(data, sizeof(char), READ_LEN, ifile)) > 0) { 
    8682                  /* Send this chunk of data */ 
    87                   if (sipc_send_data(ipc) < 0)  
     83                  if (sipc_send_data(ipc, IPC_LEN) < 0)  
    8884                          sipc_error(ipc, "Unable to send IPC message\n");                 
    8985 
     
    10096out: 
    10197        /* Cleanup */ 
    102         sipc_destroy_handle(ipc); 
     98        sipc_close(ipc); 
    10399        return retv; 
    104100} 
     
    117113        bzero(data, IPC_LEN); 
    118114        strncpy(data, DATA_END, IPC_LEN-1); 
    119         if (sipc_send_data(ipc) < 0) { 
     115        if (sipc_send_data(ipc, IPC_LEN) < 0) { 
    120116                sipc_error(ipc, "Unable to send end of transmission marker\n"); 
    121117                return -1; 
    122118        } 
    123119 
     120        printf("Send end of transmission marker: %s\n:", data); 
    124121        return 0; 
    125122} 
  • trunk/libsipc/examples/shm_creator.c

    r1 r21  
    3333 * by both the sender and receiver. The file must already exist */ 
    3434#define SIPC_KEY "sipc_shm_test"   
     35 
     36/* Amount of data to allocate inside the IPC handle */ 
     37#define READ_LEN 4096         
    3538                                            
    3639int main() 
    3740{ 
    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) { 
    3945                fprintf(stderr, "Unable to create message queue.\n"); 
    4046                return -1; 
    4147        } 
     48        sipc_close(sipc); 
    4249 
    4350        return 0; 
  • trunk/libsipc/examples/shm_destroyer.c

    r1 r21  
    3636int main() 
    3737{ 
    38         sipc_destroy_resource(SIPC_KEY, SIPC_SYSV_SHM); 
     38        sipc_unlink(SIPC_KEY, SIPC_SYSV_SHM); 
    3939        return 0; 
    4040} 
  • trunk/libsipc/examples/shm_reader.c

    r1 r21  
    4747 
    4848        /* 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); 
    5050        if (!ipc) { 
    5151                fprintf(stderr, "Could not create IPC resource\n"); 
     
    5353        } 
    5454 
    55         /* Connect shm */ 
    56         if (sipc_connect(ipc) < 0) { 
    57                 sipc_error(ipc, "Unable to connect to IPC resource\n"); 
    58       &