Changeset 49

Show
Ignore:
Timestamp:
07/01/08 14:53:20 (2 months ago)
Author:
jtang
Message:

[Untested] New Java bindings to match current libsipc code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libsipc/bindings/java/Makefile

    r30 r49  
    55JAVAC ?= $(JAVA_HOME)/bin/javac 
    66JAR ?= $(JAVA_HOME)/bin/jar 
    7 JAVADOC ?= %(JAVA_HOME)/bin/javadoc 
     7JAVADOC ?= $(JAVA_HOME)/bin/javadoc 
    88 
    99TOP_SRCDIR ?= ../.. 
     
    1414SRC = com/tresys/sipc/BadKeyException.java \ 
    1515        com/tresys/sipc/ConnectionException.java \ 
    16         com/tresys/sipc/libsipc.java \ 
    1716        com/tresys/sipc/Sipc.java \ 
    1817        com/tresys/sipc/SipcMqueue.java \ 
    1918        com/tresys/sipc/SipcShm.java 
     19SWIG_SRC = libsipc_t.java \ 
     20        sipcwrapperConstants.java \ 
     21        sipcwrapper.java \ 
     22        sipcwrapperJNI.java \ 
     23        SWIGTYPE_p_sipc_t.java 
    2024CLASSES = $(patsubst %.java,%.class,$(SRC)) 
     25SWIG_CLASSES = $(patsubst %.java,com/tresys/sipc/%.class,$(SWIG_SRC)) 
    2126 
    2227SOVERSION = 1 
     
    3641SONAME = $(TARGET).$(SOVERSION) 
    3742LIBSO = $(TARGET).$(SOVERSION).$(RELEASE) 
     43SWIG_WRAP = sipc_wrap.c 
    3844OBJS = sipc_wrap.o 
    3945 
    40 all: $(JARTARGET) $(LIBSO
     46all: $(LIBSO) $(JARTARGET
    4147 
    4248install: $(JARTARGET) $(LIBSO) 
     
    5460        cd $(DESTDIR)/$(LIBDIR) && ln -sf $(LIBSO) $(TARGET).$(SOVERSION) 
    5561 
    56 $(JARTARGET): $(CLASSES) 
    57         $(JAR) cf $@ $(CLASSES) 
     62$(JARTARGET): $(SWIG_CLASSES) $(CLASSES) 
     63        $(JAR) cf $@ $^ 
    5864 
    5965%.class: %.java 
    60         $(JAVAC) $(JFLAGS) $< 
     66        $(JAVAC) $(AM_JFLAGS) $(JFLAGS) $< 
     67 
     68$(firstword $(SWIG_CLASSES)): $(SWIG_SRC) 
     69        $(JAVAC) $(AM_JFLAGS) $(JFLAGS) -d . $^ 
     70 
     71$(wordlist 2,$(words $(SWIG_CLASSES)), $(SWIG_CLASSES)): $(firstword $(SWIG_CLASSES)) 
     72 
     73$(CLASSES): $(SWIG_CLASSES) 
    6174 
    6275$(LIBSO): $(OBJS) 
     
    6477        ln -sf $@ $(TARGET) 
    6578 
     79$(SWIG_SRC) $(SWIG_WRAP): sipc.i 
     80        swig -java -package com.tresys.sipc -o $(SWIG_WRAP) $(INCLUDEDIRS) $< 
     81 
    6682%.o: %.c 
    6783        $(CC) $(AM_CFLAGS) $(CFLAGS) $(INCLUDEDIRS) $(JNI_CFLAGS) -fPIC -c -o $@ $< 
    6884 
    69 javadoc: 
    70         $(JAVADOC) -d javadoc com.tresys.sipc 
     85javadoc: $(SWIG_CLASSES) 
     86        $(JAVADOC) -d javadoc $(SRC) $(SWIG_SRC) 
    7187 
    7288clean: 
    73         -rm -f $(CLASSES) $(JARTARGET) $(TARGET) $(LIBSO) $(OBJS) 
     89        -rm -f $(SWIG_WRAP) $(SWIG_SRC) $(CLASSES) $(SWIG_CLASSES) $(JARTARGET) $(TARGET) $(LIBSO) $(OBJS) 
    7490 
    7591.PHONY: all install clean javadoc 
  • trunk/libsipc/bindings/java/com/tresys/sipc/Sipc.java

    r24 r49  
    11/* 
    2  * Copyright (C) 2006, 2007 Tresys Technology, LLC 
     2 * Copyright (C) 2006 - 2008 Tresys Technology, LLC 
    33 * Developed Under US JFCOM Sponsorship 
    44 * 
     
    1919 
    2020package com.tresys.sipc; 
     21import com.tresys.sipc.libsipc_t; 
     22import com.tresys.sipc.sipcwrapper; 
    2123 
    2224/** 
     
    2830 */ 
    2931public abstract class Sipc { 
     32        protected libsipc_t handle; /* internal handle */ 
     33        protected boolean isConnected = false; /* internal state flag */ 
     34 
    3035        /** shared IPC key */ 
    31         public final String Key; 
    32  
    33         /** length of internal data buffer */ 
    34         public final long DATA_LEN; 
    35  
    36         /** length of system IPC bufer */ 
    37         public final long IPC_LEN; 
    38  
    39         /** determine which backend this uses */ 
    40         public abstract int GetIPCType(); 
    41  
    42         protected long handle; /* internal handle */ 
    43  
    44         protected long size_handle; /* internal message size handle */ 
    45         private java.nio.ByteBuffer dataPtr = null; /* internal pointer to data */ 
    46  
    47         private boolean isConnected = false; /* internal state flag */ 
     36        private final String key; 
     37        private int ipc_type; 
    4838 
    4939        /** 
    50          * Clean up a Sipc handle's resources.  
     40         * Clean up a Sipc handle's resources. 
    5141         */ 
    5242        protected void finalize() { 
    53                 this.Disconnect(); 
    54                 this.DestroyHandle(); 
     43                this.Close(); 
    5544        } 
    5645 
    5746        /** 
    58          * Create a Sipc handle with specified key, internal buffer size and 
    59          * system IPC buffer size.  
     47         * Create a Sipc handle with specified key, role, and system 
     48         * IPC buffer size. 
    6049         * 
    61          * @param key  Path name representing an IPC key 
    62          * @param dataLength  Size in bytes of the IPC handle's internal data buffer 
    63          * @param ipcLength  Size in bytes of the system's IPC data buffer.  This should not 
    64          *                   exceed any known system limits.   
     50         * @param key  Path name representing an IPC key. 
     51         * @param role One of SIPC_CREATOR, SIPC_SENDER, or SIPC_RECEIVER. 
     52         * @param ipc_type One of SIPC_SYSV_SHM or SIPC_SYSV_MQUEUES. 
     53         * @param ipcLength  Size in bytes of the system's IPC data buffer. 
     54         *                   This should not exceed any known system limits. 
    6555         */ 
    66         public Sipc(String key, long dataLength, long ipcLength)
    67                 Key = key
    68                 handle = 0
    69                 DATA_LEN = dataLength
    70                 IPC_LEN = ipcLength
     56        protected Sipc(String key, int role, int ipc_type, long ipcLength) throws Exception
     57                handle = new libsipc_t(key, role, ipc_type, ipcLength)
     58                this.key = key
     59                this.ipc_type = ipc_type
     60                isConnected = true
    7161        } 
    7262 
    7363        /** 
    74          * Initialize an IPC handle. This method needs to be called before Connect. 
    75          * 
    76          * @param write  Flag indicating whether this instance is a writer or a reader 
    77          * @see #Connect() Connect 
     64         * Close the IPC handle, if it is still open.  Then destroy 
     65         * the system IPC resource. 
    7866         */ 
    79         public void Init(boolean write) { 
    80                 handle = libsipc.sipc_init(Key, GetIPCType(), DATA_LEN, write ? 1 : 0); 
    81                 isConnected = handle!=0; 
    82                 size_handle=libsipc.sipc_create_length(); 
    83         } 
    84  
    85         /** 
    86          * Destroy an IPC handle. This function only destroys the 
    87          * resources associated with the handle itself; call 
    88          * Disconnect() to first disconnect. 
    89          */ 
    90         private void DestroyHandle() { 
    91                 if(handle != 0) { 
    92                         libsipc.sipc_destroy_handle(handle); 
    93                         libsipc.sipc_destroy_length(size_handle); 
    94                         size_handle=0; 
    95                         handle = 0; 
    96                         dataPtr = null; 
    97                 } 
    98         } 
    99  
    100         /** 
    101          * Connect an IPC handle to its backend. 
    102          * Init() must be called prior to calling this.  
    103          * 
    104          * @throws ConnectionException if there is an error connecting 
    105          * @see #Init(boolean write) Init 
    106          */ 
    107         public boolean Connect() throws ConnectionException { 
    108                 if(!isConnected) { 
    109                                 throw new ConnectionException("Attempt to connect failed"); 
    110                 } 
    111                 return isConnected; 
     67        public void DestroyHandle() { 
     68                this.Close(); 
     69                sipcwrapper.sipc_unlink(key, ipc_type); 
    11270        } 
    11371 
     
    11674         * 
    11775         * @return  true if the IPC handle is connected to its backend, 
    118          *                    false otherwise 
     76         *                    false otherwise 
    11977         */ 
    12078        public boolean IsConnected() { 
     
    12381 
    12482        /** 
    125          * Disconnect an IPC handle from its backend. This function has no  
     83         * Disconnect an IPC handle from its backend. This function has no 
    12684         * effect if the handle is already in a disconnected state. 
    12785         */ 
    128         public void Disconnect() { 
    129                 if(isConnected) { 
     86        public void Close() { 
     87                if (isConnected) { 
     88                        handle.close(); 
     89                        handle = null; 
    13090                        isConnected = false; 
    131                         libsipc.sipc_disconnect(handle); 
    132                         dataPtr = null; 
    13391                } 
    13492        } 
    13593 
    136         /**  
    137          * Signal the sending application that a transmission has been successfully  
     94        /** 
     95         * Signal the sending application that a transmission has been successfully 
    13896         * received and handled.  This operation is not implemented by all backends. 
    13997         */ 
    140         public void SipcRecvDone() {} 
     98        public void SipcRecvDone() throws Exception {} 
    14199 
    142100        /** 
     
    144102         * be in a connected state for this operation to succeed. 
    145103         * 
    146          * @return  A reference to the internal data pointer holding the  
    147          *         received data, null on error. 
     104         * @return  A reference to the internal data pointer holding the 
     105         *          received data, null on error. 
    148106         */ 
    149         public java.nio.ByteBuffer ReadData() { 
    150                 return libsipc.sipc_recv_data(handle, GetIPCType(), size_handle); 
     107        public byte[] ReadData() throws Exception { 
     108                if (isConnected) { 
     109                        return handle.recv_data(); 
     110                } 
     111                else { 
     112                        throw new ConnectionException("Handle was closed."); 
     113                } 
    151114        } 
    152115 
    153116        /** 
    154          * Returns the size of the last successful ReadData() operation. 
    155          * This should be identical to the capacity of the buffer returned by 
    156          * ReadData().  Return value is undefined if ReadData has not been called. 
    157          *  
    158          * @return The size of the buffer returned by the most recent call to ReadData() 
     117         * Get a reference to the IPC handle's internal data buffer. 
     118         * This buffer's contents will sent data to an IPC channel 
     119         * upon calls to SendData().  The IPC handle must be in a 
     120         * connected state for this to succeed. 
     121         * 
     122         * @return  A reference to the IPC handle's internal data buffer. 
    159123         */ 
    160         public int GetReadSize() { 
    161                 return libsipc.sipc_get_length(size_handle); 
    162         } 
    163         /** 
    164          * Get a reference to the IPC handle's internal data buffer. 
    165          * This buffer may be used to send data to or receive data from 
    166          * an IPC channel.  The IPC handle must be initialized first. 
    167          * 
    168          * @return  A reference to the IPC handle's internal data buffer 
    169          * @see     #Init(boolean write) Init 
    170          */ 
    171         public java.nio.ByteBuffer GetDataPtr() { 
    172                         dataPtr = libsipc.sipc_get_data_ptr(handle); 
    173                  
    174  
    175                 return dataPtr; 
     124        public java.nio.ByteBuffer GetDataPtr() throws Exception { 
     125                if (isConnected) { 
     126                        return handle.get_data_ptr(); 
     127                } 
     128                else { 
     129                        throw new ConnectionException("Handle was closed."); 
     130                } 
    176131        } 
    177132 
    178133        /** 
    179          * Send data to a communications channel.  The IPC handle must be in a  
     134         * Send data to a communications channel.  The data came from 
     135         * the most recent call to GetDataPtr().  The IPC handle must 
     136         * be in a connected state for this to succeed. 
     137         * 
     138         * @param len Length of the message to be sent. 
     139         */ 
     140        public void SendData(int len) throws Exception { 
     141                if (isConnected) { 
     142                        java.nio.ByteBuffer b = GetDataPtr(); 
     143                        if (len > b.limit()) { 
     144                                throw new IndexOutOfBoundsException("Len set to " + len + ", but buffer's limit was " + b.limit()); 
     145                        } 
     146                        handle.send_data(len); 
     147                } 
     148                else { 
     149                        throw new ConnectionException("Handle was closed."); 
     150                } 
     151        } 
     152 
     153        /** 
     154         * Send data to a communications channel.  The data came from 
     155         * the most recent call to GetDataPtr(); the entire buffer's 
     156         * contents will be sent.  The IPC handle must be in a 
    180157         * connected state for this to succeed. 
    181          * 
    182          * @param len Length of the message to be sent 
    183          * @return  0 on success, <0 on error. 
    184          * @see     #Connect() Connect 
    185158         */ 
    186         public int SendData(int len) { 
    187                 return libsipc.sipc_send_data(handle, len); 
    188         } 
    189          
    190         /** 
    191          * Send data to the communications channel. The amount of data to  
    192          * send is determined by the current limit of the data buffer. 
    193          * 
    194          * @return 0 on success, <0 on error 
    195          * @see #SendData(int) SendData(int) 
    196          */ 
    197         public int SendData() { 
    198                 return SendData(dataPtr.limit()); 
     159        public void SendData() throws Exception { 
     160                if (isConnected) { 
     161                        java.nio.ByteBuffer b = GetDataPtr(); 
     162                        handle.send_data(b.limit()); 
     163                } 
     164                else { 
     165                        throw new ConnectionException("Handle was closed."); 
     166                } 
    199167        } 
    200168} 
  • trunk/libsipc/bindings/java/com/tresys/sipc/SipcMqueue.java

    r1 r49  
    11/* 
    2  * Copyright (C) 2006, 2007 Tresys Technology, LLC 
     2 * Copyright (C) 2006 - 2008 Tresys Technology, LLC 
    33 * Developed Under US JFCOM Sponsorship 
    44 * 
     
    2020package com.tresys.sipc; 
    2121 
     22import com.tresys.sipc.sipcwrapperConstants.*; 
     23 
    2224/** 
    2325 * SYSV message queue backend implementation. 
    24  *  
     26 * 
    2527 * @author David Windsor <dwindsor@tresys.com> 
    2628 * @author Norman Patrick <npatrick@tresys.com> 
     
    3234         * queue backed communications channel. 
    3335         * 
    34          * @param key  Pathname of IPC key, as understood by ftok(1) 
    35          * @param dataLength  Size in bytes of handle's internal data buffer 
    36          * @param ipcLength  Size in bytes of the system's SYSV message queue buffers. 
    37          *                   Fragmentation will occur if the handle's internal data buffer 
    38          *                   is larger than the system's data buffer. 
     36         * @param key  Pathname of IPC key, as understood by ftok(1). 
     37         * @param role One of SIPC_CREATOR, SIPC_SENDER, or SIPC_RECEIVER. 
     38         * @param ipcLength  Size in bytes of the system's IPC data buffer. 
     39         *                   This should not exceed any known system limits. 
    3940         */ 
    40         public SipcMqueue(String key, long dataLength, long ipcLength)
    41                 super(key, dataLength, ipcLength); 
     41        public SipcMqueue(String key, int role, long ipcLength) throws Exception
     42                super(key, role, sipcwrapperConstants.SIPC_SYSV_MQUEUES, ipcLength); 
    4243        } 
    43  
    44         /** 
    45          * Create a SYSV message queue.   
    46          * <p> 
    47          * This function performs privileged operations and should not be  
    48          * called by normal users.  Creator processes are the only intended 
    49          * users of this method.  
    50          * 
    51          * @throws BadKeyException if the key is not valid. The key  
    52          *         must be a valid pathname, as understood by ftok(1).   
    53          * @see #Destroy(String key) Destroy 
    54          */ 
    55         public static void Create(String key) throws BadKeyException { 
    56                 int id = libsipc.sipc_create(key, libsipc.SIPC_SYSV_MQUEUES); 
    57  
    58                 if(id < 0) { 
    59                         throw new BadKeyException("Unable to create message queue."); 
    60                 } 
    61         } 
    62  
    63         /** 
    64          * Destroy a SYSV message queue. 
    65          * <p> 
    66          * This function performs privileged operations and should not be 
    67          * called by normal users.  Destroyer processes are the only intended  
    68          * users of this function. 
    69          * 
    70          * @see #Create(String key) Create 
    71          */ 
    72         public static void Destroy(String key) { 
    73                 libsipc.sipc_destroy_resource(key, libsipc.SIPC_SYSV_MQUEUES); 
    74         } 
    75  
    76         /** 
    77          * Determine which type of backend this is. 
    78          * 
    79          * @return An integer representing SYSV message queues 
    80          */ 
    81         public int GetIPCType() { 
    82                 return libsipc.SIPC_SYSV_MQUEUES; 
    83         } 
    84  
    8544} 
  • trunk/libsipc/bindings/java/com/tresys/sipc/SipcShm.java

    r1 r49  
    11/* 
    2  * Copyright (C) 2006, 2007 Tresys Technology, LLC 
     2 * Copyright (C) 2006 - 2008 Tresys Technology, LLC 
    33 * Developed Under US JFCOM Sponsorship 
    44 * 
     
    2020package com.tresys.sipc; 
    2121 
     22import com.tresys.sipc.sipcwrapperConstants.*; 
     23 
    2224/** 
    2325 * SYSV shared memory backend implementation. 
     
    3234         * memory backed communications channel. 
    3335         * 
    34          * @param key  Pathname of IPC key, as understood by ftok(1) 
    35          * @param dataLength  Size in bytes of handle's internal data buffer 
    36          * @param ipcLength  Size in bytes of the system's SYSV message queue buffers. 
    37          *                   Fragmentation will occur if the handle's internal data buffer 
    38          *                   is larger than the system's data buffer. 
     36         * @param key  Pathname of IPC key, as understood by ftok(1). 
     37         * @param role One of SIPC_CREATOR, SIPC_SENDER, or SIPC_RECEIVER. 
     38         * @param ipcLength  Size in bytes of the system's IPC data buffer. 
     39         *                   This should not exceed any known system limits. 
    3940         */ 
    40         public SipcShm(String key, long dataLength, long ipcLength)
    41                 super(key, dataLength, ipcLength); 
     41        public SipcShm(String key, int role, long ipcLength) throws Exception
     42                super(key, role, sipcwrapperConstants.SIPC_SYSV_SHM, ipcLength); 
    4243        } 
    4344 
    4445        /** 
    45          * Create a SYSV shared memory segment. 
    46          * <p> 
    47          * This function performs privileged operations and should not be 
    48          * called by normal users.  Creator processes are the only intended 
    49          * users of this function. 
    50          * 
    51          * @see #Destroy(String key) Destroy 
     46         * Signal the sending application that a transmission has been 
     47         * successfully received and handled. 
    5248         */ 
    53         public static void Create(String key) throws BadKeyException { 
    54                 int id = libsipc.sipc_create(key, libsipc.SIPC_SYSV_SHM); 
    55  
    56                 if(id < 0) { 
    57                         throw new BadKeyException("Unable to create shared memory segment."); 
     49        public void SipcRecvDone() throws Exception { 
     50                if (isConnected) { 
     51                        handle.shm_recv_done(); 
     52                } 
     53                else { 
     54                        throw new ConnectionException("Handle was closed."); 
    5855                } 
    5956        } 
    60  
    61         /** 
    62          * Destroy a SYSV shared memory segment. 
    63          * <p> 
    64          * This function performs privileged operations and should not be 
    65          * called by normal users.  Destroyer processes are the only intended 
    66          * users of this function. 
    67          * 
    68          * @see #Create(String key) Create 
    69          */ 
    70         public static void Destroy(String key) { 
    71                 libsipc.sipc_destroy_resource(key, libsipc.SIPC_SYSV_SHM); 
    72         } 
    73  
    74         /** 
    75          * Determine which type of backend this is. 
    76          * 
    77          * @return  An integer representing SYSV shared memory 
    78          */ 
    79         public int GetIPCType() { 
    80                 return libsipc.SIPC_SYSV_SHM; 
    81         } 
    82  
    83         /** 
    84          * Signal the sender that we are finished processing 
    85          * the data contained in the shared memory segment.  
    86          * This will unblock the sender, potentially clobbering  
    87          * the contents of the shared memory segment.   
    88          */ 
    89         public void SipcRecvDone() { 
    90                 libsipc.sipc_shm_recv_done(this.handle); 
    91         } 
    9257} 
  • trunk/libsipc/bindings/java/examples/Makefile

    r3 r49  
    1 FILES=$(wildcard *.java) 
    2 CLASSES=$(FILES:.java=.class) 
    3 CLASSPATH=.:.. 
     1JAVA_HOME ?= /usr/java/default 
     2JAVAC ?= $(JAVA_HOME)/bin/javac 
    43 
    5 # uncomment this region for gcj 
    6 # JFLAGS=-cp $(CLASSPATH) -1.5 
    7 # ifeq ($(DEBUG),1) 
    8 #       JFLAGS+=-warn:+null 
    9 # endif 
     4AM_JFLAGS = -cp ../libsipc-1.0.jar 
    105 
    11 # use this region for sun's java 
    12 JFLAGS=-cp $(CLASSPATH) -source 1.5 
     6SRC = MQ_Binary_Reader.java \ 
     7        MQ_Creator.java \ 
     8        MQ_Destroyer.java \ 
     9        MQ_Reader.java \ 
     10        MQ_Sender.java \ 
     11        SHM_Creator.java \ 
     12        SHM_Destroyer.java \ 
     13        SHM_Reader.java \ 
     14        SHM_Sender.java 
     15CLASSES = $(patsubst %.java,%.class,$(SRC)) 
    1316 
    1417SIPC_KEYS=sipc_mq_key sipc_shm_key 
    1518 
    16 all: $(CLASSES) 
    17         touch $(SIPC_KEYS) 
     19all: $(SIPC_KEYS) $(CLASSES) 
    1820 
    19 .SUFFIXES : .java .class 
     21$(SIPC_KEYS): 
     22        touch $@ 
    2023 
    21 .java.class: 
    22         javac $(JFLAGS) $< 
     24%.class: %.java 
     25        $(JAVAC) $(AM_JFLAGS) $(JFLAGS) $< 
    2326 
    24 .PHONY clean: 
     27.PHONY: clean 
    2528        rm -f *.class core $(SIPC_KEYS)