Skip to content

Commit

Permalink
Merge branch '115-refactor-main' into 115-refactor-main-OldPullRequst…
Browse files Browse the repository at this point in the history
…-Fix
  • Loading branch information
cmatlak authored Feb 2, 2024
2 parents 469b517 + b5feeb8 commit 99c12e8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ jobs:
run: |
Java_Version=$(mvn help:evaluate "-Dexpression=maven.compiler.release" -q -DforceStdout | sed -e 's/^1\./1.0./')
echo "Java_Version=$Java_Version" >> $GITHUB_ENV
- name: Set up JDK ${{env.Java_Version}}


uses: actions/setup-java@v4
with:
java-version: ${{ env.Java_Version }}
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ jobs:
run: |
Java_Version=$(mvn help:evaluate "-Dexpression=maven.compiler.release" -q -DforceStdout | sed -e 's/^1\./1.0./')
echo "Java_Version=$Java_Version" >> $GITHUB_ENV
- name: Set up ${{env.Java_Version}}


uses: actions/setup-java@v4
with:
java-version: ${{ env.Java_Version }}
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ jobs:
run: |
Java_Version=$(mvn help:evaluate "-Dexpression=maven.compiler.release" -q -DforceStdout | sed -e 's/^1\./1.0./')
echo "Java_Version=$Java_Version" >> $GITHUB_ENV
- name: Set up ${{env.Java_Version}}



uses: actions/setup-java@v4
with:
java-version: ${{ env.Java_Version }}
Expand Down
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>

<version>3.12.1</version>



</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
116 changes: 81 additions & 35 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Main {
static Logger logger = LogManager.getLogger(Main.class);

public static void main(String[] args) {

Initialize initialize = Initialize.getInitialize(args);

HazeDatabase hazeDatabase = new HazeDatabase();
Expand All @@ -28,50 +29,78 @@ public static void main(String[] args) {
initializeServer(args, initialize, auth);
final boolean isPasswordSet = auth.isPasswordSet();

addHook(hazeDatabase);
try (ServerSocket serverSocket = new ServerSocket()) {
initSocket(initialize, serverSocket);
whileServerOpen(hazeList, hazeDatabase, auth, isPasswordSet, serverSocket);
} catch (IOException e) {
logger.error(e);
}
logger.info("Shutting down....");
}

Thread printingHook = new Thread(() -> shutdown(hazeDatabase));
Runtime.getRuntime().addShutdownHook(printingHook);
private static void whileServerOpen(HazeList hazeList, HazeDatabase hazeDatabase, Auth auth, boolean isPasswordSet, ServerSocket serverSocket) throws IOException {
while (serverOpen) {
var client = serverSocket.accept();
logger.info("Application started: serverSocket.accept()");

try (ServerSocket serverSocket = new ServerSocket()) {
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(initialize.getPort()));
while (serverOpen) {
var client = serverSocket.accept();
logger.info("Application started: serverSocket.accept()");
runThread(hazeList, hazeDatabase, auth, isPasswordSet, client);
}
}

Runnable newThread = () -> {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
boolean clientAuthenticated = false;
while (true) {
List<String> inputList = new ArrayList<>();
private static void runThread(HazeList hazeList, HazeDatabase hazeDatabase, Auth auth, boolean isPasswordSet, Socket client) {
Runnable newThread = () -> createThread(hazeList, hazeDatabase, auth, isPasswordSet, client);
Thread.startVirtualThread(newThread);
}

String firstReading = input.readLine();
readInputStream(input, inputList, firstReading);
private static void createThread(HazeList hazeList, HazeDatabase hazeDatabase, Auth auth, boolean isPasswordSet, Socket client) {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
boolean clientAuthenticated = false;
while (true) {
List<String> inputList = getInputList(input);
clientAuthenticated = authenticateClient(auth, isPasswordSet, client, inputList, clientAuthenticated);
handleThread(hazeList, hazeDatabase, client, inputList);
}

clientAuthenticated = authenticateClient(auth, isPasswordSet, client, inputList, clientAuthenticated);
} catch (IOException e) {
logger.error(e);
}
}

client.getOutputStream().write(executeCommand(hazeDatabase, inputList, hazeList).getBytes());
private static void handleThread(HazeList hazeList, HazeDatabase hazeDatabase, Socket client, List<String> inputList) throws IOException {
controlCommand(hazeList, hazeDatabase, client, inputList);
printThreadDebug();

inputList.forEach(System.out::println); // For checking incoming message
inputList.clear();
}

printThreadDebug();
private static void controlCommand(HazeList hazeList, HazeDatabase hazeDatabase, Socket client, List<String> inputList) throws IOException {
client.getOutputStream().write(executeCommand(hazeDatabase, inputList, hazeList).getBytes());

inputList.clear();
}
inputList.forEach(System.out::println); // For checking incoming message
}

} catch (IOException e) {
logger.error(e);
}
};
Thread.startVirtualThread(newThread);
}
} catch (IOException e) {
logger.error(e);
}
logger.info("Shutting down....");
private static List<String> getInputList(BufferedReader input) throws IOException {
List<String> inputList = new ArrayList<>();

String firstReading = input.readLine();
readInputStream(input, inputList, firstReading);
return inputList;
}

private static void initSocket(Initialize initialize, ServerSocket serverSocket) throws IOException {
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(initialize.getPort()));
}

private static void addHook(HazeDatabase hazeDatabase) {
Thread printingHook = new Thread(() -> shutdown(hazeDatabase));
Runtime.getRuntime().addShutdownHook(printingHook);
}



private static void shutdown(HazeDatabase hazeDatabase) {
SaveFile.writeOnFile(hazeDatabase.copy());
logger.info("Shutting down....");
Expand All @@ -83,22 +112,40 @@ 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;

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 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);
Expand All @@ -115,7 +162,6 @@ public static String executeCommand(HazeDatabase hazeDatabase, List<String> inpu
case LMOVE -> hazeList.lMove(inputList);
case LTRIM -> hazeList.callLtrim(inputList);
case AUTH -> "+OK\r\n";

};
}

Expand Down

0 comments on commit 99c12e8

Please sign in to comment.