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

Commit

Permalink
Prettification
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed Sep 8, 2014
1 parent 8ed9029 commit 5b1bed2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
76 changes: 43 additions & 33 deletions syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ private int runCommand(Command command, String commandName, String[] commandArgs
portFile = config.getPortFile();
}

File PIDFile = new File(UserConfig.getUserConfigDir(), DaemonOperation.PID_FILE);
File daemonPidFile = new File(UserConfig.getUserConfigDir(), DaemonOperation.PID_FILE);

boolean localDirHandledInDaemonScope = portFile != null && portFile.exists();
boolean daemonRunning = PidFileUtil.isProcessRunning(PIDFile);
boolean daemonRunning = PidFileUtil.isProcessRunning(daemonPidFile);
boolean needsToRunInInitializedScope = command.getRequiredCommandScope() == CommandScope.INITIALIZED_LOCALDIR;
boolean sendToRest = daemonRunning & localDirHandledInDaemonScope && needsToRunInInitializedScope;

Expand Down Expand Up @@ -379,49 +380,58 @@ private int sendToRest(Command command, String commandName, String[] commandArgs
String SERVER_URI = SERVER_SCHEMA + SERVER_HOSTNAME + ":" + portConfig.getPort() + SERVER_REST_API;
HttpPost post = new HttpPost(SERVER_URI);

// Build and send request, print response
logger.log(Level.INFO, "Sending HTTP Request to: " + SERVER_URI);

Request request = null;

// Create and send HTTP/REST request

if (command.getRequiredCommandScope() == CommandScope.INITIALIZED_LOCALDIR) {
request = buildFolderRequestFromCommand(command, commandName, config.getLocalDir().getAbsolutePath());
}
else {
request = buildManagementRequestFromCommand(command, commandName);
}

Request request = createRestRequest(command, commandName);
post.setEntity(new StringEntity(MessageFactory.toRequest(request)));

// 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);
HttpResponse httpResponse = client.execute(post);
int exitCode = handleRestResponse(command, httpResponse);

Response response = MessageFactory.createResponse(responseStr);

if (response instanceof FolderResponse) {
FolderResponse folderResponse = (FolderResponse)response;
command.printResults(folderResponse.getResult());
}
else if (response instanceof AlreadySyncingResponse) {
out.println("Daemon is already syncing, please retry later");
}
else if (response instanceof BadRequestResponse) {
out.println("Invalid Request : " + response.getMessage());
}

return 0;
return exitCode;
}
catch (Exception e) {
logger.log(Level.SEVERE, "Command " + command.toString() + " FAILED. ", e);
return showErrorAndExit(e.getMessage());
}
}

private Request createRestRequest(Command command, String commandName) throws Exception {
if (command.getRequiredCommandScope() == CommandScope.INITIALIZED_LOCALDIR) {
return buildFolderRequestFromCommand(command, commandName, config.getLocalDir().getAbsolutePath());
}
else {
return buildManagementRequestFromCommand(command, commandName);
}
}

private int handleRestResponse(Command command, HttpResponse httpResponse) throws Exception {
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 FolderResponse) {
FolderResponse folderResponse = (FolderResponse) response;
command.printResults(folderResponse.getResult());

return 0;
}
else if (response instanceof AlreadySyncingResponse) {
out.println("Daemon is already syncing, please retry later.");
return 1;
}
else if (response instanceof BadRequestResponse) {
out.println("Invalid Request : " + response.getMessage());
return 1;
}

return 1;
}

private Request buildManagementRequestFromCommand(Command command, String commandName) throws Exception {
String thisPackage = "org.syncany.operations.daemon.messages";
String camelCaseMessageType = StringUtil.toCamelCase(commandName) + "ManagementRequest";
Expand Down
1 change: 0 additions & 1 deletion syncany-cli/src/main/java/org/syncany/cli/InitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.syncany.crypto.CipherSpec;
import org.syncany.crypto.CipherSpecs;
import org.syncany.crypto.CipherUtil;
import org.syncany.operations.OperationOptions;
import org.syncany.operations.OperationResult;
import org.syncany.operations.init.InitOperationOptions;
import org.syncany.operations.init.InitOperationResult;
Expand Down

0 comments on commit 5b1bed2

Please sign in to comment.