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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
import java.io.*;
import java.net.*;
public class SamcefServer {
// Choose a port outside of the range 1-1024:
private static final int PORT = 8080;
private static SamcefConnection connection = null;
public SamcefServer () {}
public int getPORT() {
return PORT;
}
public static void runSamcef() {
System.out.println("Samcef starts running");
String [] Cmd={"/home/Xavier/pro" };
try {
Runtime r = Runtime.getRuntime();
r.traceInstructions(true);
r.traceInstructions(true);
r.traceMethodCalls(true);
final Process proc = r.exec(Cmd);
new Thread() {
public void run() {
try {
BufferedInputStream errStream =
new BufferedInputStream(proc.getErrorStream());
byte[] buffer = new byte[128];
int read;
while ((read = errStream.read(buffer, 0, buffer.length)) != -1) {
System.err.write(buffer, 0, read);
}
errStream.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
try {
BufferedInputStream outStream =
new BufferedInputStream(proc.getInputStream());
byte[] buffer = new byte[128];
int read;
while ((read = outStream.read(buffer, 0, buffer.length)) != -1) {
System.out.write(buffer, 0, read);
}
outStream.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}.start();
Thread waitFor = new Thread() {
public void run() {
try {
proc.waitFor();
}
catch(InterruptedException ie) {
ie.printStackTrace();
}
}
};
waitFor.start();
int status = -1;
try {
waitFor.join();
status = proc.exitValue();
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
System.out.println(status);
}
catch (IOException e) {
System.out.println("erreur d'execution " + Cmd + e.toString());
}
catch (IllegalThreadStateException e) {
System.out.println("interruption 2" + Cmd + e.toString());
e.printStackTrace();
}
}
public static void runPostPro() {
String [] Cmd2={"/home/Xavier/postpro" };
try {
Runtime r2 = Runtime.getRuntime();
r2.traceInstructions(true);
r2.traceInstructions(true);
r2.traceMethodCalls(true);
final Process proc2 = r2.exec(Cmd2);
new Thread() {
public void run() {
try {
BufferedInputStream errStream2 =
new BufferedInputStream(proc2.getErrorStream());
byte[] buffer2 = new byte[128];
int read2;
while ((read2 = errStream2.read(buffer2, 0, buffer2.length)) != -1) {
System.err.write(buffer2, 0, read2);
}
errStream2.close();
}
catch (IOException ioe2) {
ioe2.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
try {
BufferedInputStream outStream2 =
new BufferedInputStream(proc2.getInputStream());
byte[] buffer2 = new byte[128];
int read2;
while ((read2 = outStream2.read(buffer2, 0, buffer2.length)) != -1) {
System.out.write(buffer2, 0, read2);
}
outStream2.close();
}
catch (IOException ioe2) {
ioe2.printStackTrace();
}
}
}.start();
Thread waitFor2 = new Thread() {
public void run() {
try {
proc2.waitFor();
}
catch(InterruptedException ie2) {
ie2.printStackTrace();
}
}
};
waitFor2.start();
int status2 = -1;
try {
waitFor2.join();
status2 = proc2.exitValue();
}
catch (InterruptedException ie2) {
ie2.printStackTrace();
}
System.out.println(status2);
}
catch (IOException e2) {
System.out.println("erreur d'execution " + Cmd2 + e2.toString());
e2.printStackTrace();
}
catch (IllegalThreadStateException e2) {
System.out.println("interruption 2" + Cmd2 + e2.toString());
e2.printStackTrace();
}
System.out.println("Computation is finish");
}
public static void main(String[] args) {
System.out.println("wait for client");
ServerSocket server;
try {
server = new ServerSocket(PORT);
while (true) {
Socket s = server.accept();
connection = new SamcefConnection(s);
while (connection.connection_closed != true) {
;
}
System.out.println("Samcef start launching computation");
runSamcef();
runPostPro();
//new FatigueAnalysisConnection(s);
}
}
catch (IOException e) {
System.out.println("Erreur à la création d'un objet Socket : "+ e.getMessage());
System.exit(1);
e.printStackTrace();
}
}
} |
Partager