Unverified Commit 577a8dae authored by Julien Veyssier's avatar Julien Veyssier
Browse files

server timeout to free the listening port after 50sec, and allow another model instance to run


Signed-off-by: default avatarJulien Veyssier <eneiluj@posteo.net>
No related merge requests found
Showing with 50 additions and 6 deletions
+50 -6
...@@ -71,15 +71,15 @@ public class Whatasit extends JAMSComponent { ...@@ -71,15 +71,15 @@ public class Whatasit extends JAMSComponent {
} }
public void run() { public void run() {
System.out.println("WHATASIT begin run "+i); System.out.println("WHATASIT waiting permission to launch run "+i);
try { try {
sOkStep.acquire(); sOkStep.acquire();
} catch (InterruptedException exc) { } catch (InterruptedException exc) {
System.out.println(exc); System.out.println(exc);
} }
System.out.println("WHATASIT !!DOING!! run "+i); System.out.println("WHATASIT !!DOING!! 2sec run "+i);
try { try {
Thread.sleep(5000); Thread.sleep(2000);
} catch (InterruptedException exc) { } catch (InterruptedException exc) {
System.out.println(exc); System.out.println(exc);
} }
...@@ -91,7 +91,7 @@ public class Whatasit extends JAMSComponent { ...@@ -91,7 +91,7 @@ public class Whatasit extends JAMSComponent {
} }
public void cleanup() throws JAMSEntity.NoSuchAttributeException { public void cleanup() throws JAMSEntity.NoSuchAttributeException {
System.out.println("STOPPING THREAD"); System.out.println("STOPPING THREAD at simulation end");
comServer.stop(); comServer.stop();
} }
...@@ -99,30 +99,69 @@ public class Whatasit extends JAMSComponent { ...@@ -99,30 +99,69 @@ public class Whatasit extends JAMSComponent {
comServer = new ComServer(9999, "WhatasitThread"); comServer = new ComServer(9999, "WhatasitThread");
comServer.start(); comServer.start();
} }
private class TimeoutProcess extends Thread {
private int nbSec;
private ComServer server;
public TimeoutProcess(int nbSec, ComServer server) {
this.nbSec = nbSec;
this.server = server;
}
@Override
public void run() {
try {
Thread.sleep(nbSec * 1000);
} catch (InterruptedException exc) {
System.out.println(exc);
}
System.out.println("timeout reached, KILLING server");
// kill socket
server.stopServer();
// stop the thread
server.stop();
}
}
private class ComServer extends Thread { private class ComServer extends Thread {
String threadName; String threadName;
int port; int port;
TimeoutProcess timeoutProcess = null;
ServerSocket ss;
public ComServer(int port, String threadName) { public ComServer(int port, String threadName) {
super(threadName); super(threadName);
this.threadName = threadName; this.threadName = threadName;
this.port = port; this.port = port;
try {
this.ss = new ServerSocket(port);
} catch (Exception e) {
System.err.println(e);
}
}
public void stopServer() {
try {
ss.close();
} catch (Exception e) {
System.err.println(e);
}
} }
@Override @Override
public void run() { public void run() {
try { try {
ServerSocket ss = new ServerSocket(port);
for (;;) { for (;;) {
System.out.println("server starting LISTENING on port "+port); System.out.println("server starting LISTENING on port "+port);
// Wait for a client to connect. // Wait for a client to connect.
Socket client = ss.accept(); Socket client = ss.accept();
System.out.println("connection received"); System.out.println("connection received");
if (timeoutProcess != null) {
timeoutProcess.stop();
}
// wait for component to tell us it has finished a step to process // wait for component to tell us it has finished a step to process
// further resquest // further resquest
...@@ -156,6 +195,11 @@ public class Whatasit extends JAMSComponent { ...@@ -156,6 +195,11 @@ public class Whatasit extends JAMSComponent {
in.close(); // Close the input stream in.close(); // Close the input stream
client.close(); // Close the socket itself client.close(); // Close the socket itself
// launch timeout
System.out.println("launching timeout");
timeoutProcess = new TimeoutProcess(50, this);
timeoutProcess.start();
sOkStep.release(); sOkStep.release();
} // Now loop again, waiting for the next connection } // Now loop again, waiting for the next connection
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment