diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/client/networking/Client.java b/src/client/networking/Client.java index a5e3b06..e4a2fa8 100644 --- a/src/client/networking/Client.java +++ b/src/client/networking/Client.java @@ -10,16 +10,22 @@ public class Client { private DataOutputStream out; private DataInputStream in; private static Scanner scanner; + private static String name; private boolean success; - public Client(String ip, int port) { + public static final String ANSI_RESET = "\u001B[0m"; + public static final String ANSI_CYAN = "\u001B[36m"; + + + public Client(String ip, int port, String name) { try { scanner = new Scanner(System.in); Socket serverSocket = new Socket(ip, port); out = new DataOutputStream(serverSocket.getOutputStream()); in = new DataInputStream(serverSocket.getInputStream()); - System.out.println(in.readUTF()); success = true; + this.name = name; + System.out.println(name); } catch (IOException e) { e.printStackTrace(); } @@ -27,9 +33,14 @@ public class Client { public void handshake() { try { - out.writeUTF("849465467842158"); + out.writeInt(165313125); out.flush(); success = in.readInt() == 200; + if (success){ + out.writeUTF(name); + System.out.printf(ANSI_CYAN + "You successfully connected to %s%n"+ANSI_RESET, in.readUTF()); + } + System.out.println(success); } catch (IOException e) { e.printStackTrace(); } @@ -41,7 +52,7 @@ public class Client { String message = scanner.nextLine(); try { out.writeUTF(message); - out.flush(); + System.out.println(in.readInt()); } catch (IOException e) { e.printStackTrace(); } @@ -64,8 +75,16 @@ public class Client { return success; } + public String getName(){return name;} + + public static void main(String[] args) { - Client client = new Client("localhost", 2589); + Client client; + if (args.length > 0) { + client = new Client("localhost", 2589, args[0]); + } else { + client = new Client("localhost", 2589, "GenericName"); + } client.handshake(); client.oneSidedMessage(); } diff --git a/src/server/logging/LogType.java b/src/server/logging/LogType.java new file mode 100644 index 0000000..29087d1 --- /dev/null +++ b/src/server/logging/LogType.java @@ -0,0 +1,7 @@ +package server.logging; + +public enum LogType { + Log, + Message, + Error +} diff --git a/src/server/logging/Logger.java b/src/server/logging/Logger.java new file mode 100644 index 0000000..9b85864 --- /dev/null +++ b/src/server/logging/Logger.java @@ -0,0 +1,39 @@ +package server.logging; + +import java.sql.Timestamp; + +public class Logger { + + 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); + } +} diff --git a/src/server/networking/Server.java b/src/server/networking/Server.java index 3a96dca..31eb856 100644 --- a/src/server/networking/Server.java +++ b/src/server/networking/Server.java @@ -1,5 +1,8 @@ package server.networking; +import server.logging.LogType; +import server.logging.Logger; + import java.io.*; import java.net.ServerSocket; import java.net.Socket; @@ -9,8 +12,10 @@ import java.util.Scanner; public class Server { private ServerSocket serverSocket; private HashMap clients; + private HashMap clientNames; private HashMap outstreams; private HashMap instreams; + private Logger logger; private Scanner scanner; private int requiredConnections; @@ -18,10 +23,14 @@ public class Server { try { serverSocket = new ServerSocket(port); clients = new HashMap<>(); + clientNames = new HashMap<>(); outstreams = new HashMap<>(); instreams = new HashMap<>(); scanner = new Scanner(System.in); + logger = new Logger(); requiredConnections = 2; + + logger.printLog("Server started successfully", LogType.Log); } catch (IOException e) { e.printStackTrace(); } @@ -30,14 +39,13 @@ public class Server { public void connectClients(){ try { int id = 0; - System.out.printf("Waiting for %d clients to connect%n", requiredConnections); + 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++; - System.out.printf("networking.Client %d got connected%n", id); } } catch (IOException e) { e.printStackTrace(); @@ -47,57 +55,54 @@ public class Server { public void handshake(){ for (Socket client: clients.values()) { try { - String handshakeValue = instreams.get(client).readUTF(); - if (handshakeValue.equals("849465467842158")) { + 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(); } - outstreams.get(client).flush(); - } catch (IOException e) { - e.printStackTrace(); - } - } - for (Socket client: clients.values()) { - try { - String response = instreams.get(client).readUTF(); - System.out.println(response); - if (response.equals("1")){ - outstreams.get(client).writeUTF("Connection confirmed :)"); - outstreams.get(client).writeUTF("Send me a message ..."); - System.out.println("Connection confirmed :)"); - } else { - outstreams.get(client).writeUTF("Connection failed"); - System.out.println("Connection failed"); - client.close(); - } - outstreams.get(client).flush(); + } catch (IOException e) { e.printStackTrace(); } } } - public void getMessage(){ + public void getMessages(){ for (Socket client: clients.values()) { try { while (true) { String message = instreams.get(client).readUTF(); - System.out.println(message); - if(message.equalsIgnoreCase("exit()")) + 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(); + } } } } public static void main(String[] args) { Server server = new Server(2589); - System.out.println("networking.Server got started"); server.connectClients(); server.handshake(); - server.getMessage(); + server.getMessages(); } } \ No newline at end of file