Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Writing and reading portFile
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 3, 2014
1 parent 1c9ba58 commit 457d44c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
26 changes: 24 additions & 2 deletions syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java
Expand Up @@ -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;
Expand All @@ -42,13 +44,15 @@
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;
import org.apache.http.entity.StringEntity;
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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -299,14 +307,21 @@ 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();
boolean needsToRunInInitializedScope = command.getRequiredCommandScope() == CommandScope.INITIALIZED_LOCALDIR;
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 {
Expand All @@ -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();

Expand All @@ -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) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -85,16 +86,18 @@ public class WatchRunner implements WatchOperationListener {

private Config config;
private File portFile;
private int port;
private Thread watchThread;
private WatchOperation watchOperation;
private WatchOperationResult watchOperationResult;
private LocalEventBus eventBus;

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);
Expand All @@ -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());
Expand Down
Expand Up @@ -55,6 +55,7 @@ public class WatchServer {

private Map<File, WatchRunner> watchOperations;
private LocalEventBus eventBus;
private int port;

public WatchServer() {
this.watchOperations = new TreeMap<File, WatchRunner>();
Expand All @@ -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<File, FolderTO> watchedFolders = getFolderMap(daemonConfigTO.getFolders());

Expand Down Expand Up @@ -109,7 +114,7 @@ private void startWatchOperations(Map<File, FolderTO> 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);
Expand Down
1 change: 1 addition & 0 deletions syncany-lib/src/main/java/org/syncany/config/Config.java
Expand Up @@ -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;
Expand Down

0 comments on commit 457d44c

Please sign in to comment.