Skip to content

Commit

Permalink
fixed errors after update
Browse files Browse the repository at this point in the history
Co-authored-by:  robinalfengard<robin.alfengard@hotmail.com>
  • Loading branch information
cmatlak committed Feb 5, 2024
1 parent 99c12e8 commit b2cf5cf
Showing 1 changed file with 74 additions and 76 deletions.
150 changes: 74 additions & 76 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OptionalDataException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
Expand All @@ -21,7 +20,7 @@ public class Main {

public static void main(String[] args) {

Initialize initialize = Initialize.getInitialize(args);
Initialize initialize = Initialize.getInitialize(args);

HazeDatabase hazeDatabase = new HazeDatabase();
HazeList hazeList = new HazeList(hazeDatabase);
Expand Down Expand Up @@ -100,7 +99,6 @@ private static void addHook(HazeDatabase hazeDatabase) {
}



private static void shutdown(HazeDatabase hazeDatabase) {
SaveFile.writeOnFile(hazeDatabase.copy());
logger.info("Shutting down....");
Expand All @@ -112,97 +110,97 @@ private static void printThreadDebug() {
}

public static String executeCommand(HazeDatabase hazeDatabase, List<String> inputList, HazeList hazeList) {



if (inputList.isEmpty() || inputList.getFirst().isEmpty()) {

return "-ERR no command provided\r\n";
}

logger.debug("executeCommand: {} {} ", () -> hazeDatabase, () -> inputList);


String command = inputList.getFirst().toUpperCase();


Command commandEnum = getCommand(inputList);
if (commandEnum == null)
return "-ERR unknown command\r\n";

return commandSwitch(hazeDatabase, inputList, hazeList, commandEnum);
}

private static Command getCommand(List<String> inputList) {
String command = inputList.get(0).toUpperCase();
Command commandEnum;

try {
commandEnum = Command.valueOf(command);
} catch (IllegalArgumentException ex) {

return Optional.ofNullable(command).orElse("Default value");

return "-ERR unknown command\r\n";
}
return commandSwitch (hazeDatabase, inputList, hazeList, commandEnum);
}
// private static Command getCommand (List < String > inputList) {
// String command = inputList.getFirst().toUpperCase();
// Command commandEnum;
// try {
// commandEnum = Command.valueOf(command);
// } catch (IllegalArgumentException ex) {
// return Command.GET;
// }
// return commandEnum;
// }

private static String commandSwitch (HazeDatabase hazeDatabase, List < String > inputList, HazeList
hazeList, Command commandEnum){
return switch (commandEnum) {
case SET -> hazeDatabase.set(inputList);
case GET -> hazeDatabase.get(inputList);
case DEL -> hazeDatabase.delete(inputList.subList(1, inputList.size()));
case PING -> hazeDatabase.ping(inputList);
case SETNX -> hazeDatabase.setNX(inputList);
case EXISTS -> hazeDatabase.exists(inputList.subList(1, inputList.size()));
case SAVE -> SaveFile.writeOnFile(hazeDatabase.copy());
case RPUSH -> hazeList.rPush(inputList);
case LPUSH -> hazeList.lPush(inputList);
case LPOP -> hazeList.callLPop(inputList);
case RPOP -> hazeList.callRPop(inputList);
case LLEN -> hazeList.lLen(inputList);
case LMOVE -> hazeList.lMove(inputList);
case LTRIM -> hazeList.callLtrim(inputList);
case AUTH -> "+OK\r\n";
};
}
return commandEnum;
}

private static String commandSwitch(HazeDatabase hazeDatabase, List<String> inputList, HazeList hazeList, Command commandEnum) {
return switch (commandEnum) {
case SET -> hazeDatabase.set(inputList);
case GET -> hazeDatabase.get(inputList);
case DEL -> hazeDatabase.delete(inputList.subList(1, inputList.size()));
case PING -> hazeDatabase.ping(inputList);
case SETNX -> hazeDatabase.setNX(inputList);
case EXISTS -> hazeDatabase.exists(inputList.subList(1, inputList.size()));
case SAVE -> SaveFile.writeOnFile(hazeDatabase.copy());
case RPUSH -> hazeList.rPush(inputList);
case LPUSH -> hazeList.lPush(inputList);
case LPOP -> hazeList.callLPop(inputList);
case RPOP -> hazeList.callRPop(inputList);
case LLEN -> hazeList.lLen(inputList);
case LMOVE -> hazeList.lMove(inputList);
case LTRIM -> hazeList.callLtrim(inputList);
case AUTH -> "+OK\r\n";
};
}

private static void readInputStream(BufferedReader input, List<String> inputList, String firstReading) throws
IOException {
logger.debug("readInputStream: {} {} {}", () -> input, () -> inputList, () -> firstReading);
int size;
if (firstReading.startsWith("*")) {
size = Integer.parseInt(firstReading.substring(1)) * 2;
for (int i = 0; i < size; i++) {
String temp = input.readLine();
if (!temp.contains("$"))
inputList.add(temp);
private static void readInputStream (BufferedReader input, List < String > inputList, String firstReading) throws
IOException {
logger.debug("readInputStream: {} {} {}", () -> input, () -> inputList, () -> firstReading);
int size;
if (firstReading.startsWith("*")) {
size = Integer.parseInt(firstReading.substring(1)) * 2;
for (int i = 0; i < size; i++) {
String temp = input.readLine();
if (!temp.contains("$"))
inputList.add(temp);
}
} else {
String[] seperated = firstReading.split("\\s");
inputList.addAll(Arrays.asList(seperated));
}
} else {
String[] seperated = firstReading.split("\\s");
inputList.addAll(Arrays.asList(seperated));
}
}

private static void initializeServer(String[] args, Initialize initialize, Auth auth) {
initialize.importCliOptions(args);
auth.setPassword(initialize.getPassword());
}
private static void initializeServer (String[]args, Initialize initialize, Auth auth){
initialize.importCliOptions(args);
auth.setPassword(initialize.getPassword());
}

private static boolean authenticateClient(Auth auth, boolean isPasswordSet, Socket client, List<String> inputList, boolean clientAuthenticated) throws IOException {
if (authCommandReceived(isPasswordSet, inputList, clientAuthenticated))
return auth.authenticate(inputList.get(1), client);
private static boolean authenticateClient (Auth auth,boolean isPasswordSet, Socket
client, List < String > inputList,boolean clientAuthenticated) throws IOException {
if (authCommandReceived(isPasswordSet, inputList, clientAuthenticated))
return auth.authenticate(inputList.get(1), client);

shutdownClientIfNotAuthenticated(client, clientAuthenticated, isPasswordSet);
return clientAuthenticated;
}
shutdownClientIfNotAuthenticated(client, clientAuthenticated, isPasswordSet);
return clientAuthenticated;
}

private static void shutdownClientIfNotAuthenticated(Socket client, boolean clientAuthenticated, boolean isPasswordSet) throws IOException {
if (!clientAuthenticated && isPasswordSet) {
client.getOutputStream().write(Auth.printAuthError());
client.shutdownOutput();
private static void shutdownClientIfNotAuthenticated (Socket client,boolean clientAuthenticated,
boolean isPasswordSet) throws IOException {
if (!clientAuthenticated && isPasswordSet) {
client.getOutputStream().write(Auth.printAuthError());
client.shutdownOutput();
}
}
}

private static boolean authCommandReceived(boolean isPasswordSet, List<String> inputList, boolean clientAuthenticated) {
return isPasswordSet && !clientAuthenticated && inputList.size() == 2 && inputList.getFirst().equals("AUTH");
private static boolean authCommandReceived ( boolean isPasswordSet, List<String > inputList,
boolean clientAuthenticated){
return isPasswordSet && !clientAuthenticated && inputList.size() == 2 && inputList.getFirst().equals("AUTH");
}
}
}


0 comments on commit b2cf5cf

Please sign in to comment.