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

Commit

Permalink
Add StatusRequest|Response
Browse files Browse the repository at this point in the history
  • Loading branch information
vwiencek committed Sep 3, 2014
1 parent f5aa457 commit db304ae
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 107 deletions.
27 changes: 16 additions & 11 deletions syncany-cli/src/main/java/org/syncany/cli/CommandLineClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
Expand Down Expand Up @@ -64,11 +63,12 @@
import org.syncany.config.Logging;
import org.syncany.config.UserConfig;
import org.syncany.config.to.PortTO;
import org.syncany.operations.daemon.messages.CliRequest;
import org.syncany.operations.daemon.messages.CliResponse;
import org.syncany.operations.daemon.messages.MessageFactory;
import org.syncany.operations.daemon.messages.Response;
import org.syncany.operations.daemon.messages.StatusRequest;
import org.syncany.util.EnvironmentUtil;
import org.syncany.util.PidFileUtil;

/**
* The command line client implements a typical CLI. It represents the first entry
Expand Down Expand Up @@ -315,9 +315,11 @@ private int runCommand(Command command, String commandName, String[] commandArgs
portFile = config.getPortFile();
}

File PIDFile = new File(UserConfig.getUserConfigDir(), "daemon.pid");
boolean localDirHandledInDaemonScope = portFile != null && portFile.exists();
boolean daemonRunning = PidFileUtil.isProcessRunning(PIDFile);
boolean needsToRunInInitializedScope = command.getRequiredCommandScope() == CommandScope.INITIALIZED_LOCALDIR;
boolean sendToRest = localDirHandledInDaemonScope && needsToRunInInitializedScope;
boolean sendToRest = daemonRunning & localDirHandledInDaemonScope && needsToRunInInitializedScope;

if (sendToRest) {
return sendToRest(command, commandName, commandArgs, portFile);
Expand Down Expand Up @@ -374,14 +376,17 @@ private int sendToRest(Command command, String commandName, String[] commandArgs
logger.log(Level.INFO, "Sending HTTP Request to: " + SERVER_URI);

// Create and send HTTP/REST request
CliRequest cliRequest = new CliRequest();

cliRequest.setId(Math.abs(new Random().nextInt()));
cliRequest.setRoot(config.getLocalDir().getAbsolutePath());
cliRequest.setCommand(commandName);
cliRequest.setCommandArgs(Arrays.asList(commandArgs));

post.setEntity(new StringEntity(MessageFactory.toRequest(cliRequest)));
switch (commandName.toLowerCase()) {
case "status":
StatusRequest sr = new StatusRequest();
sr.setId(Math.abs(new Random().nextInt()));
sr.setRoot(config.getLocalDir().getAbsolutePath());
post.setEntity(new StringEntity(MessageFactory.toRequest(sr)));
break;

default:
return 0;
}

// Handle response
HttpResponse httpResponse = client.execute(post);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package org.syncany.operations.daemon;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -29,7 +28,6 @@
import java.util.logging.Logger;

import org.simpleframework.xml.core.Persister;
import org.syncany.Client;
import org.syncany.config.Config;
import org.syncany.config.ConfigException;
import org.syncany.config.to.PortTO;
Expand All @@ -45,8 +43,6 @@
import org.syncany.operations.Assembler;
import org.syncany.operations.Downloader;
import org.syncany.operations.daemon.messages.BadRequestResponse;
import org.syncany.operations.daemon.messages.CliRequest;
import org.syncany.operations.daemon.messages.CliResponse;
import org.syncany.operations.daemon.messages.GetDatabaseVersionHeadersRequest;
import org.syncany.operations.daemon.messages.GetDatabaseVersionHeadersResponse;
import org.syncany.operations.daemon.messages.GetFileHistoryRequest;
Expand All @@ -58,11 +54,15 @@
import org.syncany.operations.daemon.messages.GetFileTreeResponse;
import org.syncany.operations.daemon.messages.RestoreRequest;
import org.syncany.operations.daemon.messages.RestoreResponse;
import org.syncany.operations.daemon.messages.StatusRequest;
import org.syncany.operations.daemon.messages.StatusResponse;
import org.syncany.operations.daemon.messages.WatchEventResponse;
import org.syncany.operations.daemon.messages.WatchRequest;
import org.syncany.operations.restore.RestoreOperation;
import org.syncany.operations.restore.RestoreOperationOptions;
import org.syncany.operations.restore.RestoreOperationResult;
import org.syncany.operations.status.StatusOperation;
import org.syncany.operations.status.StatusOperationResult;
import org.syncany.operations.watch.WatchOperation;
import org.syncany.operations.watch.WatchOperationListener;
import org.syncany.operations.watch.WatchOperationOptions;
Expand Down Expand Up @@ -166,15 +166,42 @@ else if (watchRequest instanceof GetDatabaseVersionHeadersRequest) {
else if (watchRequest instanceof RestoreRequest) {
handleRestoreRequest((RestoreRequest) watchRequest);
}
else if (watchRequest instanceof CliRequest) {
handleCliRequest((CliRequest) watchRequest);
else if (watchRequest instanceof StatusRequest) {
handleStatusRequest((StatusRequest) watchRequest);
}
// else if (watchRequest instanceof CliRequest) {
// handleCliRequest((CliRequest) watchRequest);
// }
else {
eventBus.post(new BadRequestResponse(watchRequest.getId(), "Invalid watch request for root."));
}
}
}

private void handleStatusRequest(StatusRequest statusRequest) {
try {
StatusOperation statusOperation = new StatusOperation(config);
StatusOperationResult statusOperationResult = statusOperation.execute();
StatusResponse statusResponse = new StatusResponse(statusRequest.getId());

for (String f : statusOperationResult.getChangeSet().getChangedFiles()) {
statusResponse.addChangedFile(f);
}
for (String f : statusOperationResult.getChangeSet().getDeletedFiles()) {
statusResponse.addDeletedFile(f);
}
for (String f : statusOperationResult.getChangeSet().getNewFiles()) {
statusResponse.addNewFile(f);
}

eventBus.post(statusResponse);
}
catch (Exception e) {
logger.log(Level.WARNING, "Cannot reassemble file.", e);
eventBus.post(new BadRequestResponse(statusRequest.getId(), "Cannot obtain status."));
}
}

private void handleGetFileRequest(GetFileRequest fileRequest) {
try {
FileHistoryId fileHistoryId = FileHistoryId.parseFileId(fileRequest.getFileHistoryId());
Expand Down Expand Up @@ -222,50 +249,41 @@ private void handleRestoreRequest(RestoreRequest restoreRequest) {
}
}

private void handleCliRequest(CliRequest cliRequest) {
if (watchOperation.isSyncRunning() || watchOperation.isSyncRequested()) {
handleCliRequestSyncRunning(cliRequest);
}
else {
watchOperation.pause();
handleCliRequestNoSyncRunning(cliRequest);
watchOperation.resume();
}
}
// private void handleCliRequest(CliRequest cliRequest) {
// if (watchOperation.isSyncRunning() || watchOperation.isSyncRequested()) {
// handleCliRequestSyncRunning(cliRequest);
// }
// else {
// watchOperation.pause();
// handleCliRequestNoSyncRunning(cliRequest);
// watchOperation.resume();
// }
// }

private void handleCliRequestSyncRunning(CliRequest cliRequest) {
CliResponse cliResponse = new CliResponse(cliRequest.getId(), "Cannot run CLI commands while sync is running or requested.\n");
eventBus.post(cliResponse);
}
// private void handleCliRequestSyncRunning(CliRequest cliRequest) {
// CliResponse cliResponse = new CliResponse(cliRequest.getId(), "Cannot run CLI commands while sync is running or requested.\n");
// eventBus.post(cliResponse);
// }

private void handleCliRequestNoSyncRunning(CliRequest cliRequest) {
try {
//Command command = CommandFactory.getInstance(cliRequest.getCommand());
String[] commandArgs = cliRequest.getCommandArgs().toArray(new String[0]);

ByteArrayOutputStream cliOutputStream = new ByteArrayOutputStream();

Client client = new Client();
client.setConfig(config);

//command.setOut(new PrintStream(cliOutputStream));
//command.setClient(client);
//command.setLocalDir(config.getLocalDir());

//command.execute(commandArgs);

String cliOutput = cliOutputStream.toString();

CliResponse cliResponse = new CliResponse(cliRequest.getId(), cliOutput);
eventBus.post(cliResponse);

cliOutputStream.close();
}
catch (Exception e) {
logger.log(Level.WARNING, "Exception thrown when running CLI command through daemon: " + e, e);
eventBus.post(new BadRequestResponse(cliRequest.getId(), e.getMessage()));
}
}
// private void handleCliRequestNoSyncRunning(CliRequest cliRequest) {
// try {
// Operation op = OperationFactory.getInstance(cliRequest.getCommand(), config);
// OperationResult result = op.execute();
//
// ByteArrayOutputStream cliOutputStream = new ByteArrayOutputStream();
//
// String cliOutput = cliOutputStream.toString();
//
// CliResponse cliResponse = new CliResponse(cliRequest.getId(), cliOutput);
// eventBus.post(cliResponse);
//
// cliOutputStream.close();
// }
// catch (Exception e) {
// logger.log(Level.WARNING, "Exception thrown when running CLI command through daemon: " + e, e);
// eventBus.post(new BadRequestResponse(cliRequest.getId(), e.getMessage()));
// }
// }

private void handleGetFileTreeRequest(GetFileTreeRequest fileTreeRequest) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.operations.daemon.messages;

import java.util.ArrayList;
import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;

public class CliRequest extends WatchRequest {
@Element(required = true)
private String command;

@ElementList(entry = "arg")
private ArrayList<String> commandArgs;

public void setCommand(String command) {
this.command = command;
}

public void setCommandArgs(List<String> commandArgs) {
this.commandArgs = new ArrayList<>(commandArgs);
}

public String getCommand() {
return command;
}

public ArrayList<String> getCommandArgs() {
return commandArgs;
}
}
///*
// * Syncany, www.syncany.org
// * Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// */
//package org.syncany.operations.daemon.messages;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import org.simpleframework.xml.Element;
//import org.simpleframework.xml.ElementList;
//
//public class CliRequest extends WatchRequest {
// @Element(required = true)
// private String command;
//
// @ElementList(entry = "arg")
// private ArrayList<String> commandArgs;
//
// public void setCommand(String command) {
// this.command = command;
// }
//
// public void setCommandArgs(List<String> commandArgs) {
// this.commandArgs = new ArrayList<>(commandArgs);
// }
//
// public String getCommand() {
// return command;
// }
//
// public ArrayList<String> getCommandArgs() {
// return commandArgs;
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.operations.daemon.messages;


public class StatusRequest extends WatchRequest {

}

0 comments on commit db304ae

Please sign in to comment.