Changeset 54
- Timestamp:
- 07/17/08 12:54:18
(5 months ago)
- Author:
- jtang
- Message:
Added ioctl() to Java bindings.
Clean up and Java unit tests.
Make C test script executable.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r50 |
r54 |
|
| 30 | 30 | */ |
|---|
| 31 | 31 | public abstract class Sipc { |
|---|
| | 32 | public final static int CREATOR = sipcwrapperConstants.SIPC_CREATOR; |
|---|
| | 33 | public final static int SENDER = sipcwrapperConstants.SIPC_SENDER; |
|---|
| | 34 | public final static int RECEIVER = sipcwrapperConstants.SIPC_RECEIVER; |
|---|
| | 35 | public final static int BLOCK = sipcwrapperConstants.SIPC_BLOCK; |
|---|
| | 36 | public final static int NOBLOCK = sipcwrapperConstants.SIPC_NOBLOCK; |
|---|
| | 37 | |
|---|
| 32 | 38 | protected libsipc_t handle; /* internal handle */ |
|---|
| 33 | 39 | protected boolean isConnected = false; /* internal state flag */ |
|---|
| … | … | |
| 36 | 42 | /** shared IPC key */ |
|---|
| 37 | 43 | private final String key; |
|---|
| | 44 | |
|---|
| | 45 | /** the type of IPC, either SIPC_SYSV_SHM or SIPC_SYSV_MQUEUES */ |
|---|
| 38 | 46 | private int ipc_type; |
|---|
| 39 | 47 | |
|---|
| … | … | |
| 50 | 58 | * |
|---|
| 51 | 59 | * @param key Path name representing an IPC key. |
|---|
| 52 | | * @param role One of SIPC_CREATOR, SIPC_SENDER, or SIPC_RECEIVER. |
|---|
| | 60 | * @param role One of Sipc.CREATOR, Sipc.SENDER, or Sipc.RECEIVER. |
|---|
| 53 | 61 | * @param ipc_type One of SIPC_SYSV_SHM or SIPC_SYSV_MQUEUES. |
|---|
| 54 | 62 | * @param ipcLength Size in bytes of the system's IPC data buffer. |
|---|
| … | … | |
| 56 | 64 | */ |
|---|
| 57 | 65 | protected Sipc(String key, int role, int ipc_type, long ipcLength) throws Exception { |
|---|
| | 66 | if (role != Sipc.CREATOR && role != Sipc.SENDER && role != Sipc.RECEIVER) { |
|---|
| | 67 | throw new Error("Role must be one of Sipc.CREATOR, Sipc.SENDER, or Sipc.RECEIVER"); |
|---|
| | 68 | } |
|---|
| | 69 | if (ipc_type != sipcwrapperConstants.SIPC_SYSV_MQUEUES && |
|---|
| | 70 | ipc_type != sipcwrapperConstants.SIPC_SYSV_SHM) { |
|---|
| | 71 | throw new Error("IPC type must be one of sipcwrapperConstants.SIPC_SYSV_MQUEUES or sipcwrapperConstants.SIPC_SYSV_SHM"); |
|---|
| | 72 | } |
|---|
| 58 | 73 | handle = new libsipc_t(key, role, ipc_type, ipcLength); |
|---|
| 59 | 74 | this.key = key; |
|---|
| … | … | |
| 95 | 110 | |
|---|
| 96 | 111 | /** |
|---|
| 97 | | * Signal the sending application that a transmission has been successfully |
|---|
| 98 | | * received and handled. This operation is not implemented by all backends. |
|---|
| | 112 | * Signal the sending application that a transmission has been |
|---|
| | 113 | * successfully received and handled. This operation is not |
|---|
| | 114 | * implemented by all backends. |
|---|
| 99 | 115 | */ |
|---|
| 100 | 116 | public void SipcRecvDone() throws Exception {} |
|---|
| … | … | |
| 104 | 120 | * be in a connected state for this operation to succeed. |
|---|
| 105 | 121 | * |
|---|
| 106 | | * @return A reference to the internal data pointer holding the |
|---|
| 107 | | * received data, null on error. |
|---|
| | 122 | * @return A reference to the internal data pointer holding |
|---|
| | 123 | * the received data, or NULL if the channel is set to |
|---|
| | 124 | * non-blocking read and there was nothing available to be |
|---|
| | 125 | * read. |
|---|
| 108 | 126 | */ |
|---|
| 109 | 127 | public byte[] ReadData() throws Exception { |
|---|
| … | … | |
| 175 | 193 | } |
|---|
| 176 | 194 | } |
|---|
| | 195 | |
|---|
| | 196 | /** |
|---|
| | 197 | * Modify the behavior of an open communications channel. |
|---|
| | 198 | * |
|---|
| | 199 | * @param request If Sipc.BLOCK, set the channel to block when |
|---|
| | 200 | * reading. (This is the default). If Sipc.NONBLOCK, then |
|---|
| | 201 | * set the channel to not block when reading. |
|---|
| | 202 | */ |
|---|
| | 203 | public void IOCtl(int request) throws Exception { |
|---|
| | 204 | if (isConnected) { |
|---|
| | 205 | handle.ioctl(request); |
|---|
| | 206 | } |
|---|
| | 207 | else { |
|---|
| | 208 | throw new ConnectionException("Handle was closed."); |
|---|
| | 209 | } |
|---|
| | 210 | } |
|---|
| 177 | 211 | } |
|---|
| r50 |
r54 |
|
| 35 | 35 | * |
|---|
| 36 | 36 | * @param key Pathname of IPC key, as understood by ftok(1). |
|---|
| 37 | | * @param role One of SIPC_CREATOR, SIPC_SENDER, or SIPC_RECEIVER. |
|---|
| | 37 | * @param role One of Sipc.CREATOR, Sipc.SENDER, or Sipc.RECEIVER. |
|---|
| 38 | 38 | * @param ipcLength Size in bytes of the system's IPC data buffer. |
|---|
| 39 | 39 | * This should not exceed any known system limits. |
|---|
| r50 |
r54 |
|
| 35 | 35 | * |
|---|
| 36 | 36 | * @param key Pathname of IPC key, as understood by ftok(1). |
|---|
| 37 | | * @param role One of SIPC_CREATOR, SIPC_SENDER, or SIPC_RECEIVER. |
|---|
| | 37 | * @param role One of Sipc.CREATOR, Sipc.SENDER, or Sipc.RECEIVER. |
|---|
| 38 | 38 | * @param ipcLength Size in bytes of the system's IPC data buffer. |
|---|
| 39 | 39 | * This should not exceed any known system limits. |
|---|
| r49 |
r54 |
|
| 150 | 150 | BEGIN_EXCEPTION |
|---|
| 151 | 151 | if (sipc_recv_data(self->sipc, &data, &len) < 0) { |
|---|
| | 152 | if (errno == EAGAIN) { |
|---|
| | 153 | return NULL; |
|---|
| | 154 | } |
|---|
| 152 | 155 | SWIG_exception(SWIG_RuntimeError, strerror(errno)); |
|---|
| 153 | 156 | } |
|---|
| r52 |
r54 |
|
| 33 | 33 | public static void main(String[] args) { |
|---|
| 34 | 34 | try { |
|---|
| 35 | | Sipc sipc = new SipcMqueue(sipc_key, sipcwrapperConstants.SIPC_CREATOR, 8196); |
|---|
| | 35 | Sipc sipc = new SipcMqueue(sipc_key, Sipc.CREATOR, 8196); |
|---|
| 36 | 36 | sipc.Close(); |
|---|
| 37 | 37 | } catch (Exception e) { |
|---|
| r52 |
r54 |
|
| 47 | 47 | /* Create and initialize the IPC handle */ |
|---|
| 48 | 48 | try { |
|---|
| 49 | | queue = new SipcMqueue(sipc_key, sipcwrapperConstants.SIPC_RECEIVER, ipc_len); |
|---|
| | 49 | queue = new SipcMqueue(sipc_key, Sipc.RECEIVER, ipc_len); |
|---|
| 50 | 50 | } |
|---|
| 51 | 51 | catch (Exception e) { |
|---|
| … | … | |
| 59 | 59 | "out.dat"); |
|---|
| 60 | 60 | /* Read data from the queue */ |
|---|
| | 61 | byte[] bbuf; |
|---|
| 61 | 62 | while (true) { |
|---|
| 62 | | byte[] bbuf = queue.ReadData(); |
|---|
| | 63 | bbuf = queue.ReadData(); |
|---|
| 63 | 64 | /* Check for an end of transmission marker */ |
|---|
| 64 | 65 | if (bbuf.length == 11) { |
|---|
| … | … | |
| 71 | 72 | } |
|---|
| 72 | 73 | |
|---|
| | 74 | /* Try a non-blocking read */ |
|---|
| | 75 | queue.IOCtl(Sipc.NOBLOCK); |
|---|
| | 76 | bbuf = queue.ReadData(); |
|---|
| | 77 | if (bbuf != null) { |
|---|
| | 78 | throw new Exception("Non-blocking read returned something."); |
|---|
| | 79 | } |
|---|
| 73 | 80 | fileOut.close(); |
|---|
| 74 | 81 | } |
|---|
| r52 |
r54 |
|
| 49 | 49 | try { |
|---|
| 50 | 50 | /* Create and initialize the IPC handle */ |
|---|
| 51 | | queue = new SipcMqueue(sipc_key, sipcwrapperConstants.SIPC_SENDER, ipc_len); |
|---|
| | 51 | queue = new SipcMqueue(sipc_key, Sipc.SENDER, ipc_len); |
|---|
| 52 | 52 | /* Read data from the file and send it to the channel */ |
|---|
| 53 | 53 | stream = new FileInputStream(filename); |
|---|
| r52 |
r54 |
|
| 33 | 33 | public static void main(String[] args) { |
|---|
| 34 | 34 | try { |
|---|
| 35 | | Sipc sipc = new SipcShm(sipc_key, sipcwrapperConstants.SIPC_CREATOR, 8192); |
|---|
| | 35 | Sipc sipc = new SipcShm(sipc_key, Sipc.CREATOR, 8192); |
|---|
| 36 | 36 | sipc.Close(); |
|---|
| 37 | 37 | } catch (Exception e) { |
|---|
| r52 |
r54 |
|
| 46 | 46 | /* Create and initialize the IPC handle */ |
|---|
| 47 | 47 | try { |
|---|
| 48 | | shm = new SipcShm(sipc_key, sipcwrapperConstants.SIPC_RECEIVER, ipc_len); |
|---|
| | 48 | shm = new SipcShm(sipc_key, Sipc.RECEIVER, ipc_len); |
|---|
| 49 | 49 | } catch (Exception e) { |
|---|
| 50 | 50 | e.printStackTrace(); |
|---|
| … | … | |
| 60 | 60 | "out.dat"); |
|---|
| 61 | 61 | /* Read data from the shared memory */ |
|---|
| | 62 | byte[] bbuf; |
|---|
| 62 | 63 | while(true) { |
|---|
| 63 | | byte[] bbuf = shm.ReadData(); |
|---|
| | 64 | bbuf = shm.ReadData(); |
|---|
| 64 | 65 | /* Check for the end of transmission marker */ |
|---|
| 65 | 66 | if (IsEndXmit(bbuf)) { |
|---|
| … | … | |
| 69 | 70 | fileOut.write(bbuf); |
|---|
| 70 | 71 | shm.RecvDone(); |
|---|
| | 72 | } |
|---|
| | 73 | |
|---|
| | 74 | /* Try a non-blocking read */ |
|---|
| | 75 | shm.IOCtl(Sipc.NOBLOCK); |
|---|
| | 76 | bbuf = shm.ReadData(); |
|---|
| | 77 | if (bbuf != null) { |
|---|
| | 78 | throw new Exception("Non-blocking read returned something."); |
|---|
| 71 | 79 | } |
|---|
| 72 | 80 | } |
|---|
| r52 |
r54 |
|
| 49 | 49 | try { |
|---|
| 50 | 50 | /* Create and initialize the IPC handle */ |
|---|
| 51 | | shm = new SipcShm(sipc_key, sipcwrapperConstants.SIPC_SENDER, ipc_len); |
|---|
| | 51 | shm = new SipcShm(sipc_key, Sipc.SENDER, ipc_len); |
|---|
| 52 | 52 | |
|---|
| 53 | 53 | /* Read data from the file and send it across the channel */ |
|---|
- Property svn:executable set to *
Download in other formats:
* Generating other formats may take time.