Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified logging for BundleClient's bundlerouting package #104

Merged
merged 9 commits into from
Jun 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import java.util.logging.Logger;

import static java.util.logging.Level.FINER;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Level.SEVERE;

import com.ddd.bundlesecurity.BundleIDGenerator;

import com.ddd.client.bundlesecurity.ClientSecurity;
import com.ddd.bundlesecurity.SecurityExceptions.BundleIDCryptographyException;
import com.ddd.bundlesecurity.SecurityExceptions.IDGenerationException;
import com.ddd.bundlesecurity.SecurityUtils;

public class ClientBundleGenerator {

private static final Logger logger = Logger.getLogger(ClientBundleGenerator.class.getName());

static ClientBundleGenerator singleGeneratorInstance = null;
ClientSecurity clientSecurity = null;

Expand All @@ -37,15 +49,15 @@ private void updateBundleIDFile() {
try (FileOutputStream stream = new FileOutputStream(counterFilePath)) {
stream.write(Long.toUnsignedString(currentCounter).getBytes(StandardCharsets.UTF_8));
} catch (IOException ex) {
System.out.println("[BR]: Failed to create counter backup file! " + ex);
logger.log(WARNING, "[BR]: Failed to create counter backup file! " + ex);
}
}

public static ClientBundleGenerator initializeInstance(ClientSecurity clientSecurity, String rootPath) {
if (singleGeneratorInstance == null) {
singleGeneratorInstance = new ClientBundleGenerator(clientSecurity, rootPath);
} else {
System.out.println("[BR]: Client bundle generator instance is already created!");
logger.log(WARNING, "[BR]: Client bundle generator instance is already created!");
}
return singleGeneratorInstance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@

import java.util.HashMap;

import java.util.logging.Logger;

import static java.util.logging.Level.FINER;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Level.SEVERE;

import com.ddd.bundlesecurity.SecurityUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
Expand All @@ -18,6 +27,9 @@
import com.ddd.bundlerouting.RoutingExceptions.ClientMetaDataFileException;

public class ClientRouting {

private static final Logger logger = Logger.getLogger(ClientRouting.class.getName());

private static ClientRouting singleClientRoutingInstance = null;
HashMap<String, Long> metadata = null;
String metaDataPath = null;
Expand Down Expand Up @@ -57,7 +69,7 @@ public static ClientRouting initializeInstance(String metaDataPath) throws Clien
if (singleClientRoutingInstance == null) {
singleClientRoutingInstance = new ClientRouting(metaDataPath);
} else {
System.out.println("[BR]: Client Routing Instance already Exists!");
logger.log(INFO, "[BR]: Client Routing Instance already Exists!");
}
return singleClientRoutingInstance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@
import com.ddd.bundlesecurity.SecurityExceptions.BundleIDCryptographyException;
import com.ddd.bundlesecurity.SecurityUtils;

import java.util.logging.Logger;

import static java.util.logging.Level.FINER;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Level.SEVERE;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ClientWindow {

private static final Logger logger = Logger.getLogger(ClientWindow.class.getName());

static private ClientWindow singleClientWindowInstance = null;
private String clientWindowDataPath = null;
final private String windowFile = "clientWindow.csv";
Expand Down Expand Up @@ -56,7 +67,7 @@ private void updateDBWindow() {
String metadata = Long.toUnsignedString(begin) + "," + Long.toUnsignedString(end) + "," + windowLength;
stream.write(metadata.getBytes());
} catch (IOException e) {
System.out.println("Error: Failed to write Window to file! " + e);
logger.log(WARNING, "Error: Failed to write Window to file! " + e);
}
}

Expand Down Expand Up @@ -87,12 +98,12 @@ private ClientWindow(int length, String clientID, String rootPath) throws Invali
try {
initializeWindow();
} catch (IOException e) {
Log.e(TAG, "Failed to initialize Window from Disk, creating new window\n" + e);
logger.log(WARNING, "Failed to initialize Window from Disk -- creating new window\n" + e);
if (length > 0) {
windowLength = length;
} else {
//TODO: Change to log
Log.e(TAG, "Invalid window size, using default size: " + windowLength);

logger.log(WARNING, "Invalid window size -- using default size: " + windowLength);
}
}

Expand All @@ -107,7 +118,7 @@ public static ClientWindow initializeInstance(int windowLength, String clientID,
if (singleClientWindowInstance == null) {
singleClientWindowInstance = new ClientWindow(windowLength, clientID, rootPath);
} else {
System.out.println("[WIN]: Client Window Instance is already initialized!");
logger.log(INFO, "[WIN]: Client Window Instance is already initialized!");
}
return singleClientWindowInstance;
}
Expand All @@ -128,16 +139,17 @@ public static ClientWindow getInstance() {
public void processBundle(String bundleID, ClientSecurity clientSecurity) throws BufferOverflow,
BundleIDCryptographyException {
String decryptedBundleID = clientSecurity.decryptBundleID(bundleID);
System.out.println("Largest Bundle ID = " + decryptedBundleID);
logger.log(FINE, "Largest Bundle ID = " + decryptedBundleID);
long ack = BundleIDGenerator.getCounterFromBundleID(decryptedBundleID, BundleIDGenerator.DOWNSTREAM);

if (Long.compareUnsigned(ack, begin) == -1) {
System.out.println(
"Received old [" + Long.toUnsignedString(ack) + " < " + Long.toUnsignedString(begin) + "]");
logger.log(FINE,
"Received old [" + Long.toUnsignedString(ack) + " < " + Long.toUnsignedString(begin) + "]");
return;
} else if (Long.compareUnsigned(ack, end) == 1) {
System.out.println(
"Received Invalid ACK [" + Long.toUnsignedString(ack) + " < " + Long.toUnsignedString(end) + "]");
logger.log(FINE,
"Received Invalid ACK [" + Long.toUnsignedString(ack) + " < " + Long.toUnsignedString(end) +
"]");
return;
}

Expand All @@ -149,14 +161,14 @@ public void processBundle(String bundleID, ClientSecurity clientSecurity) throws
try {
noDeleted = window.deleteUntilIndex(ackIndex);
} catch (InvalidLength e) {
System.out.println("Received Invalid ACK [" + Long.toUnsignedString(ack) + "]");
logger.log(WARNING, "Received Invalid ACK [" + Long.toUnsignedString(ack) + "]");
}

begin = ack + 1;
/* Add new bundleIDs to window */
fillWindow(noDeleted, end);
// TODO: Change to log
System.out.println("Updated Begin: " + Long.toUnsignedString(begin) + "; End: " + Long.toUnsignedString(end));

logger.log(FINE, "Updated Begin: " + Long.toUnsignedString(begin) + "; End: " + Long.toUnsignedString(end));
}

/* Returns the entire window
Expand Down
Loading