Skip to content

Commit

Permalink
Replace sleeping while loop with watchservice
Browse files Browse the repository at this point in the history
  • Loading branch information
karoliineh committed Feb 28, 2022
1 parent ae9b5a0 commit db670e6
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/main/java/goblintserver/GoblintServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import java.io.*;
import java.nio.file.*;

import com.google.gson.*;

Expand Down Expand Up @@ -64,13 +65,23 @@ public boolean startGoblintServer() {
return false;
}

while(!goblintSocket.exists()) {
Thread.sleep(1000);
// wait until Goblint socket is created before continuing
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get(System.getProperty("user.dir"));
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE );
WatchKey key;
while ((key = watchService.take()) != null) {
for (WatchEvent<?> event : key.pollEvents()) {
if (event.context().toString().equals(goblintSocket.toString())) {
log.info("Goblint server started.");
return true;
}
}
key.reset();
}

log.info("Goblint server started.");

return true;
return false;

} catch (IOException | InvalidExitValueException | InterruptedException | TimeoutException e) {
this.magpieServer.forwardMessageToClient(new MessageParams(MessageType.Error, "Running Goblint failed. " + e.getMessage()));
return false;
Expand Down

0 comments on commit db670e6

Please sign in to comment.