From 457d44c8562c2a11a11d6bad5bb990eac5373d77 Mon Sep 17 00:00:00 2001 From: Pim Otte Date: Sun, 3 Aug 2014 17:03:11 +0200 Subject: [PATCH] Writing and reading portFile --- .../org/syncany/cli/CommandLineClient.java | 26 +++++++++++++++++-- .../operations/daemon/WatchRunner.java | 20 +++++++++++--- .../operations/daemon/WatchServer.java | 9 +++++-- .../main/java/org/syncany/config/Config.java | 1 + 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java b/syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java index 48e203669..3368a3ea0 100644 --- a/syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java +++ b/syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java @@ -19,7 +19,9 @@ import static java.util.Arrays.asList; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -42,6 +44,7 @@ import joptsimple.OptionSet; import joptsimple.OptionSpec; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; @@ -49,6 +52,7 @@ import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.syncany.Client; +import org.syncany.config.Config; import org.syncany.config.ConfigException; import org.syncany.config.ConfigHelper; import org.syncany.config.LogFormatter; @@ -58,6 +62,7 @@ import org.syncany.operations.daemon.messages.MessageFactory; import org.syncany.operations.daemon.messages.Response; import org.syncany.util.EnvironmentUtil; +import org.syncany.util.FileUtil; /** * The command line client implements a typical CLI. It represents the first entry @@ -69,7 +74,10 @@ public class CommandLineClient extends Client { private static final Logger logger = Logger.getLogger(CommandLineClient.class.getSimpleName()); - private static final String SERVER_URI = "http://localhost:8080/api/rs"; + private static final String SERVER_PROTOCOL = "http://"; + private static final String SERVER_HOSTNAME = "localhost"; + private static int SERVER_PORT = 8080; + private static final String SERVER_REST_API = "/api/rs"; private static final String LOG_FILE_PATTERN = "syncany.log"; private static final int LOG_FILE_COUNT = 4; @@ -299,7 +307,7 @@ private int runCommand(Command command, String commandName, String[] commandArgs File portFile = null; if (config != null) { - portFile = new File(config.getAppDir(), "port"); // TODO + portFile = new File(config.getAppDir(), Config.FILE_PORT); } boolean localDirHandledInDaemonScope = portFile != null && portFile.exists(); @@ -307,6 +315,13 @@ private int runCommand(Command command, String commandName, String[] commandArgs boolean sendToRest = localDirHandledInDaemonScope && needsToRunInInitializedScope; if (sendToRest) { + try (BufferedReader portFileReader = new BufferedReader(new FileReader(portFile))) { + SERVER_PORT = Integer.parseInt(portFileReader.readLine()); + } + catch (Exception e) { + logger.log(Level.SEVERE, "Cannot read REST server port from: " + portFile + ", because: " + e.getMessage()); + SERVER_PORT = 8080; + } return sendToRest(command, commandName, commandArgs); } else { @@ -332,9 +347,12 @@ private int runLocally(Command command, String[] commandArgs) { private int sendToRest(Command command, String commandName, String[] commandArgs) { CloseableHttpClient client = HttpClients.createDefault(); + String SERVER_URI = SERVER_PROTOCOL + SERVER_HOSTNAME + ":" + SERVER_PORT + SERVER_REST_API; HttpPost post = new HttpPost(SERVER_URI); try { + logger.log(Level.INFO, "Sending HTTP Request to: " + SERVER_URI); + // Create and send HTTP/REST request CliRequest cliRequest = new CliRequest(); @@ -347,8 +365,12 @@ private int sendToRest(Command command, String commandName, String[] commandArgs // Handle response HttpResponse httpResponse = client.execute(post); + logger.log(Level.FINE, "Received HttpResponse: " + httpResponse); + String responseStr = IOUtils.toString(httpResponse.getEntity().getContent()); + logger.log(Level.FINE, "Responding to message with responseString: " + responseStr); + Response response = MessageFactory.createResponse(responseStr); if (response instanceof CliResponse) { diff --git a/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchRunner.java b/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchRunner.java index 2e1d38a28..dc6707fa2 100644 --- a/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchRunner.java +++ b/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchRunner.java @@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileWriter; import java.io.PrintStream; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -85,6 +86,7 @@ public class WatchRunner implements WatchOperationListener { private Config config; private File portFile; + private int port; private Thread watchThread; private WatchOperation watchOperation; private WatchOperationResult watchOperationResult; @@ -92,9 +94,10 @@ public class WatchRunner implements WatchOperationListener { private SqlDatabase localDatabase; - public WatchRunner(Config config, WatchOperationOptions watchOperationOptions) throws ConfigException { + public WatchRunner(Config config, WatchOperationOptions watchOperationOptions, int port) throws ConfigException { this.config = config; - this.portFile = new File(config.getAppDir(), "port"); // TODO actually use this file properly, see #171 + this.portFile = new File(config.getAppDir(), Config.FILE_PORT); + this.port = port; this.watchOperation = new WatchOperation(config, watchOperationOptions, this); this.localDatabase = new SqlDatabase(config); @@ -110,9 +113,20 @@ public void run() { try { logger.log(Level.INFO, "STARTING watch at" + config.getLocalDir()); watchOperationResult = null; - portFile.createNewFile(); // TODO actually use this file properly, see #171 + + // Write port to portFile + portFile.createNewFile(); + try (FileWriter portFileWriter = new FileWriter(portFile)) { + String portStr = Integer.toString(port); + + logger.log(Level.INFO, "Writing Port file (for Port " + portStr + ") to " + portFile + " ..."); + + portFileWriter.write(portStr); + portFileWriter.close(); + } portFile.deleteOnExit(); + // Start operation watchOperationResult = watchOperation.execute(); logger.log(Level.INFO, "STOPPED watch at " + config.getLocalDir()); diff --git a/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchServer.java b/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchServer.java index 1cd47557c..9a4470c2e 100644 --- a/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchServer.java +++ b/syncany-daemon/src/main/java/org/syncany/operations/daemon/WatchServer.java @@ -55,6 +55,7 @@ public class WatchServer { private Map watchOperations; private LocalEventBus eventBus; + private int port; public WatchServer() { this.watchOperations = new TreeMap(); @@ -69,7 +70,11 @@ public void start(DaemonConfigTO daemonConfigTO) { public void reload(DaemonConfigTO daemonConfigTO) { logger.log(Level.INFO, "Starting/reloading watch server ... "); - + + // Update port number + port = daemonConfigTO.getWebServer().getPort(); + + // Restart threads try { Map watchedFolders = getFolderMap(daemonConfigTO.getFolders()); @@ -109,7 +114,7 @@ private void startWatchOperations(Map newWatchedFolderTOs) throw if (watchConfig != null) { logger.log(Level.INFO, "- Starting watch operation at " + localDir + " ..."); - WatchRunner watchOperationThread = new WatchRunner(watchConfig, watchOperationOptions); + WatchRunner watchOperationThread = new WatchRunner(watchConfig, watchOperationOptions, port); watchOperationThread.start(); watchOperations.put(localDir, watchOperationThread); diff --git a/syncany-lib/src/main/java/org/syncany/config/Config.java b/syncany-lib/src/main/java/org/syncany/config/Config.java index f8baa8526..4908ad215 100644 --- a/syncany-lib/src/main/java/org/syncany/config/Config.java +++ b/syncany-lib/src/main/java/org/syncany/config/Config.java @@ -60,6 +60,7 @@ public class Config { public static final String FILE_MASTER = "master"; public static final String FILE_IGNORE = ".syignore"; public static final String FILE_DATABASE = "local.db"; + public static final String FILE_PORT = "port"; private byte[] repoId; private String machineName;