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

Commit

Permalink
Prevent standby implementation in ActionFileHandler, relates to #164
Browse files Browse the repository at this point in the history
  • Loading branch information
binwiederhier committed Aug 8, 2014
1 parent bc5d0dc commit 04da8b0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 83 deletions.
Expand Up @@ -76,7 +76,6 @@ public class DaemonOperation extends Operation {
private WebServer webServer;
private WatchServer watchServer;
private ControlServer controlServer;
private StandbyPreventor standbyPreventor;
private LocalEventBus eventBus;
private DaemonConfigTO daemonConfig;
private PortTO portTO;
Expand All @@ -86,7 +85,6 @@ public DaemonOperation(Config config) {

this.pidFile = new File(UserConfig.getUserConfigDir(), PID_FILE);
this.daemonConfigFile = new File(UserConfig.getUserConfigDir(), DAEMON_FILE);
this.standbyPreventor = new StandbyPreventor();
}

@Override
Expand All @@ -109,7 +107,6 @@ private void startOperation() throws ServiceAlreadyStartedException, ConfigExcep

startWebServer();
startWatchServer();
startStandbyPreventor();

enterControlLoop(); // This blocks until SHUTDOWN is received!
}
Expand Down Expand Up @@ -148,7 +145,6 @@ private void enterControlLoop() throws IOException, ServiceAlreadyStartedExcepti
private void stopOperation() {
stopWebServer();
stopWatchServer();
stopStandbyPreventor();
}

private void reloadOperation() {
Expand Down Expand Up @@ -261,16 +257,4 @@ private void stopWatchServer() {
logger.log(Level.INFO, "Stopping watch server ...");
watchServer.stop();
}

// Standby preventor

private void startStandbyPreventor() {
logger.log(Level.INFO, "Starting standby preventor...");
new Thread(standbyPreventor).start();
}

private void stopStandbyPreventor() {
logger.log(Level.INFO, "Stopping standby preventor ...");
standbyPreventor.stop();
}
}

This file was deleted.

17 changes: 14 additions & 3 deletions syncany-lib/src/main/java/org/syncany/config/Logging.java
Expand Up @@ -56,9 +56,8 @@ public synchronized static void init() {
}

// Turn off INFO message of Reflections library (dirty, but the only way!)
Reflections.log = null;
System.setProperty("hsqldb.reconfig_logging", "false");

disableUnwantedLoggers();

// Load logging.properties
try {
// Use file if exists, else use file embedded in JAR
Expand All @@ -78,6 +77,15 @@ public synchronized static void init() {
}
}

private static void disableUnwantedLoggers() {
Reflections.log = null;
System.setProperty("hsqldb.reconfig_logging", "false");

if (Logger.getLogger("sun.awt.X11.timeoutTask.XToolkit") != null) {
Logger.getLogger("sun.awt.X11.timeoutTask.XToolkit").setLevel(Level.OFF);
}
}

