Skip to content

Commit

Permalink
Partial commit 2. Changes include:
Browse files Browse the repository at this point in the history
- Added keeping of the last 25 lines (or exception traces in the case of errors) of log, to make identifying issue easier.
  • Loading branch information
dries007 committed Jul 16, 2017
1 parent 4824afc commit 26e3211
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
57 changes: 49 additions & 8 deletions src/main/java/net/doubledoordev/backend/TestMain.java
Expand Up @@ -19,23 +19,64 @@
package net.doubledoordev.backend;

import net.doubledoordev.backend.server.query.MCQuery;
import net.doubledoordev.backend.server.query.QueryResponse;

import java.io.IOException;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TestMain
{
public static void main(String[] args) throws Exception
{
MCQuery query = new MCQuery("vps2.dries007.net", 25569);
QueryResponse queryResponse;
while (true)
// testMCQuery();
testTaskStuff();
}

private static void testTaskStuff()
{
Timer timer = new Timer();

// timer.schedule();

// Calendar now = Calendar.getInstance();
Date now = Date.from(Instant.now());
// Instant now = Instant.now();


timer.scheduleAtFixedRate(new Task(), 0, 1000 * 10); // 10 sec
}

private static class Task extends TimerTask
{
@Override
public void run()
{
queryResponse = query.fullStat();
if (queryResponse != null) System.out.println(queryResponse.toString());
else System.out.println("null");
synchronized (Thread.currentThread())
System.out.println("Task run()");

Calendar now = Calendar.getInstance();
}
}

private static void testMCQuery() throws IOException
{
System.out.println(new MCQuery("vps2.dries007.net", 25501).fullStat());
}

private static void sleep1sec()
{
synchronized (Thread.currentThread())
{
try
{
Thread.currentThread().wait(1000);
}
catch (InterruptedException ignored)
{

}
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/doubledoordev/backend/server/Server.java
Expand Up @@ -18,6 +18,7 @@

package net.doubledoordev.backend.server;

import com.google.common.collect.EvictingQueue;
import com.google.gson.JsonObject;
import com.google.gson.annotations.Expose;
import net.doubledoordev.backend.Main;
Expand Down Expand Up @@ -70,6 +71,7 @@ public class Server
*/
public int[] size = new int[3];
public QueryResponse cachedResponse;
public EvictingQueue<String> lastConsoleLines = EvictingQueue.create(25);

@Expose
private String ID;
Expand Down Expand Up @@ -1039,6 +1041,7 @@ public void printLine(String line)
{
line = Helper.stripColor(line);
logger.info(line);
lastConsoleLines.add(line);
ServerconsoleSocketApplication.sendLine(this, line);
}

Expand All @@ -1047,6 +1050,7 @@ public void error(Throwable e)
logger.catching(e);
StringWriter error = new StringWriter();
e.printStackTrace(new PrintWriter(error));
lastConsoleLines.add(error.toString());
ServerconsoleSocketApplication.sendLine(this, error.toString());
}

Expand Down
Expand Up @@ -56,20 +56,14 @@ public void onConnect(WebSocket socket)
}
((DefaultWebSocket) socket).getUpgradeRequest().setAttribute(USER, session.getAttribute(USER));
String[] serverName = ((DefaultWebSocket) socket).getUpgradeRequest().getPathInfo().substring(1).split("/");
if (Strings.isNullOrEmpty(serverName[0]) || Strings.isNullOrEmpty(serverName[0]))
{
WebSocketHelper.sendError(socket, "No valid server.");
socket.close();
return;
}
Server server = Settings.getServerByName(serverName[0]);
if (server == null)
if (Strings.isNullOrEmpty(serverName[0]) || server == null)
{
WebSocketHelper.sendError(socket, "No valid server.");
socket.close();
return;
}
else if (!server.canUserControl((User) ((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(USER)))
if (!server.canUserControl((User) ((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(USER)))
{
WebSocketHelper.sendError(socket, "You have no rights to this server.");
socket.close();
Expand Down
Expand Up @@ -62,6 +62,16 @@ public static void sendLine(Server server, String line)
}
}

@Override
public void onConnect(WebSocket socket)
{
super.onConnect(socket);
for (String line : ((Server) ((DefaultWebSocket) socket).getUpgradeRequest().getAttribute(SERVER)).lastConsoleLines)
{
WebSocketHelper.sendData(socket, line);
}
}

@Override
public void onMessage(WebSocket socket, String text)
{
Expand Down

0 comments on commit 26e3211

Please sign in to comment.