Skip to content

Commit

Permalink
whole bunch of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dries007 committed Nov 16, 2014
1 parent a824455 commit 6bd9f4f
Show file tree
Hide file tree
Showing 21 changed files with 416 additions and 78 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/doubledoordev/backend/Main.java
Expand Up @@ -146,6 +146,7 @@ public static void main(String[] args) throws Exception
FileManagerSocketApplication.register();
ServerconsoleSocketApplication.register();
ConsoleSocketApplication.register();
AdvancedSettingsSocketApplication.register();

final NetworkListener networkListener = new NetworkListener("secured-listener");
//networkListener.setSecure(true);
Expand Down
Expand Up @@ -220,4 +220,14 @@ public void cmdShutdown(@Optional @Switch('f') boolean force) throws CommandExce
if (force) System.exit(0);
Main.shutdown();
}

@Command(aliases = {"command", "cmd"}, desc = "Send a command to one or more servers", usage = "<server ID (regex)> <message ...>", min = 2)
public void cmdCommand(Server[] servers, @Text String cmd) throws CommandException
{
for (Server server : servers)
{
if (!server.getOnline()) continue;
server.sendCmd(cmd);
}
}
}
88 changes: 88 additions & 0 deletions src/main/java/net/doubledoordev/backend/server/RestartingInfo.java
@@ -0,0 +1,88 @@
/*
* Unless otherwise specified through the '@author' tag or comments at
* the top of the file or on a specific portion of the code the following license applies:
*
* Copyright (c) 2014, DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* The header specified or the above copyright notice, this list of conditions
* and the following disclaimer below must be displayed at the top of the source code
* of any web page received while using any part of the service this software provides.
*
* The header to be displayed:
* This page was generated by DoubleDoorDevelopment's D3Backend or a derivative thereof.
*
* Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

package net.doubledoordev.backend.server;

import com.google.gson.annotations.Expose;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author Dries007
*/
@SuppressWarnings("UnusedDeclaration")
public class RestartingInfo
{
@Expose
public int globalTimeout = 24;
@Expose
public int whenEmpty = 30;
@Expose
public boolean timer = false;
@Expose
public boolean restartDailyTimeout = false;

private boolean restartNextRun = false;
private Date lastRestart = new Date(0L);

public void run(Server server)
{
try
{
if (!server.getOnline() && !server.isDownloading() && restartNextRun)
{
restartNextRun = false;
server.startServer();
}
}
catch (Exception e)
{

e.printStackTrace();
}
}

public String getLastRestart(String format)
{
return new SimpleDateFormat(format).format(lastRestart);
}
}
48 changes: 22 additions & 26 deletions src/main/java/net/doubledoordev/backend/server/Server.java
Expand Up @@ -49,6 +49,7 @@
import net.doubledoordev.backend.util.exceptions.ServerOfflineException;
import net.doubledoordev.backend.util.exceptions.ServerOnlineException;
import net.doubledoordev.backend.util.methodCaller.IMethodCaller;
import net.doubledoordev.backend.web.socket.ServerControlSocketApplication;
import net.doubledoordev.backend.web.socket.ServerconsoleSocketApplication;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
Expand Down Expand Up @@ -79,9 +80,6 @@ public class Server
public static final String SERVER_PORT = "server-port";
public static final String QUERY_PORT = "query.port";
public static final String QUERY_ENABLE = "enable-query";
public static final String RCON_ENABLE = "enable-rcon";
public static final String RCON_PASSWORD = "rcon.password";
public static final String RCON_PORT = "rcon.port";
public static final String SERVER_IP = "server-ip";

/*
Expand Down Expand Up @@ -119,19 +117,21 @@ public class Server
private List<String> coOwners = new ArrayList<>();
@Expose
private final Map<Integer, Dimension> dimensionMap = new HashMap<>();
@Expose
private RestartingInfo restartingInfo = new RestartingInfo();
/*
* END exposed Json data
*/
/**
* Diskspace var + timer to avoid long page load times.
*/
public int[] size = new int[3];
public int[] size = new int[3];
public QueryResponse cachedResponse;
/**
* Used to reroute server output to our console.
* NOT LOGGED TO FILE!
*/
Logger logger;
private Logger logger;
private File folder;
private File propertiesFile;
private long propertiesFileLastModified = 0L;
Expand Down Expand Up @@ -208,11 +208,6 @@ public String getRconPswd()
return rconPswd;
}

public Logger getLogger()
{
return logger;
}

public boolean isDownloading()
{
return downloading;
Expand Down Expand Up @@ -316,11 +311,6 @@ public int getServerPort()
return Integer.parseInt(properties.containsKey(SERVER_PORT) ? getProperty(SERVER_PORT) : "-1");
}

public int getRconPort()
{
return Integer.parseInt(properties.containsKey(RCON_PORT) ? getProperty(RCON_PORT) : "-1");
}

