Updated the project structure

This commit is contained in:
2021-02-14 21:13:09 +01:00
parent 19ed869006
commit 51db69bee8
15 changed files with 119 additions and 15 deletions

8
.idea/artifacts/Client_jar.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" name="Client:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Client_jar</output-path>
<root id="archive" name="ArcadeMachine.jar">
<element id="module-output" name="ArcadeMachine" />
</root>
</artifact>
</component>

2
.idea/modules.xml generated
View File

@@ -3,6 +3,8 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/ArcadeMachine.iml" filepath="$PROJECT_DIR$/ArcadeMachine.iml" /> <module fileurl="file://$PROJECT_DIR$/ArcadeMachine.iml" filepath="$PROJECT_DIR$/ArcadeMachine.iml" />
<module fileurl="file://$PROJECT_DIR$/Client/Client.iml" filepath="$PROJECT_DIR$/Client/Client.iml" />
<module fileurl="file://$PROJECT_DIR$/Server/Server.iml" filepath="$PROJECT_DIR$/Server/Server.iml" />
</modules> </modules>
</component> </component>
</project> </project>

11
Client/Client.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -1,4 +1,4 @@
package client.games; package games;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;

View File

@@ -1,4 +1,4 @@
package client.games; package games;
import javax.swing.*; import javax.swing.*;

View File

@@ -1,4 +1,4 @@
package client.games; package games;
import javax.swing.*; import javax.swing.*;

View File

@@ -1,4 +1,4 @@
package client.games; package games;
import java.awt.*; import java.awt.*;

View File

@@ -0,0 +1,47 @@
package logging;
import java.sql.Timestamp;
public class ClientLogger {
//Text colors
private static final String ANSI_RESET = "\u001B[0m";
private static final String ANSI_BLACK = "\u001B[30m";
private static final String ANSI_RED = "\u001B[31m";
private static final String ANSI_GREEN = "\u001B[32m";
private static final String ANSI_YELLOW = "\u001B[33m";
private static final String ANSI_BLUE = "\u001B[34m";
private static final String ANSI_PURPLE = "\u001B[35m";
private static final String ANSI_CYAN = "\u001B[36m";
private static final String ANSI_WHITE = "\u001B[37m";
private static final String ANSI_BRIGHT_WHITE = "\u001b[37;1m";
public void printLog(String message, String name, boolean success, LogType logType){
switch (logType){
case Log:
if (success){
System.out.printf( ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_GREEN + "%s%n" + ANSI_RESET,
new Timestamp(System.currentTimeMillis()), name, message);
} else {
System.out.printf(ANSI_BLUE + "(%s) " + ANSI_CYAN + "[%s] " + ANSI_RED + "%s%n" + ANSI_RESET,
new Timestamp(System.currentTimeMillis()), name, message);
}
break;
case Error:
System.out.printf(ANSI_PURPLE + "(%s) [%s] %s%n"+ ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
break;
case Message:
System.out.printf(ANSI_WHITE + " (%s) [%s] %s%n" +ANSI_RESET, new Timestamp(System.currentTimeMillis()), name, message);
break;
default:
System.err.println("NO VALID LOGTYPE GIVEN");;
}
}
public void printLog(String message, boolean success, LogType logType){
this.printLog(message, "server", success, logType);
}
}

View File

@@ -1,4 +1,4 @@
package server.logging; package logging;
public enum LogType { public enum LogType {
Log, Log,

View File

@@ -1,4 +1,7 @@
package client.networking; package networking;
import logging.ClientLogger;
import logging.LogType;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@@ -10,7 +13,9 @@ public class Client {
private DataOutputStream out; private DataOutputStream out;
private DataInputStream in; private DataInputStream in;
private static Scanner scanner; private static Scanner scanner;
private ClientLogger clientLogger;
private static String name; private static String name;
private String serverName;
private boolean success; private boolean success;
public static final String ANSI_RESET = "\u001B[0m"; public static final String ANSI_RESET = "\u001B[0m";
@@ -23,9 +28,10 @@ public class Client {
Socket serverSocket = new Socket(ip, port); Socket serverSocket = new Socket(ip, port);
out = new DataOutputStream(serverSocket.getOutputStream()); out = new DataOutputStream(serverSocket.getOutputStream());
in = new DataInputStream(serverSocket.getInputStream()); in = new DataInputStream(serverSocket.getInputStream());
clientLogger = new ClientLogger();
success = true; success = true;
this.name = name; this.name = name;
System.out.println(name); clientLogger.printLog(String.format("Client with the name %s successfully initialized", name), success, LogType.Log);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -33,14 +39,18 @@ public class Client {
public void handshake() { public void handshake() {
try { try {
out.writeInt(165313125); out.writeInt(15315);
out.flush(); out.flush();
success = in.readInt() == 200; success = in.readInt() == 200;
if (success){ if (success){
out.writeUTF(name); out.writeUTF(name);
System.out.printf(ANSI_CYAN + "You successfully connected to %s%n"+ANSI_RESET, in.readUTF()); serverName = in.readUTF();
System.out.println(serverName);
clientLogger.printLog("You successfully connected to me", serverName, success, LogType.Log);
} else {
clientLogger.printLog("Connection failed try again", success, LogType.Log);
System.exit(1);
} }
System.out.println(success);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

11
Server/Server.iml Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@@ -0,0 +1,7 @@
package logging;
public enum LogType {
Log,
Message,
Error
}

View File

@@ -1,4 +1,4 @@
package server.logging; package logging;
import java.sql.Timestamp; import java.sql.Timestamp;

View File

@@ -1,7 +1,7 @@
package server.networking; package networking;
import server.logging.LogType; import logging.LogType;
import server.logging.ServerLogger; import logging.ServerLogger;
import java.io.*; import java.io.*;
import java.net.ServerSocket; import java.net.ServerSocket;
@@ -28,7 +28,7 @@ public class Server {
instreams = new HashMap<>(); instreams = new HashMap<>();
scanner = new Scanner(System.in); scanner = new Scanner(System.in);
logger = new ServerLogger(); logger = new ServerLogger();
requiredConnections = 2; requiredConnections = 1;
logger.printLog("Server started successfully", LogType.Log); logger.printLog("Server started successfully", LogType.Log);
} catch (IOException e) { } catch (IOException e) {
@@ -97,6 +97,11 @@ public class Server {
} }
} }
} }
clients.clear();
System.out.println("Do you want to keep the server alive and wait for other clients ? [y]/[n]");
if (scanner.nextLine().equalsIgnoreCase("y")){
this.connectClients();
}
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: client.networking.Client