1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| /*
* Created on 05.02.2006
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package ch.marazzato.sharedmemory;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Random;
public class TestSharedMemory {
RandomAccessFile file = null;
FileChannel channel = null;
MappedByteBuffer sharedMemory = null;
public TestSharedMemory(String filename) {
try {
file = new RandomAccessFile(filename, "rw");
channel = file.getChannel();
sharedMemory = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1024);
sharedMemory.load();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace(System.err);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(System.err);
}
}
private void displayMemory() {
byte[] memory = new byte[32];
/* */
sharedMemory.position(0);
if (sharedMemory.hasArray()) {
memory = sharedMemory.array();
}
else {
sharedMemory.get(memory);
}
for (int i = 0; i < memory.length; ++i) {
System.out.print(memory[i] + " ");
}
}
/* */
public void run(byte id) {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
boolean stop = false;
String line;
byte[] memory = new byte[32];
System.out.println("? Affiche le contenu de la mémoire");
System.out.println("t Effectue un test");
System.out.println("-128..127 Modifie la mémoire");
System.out.println("q Quitte le programme");
try {
sharedMemory.position(id);
sharedMemory.put((byte)1);
System.out.println("Memory is loaded? " + sharedMemory.isLoaded());
while (!stop) {
System.out.print("Valeur " + id + "> ");
line = reader.readLine();
if (!"".equals(line)) {
if ("?".equals(line)) {
displayMemory();
System.out.println();System.out.println();
}
else if ("t".equals(line)) {
Random random = new Random();
byte[] value = new byte[1];
for (int counter = 0; counter < 100; ++counter) {
Thread.sleep(random.nextInt(30)+1 *100);
sharedMemory.position(id);
random.nextBytes(value);
sharedMemory.put(value[0]);
displayMemory();
System.out.println();System.out.println();
}
}
else if ("q".equals(line)) {
stop = true;
}
else {
byte value = Byte.parseByte(line);
sharedMemory.position(id);
sharedMemory.put(value);
displayMemory();
System.out.println();System.out.println();
}
}
}
}
catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
catch (Exception e) {
e.printStackTrace(System.err);
}
finally {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (channel != null) {
channel.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (file != null) {
file.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if (args.length >= 2) {
TestSharedMemory test = new TestSharedMemory(args[0]);
byte id = Byte.parseByte(args[1]);
test.run(id);
System.out.println("End Program");
}
else {
System.err.println("Usage: TestSharedMemory <File Name> <ID>");
}
}
} |
Partager