public int getOnlinePlayers()
{
return cachedResponse == null ? 0 : cachedResponse.getOnlinePlayers();
Expand Down Expand Up @@ -469,6 +459,12 @@ public String toString()
return getID();
}

public RestartingInfo getRestartingInfo()
{
if (restartingInfo == null) restartingInfo = new RestartingInfo();
return restartingInfo;
}

/*
* ========================================================================================
* SETTERS
Expand Down Expand Up @@ -658,10 +654,7 @@ public void run()
}
catch (Exception e)
{
printLine("##################################################################");
printLine("Error downloading a new minecraft jar (version " + version + ")");
printLine("##################################################################");
logger.error(e);
error(e);
}
downloading = false;
}
Expand Down Expand Up @@ -990,7 +983,7 @@ public void run()
}
catch (IOException e)
{
logger.error(e);
error(e);
}
}
}, ID.concat("-streamEater")).start();
Expand All @@ -999,7 +992,7 @@ public void run()
}
catch (IOException e)
{
logger.error(e);
error(e);
}
starting = false;
}
Expand All @@ -1012,6 +1005,14 @@ public void printLine(String line)
ServerconsoleSocketApplication.sendLine(this, line);
}

public void error(Throwable e)
{
logger.error(e);
StringWriter error = new StringWriter();
e.printStackTrace(new PrintWriter(error));
ServerconsoleSocketApplication.sendLine(this, error.toString());
}

/**
* Stop the server gracefully
*/
Expand Down Expand Up @@ -1127,12 +1128,7 @@ private void normalizeProperties()
if (Settings.SETTINGS.fixedIP) properties.setProperty(SERVER_IP, ip);
else ip = properties.getProperty(SERVER_IP, ip);

if (Settings.SETTINGS.fixedPorts) properties.setProperty(RCON_PORT, String.valueOf(rconPort));
else rconPort = Integer.parseInt(properties.getProperty(RCON_PORT, String.valueOf(rconPort)));

properties.put(RCON_ENABLE, "true");
properties.put(QUERY_ENABLE, "true");
properties.put(RCON_PASSWORD, rconPswd);
}

private void update()
Expand Down
Expand Up @@ -154,8 +154,7 @@ public void doBackup(File zip, File folder, FilenameFilter filter)
catch (IOException | ZipException e)
{
if (server.getOnline()) server.sendCmd("say Error when making backup");
server.logger.warn(e);
e.printStackTrace();
server.error(e);
}
if (server.getOnline())
{
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/doubledoordev/backend/util/Cache.java
Expand Up @@ -330,6 +330,7 @@ public void run()
for (Server server : Settings.SETTINGS.servers.values())
{
server.renewQuery();
server.getRestartingInfo().run(server);
}
}
}, "cache-rConAndQuery").start();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/doubledoordev/backend/util/Constants.java
Expand Up @@ -80,6 +80,7 @@ public class Constants
public static final String ERROR = "error";
public static final String DATA = "data";
public static final String DIM = "DIM";
public static final String RESTARTING_INFO = "RestartingInfo";
/*
* FilenameFilter constants
*/
Expand Down Expand Up @@ -136,7 +137,7 @@ public boolean accept(File dir, String name)
public static final long REALLY_LONG_CACHE_TIMEOUT = 1000 * 60 * 60 * 24; // 24 hours
public static final long LONG_CACHE_TIMEOUT = 1000 * 60 * 60; // 1 hour
public static final long MEDIUM_CACHE_TIMEOUT = 1000 * 60; // 1 minute
public static final long SHORT_CACHE_TIMEOUT = 1000 * 10; // 20 seconds
public static final long SHORT_CACHE_TIMEOUT = 1000 * 10; // 10 seconds
/*
* Pattern constants
*/
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/doubledoordev/backend/util/Helper.java
Expand Up @@ -309,12 +309,9 @@ public static String getReadOnlyProperties()
{
array.add(new JsonPrimitive(Server.SERVER_PORT));
array.add(new JsonPrimitive(Server.QUERY_PORT));
array.add(new JsonPrimitive(Server.RCON_PORT));
}
if (SETTINGS.fixedIP) array.add(new JsonPrimitive(Server.SERVER_IP));

array.add(new JsonPrimitive(Server.RCON_ENABLE));
array.add(new JsonPrimitive(Server.RCON_PASSWORD));
array.add(new JsonPrimitive(Server.QUERY_ENABLE));

return array.toString();
Expand Down
Expand Up @@ -61,7 +61,6 @@ public int getNextAvailablePort(int ignored) throws OutOfPortsException
for (Server server : Settings.SETTINGS.servers.values())
{
usedPorts.add(server.getServerPort());
usedPorts.add(server.getRconPort());
}
for (int port = min; port < max; port++)
{
Expand Down

0 comments on commit 6bd9f4f

Please sign in to comment.