Skip to content

Commit

Permalink
refined paths in client with clientPaths class (#360)
Browse files Browse the repository at this point in the history
fixed small method mistake in transportPaths
  • Loading branch information
pankinkun authored Oct 18, 2024
1 parent 9ac59c9 commit 2b509a1
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.reflect.TypeToken;
import net.discdd.model.ADU;
import net.discdd.model.UncompressedPayload;
import net.discdd.pathutils.ClientPaths;
import net.discdd.utils.BundleUtils;
import net.discdd.utils.StoreADUs;
import net.discdd.utils.StreamExt;
Expand All @@ -15,7 +16,6 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -38,27 +38,22 @@ public class ApplicationDataManager {
private StoreADUs receiveADUsStorage;
private Consumer<ADU> aduConsumer;
/* Database tables */
private static String SENT_BUNDLE_DETAILS = "Shared/DB/SENT_BUNDLE_DETAILS.json";

private static String LAST_SENT_BUNDLE_STRUCTURE = "Shared/DB/LAST_SENT_BUNDLE_STRUCTURE.json";

private Long APP_DATA_SIZE_LIMIT = 1000000000L;

private static List<String> REGISTER_APP_IDS = List.of("com.example.mysignal", "com.fsck.k9.debug", "testAppId");

private final Path ROOT_DIR;
private ClientPaths clientPaths;

public ApplicationDataManager(Path rootDir, Consumer<ADU> aduConsumer) {
ROOT_DIR = rootDir;
sendADUsStorage = new StoreADUs(rootDir.resolve("send"));
receiveADUsStorage = new StoreADUs(rootDir.resolve("receive"));
public ApplicationDataManager(ClientPaths clientPaths, Consumer<ADU> aduConsumer) {
this.clientPaths = clientPaths;
sendADUsStorage = new StoreADUs(clientPaths.sendADUsPath);
receiveADUsStorage = new StoreADUs(clientPaths.receiveADUsPath);
this.aduConsumer = aduConsumer;

try {
File sentBundleDetails = ROOT_DIR.resolve(SENT_BUNDLE_DETAILS).toFile();
File sentBundleDetails = clientPaths.sendBundleDetailsPath.toFile();
sentBundleDetails.createNewFile();

File lastSentBundleStructure = ROOT_DIR.resolve(LAST_SENT_BUNDLE_STRUCTURE).toFile();
File lastSentBundleStructure = clientPaths.lastSentBundleStructurePath.toFile();
lastSentBundleStructure.createNewFile();
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -109,7 +104,7 @@ public void storeReceivedADUs(String clientId, String bundleId, List<ADU> adus)

public List<ADU> fetchADUsToSend(long initialSize, String clientId) throws IOException {
List<ADU> adusToSend = new ArrayList<>();
final long dataSizeLimit = this.APP_DATA_SIZE_LIMIT;
final long dataSizeLimit = clientPaths.APP_DATA_SIZE_LIMIT;
var sizeLimiter = new SizeLimiter(dataSizeLimit - initialSize);
for (String appId : this.getRegisteredAppIds()) {
StreamExt.takeWhile(sendADUsStorage.getADUs(clientId, appId), a -> sizeLimiter.test(a.getSize()))
Expand Down Expand Up @@ -141,17 +136,17 @@ public void notifyBundleSent(UncompressedPayload bundle) {
}

public Optional<UncompressedPayload.Builder> getLastSentBundleBuilder() {
return BundleUtils.jsonToBundleBuilder(ROOT_DIR.resolve(LAST_SENT_BUNDLE_STRUCTURE).toFile());
return BundleUtils.jsonToBundleBuilder(clientPaths.lastSentBundleStructurePath.toFile());
}

private void writeLastSentBundleStructure(UncompressedPayload lastSentBundle) {
BundleUtils.writeBundleStructureToJson(lastSentBundle, ROOT_DIR.resolve(LAST_SENT_BUNDLE_STRUCTURE).toFile());
BundleUtils.writeBundleStructureToJson(lastSentBundle,clientPaths.lastSentBundleStructurePath.toFile());
}

private void writeSentBundleDetails(Map<String, Map<String, Long>> sentBundleDetails) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonString = gson.toJson(sentBundleDetails);
try (FileWriter writer = new FileWriter(ROOT_DIR.resolve(SENT_BUNDLE_DETAILS).toFile())) {
try (FileWriter writer = new FileWriter(clientPaths.sendBundleDetailsPath.toFile())) {
writer.write(jsonString);
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -179,7 +174,7 @@ public boolean test(Long size) {
private Map<String, Map<String, Long>> getSentBundleDetails() {
Gson gson = new Gson();
Map<String, Map<String, Long>> ret = new HashMap<>();
try (FileReader reader = new FileReader(ROOT_DIR.resolve(SENT_BUNDLE_DETAILS).toFile())) {
try (FileReader reader = new FileReader(clientPaths.sendBundleDetailsPath.toFile())) {
Type mapType = new TypeToken<Map<String, Map<String, Long>>>() {}.getType();
ret = gson.fromJson(reader, mapType);
if (ret == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package net.discdd.client.bundlerouting;

import static java.util.logging.Level.WARNING;

import net.discdd.bundlesecurity.BundleIDGenerator;

import net.discdd.client.bundlesecurity.ClientSecurity;

import net.discdd.pathutils.ClientPaths;
import org.whispersystems.libsignal.InvalidKeyException;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.util.Objects;
import java.util.logging.Logger;

import static java.util.logging.Level.WARNING;

public class ClientBundleGenerator {

private static final Logger logger = Logger.getLogger(ClientBundleGenerator.class.getName());
Expand All @@ -27,28 +24,28 @@ public class ClientBundleGenerator {
/* Counter value used as unsigned long */
private long currentCounter = 0;

final private Path counterFilePath;
private ClientPaths clientPaths;

private ClientBundleGenerator(ClientSecurity clientSecurity, Path rootPath) throws IOException {
private ClientBundleGenerator(ClientSecurity clientSecurity, ClientPaths clientPaths) throws IOException {
this.clientSecurity = clientSecurity;
counterFilePath = rootPath.resolve(Paths.get("BundleRouting", "sentBundle.id"));
this.clientPaths = clientPaths;

try {
byte[] counterFromFile = Files.readAllBytes(counterFilePath);
byte[] counterFromFile = Files.readAllBytes(clientPaths.counterFilePath);
currentCounter = Long.parseUnsignedLong(new String(counterFromFile, StandardCharsets.UTF_8));
} catch (IOException e) {
updateBundleIDFile();
}
}

private void updateBundleIDFile() throws IOException {
counterFilePath.getParent().toFile().mkdirs();
Files.write(counterFilePath, Long.toUnsignedString(currentCounter).getBytes());
clientPaths.counterFilePath.getParent().toFile().mkdirs();
Files.write(clientPaths.counterFilePath, Long.toUnsignedString(currentCounter).getBytes());
}

synchronized public static ClientBundleGenerator initializeInstance(ClientSecurity clientSecurity, Path rootPath) throws IOException {
synchronized public static ClientBundleGenerator initializeInstance(ClientSecurity clientSecurity, ClientPaths clientPaths) throws IOException {
if (singleGeneratorInstance == null) {
singleGeneratorInstance = new ClientBundleGenerator(clientSecurity, rootPath);
singleGeneratorInstance = new ClientBundleGenerator(clientSecurity, clientPaths);
} else {
logger.log(WARNING, "[BR]: Client bundle generator instance is already created!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.JsonParseException;
import net.discdd.bundlerouting.RoutingExceptions.ClientMetaDataFileException;
import net.discdd.pathutils.ClientPaths;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.logging.Logger;

Expand All @@ -23,8 +22,7 @@ public class ClientRouting {

private static ClientRouting singleClientRoutingInstance = null;
HashMap<String, Long> metadata = null;
Path metaDataPath = null;
private final String METADATAFILE = "routing.metadata";
ClientPaths clientPaths;

/* Initialize client routing score table
* Reads from json file if it exists, creates a new table otherwise
Expand All @@ -33,31 +31,26 @@ public class ClientRouting {
* Return:
* None
*/
private ClientRouting(Path rootPath) throws IOException, ClientMetaDataFileException {
this.metaDataPath = rootPath.resolve("BundleRouting");
metaDataPath.toFile().mkdirs();

metaDataPath = metaDataPath.resolve(METADATAFILE);

File metadataFile = metaDataPath.toFile();
private ClientRouting(ClientPaths clientPaths) throws IOException, ClientMetaDataFileException {
this.clientPaths = clientPaths;
ObjectMapper objectMapper = new ObjectMapper();

metadata = new HashMap<>();

if (metadataFile.exists()) {
if (clientPaths.metadataFile.exists()) {
try {
metadata = objectMapper.readValue(metadataFile, new TypeReference<HashMap<String, Long>>() {});
metadata = objectMapper.readValue(clientPaths.metadataFile, new TypeReference<HashMap<String, Long>>() {});
} catch (JsonParseException | JsonMappingException e) {
throw new ClientMetaDataFileException("Corrupted JSON File:" + e);
}
} else {
objectMapper.writeValue(metadataFile, metadata);
objectMapper.writeValue(clientPaths.metadataFile, metadata);
}
}

public static ClientRouting initializeInstance(Path metaDataPath) throws ClientMetaDataFileException, IOException {
public static ClientRouting initializeInstance(ClientPaths clientPaths) throws ClientMetaDataFileException, IOException {
if (singleClientRoutingInstance == null) {
singleClientRoutingInstance = new ClientRouting(metaDataPath);
singleClientRoutingInstance = new ClientRouting(clientPaths);
} else {
logger.log(INFO, "[BR]: Client Routing Instance already Exists!");
}
Expand Down Expand Up @@ -89,10 +82,9 @@ public void updateMetaData(String senderId) throws ClientMetaDataFileException {

ObjectMapper mapper = new ObjectMapper();
try {
File metadataFile = metaDataPath.toFile();
JsonNode rootNode = mapper.readTree(metadataFile);
JsonNode rootNode = mapper.readTree(clientPaths.metadataFile);
((ObjectNode) rootNode).put(senderId, count);
mapper.writeValue(metadataFile, rootNode);
mapper.writeValue(clientPaths.metadataFile, rootNode);
} catch (Exception e) {
throw new ClientMetaDataFileException("Error updating Routing Meta Data:" + e);
}
Expand All @@ -105,6 +97,6 @@ public void updateMetaData(String senderId) throws ClientMetaDataFileException {
* None
*/
public byte[] bundleMetaData() throws IOException {
return Files.readAllBytes(metaDataPath);
return Files.readAllBytes(clientPaths.metadataFile.toPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import net.discdd.bundlerouting.WindowUtils.WindowExceptions.BufferOverflow;
import net.discdd.bundlesecurity.BundleIDGenerator;
import net.discdd.client.bundlesecurity.ClientSecurity;
import net.discdd.pathutils.ClientPaths;
import net.discdd.utils.Constants;
import org.whispersystems.libsignal.InvalidKeyException;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -27,17 +27,14 @@
public class ClientWindow {

private static final Logger logger = Logger.getLogger(ClientWindow.class.getName());
public static final String CLIENT_WINDOW_SUBDIR = "ClientWindow";

static private ClientWindow singleClientWindowInstance = null;
final private Path clientWindowDataPath;
final private String WINDOW_FILE = "clientWindow.csv";

record UnencryptedBundleId(String bundleId, long bundleCounter) {}

private final LinkedList<UnencryptedBundleId> windowOfUnencryptedBundleIds = new LinkedList<>();
private final String clientID;
private int windowLength = 10; /* Default Value */
private final ClientPaths clientPaths;

/* Generates bundleIDs for window slots
* Parameter:
Expand All @@ -58,23 +55,21 @@ private void fillWindow(long startCounter, int count) throws IOException {
}

private void updateDBWindow() throws IOException {
var dbFile = clientWindowDataPath.resolve(WINDOW_FILE);

Files.write(dbFile, String.format(Locale.US, "%d,%d", windowOfUnencryptedBundleIds.getFirst().bundleCounter(),
Files.write(clientPaths.dbFile, String.format(Locale.US, "%d,%d", windowOfUnencryptedBundleIds.getFirst().bundleCounter(),
windowOfUnencryptedBundleIds.getLast().bundleCounter()).getBytes());

logger.log(FINE, "Update window: " + windowOfUnencryptedBundleIds.getFirst().bundleCounter() + " - " +
windowOfUnencryptedBundleIds.getLast().bundleCounter());
}

private void initializeWindow() throws IOException {
var dbFile = clientWindowDataPath.resolve(WINDOW_FILE);

var start = 0L;
windowLength = Constants.DEFAULT_WINDOW_SIZE;
var end = start + windowLength - 1;

try {
String dbData = new String(Files.readAllBytes(dbFile));
String dbData = new String(Files.readAllBytes(clientPaths.dbFile));
String[] dbCSV = dbData.split(",");
start = Long.parseLong(dbCSV[0]);
end = Long.parseLong(dbCSV[1]);
Expand All @@ -94,10 +89,9 @@ private void initializeWindow() throws IOException {
* Returns:
* None
*/
private ClientWindow(int length, String clientID, Path rootPath) {
clientWindowDataPath = rootPath.resolve(CLIENT_WINDOW_SUBDIR);
clientWindowDataPath.toFile().mkdirs();
private ClientWindow(int length, String clientID, ClientPaths clientPaths) {
this.clientID = clientID;
this.clientPaths = clientPaths;

try {
initializeWindow();
Expand All @@ -111,9 +105,9 @@ private ClientWindow(int length, String clientID, Path rootPath) {
}
}

public static ClientWindow initializeInstance(int windowLength, String clientID, Path rootPath) throws BufferOverflow, IOException {
public static ClientWindow initializeInstance(int windowLength, String clientID, ClientPaths clientPaths) throws BufferOverflow, IOException {
if (singleClientWindowInstance == null) {
singleClientWindowInstance = new ClientWindow(windowLength, clientID, rootPath);
singleClientWindowInstance = new ClientWindow(windowLength, clientID, clientPaths);
} else {
logger.log(INFO, "[WIN]: Client Window Instance is already initialized!");
}
Expand Down
Loading

0 comments on commit 2b509a1

Please sign in to comment.