public static void setGlobalLogLevel(Level targetLogLevel) {
for (Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames(); loggerNames.hasMoreElements(); ) {
String loggerName = loggerNames.nextElement();
Expand All @@ -93,6 +101,9 @@ public static void setGlobalLogLevel(Level targetLogLevel) {
}

Logger.getLogger("").setLevel(targetLogLevel);

// Make sure the unwanted loggers stay quiet
disableUnwantedLoggers();
}

public static void addGlobalHandler(Handler targetHandler) {
Expand Down
15 changes: 13 additions & 2 deletions syncany-lib/src/main/java/org/syncany/config/UserConfig.java
Expand Up @@ -41,9 +41,10 @@ public class UserConfig {
*
*/

private static final File USER_APP_DIR_WINDOWS = new File(System.getenv("APPDATA") + "\\Syncany");
// This field is not final to enable a PluginOperationTest
// These fields are not final to enable a PluginOperationTest
private static File USER_APP_DIR_WINDOWS = new File(System.getenv("APPDATA") + "\\Syncany");
private static File USER_APP_DIR_UNIX_LIKE = new File(System.getProperty("user.home") + "/.config/syncany");

private static final String USER_PLUGINS_LIB_DIR = "plugins/lib";
private static final String USER_PLUGINS_USERDATA_DIR_FORMAT = "plugins/userdata/%s";
private static final String USER_CONFIG_FILE = "userconfig.xml";
Expand All @@ -55,6 +56,8 @@ public class UserConfig {
private static File userTrustStoreFile;
private static KeyStore userTrustStore;

private static boolean preventStandby;

static {
init();
}
Expand Down Expand Up @@ -126,6 +129,10 @@ public static void storeTrustStore() {
}
}

public static boolean preventStandbyEnabled() {
return preventStandby;
}

private static void initUserAppDirs() {
userConfigDir = (EnvironmentUtil.isWindows()) ? USER_APP_DIR_WINDOWS : USER_APP_DIR_UNIX_LIKE;
userConfigDir.mkdirs();
Expand All @@ -149,9 +156,13 @@ private static void loadAndInitUserConfigFile(File userConfigFile) {
try {
UserConfigTO userConfigTO = UserConfigTO.load(userConfigFile);

// System properties
for (Map.Entry<String, String> systemProperty : userConfigTO.getSystemProperties().entrySet()) {
System.setProperty(systemProperty.getKey(), systemProperty.getValue());
}

// Other options
preventStandby = userConfigTO.preventStandbyEnabled();
}
catch (ConfigException e) {
System.err.println("ERROR: " + e.getMessage());
Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.TreeMap;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;
Expand All @@ -43,14 +44,22 @@ public class UserConfigTO {
@ElementMap(name="systemProperties", entry="property", key="name", required=false, attribute=true)
private TreeMap<String, String> systemProperties;

@Element(name="preventStandby", required=false)
private boolean preventStandby;

public UserConfigTO() {
this.systemProperties = new TreeMap<String, String>();
this.preventStandby = false;
}

public Map<String, String> getSystemProperties() {
return systemProperties;
}

public boolean preventStandbyEnabled() {
return preventStandby;
}

public static UserConfigTO load(File file) throws ConfigException {
try {
return new Persister().read(UserConfigTO.class, file);
Expand Down
Expand Up @@ -17,12 +17,16 @@
*/
package org.syncany.operations;

import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.syncany.config.UserConfig;
import org.syncany.operations.cleanup.CleanupOperation;
import org.syncany.plugins.StorageException;
import org.syncany.plugins.transfer.TransferManager;
Expand Down Expand Up @@ -107,7 +111,11 @@ private void scheduleActionRenewalTask() {
actionRenewalTimer.schedule(new TimerTask() {
@Override
public void run() {
renewActionFile();
renewActionFile();

if (UserConfig.preventStandbyEnabled()) {
preventStandby();
}
}
}, ACTION_RENEWAL_INTERVAL, ACTION_RENEWAL_INTERVAL);
}
Expand All @@ -133,4 +141,21 @@ private synchronized void renewActionFile() {
logger.log(Level.SEVERE, "ERROR: Cannot renew action file!", e);
}
}

private void preventStandby() {
try {
Robot robot = new Robot();

Point currentMousePosition = MouseInfo.getPointerInfo().getLocation();
Point tempMousePosition = (currentMousePosition.x > 0) ? new Point(currentMousePosition.x-10, currentMousePosition.y) : new Point(currentMousePosition.x+10, currentMousePosition.y);

logger.log(Level.INFO, "Standby prevention: Moving mouse 1px (and back): " + currentMousePosition);

robot.mouseMove(tempMousePosition.x, tempMousePosition.y);
robot.mouseMove(currentMousePosition.x, currentMousePosition.y);
}
catch (Exception e) {
logger.log(Level.WARNING, "Standby prevention failed (headless mode?).", e);
}
}
}

0 comments on commit 04da8b0

Please sign in to comment.