Updated the project structure
This commit is contained in:
11
Server/Server.iml
Normal file
11
Server/Server.iml
Normal 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>
|
||||
7
Server/src/logging/LogType.java
Normal file
7
Server/src/logging/LogType.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package logging;
|
||||
|
||||
public enum LogType {
|
||||
Log,
|
||||
Message,
|
||||
Error
|
||||
}
|
||||
39
Server/src/logging/ServerLogger.java
Normal file
39
Server/src/logging/ServerLogger.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package logging;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
public class ServerLogger {
|
||||
|
||||
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";
|
||||
|
||||
public void printLog(String message, String name, LogType logType){
|
||||
switch (logType){
|
||||
case Log:
|
||||
System.out.printf(ANSI_CYAN + "%s %s%n"+ANSI_RESET, new Timestamp(System.currentTimeMillis()), message);
|
||||
break;
|
||||
|
||||
case Error:
|
||||
System.out.printf(ANSI_RED + "%s %s%n"+ ANSI_RESET, new Timestamp(System.currentTimeMillis()), 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, LogType logType){
|
||||
this.printLog(message, "", logType);
|
||||
}
|
||||
}
|
||||
113
Server/src/networking/Server.java
Normal file
113
Server/src/networking/Server.java
Normal file
@@ -0,0 +1,113 @@
|
||||
package networking;
|
||||
|
||||
import logging.LogType;
|
||||
import logging.ServerLogger;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Server {
|
||||
private ServerSocket serverSocket;
|
||||
private HashMap<Integer, Socket> clients;
|
||||
private HashMap<Socket, String> clientNames;
|
||||
private HashMap<Socket, DataOutputStream> outstreams;
|
||||
private HashMap<Socket, DataInputStream> instreams;
|
||||
private ServerLogger logger;
|
||||
private Scanner scanner;
|
||||
private int requiredConnections;
|
||||
|
||||
public Server(int port){
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
clients = new HashMap<>();
|
||||
clientNames = new HashMap<>();
|
||||
outstreams = new HashMap<>();
|
||||
instreams = new HashMap<>();
|
||||
scanner = new Scanner(System.in);
|
||||
logger = new ServerLogger();
|
||||
requiredConnections = 1;
|
||||
|
||||
logger.printLog("Server started successfully", LogType.Log);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void connectClients(){
|
||||
try {
|
||||
int id = 0;
|
||||
logger.printLog(String.format("Waiting for %d clients to connect ...", requiredConnections), LogType.Log);
|
||||
while(clients.size() < requiredConnections) {
|
||||
Socket momentaryClient = serverSocket.accept();
|
||||
clients.put(id, momentaryClient);
|
||||
outstreams.put(momentaryClient, new DataOutputStream(momentaryClient.getOutputStream()));
|
||||
instreams.put(momentaryClient, new DataInputStream(momentaryClient.getInputStream()));
|
||||
id++;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void handshake(){
|
||||
for (Socket client: clients.values()) {
|
||||
try {
|
||||
int handshakeValue = instreams.get(client).readInt();
|
||||
if (handshakeValue == 165313125) {
|
||||
outstreams.get(client).writeInt(200);
|
||||
outstreams.get(client).flush();
|
||||
clientNames.put(client, instreams.get(client).readUTF());
|
||||
outstreams.get(client).writeUTF(serverSocket.getInetAddress().getHostAddress()+":"+serverSocket.getLocalPort());
|
||||
outstreams.get(client).flush();
|
||||
logger.printLog(String.format("%s got connected", clientNames.get(client)), LogType.Log);
|
||||
} else {
|
||||
outstreams.get(client).writeInt(403);
|
||||
outstreams.get(client).flush();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void getMessages(){
|
||||
for (Socket client: clients.values()) {
|
||||
try {
|
||||
while (true) {
|
||||
String message = instreams.get(client).readUTF();
|
||||
if (!message.equalsIgnoreCase("exit()")) {
|
||||
logger.printLog(message, clientNames.get(client), LogType.Message);
|
||||
} else {
|
||||
outstreams.get(client).writeInt(200);
|
||||
logger.printLog(String.format("%s closed the connection",clientNames.get(client)), LogType.Log);
|
||||
break;
|
||||
}
|
||||
outstreams.get(client).writeInt(200);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException ioException) {
|
||||
ioException.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
Server server = new Server(2589);
|
||||
server.connectClients();
|
||||
server.handshake();
|
||||
server.getMessages();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user