Changeset 50
- Timestamp:
- 07/01/08 17:23:07
(3 months ago)
- Author:
- jtang
- Message:
Updated Java unit tests. (incremental checkin)
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r49 |
r50 |
|
| 32 | 32 | protected libsipc_t handle; /* internal handle */ |
|---|
| 33 | 33 | protected boolean isConnected = false; /* internal state flag */ |
|---|
| | 34 | private java.nio.ByteBuffer dataPtr = null; /* shared buffer for sending data */ |
|---|
| 34 | 35 | |
|---|
| 35 | 36 | /** shared IPC key */ |
|---|
| … | … | |
| 88 | 89 | handle.close(); |
|---|
| 89 | 90 | handle = null; |
|---|
| | 91 | dataPtr = null; |
|---|
| 90 | 92 | isConnected = false; |
|---|
| 91 | 93 | } |
|---|
| … | … | |
| 124 | 126 | public java.nio.ByteBuffer GetDataPtr() throws Exception { |
|---|
| 125 | 127 | if (isConnected) { |
|---|
| 126 | | return handle.get_data_ptr(); |
|---|
| | 128 | /* cache the dataPtr, to avoid re-creating it for |
|---|
| | 129 | * each call -- this pointer is supposed to |
|---|
| | 130 | * persistent across all calls, and there is |
|---|
| | 131 | * supposed to be exactly one that is ever |
|---|
| | 132 | * created */ |
|---|
| | 133 | if (dataPtr == null) { |
|---|
| | 134 | dataPtr = handle.get_data_ptr(); |
|---|
| | 135 | } |
|---|
| | 136 | return dataPtr; |
|---|
| 127 | 137 | } |
|---|
| 128 | 138 | else { |
|---|
| … | … | |
| 159 | 169 | public void SendData() throws Exception { |
|---|
| 160 | 170 | if (isConnected) { |
|---|
| 161 | | java.nio.ByteBuffer b = GetDataPtr(); |
|---|
| 162 | | handle.send_data(b.limit()); |
|---|
| | 171 | handle.send_data(dataPtr.limit()); |
|---|
| 163 | 172 | } |
|---|
| 164 | 173 | else { |
|---|
| r49 |
r50 |
|
| 42 | 42 | super(key, role, sipcwrapperConstants.SIPC_SYSV_MQUEUES, ipcLength); |
|---|
| 43 | 43 | } |
|---|
| | 44 | |
|---|
| | 45 | /** |
|---|
| | 46 | * Destroys the IPC resources associated with the message |
|---|
| | 47 | * queue handle. Existing handles can continue operating |
|---|
| | 48 | * until they call Close(). |
|---|
| | 49 | */ |
|---|
| | 50 | public static void Unlink(String key) { |
|---|
| | 51 | sipcwrapper.sipc_unlink(key, sipcwrapperConstants.SIPC_SYSV_MQUEUES); |
|---|
| | 52 | } |
|---|
| 44 | 53 | } |
|---|
| r49 |
r50 |
|
| 47 | 47 | * successfully received and handled. |
|---|
| 48 | 48 | */ |
|---|
| 49 | | public void SipcRecvDone() throws Exception { |
|---|
| | 49 | public void RecvDone() throws Exception { |
|---|
| 50 | 50 | if (isConnected) { |
|---|
| 51 | 51 | handle.shm_recv_done(); |
|---|
| … | … | |
| 55 | 55 | } |
|---|
| 56 | 56 | } |
|---|
| | 57 | |
|---|
| | 58 | /** |
|---|
| | 59 | * Destroys the IPC resources associated with the message |
|---|
| | 60 | * queue handle. Existing handles can continue operating |
|---|
| | 61 | * until they call Close(). |
|---|
| | 62 | */ |
|---|
| | 63 | public static void Unlink(String key) { |
|---|
| | 64 | sipcwrapper.sipc_unlink(key, sipcwrapperConstants.SIPC_SYSV_SHM); |
|---|
| | 65 | } |
|---|
| 57 | 66 | } |
|---|
| r1 |
r50 |
|
| 1 | 1 | /* |
|---|
| 2 | 2 | * MQ_Creator.java |
|---|
| 3 | | * Create a message queue. This is a privileged application. |
|---|
| | 3 | * Create a message queue. This is a privileged application. |
|---|
| 4 | 4 | * |
|---|
| 5 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 5 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 6 | 6 | * Developed Under US JFCOM Sponsorship |
|---|
| 7 | 7 | * |
|---|
| … | … | |
| 25 | 25 | /** |
|---|
| 26 | 26 | * Creator process for the SIPC shared memory backend. |
|---|
| 27 | | * |
|---|
| | 27 | * |
|---|
| 28 | 28 | * @author David Windsor <dwindsor@tresys.com> |
|---|
| 29 | 29 | */ |
|---|
| 30 | 30 | public class MQ_Creator { |
|---|
| 31 | | private static final String sipc_key = "sipc_mq_key"; /* shared IPC key */ |
|---|
| | 31 | private static final String sipc_key = "sipc_mq_key"; /* shared IPC key */ |
|---|
| 32 | 32 | |
|---|
| 33 | 33 | public static void main(String[] args) { |
|---|
| 34 | 34 | try { |
|---|
| 35 | | SipcMqueue.Create(sipc_key); |
|---|
| 36 | | } catch (BadKeyException e) { |
|---|
| | 35 | Sipc sipc = new SipcMqueue(sipc_key, sipcwrapperConstants.SIPC_CREATOR, 8196); |
|---|
| | 36 | sipc.Close(); |
|---|
| | 37 | } catch (Exception e) { |
|---|
| 37 | 38 | e.printStackTrace(); |
|---|
| 38 | 39 | System.exit(1); |
|---|
| r1 |
r50 |
|
| 3 | 3 | * Free a message queue. This is a privileged application. |
|---|
| 4 | 4 | * |
|---|
| 5 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 5 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 6 | 6 | * Developed Under US JFCOM Sponsorship |
|---|
| 7 | 7 | * |
|---|
| … | … | |
| 32 | 32 | |
|---|
| 33 | 33 | public static void main(String[] args) { |
|---|
| 34 | | SipcMqueue.Destroy(sipc_key); |
|---|
| | 34 | SipcMqueue.Unlink(sipc_key); |
|---|
| 35 | 35 | } |
|---|
| 36 | 36 | } |
|---|
| r1 |
r50 |
|
| 3 | 3 | * Read character data from a message queue backed communications channel. |
|---|
| 4 | 4 | * |
|---|
| 5 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 5 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 6 | 6 | * Developed Under US JFCOM Sponsorship |
|---|
| 7 | 7 | * |
|---|
| … | … | |
| 24 | 24 | |
|---|
| 25 | 25 | import java.nio.*; |
|---|
| 26 | | import java.nio.charset.*; |
|---|
| 27 | 26 | |
|---|
| 28 | 27 | /** |
|---|
| 29 | 28 | * Read data from a SYSV message queue backed IPC resource. |
|---|
| 30 | 29 | * The IPC resource must be created by a privileged creation processs. |
|---|
| 31 | | * |
|---|
| 32 | | * @author David Windsor <dwindsor@tresys.com> |
|---|
| | 30 | * |
|---|
| | 31 | * @author David Windsor <dwindsor@tresys.com> |
|---|
| 33 | 32 | * @author Norman Patrick <npatrick@tresys.com> |
|---|
| | 33 | * @author L. Ross Raszewski <lraszewski@tresys.com> |
|---|
| 34 | 34 | */ |
|---|
| 35 | 35 | public class MQ_Reader { |
|---|
| 36 | | private static final String sipc_key = "sipc_mq_key"; /* shared IPC key */ |
|---|
| 37 | | private static final String data_end = "0xDEADBEEF"; /* end of transmission marker */ |
|---|
| | 36 | private static final String sipc_key = "sipc_mq_key"; /* shared IPC key */ |
|---|
| | 37 | |
|---|
| | 38 | private static final String data_end = "0xDEADBEEF"; /* end of transmission marker */ |
|---|
| 38 | 39 | |
|---|
| 39 | 40 | /** |
|---|
| 40 | 41 | * @param args |
|---|
| 41 | 42 | */ |
|---|
| 42 | | public static void main(String[] args) throws CharacterCodingException { |
|---|
| | 43 | public static void main(String[] args) { |
|---|
| 43 | 44 | SipcMqueue queue = null; |
|---|
| 44 | | int data_len = 4096; |
|---|
| 45 | 45 | int ipc_len = 8196; |
|---|
| 46 | 46 | |
|---|
| 47 | 47 | /* Create and initialize the IPC handle */ |
|---|
| 48 | | queue = new SipcMqueue(sipc_key, data_len, ipc_len); |
|---|
| 49 | | queue.Init(false); |
|---|
| 50 | | |
|---|
| 51 | | /* Connect the handle to its backend */ |
|---|
| 52 | 48 | try { |
|---|
| 53 | | queue.Connect(); |
|---|
| 54 | | } catch (ConnectionException e) { |
|---|
| | 49 | queue = new SipcMqueue(sipc_key, sipcwrapperConstants.SIPC_RECEIVER, ipc_len); |
|---|
| | 50 | } |
|---|
| | 51 | catch (Exception e) { |
|---|
| 55 | 52 | e.printStackTrace(); |
|---|
| 56 | 53 | System.err.println("Unable to connect message queue!"); |
|---|
| … | … | |
| 58 | 55 | } |
|---|
| 59 | 56 | |
|---|
| 60 | | /* Read data from the queue */ |
|---|
| 61 | | ByteBuffer bbuf = queue.ReadData(); |
|---|
| 62 | | String data = ByteBufferToString(bbuf); |
|---|
| 63 | | while(data.length() > 0) { |
|---|
| 64 | | /* Check for an end of transmission marker */ |
|---|
| 65 | | if(data.equals(data_end)) |
|---|
| 66 | | break; |
|---|
| | 57 | try { |
|---|
| | 58 | java.io.FileOutputStream fileOut = new java.io.FileOutputStream( |
|---|
| | 59 | "out.dat"); |
|---|
| | 60 | /* Read data from the queue */ |
|---|
| | 61 | while (true) { |
|---|
| | 62 | byte[] bbuf = queue.ReadData(); |
|---|
| | 63 | /* Check for an end of transmission marker */ |
|---|
| | 64 | if (bbuf.length == 11) { |
|---|
| | 65 | String data = new String(bbuf); |
|---|
| | 66 | if(data.startsWith(data_end) |
|---|
| | 67 | && ((int) data.charAt(10)) == 0) |
|---|
| | 68 | break; |
|---|
| | 69 | } |
|---|
| | 70 | fileOut.write(bbuf); |
|---|
| | 71 | } |
|---|
| 67 | 72 | |
|---|
| 68 | | System.out.println(data); |
|---|
| 69 | | bbuf.clear(); |
|---|
| 70 | | bbuf = queue.ReadData(); |
|---|
| 71 | | data = ByteBufferToString(bbuf); |
|---|
| | 73 | fileOut.close(); |
|---|
| 72 | 74 | } |
|---|
| 73 | | |
|---|
| 74 | | queue.Disconnect(); |
|---|
| 75 | | } |
|---|
| 76 | | |
|---|
| 77 | | /** |
|---|
| 78 | | * Convert a ByteBuffer to a String |
|---|
| 79 | | * @param buf a ByteBuffer |
|---|
| 80 | | * @return a String, null on error |
|---|
| 81 | | */ |
|---|
| 82 | | private static final String ByteBufferToString(ByteBuffer b) |
|---|
| 83 | | throws CharacterCodingException { |
|---|
| 84 | | Charset cset = Charset.forName("ISO-8859-1"); |
|---|
| 85 | | CharsetDecoder decoder = cset.newDecoder(); |
|---|
| 86 | | CharBuffer cbuf = null; |
|---|
| 87 | | |
|---|
| 88 | | cbuf = decoder.decode(b); |
|---|
| 89 | | return cbuf.toString(); |
|---|
| | 75 | catch (Exception e) { |
|---|
| | 76 | e.printStackTrace(); |
|---|
| | 77 | System.exit(1); |
|---|
| | 78 | } |
|---|
| 90 | 79 | } |
|---|
| 91 | 80 | } |
|---|
| r1 |
r50 |
|
| 3 | 3 | * Send character data to a message queue backed communications channel. |
|---|
| 4 | 4 | * |
|---|
| 5 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 5 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 6 | 6 | * Developed Under US JFCOM Sponsorship |
|---|
| 7 | 7 | * |
|---|
| … | … | |
| 24 | 24 | import com.tresys.sipc.*; |
|---|
| 25 | 25 | |
|---|
| | 26 | import java.nio.*; |
|---|
| | 27 | import java.nio.charset.*; |
|---|
| | 28 | |
|---|
| 26 | 29 | /** |
|---|
| 27 | 30 | * Write data to a SYSV message queue backed IPC resource. |
|---|
| 28 | 31 | * The IPC resource must be created by a privileged creation processs. |
|---|
| 29 | | * |
|---|
| | 32 | * |
|---|
| 30 | 33 | * @author David Windsor <dwindsor@tresys.com> |
|---|
| 31 | 34 | * @author Norman Patrick <npatrick@tresys.com> |
|---|
| … | … | |
| 40 | 43 | */ |
|---|
| 41 | 44 | public static void main(String[] args) { |
|---|
| 42 | | long data_len = 4096; |
|---|
| 43 | | long ipc_len = 8192; |
|---|
| | 45 | SipcMqueue queue; |
|---|
| | 46 | int ipc_len = 8192; |
|---|
| 44 | 47 | String filename = args[0]; |
|---|
| 45 | 48 | FileInputStream stream = null; |
|---|
| 46 | 49 | |
|---|
| 47 | | /* Create and initialize the IPC handle */ |
|---|
| 48 | | SipcMqueue queue = new SipcMqueue(sipc_key, data_len, ipc_len); |
|---|
| 49 | | queue.Init(true); |
|---|
| 50 | | |
|---|
| 51 | | try { |
|---|
| 52 | | /* Connect the handle to its backend */ |
|---|
| 53 | | queue.Connect(); |
|---|
| 54 | | |
|---|
| | 50 | try { |
|---|
| | 51 | /* Create and initialize the IPC handle */ |
|---|
| | 52 | queue = new SipcMqueue(sipc_key, sipcwrapperConstants.SIPC_SENDER, ipc_len); |
|---|
| 55 | 53 | /* Read data from the file and send it to the channel */ |
|---|
| 56 | 54 | stream = new FileInputStream(filename); |
|---|
| 57 | 55 | SendData(queue, stream); |
|---|
| 58 | 56 | stream.close(); |
|---|
| 59 | | } catch (ConnectionException e) { |
|---|
| 60 | | e.printStackTrace(); |
|---|
| 61 | | System.err.println("Unable to connect message queue"); |
|---|
| 62 | 57 | } catch (IOException ex) { |
|---|
| 63 | 58 | ex.printStackTrace(); |
|---|
| 64 | 59 | System.err.println("Unable to read from file " + filename); |
|---|
| 65 | | } finally { |
|---|
| 66 | | queue.Disconnect(); |
|---|
| | 60 | System.exit(1); |
|---|
| | 61 | } catch (Exception e) { |
|---|
| | 62 | e.printStackTrace(); |
|---|
| | 63 | System.err.println("Unable to connect message queue"); |
|---|
| | 64 | System.exit(1); |
|---|
| 67 | 65 | } |
|---|
| 68 | 66 | } |
|---|
| 69 | 67 | |
|---|
| 70 | | private static void SendData(SipcMqueue queue, FileInputStream stream) { |
|---|
| 71 | | try { |
|---|
| 72 | | java.nio.ByteBuffer buffer = queue.GetDataPtr(); |
|---|
| 73 | | java.nio.channels.FileChannel fc = stream.getChannel(); |
|---|
| 74 | | int read = fc.read(buffer); |
|---|
| 75 | | buffer.flip(); |
|---|
| | 68 | private static void SendData(SipcMqueue queue, FileInputStream stream) throws Exception { |
|---|
| | 69 | java.nio.ByteBuffer buffer = queue.GetDataPtr(); |
|---|
| | 70 | java.nio.channels.FileChannel fc = stream.getChannel(); |
|---|
| | 71 | int read = fc.read(buffer); |
|---|
| | 72 | while (read >= 0) { |
|---|
| | 73 | /* Explicit-length version of SendData: |
|---|
| | 74 | Send the number of bytes we read */ |
|---|
| | 75 | queue.SendData(read); |
|---|
| | 76 | buffer.clear(); |
|---|
| | 77 | read = fc.read(buffer); |
|---|
| | 78 | } |
|---|
| 76 | 79 | |
|---|
| 77 | | if(read < queue.DATA_LEN) |
|---|
| 78 | | buffer.put((byte) 0); |
|---|
| 79 | | |
|---|
| 80 | | while(read >= 0) { |
|---|
| 81 | | if(queue.SendData() < 0) |
|---|
| 82 | | throw new IOException("Cannot send data."); |
|---|
| 83 | | |
|---|
| 84 | | buffer.clear(); |
|---|
| 85 | | read = fc.read(buffer); |
|---|
| 86 | | if(read < queue.DATA_LEN) |
|---|
| 87 | | buffer.put((byte) 0); |
|---|
| 88 | | } |
|---|
| 89 | | |
|---|
| 90 | | buffer.clear(); |
|---|
| 91 | | //Send the end of transmission marker |
|---|
| 92 | | buffer.put(MQ_Sender.data_end.getBytes()); |
|---|
| 93 | | buffer.put((byte) 0); |
|---|
| 94 | | if(queue.SendData() < 0) |
|---|
| 95 | | throw new IOException("Cannot send end of transmission marker"); |
|---|
| 96 | | } catch (IOException ex) { |
|---|
| 97 | | ex.printStackTrace(); |
|---|
| 98 | | } |
|---|
| | 80 | buffer.clear(); |
|---|
| | 81 | //Send the end of transmission marker |
|---|
| | 82 | buffer.put(MQ_Sender.data_end.getBytes()); |
|---|
| | 83 | buffer.put((byte) 0); |
|---|
| | 84 | /* Inferred-length version of SendData: |
|---|
| | 85 | flip the buffer to set its limit, and |
|---|
| | 86 | Sipc will use that as the data length |
|---|
| | 87 | */ |
|---|
| | 88 | buffer.flip(); |
|---|
| | 89 | queue.SendData(); |
|---|
| 99 | 90 | } |
|---|
| 100 | 91 | } |
|---|
| r49 |
r50 |
|
| 4 | 4 | AM_JFLAGS = -cp ../libsipc-1.0.jar |
|---|
| 5 | 5 | |
|---|
| 6 | | SRC = MQ_Binary_Reader.java \ |
|---|
| 7 | | MQ_Creator.java \ |
|---|
| | 6 | SRC = MQ_Creator.java \ |
|---|
| 8 | 7 | MQ_Destroyer.java \ |
|---|
| 9 | 8 | MQ_Reader.java \ |
|---|
| … | … | |
| 17 | 16 | SIPC_KEYS=sipc_mq_key sipc_shm_key |
|---|
| 18 | 17 | |
|---|
| 19 | | all: $(SIPC_KEYS) $(CLASSES) |
|---|
| | 18 | all: |
|---|
| | 19 | |
|---|
| | 20 | tests: $(CLASSES) |
|---|
| | 21 | |
|---|
| | 22 | check: $(SIPC_KEYS) tests |
|---|
| | 23 | sh run_tests.sh |
|---|
| 20 | 24 | |
|---|
| 21 | 25 | $(SIPC_KEYS): |
|---|
| … | … | |
| 25 | 29 | $(JAVAC) $(AM_JFLAGS) $(JFLAGS) $< |
|---|
| 26 | 30 | |
|---|
| 27 | | .PHONY: clean |
|---|
| | 31 | .PHONY: all tests check clean |
|---|
| 28 | 32 | rm -f *.class core $(SIPC_KEYS) |
|---|
| r1 |
r50 |
|
| 3 | 3 | * Create a shared memory segment. This is a prvileged application. |
|---|
| 4 | 4 | * |
|---|
| 5 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 5 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 6 | 6 | * Developed Under US JFCOM Sponsorship |
|---|
| 7 | 7 | * |
|---|
| … | … | |
| 25 | 25 | /** |
|---|
| 26 | 26 | * Creator process for the SIPC shared memory backend. |
|---|
| 27 | | * |
|---|
| | 27 | * |
|---|
| 28 | 28 | * @author David Windsor <dwindsor@tresys.com> |
|---|
| 29 | 29 | */ |
|---|
| … | … | |
| 33 | 33 | public static void main(String[] args) { |
|---|
| 34 | 34 | try { |
|---|
| 35 | | SipcShm.Create(sipc_key); |
|---|
| 36 | | } catch (BadKeyException ex) { |
|---|
| 37 | | ex.printStackTrace(); |
|---|
| | 35 | Sipc sipc = new SipcShm(sipc_key, sipcwrapperConstants.SIPC_CREATOR, 8196); |
|---|
| | 36 | sipc.Close(); |
|---|
| | 37 | } catch (Exception e) { |
|---|
| | 38 | e.printStackTrace(); |
|---|
| 38 | 39 | System.exit(1); |
|---|
| 39 | 40 | } |
|---|
| r1 |
r50 |
|
| 1 | 1 | /* |
|---|
| 2 | 2 | * SHM_Destroyer.java |
|---|
| 3 | | * Free a shared memory segment. This is a provileged application. |
|---|
| 4 | | * |
|---|
| 5 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 3 | * Free a shared memory segment. This is a provileged application. |
|---|
| | 4 | * |
|---|
| | 5 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 6 | 6 | * Developed Under US JFCOM Sponsorship |
|---|
| 7 | 7 | * |
|---|
| … | … | |
| 25 | 25 | /** |
|---|
| 26 | 26 | * Destroyer process for the SIPC shared memory backend. |
|---|
| 27 | | * |
|---|
| | 27 | * |
|---|
| 28 | 28 | * @author David Windsor <dwindsor@tresys.com> |
|---|
| 29 | 29 | */ |
|---|
| … | … | |
| 32 | 32 | |
|---|
| 33 | 33 | public static void main(String[] args) { |
|---|
| 34 | | SipcShm.Destroy(sipc_key); |
|---|
| | 34 | SipcShm.Unlink(sipc_key); |
|---|
| 35 | 35 | } |
|---|
| 36 | 36 | } |
|---|
| r1 |
r50 |
|
| 3 | 3 | * Read character data from a shared memory backed communications channel. |
|---|
| 4 | 4 | * |
|---|
| 5 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 5 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 6 | 6 | * Developed Under US JFCOM Sponsorship |
|---|
| 7 | 7 | * |
|---|
| … | … | |
| 24 | 24 | |
|---|
| 25 | 25 | import java.nio.ByteBuffer; |
|---|
| 26 | | import java.nio.CharBuffer; |
|---|
| 27 | | import java.nio.charset.*; |
|---|
| 28 | 26 | |
|---|
| 29 | 27 | /** |
|---|
| 30 | 28 | * Read data from a SYSV shared memory backed IPC resource. |
|---|
| 31 | 29 | * The IPC resource must be created by a privileged creation processs. |
|---|
| 32 | | * |
|---|
| | 30 | * |
|---|
| 33 | 31 | * @author David Windsor <dwindsor@tresys.com> |
|---|
| 34 | 32 | * @author Norman Patrick <npatrick@tresys.com> |
|---|
| 35 | 33 | */ |
|---|
| 36 | 34 | public class SHM_Reader { |
|---|
| 37 | | private static final String sipc_key = "sipc_shm_key"; /* shared IPC key */ |
|---|
| | 35 | private static final String sipc_key = "sipc_shm_key"; /* shared IPC key */ |
|---|
| 38 | 36 | |
|---|
| 39 | | private static final String data_end = "0xDEADBEEF"; /* end of transmission marker */ |
|---|
| 40 | | |
|---|
| 41 | | /* canonical encoding name, as understood by nio Charset */ |
|---|
| 42 | | private static final String encoding = "ISO-8859-1"; |
|---|
| | 37 | private static final String data_end = "0xDEADBEEF"; /* end of transmission marker */ |
|---|
| 43 | 38 | |
|---|
| 44 | 39 | /** |
|---|
| … | … | |
| 46 | 41 | */ |
|---|
| 47 | 42 | public static void main(String[] args) { |
|---|
| 48 | | String data = null; |
|---|
| 49 | | int data_len = 4096; |
|---|
| | 43 | SipcShm shm = null; |
|---|
| 50 | 44 | int ipc_len = 8196; |
|---|
| 51 | | ByteBuffer buffer = null; |
|---|
| 52 | 45 | |
|---|
| 53 | 46 | /* Create and initialize the IPC handle */ |
|---|
| 54 | | SipcShm shm = new SipcShm(sipc_key, data_len, ipc_len); |
|---|
| 55 | | shm.Init(false); |
|---|
| 56 | | |
|---|
| 57 | | /* Connect the handle to its backend */ |
|---|
| 58 | 47 | try { |
|---|
| 59 | | shm.Connect(); |
|---|
| 60 | | } catch (ConnectionException e) { |
|---|
| | 48 | shm = new SipcShm(sipc_key, sipcwrapperConstants.SIPC_RECEIVER, ipc_len); |
|---|
| | 49 | } catch (Exception e) { |
|---|
| 61 | 50 | e.printStackTrace(); |
|---|
| 62 | 51 | System.err.println("Unable to connect shared memory!"); |
|---|
| … | … | |
| 67 | 56 | * marker is encountered |
|---|
| 68 | 57 | */ |
|---|
| 69 | | while((buffer = shm.ReadData()) != null) { |
|---|
| 70 | | data = ByteBufferToString(buffer, data_len); |
|---|
| 71 | | if(data == null) |
|---|
| 72 | | return; |
|---|
| 73 | | |
|---|
| 74 | | /* Check for the end of transmission marker */ |
|---|
| 75 | | if(IsEndXmit(data)) { |
|---|
| 76 | | shm.SipcRecvDone(); |
|---|
| 77 | | break; |
|---|
| | 58 | try { |
|---|
| | 59 | java.io.FileOutputStream fileOut = new java.io.FileOutputStream( |
|---|
| | 60 | "out.dat"); |
|---|
| | 61 | /* Read data from the shared memory */ |
|---|
| | 62 | while(true) { |
|---|
| | 63 | byte[] bbuf = shm.ReadData(); |
|---|
| | 64 | /* Check for the end of transmission marker */ |
|---|
| | 65 | if (IsEndXmit(bbuf)) { |
|---|
| | 66 | shm.RecvDone(); |
|---|
| | 67 | break; |
|---|
| | 68 | } |
|---|
| | 69 | fileOut.write(bbuf); |
|---|
| | 70 | shm.RecvDone(); |
|---|
| 78 | 71 | } |
|---|
| 79 | | |
|---|
| 80 | | System.out.print(data); |
|---|
| 81 | | shm.SipcRecvDone(); |
|---|
| 82 | 72 | } |
|---|
| 83 | | shm.Disconnect(); |
|---|
| | 73 | catch (Exception e) { |
|---|
| | 74 | e.printStackTrace(); |
|---|
| | 75 | System.exit(1); |
|---|
| | 76 | } |
|---|
| | 77 | shm.Close(); |
|---|
| 84 | 78 | } |
|---|
| 85 | 79 | |
|---|
| 86 | | private static final boolean IsEndXmit(String data) { |
|---|
| 87 | | if(data.length() >= data_end.length()) { |
|---|
| | 80 | private static final boolean IsEndXmit(byte[] bbuf) { |
|---|
| | 81 | String data = new String(bbuf); |
|---|
| | 82 | if (data.length() >= data_end.length()) { |
|---|
| 88 | 83 | String sub = data.substring(0, data_end.length()); |
|---|
| 89 | | if(sub.equals(data_end)) |
|---|
| | 84 | if (sub.equals(data_end)) |
|---|
| 90 | 85 | return true; |
|---|
| 91 | 86 | } |
|---|
| … | … | |
| 93 | 88 | return false; |
|---|
| 94 | 89 | } |
|---|
| 95 | | |
|---|
| 96 | | private static final String ByteBufferToString(ByteBuffer b, int msglen) { |
|---|
| 97 | | Charset cset = Charset.forName(encoding); |
|---|
| 98 | | CharsetDecoder decoder = cset.newDecoder(); |
|---|
| 99 | | CharBuffer cbuf = null; |
|---|
| 100 | | |
|---|
| 101 | | try { |
|---|
| 102 | | cbuf = decoder.decode(b); |
|---|
| 103 | | } catch (CharacterCodingException ex) { |
|---|
| 104 | | ex.printStackTrace(); |
|---|
| 105 | | return null; |
|---|
| 106 | | } |
|---|
| 107 | | |
|---|
| 108 | | return cbuf.toString(); |
|---|
| 109 | | } |
|---|
| 110 | 90 | } |
|---|
| r1 |
r50 |
|
| 2 | 2 | * SHM_Sender.java |
|---|
| 3 | 3 | * Send character data to a shared memory backed communications channel. |
|---|
| 4 | | * |
|---|
| 5 | | * Copyright (C) 2006, 2007 Tresys Technology, LLC |
|---|
| | 4 | * |
|---|
| | 5 | * Copyright (C) 2006 - 2008 Tresys Technology, LLC |
|---|
| 6 | 6 | * Developed Under US JFCOM Sponsorship |
|---|
| 7 | 7 | * |
|---|
| … | … | |
| 38 | 38 | private static final String data_end = "0xDEADBEEF"; /* end of transmission marker */ |
|---|
| 39 | 39 | |
|---|
| 40 | | /* canonical encoding name, as understood by nio Charset */ |
|---|
| 41 | | private static final String encoding = "ISO-8859-1"; |
|---|
| 42 | | |
|---|
| 43 | 40 | /** |
|---|
| 44 | 41 | * @param args |
|---|
| 45 | 42 | */ |
|---|
| 46 | 43 | public static void main(String[] args) { |
|---|
| 47 | | int data_len = 4096; |
|---|
| | 44 | SipcShm shm = null; |
|---|
| 48 | 45 | int ipc_len = 8192; |
|---|
| 49 | 46 | String filename = args[0]; |
|---|
| 50 | | SipcShm shm = null; |
|---|
| 51 | | |
|---|
| 52 | | /* Create and initialize the IPC handle */ |
|---|
| 53 | | shm = new SipcShm(sipc_key, data_len, ipc_len); |
|---|
| 54 | | shm.Init(true); |
|---|
| | 47 | FileInputStream stream = null; |
|---|
| 55 | 48 | |
|---|
| 56 | 49 | try { |
|---|
| 57 | | /* Connect the handle to its backend */ |
|---|
| 58 | | shm.Connect(); |
|---|
| | 50 | /* Create and initialize the IPC handle */ |
|---|
| | 51 | shm = new SipcShm(sipc_key, sipcwrapperConstants.SIPC_SENDER, ipc_len); |
|---|
| 59 | 52 | |
|---|
| 60 | 53 | /* Read data from the file and send it across the channel */ |
|---|
| 61 | | File file = new File(sipc_key); |
|---|
| 62 | | file.createNewFile(); |
|---|
| 63 | | FileInputStream stream = new FileInputStream(filename); |
|---|
| | 54 | stream = new FileInputStream(filename); |
|---|
| 64 | 55 | SendData(shm, stream); |
|---|
| 65 | 56 | stream.close(); |
|---|
| | 57 | shm.Close(); |
|---|
| 66 | 58 | } catch (IOException ioe) { |
|---|
| 67 | 59 | ioe.printStackTrace(); |
|---|
| 68 | 60 | System.err.println("Unable to read from file " + filename); |
|---|
| 69 | | } catch (ConnectionException e) { |
|---|
| | 61 | System.exit(1); |
|---|
| | 62 | } catch (Exception e) { |
|---|
| 70 | 63 | e.printStackTrace(); |
|---|
| 71 | 64 | System.err.println("Unable to connect shared memory!"); |
|---|
| 72 | | } |
|---|
| 73 | | finally { |
|---|
| 74 | | shm.Disconnect(); |
|---|
| | 65 | System.exit(1); |
|---|
| 75 | 66 | } |
|---|
| 76 | 67 | } |
|---|
| 77 | 68 | |
|---|
| 78 | | private static void SendData(SipcShm shm, FileInputStream stream) { |
|---|
| 79 | | try { |
|---|
| 80 | | java.nio.ByteBuffer buffer = shm.GetDataPtr(); |
|---|
| 81 | | java.nio.channels.FileChannel fc = stream.getChannel(); |
|---|
| | 69 | private static void SendData(SipcShm shm, FileInputStream stream) throws Exception { |
|---|
| | 70 | java.nio.ByteBuffer buffer = shm.GetDataPtr(); |
|---|
| | 71 | java.nio.channels.FileChannel fc = stream.getChannel(); |
|---|
| | 72 | int read = fc.read(buffer); |
|---|
| | 73 | while (read >= 0) { |
|---|
| | 74 | /* Explicit-length version of SendData: |
|---|
| | 75 | Send the number of bytes we read */ |
|---|
| | 76 | shm.SendData(read); |
|---|
| | 77 | buffer.clear(); |
|---|
| | 78 | read = fc.read(buffer); |
|---|
| | 79 | } |
|---|
| 82 | 80 | |
|---|
| 83 | | int read = fc.read(buffer); |
|---|
| 84 | | |
|---|
| 85 | | while(read >= 0) { |
|---|
| 86 | | if(buffer.remaining() > 0) |
|---|
| 87 | | buffer.put((byte) 0); |
|---|
| 88 | | |
|---|
| 89 | | if(shm.SendData() < 0) |
|---|
| 90 | | throw new IOException("Cannot send data."); |
|---|
| 91 | | |
|---|
| 92 | | // Clear out the data in the buffer |
|---|
| 93 | | ClearBuffer(buffer); |
|---|
| 94 | | |
|---|
| 95 | | buffer.clear(); // Set limit = capacity, position = 0 |
|---|
| 96 | | read = fc.read(buffer); |
|---|
| 97 | | } |
|---|
| 98 | | |
|---|
| 99 | | // Clear out the data in the buffer |
|---|
| 100 | | ClearBuffer(buffer); |
|---|
| 101 | | |
|---|
| 102 | | // Send the end of transmission marker |
|---|
| 103 | | // Set the limit so that the buffer contains only end of xmit message |
|---|
| 104 | | buffer.rewind(); |
|---|
| 105 | | buffer.limit(data_end.getBytes().length + 1); |
|---|
| 106 | | buffer.put(data_end.getBytes(encoding)); |
|---|
| 107 | | if(shm.SendData() < 0) |
|---|
| 108 | | throw new IOException("Cannot send end of transmission marker"); |
|---|
| 109 | | } catch (IOException ex) { |
|---|
| 110 | | ex.printStackTrace(System.out); |
|---|
| 111 | | } |
|---|
| 112 | | } |
|---|
| 113 | | |
|---|
| 114 | | private static void ClearBuffer(ByteBuffer b) { |
|---|
| 115 | | byte[] bytes = new byte[b.capacity()]; |
|---|
| 116 | | b.clear(); // Set limit = capacity, position = 0 |
|---|
| 117 | | b.put(bytes); |
|---|
| | 81 | buffer.clear(); |
|---|
| | 82 | //Send the end of transmission marker |
|---|
| | 83 | buffer.put(SHM_Sender.data_end.getBytes()); |
|---|
| | 84 | buffer.put((byte) 0); |
|---|
| | 85 | /* Inferred-length version of SendData: |
|---|
| | 86 | flip the buffer to set its limit, and |
|---|
| | 87 | Sipc will use that as the data length |
|---|
| | 88 | */ |
|---|
| | 89 | buffer.flip(); |
|---|
| | 90 | shm.SendData(); |
|---|
| 118 | 91 | } |
|---|
| 119 | 92 | } |
|---|
| r45 |
r50 |
|
| 26 | 26 | struct sipc; |
|---|
| 27 | 27 | typedef struct sipc sipc_t; |
|---|
| 28 | | |
|---|
| 29 | | struct sipc_msg; |
|---|
| 30 | | typedef struct sipc_msg sipc_msg_t; |
|---|
| 31 | 28 | |
|---|
| 32 | 29 | /* SIPC roles */ |
|---|
| r45 |
r50 |
|
| 12 | 12 | }; |
|---|
| 13 | 13 | |
|---|
| 14 | | VERS_1.1{ |
|---|
| | 14 | VERS_1.2{ |
|---|
| 15 | 15 | global: |
|---|
| 16 | 16 | sipc_ioctl; |
|---|
Download in other formats:
* Generating other formats may take time.