ReportServer fixes and improvements.

This commit is contained in:
Keir 2015-12-16 15:05:00 +00:00
parent 6f82f8349d
commit cd18d2a94f
3 changed files with 39 additions and 38 deletions

View File

@ -4,11 +4,9 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import redis.clients.jedis.JedisPubSub;
/**
@ -18,9 +16,7 @@ public class JedisPubSubHandler extends JedisPubSub
{
private File _directory;
private Gson _gson = new GsonBuilder()
.setPrettyPrinting()
.create();
private Gson _gson = new Gson();
public JedisPubSubHandler(File directory)
{
@ -35,33 +31,44 @@ public class JedisPubSubHandler extends JedisPubSub
@Override
public void onMessage(String channel, String dataString)
{
if (channel.equals(ReportServer.CHANNEL_DEPLOY))
try
{
JsonObject data = _gson.fromJson(dataString, JsonObject.class);
String token = data.get("token").getAsString();
JsonArray snapshotArray = data.get("snapshots").getAsJsonArray();
File target = new File(_directory, token + ".json");
String json = _gson.toJson(snapshotArray);
try
if (channel.equals(ReportServer.CHANNEL_DEPLOY))
{
Files.write(target.toPath(), Arrays.asList(json.split("\n")));
Map<String, Object> data = _gson.fromJson(dataString, Map.class);
String token = (String) data.get("token");
File target = new File(_directory, token + ".json");
System.out.println("Received ChatSnap, token = " + token + ", writing to file.");
try
{
Files.write(target.toPath(), Arrays.asList(dataString.split("\n")));
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch (IOException e)
else if (channel.equals(ReportServer.CHANNEL_DESTROY))
{
e.printStackTrace();
// dataString = token
File target = new File(_directory, dataString + ".json");
System.out.println("Removing ChatSnap, token = " + dataString);
if (target.exists() && !target.delete())
{
System.out.println("Failed to delete: " + target.getPath());
}
}
}
else if (channel.equals(ReportServer.CHANNEL_DESTROY))
catch (Exception e)
{
// dataString = token
File target = new File(_directory, dataString + ".json");
if (target.exists() && !target.delete())
{
System.out.println("Failed to delete: " + target.getPath());
}
System.out.println("Excepting whilst receiving redis message.");
System.out.println("Channel = " + channel);
System.out.println("Data: ");
System.out.println(dataString);
e.printStackTrace();
}
}

View File

@ -16,8 +16,8 @@ public class ReportServer
public static void main(String[] args)
{
new ReportServer(new JedisPool(new JedisPoolConfig(), "10.3.203.80", 6379)); // TODO is this okay?
System.out.println("Started.");
System.out.println("Starting...");
new ReportServer(new JedisPool(new JedisPoolConfig(), "10.3.203.80", 6379, 10)); // TODO is this okay?
}
private JedisPool _jedisPool;

View File

@ -1,9 +1,5 @@
package mineplex.core.chatsnap.publishing;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collection;
@ -15,8 +11,6 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
@ -70,12 +64,12 @@ public class SnapshotPublisher
jsonObject.addProperty("token", token);
String json = GSON.toJson(jsonObject);
/*try (Jedis jedis = _jedisPool.getResource())
try (Jedis jedis = _jedisPool.getResource())
{
jedis.publish(CHANNEL_DEPLOY, json);
}*/
}
try
/*try
{
File dir = new File("data");
dir.mkdir();
@ -88,7 +82,7 @@ public class SnapshotPublisher
catch (IOException e)
{
e.printStackTrace();
}
}*/
}
public void unpublishChatLog(String token)