Skip to content

Commit ab4617f

Browse files
committed
adding server status listener
1 parent 1526857 commit ab4617f

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/main/java/eu/mihosoft/vrl/v3d/CSGServer.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.security.SecureRandom;
1616
import java.security.cert.Certificate;
1717
import java.security.cert.X509Certificate;
18+
import java.util.ArrayList;
1819
import java.util.Date;
1920
import java.time.Instant;
2021
import java.time.temporal.ChronoUnit;
@@ -38,6 +39,7 @@ public class CSGServer {
3839
private static final String KEYSTORE_NAME = "CSGSelfSign";
3940
private String[] lines = null;
4041
private SSLServerSocket serverSocket2;
42+
private ArrayList<ICSGServerEvent> listeners = new ArrayList<>();
4143

4244
public CSGServer(int port, File APIKEYS) throws IOException {
4345
this.port = port;
@@ -54,7 +56,27 @@ public CSGServer(int port, File APIKEYS) throws IOException {
5456
System.err.println("NO API KEYFILE Provided: "+APIKEYS.getAbsolutePath());
5557
}
5658
}
57-
59+
public void addListener(ICSGServerEvent e) {
60+
if(listeners.contains(e))
61+
return;
62+
listeners.add(e);
63+
}
64+
public void removeListener(ICSGServerEvent e) {
65+
if(!listeners.contains(e))
66+
return;
67+
listeners.remove(e);
68+
}
69+
70+
private void fireStart() {
71+
for(ICSGServerEvent e:listeners) {
72+
try {
73+
e.starting();
74+
}catch(Throwable t) {
75+
t.printStackTrace();
76+
}
77+
}
78+
}
79+
5880
public static void ensureKeystoreExists(String keystorePath, String keystorePassword, String alias,
5981
String commonName) {
6082
File keystoreFile = new File(keystorePath);
@@ -181,8 +203,9 @@ public void start() throws Exception {
181203

182204
while (isRunning()) {
183205
try {
206+
fireStart();
184207
SSLSocket clientSocket = (SSLSocket) serverSocket2.accept();
185-
threadPool.execute(new CSGServerHandler(clientSocket,lines));
208+
threadPool.execute(new CSGServerHandler(clientSocket,lines,listeners));
186209
} catch (IOException e) {
187210
if (isRunning()) {
188211
System.err.println("Error accepting client connection: " + e.getMessage());

src/main/java/eu/mihosoft/vrl/v3d/CSGServerHandler.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ class CSGServerHandler implements Runnable {
1616

1717
private String[] APIKEY = null;
1818

19-
public CSGServerHandler(SSLSocket socket, String[] lines) {
19+
private ArrayList<ICSGServerEvent> listeners;
20+
21+
private ServerActionState state;
22+
23+
public CSGServerHandler(SSLSocket socket, String[] lines,ArrayList<ICSGServerEvent> listeners ) {
2024
this.clientSocket = socket;
2125
APIKEY = lines;
26+
this.listeners = listeners;
2227
}
2328

2429
@Override
@@ -31,6 +36,13 @@ public void run() {
3136

3237
// Read the CSG request
3338
CSGRequest request = (CSGRequest) ois.readObject();
39+
for(ICSGServerEvent e:listeners) {
40+
try {
41+
e.gotRequest(request.getOperation(),this);
42+
}catch(Throwable t) {
43+
t.printStackTrace();
44+
}
45+
}
3446
// System.out.println("Received request: " + request.getOperation());
3547
boolean APIPass = true;
3648
String apiKey2 = request.getAPIKey();
@@ -64,7 +76,7 @@ public void run() {
6476
// Send back the response
6577
oos.writeObject(response);
6678
oos.flush();
67-
79+
state = response.getState();
6880
// System.out.println("Sent response: " + response);
6981

7082
} catch (IOException | ClassNotFoundException e) {
@@ -83,6 +95,13 @@ private void close() {
8395
} catch (IOException e) {
8496
System.err.println("Error closing client socket: " + e.getMessage());
8597
}
98+
for(ICSGServerEvent e:listeners) {
99+
try {
100+
e.finishedOp(state,this);
101+
}catch(Throwable t) {
102+
t.printStackTrace();
103+
}
104+
}
86105
}
87106

88107
private CSGResponse processCSGRequest(CSGRequest request) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package eu.mihosoft.vrl.v3d;
2+
3+
public interface ICSGServerEvent {
4+
public void starting();
5+
6+
public void finishedOp(ServerActionState state,CSGServerHandler source);
7+
8+
public void gotRequest(CSGRemoteOperation operation,CSGServerHandler source);
9+
10+
}

0 commit comments

Comments
 (0)