Add data accounting, progress bar, and finish implementation of slicing
This commit is contained in:
parent
181c2bb9fb
commit
24dc4fd030
7 changed files with 132 additions and 17 deletions
|
@ -4,6 +4,6 @@ Bus_Patch=33
|
|||
|
||||
|
||||
LibAC_API=1.4
|
||||
LibAC_Patch=29
|
||||
LibAC_Patch=35
|
||||
|
||||
org.gradle.daemon=false
|
|
@ -10,17 +10,26 @@ import dev.zontreck.ariaslib.args.StringArgument;
|
|||
import dev.zontreck.ariaslib.terminal.Banners;
|
||||
import dev.zontreck.ariaslib.util.FileIO;
|
||||
import dev.zontreck.ariaslib.util.Hashing;
|
||||
import dev.zontreck.eventsbus.Bus;
|
||||
import dev.zontreck.playsync.data.DataAccountant;
|
||||
import dev.zontreck.playsync.data.DataFragment;
|
||||
import dev.zontreck.playsync.data.Manifest;
|
||||
import dev.zontreck.playsync.events.EventHandlers;
|
||||
import dev.zontreck.playsync.exceptions.UnsupportedAlgorithmException;
|
||||
import dev.zontreck.playsync.exceptions.UnsupportedIDException;
|
||||
import dev.zontreck.playsync.games.Platform;
|
||||
import dev.zontreck.playsync.games.nintendo.ID6;
|
||||
import dev.zontreck.playsync.server.HTTPServer;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PlaySyncServer {
|
||||
public static final Arguments DEFAULT_ARGS;
|
||||
private static HTTPServer server = new HTTPServer();
|
||||
|
||||
|
||||
static {
|
||||
|
||||
DEFAULT_ARGS = new Arguments();
|
||||
|
@ -72,6 +81,11 @@ public class PlaySyncServer {
|
|||
StringArgument dataDir = (StringArgument) active.getArg("dataDir");
|
||||
StringArgument hashing = (StringArgument) active.getArg("hash");
|
||||
|
||||
log("Registering EventHandlers");
|
||||
Bus.Register(EventHandlers.class, new EventHandlers());
|
||||
log("EventHandlers have been registered");
|
||||
|
||||
|
||||
|
||||
if(active.hasArg("port"))
|
||||
Settings.PORT_NUMBER = port.getValue();
|
||||
|
@ -112,6 +126,11 @@ public class PlaySyncServer {
|
|||
mf.FileHash = Hashing.md5(fullFile);
|
||||
mf.GamePlatform = Platform.Nintendo;
|
||||
|
||||
DataAccountant.DataTotal = fullFile.length;
|
||||
|
||||
DataAccountant.updateMax(DataAccountant.DataTotal);
|
||||
|
||||
|
||||
try{
|
||||
mf.GameID = ID6.tryDetect(fullFile, "test.iso");
|
||||
|
||||
|
@ -120,6 +139,7 @@ public class PlaySyncServer {
|
|||
}
|
||||
|
||||
|
||||
List<DataFragment> fragments = new ArrayList<>();
|
||||
boolean hasData = true;
|
||||
while(hasData)
|
||||
{
|
||||
|
@ -131,17 +151,46 @@ public class PlaySyncServer {
|
|||
}
|
||||
|
||||
mf.Fragments.add(frag.getHash());
|
||||
mf.save();
|
||||
fragments.add(frag);
|
||||
DataAccountant.ChunksTotal++;
|
||||
DataAccountant.DataWritten += frag.getSize();
|
||||
|
||||
frag.Save();
|
||||
DataAccountant.updateCur(DataAccountant.DataWritten);
|
||||
|
||||
System.out.print("\r" + DataAccountant.getProgressStr() + "\r");
|
||||
} catch (IOException e) {
|
||||
hasData=false;
|
||||
} catch (UnsupportedAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
log("\r\n\n");
|
||||
DataAccountant.IsWritingChunks.set(true);
|
||||
|
||||
DataAccountant.updateCur(0);
|
||||
|
||||
log("File analysis completed.");
|
||||
mf.save();
|
||||
log("Manifest saved, updating data accountant");
|
||||
log("Saving chunks...");
|
||||
DataAccountant.ChunksRemain = fragments.size();
|
||||
DataAccountant.updateMax(DataAccountant.ChunksTotal);
|
||||
|
||||
for(DataFragment frag : fragments)
|
||||
{
|
||||
frag.Save();
|
||||
DataAccountant.ChunksWritten++;
|
||||
DataAccountant.ChunksRemain--;
|
||||
DataAccountant.updateCur(DataAccountant.ChunksWritten);
|
||||
|
||||
System.out.print("\r" + DataAccountant.getProgressStr() + "\r");
|
||||
}
|
||||
|
||||
log("\r\n\nCompleted operation. Program Terminating.");
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
|
|
|
@ -36,16 +36,4 @@ public class Settings
|
|||
|
||||
return pth;
|
||||
}
|
||||
|
||||
public static int BYTES_SAVED = 0;
|
||||
|
||||
public static int CHUNKS_SKIPPED = 0;
|
||||
|
||||
public static void updateSave(int bytes)
|
||||
{
|
||||
CHUNKS_SKIPPED++;
|
||||
BYTES_SAVED += bytes;
|
||||
|
||||
System.out.println("In Progress : " + BYTES_SAVED + "b saved, " + CHUNKS_SKIPPED + " chunks identical");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package dev.zontreck.playsync.data;
|
||||
|
||||
import dev.zontreck.ariaslib.util.MathUtil;
|
||||
import dev.zontreck.ariaslib.util.ProgressBar;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class DataAccountant
|
||||
{
|
||||
public static int DataWritten = 0;
|
||||
public static int DataSkipped = 0;
|
||||
public static int DataTotal = 0;
|
||||
public static int ChunksWritten = 0;
|
||||
public static int ChunksTotal = 0;
|
||||
public static int ChunksRemain = 0;
|
||||
public static AtomicBoolean IsWritingChunks = new AtomicBoolean(false);
|
||||
|
||||
|
||||
private static int Max = 100;
|
||||
private static int Cur = 0;
|
||||
|
||||
public static void updateMax(int val)
|
||||
{
|
||||
Max=val;
|
||||
}
|
||||
|
||||
public static void updateCur(int val)
|
||||
{
|
||||
Cur=val;
|
||||
}
|
||||
|
||||
|
||||
public static int getWritingPercent()
|
||||
{
|
||||
return MathUtil.getPercent(Cur, Max);
|
||||
}
|
||||
|
||||
public static int getRemainingData()
|
||||
{
|
||||
return DataTotal - DataWritten;
|
||||
}
|
||||
|
||||
public static String getProgressStr()
|
||||
{
|
||||
if(IsWritingChunks.get())
|
||||
return ProgressBar.printProgressBar(getWritingPercent(), "" + ChunksTotal+"c ", " " + DataSkipped+" b/s " + ChunksWritten+" cw " + ChunksRemain + " cr");
|
||||
else // Loading into memory/prewriting
|
||||
return ProgressBar.printProgressBar(getWritingPercent(), "" + DataTotal + "b", " " + DataWritten + " / " + getRemainingData() + " bw/br");
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import dev.zontreck.ariaslib.util.Hashing;
|
|||
import dev.zontreck.playsync.Settings;
|
||||
import dev.zontreck.playsync.exceptions.UnsupportedAlgorithmException;
|
||||
|
||||
import javax.xml.crypto.Data;
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
|
||||
|
@ -36,8 +37,9 @@ public class DataFragment
|
|||
Path chunk = chunks.resolve(getHash() + ".bin");
|
||||
if(!chunks.toFile().exists()) chunks.toFile().mkdirs();
|
||||
|
||||
//DataAccountant.DataWritten += data.length;
|
||||
if(chunk.toFile().exists()) {
|
||||
Settings.updateSave(data.length);
|
||||
DataAccountant.DataSkipped += data.length;
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -68,7 +70,7 @@ public class DataFragment
|
|||
String hash = "";
|
||||
|
||||
if(Settings.HASH_ALGORITHM.equalsIgnoreCase("md5"))
|
||||
hash=Hashing.md5(data);
|
||||
hash = Hashing.md5(data);
|
||||
if(Settings.HASH_ALGORITHM.equalsIgnoreCase("sha256"))
|
||||
hash = Hashing.sha256(data);
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package dev.zontreck.playsync.events;
|
||||
|
||||
public class EventHandlers
|
||||
{
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package dev.zontreck.playsync.server;
|
||||
|
||||
|
||||
import dev.zontreck.eventsbus.Bus;
|
||||
import dev.zontreck.playsync.server.events.HTTPRequestEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class RequestHandler {
|
||||
public HTTPResponseData handleRequest(String path, String requestBody, Map<String, String> headers) {
|
||||
|
||||
HTTPRequestEvent ev = new HTTPRequestEvent(path, requestBody, headers);
|
||||
if(Bus.Post(ev))
|
||||
{
|
||||
return new HTTPResponseData(404, "Requested Data Was Not Found", ev.Headers);
|
||||
}
|
||||
|
||||
return ev.getResponse();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue