From 76e58e86c70f09421e7c5a60b66947fa47a11e8f Mon Sep 17 00:00:00 2001
From: mateis00 <mateis00@tu-dortmund.de>
Date: Sat, 10 Dec 2022 20:50:11 +0100
Subject: [PATCH] Switch implemented

---
 .../src/heimaufgaben/Client.java              |  36 +++++--
 .../src/heimaufgaben/Server.java              | 102 +++++++++++++-----
 2 files changed, 100 insertions(+), 38 deletions(-)

diff --git a/AufgabenStuff/ClientAnwendung/src/heimaufgaben/Client.java b/AufgabenStuff/ClientAnwendung/src/heimaufgaben/Client.java
index 7211667..ff8aff7 100644
--- a/AufgabenStuff/ClientAnwendung/src/heimaufgaben/Client.java
+++ b/AufgabenStuff/ClientAnwendung/src/heimaufgaben/Client.java
@@ -17,9 +17,9 @@ public class Client {
 
         // Eingaben müssen aufgenommen werden:
         BufferedReader info = new BufferedReader(new InputStreamReader(System.in)); //Eingabe von IP und Port, später Methodenaufrufe
-        System.out.println("IP-Adresse?");
+        System.out.print("IP-Adresse: ");
         ip = info.readLine();
-        System.out.println("Port?");
+        System.out.print("Port: ");
         String portFragezeichen = info.readLine();  // Port ist vorerst String
         
 
@@ -65,25 +65,37 @@ public class Client {
         System.out.println("Verbindung hergestellt, erwarte Auftrag:");
         BufferedReader info2 = new BufferedReader(new InputStreamReader(System.in));
         System.out.println("command: ");
-        String eingabe = info2.readLine();
-        
+        String eingabe = "";
+       
         while(true)
         {
+            while(eingabe == "")        
+            {
+               eingabe = info2.readLine();
+            }
+        
             
-            if(eingabe.equalsIgnoreCase("close")){
+            if(eingabe.equalsIgnoreCase("close"))
+            {
+                zumServer.write("PING\n");
+            zumServer.flush();
                 break;
             }
             else if(eingabe.equalsIgnoreCase("ping")){
+                eingabe ="";
                 ping();
             }
             else if(eingabe.substring(0,4).equalsIgnoreCase("echo")){
                 echo(eingabe);
+                eingabe ="";
             }
             else if(eingabe.equalsIgnoreCase("current time")){
                 time();
+                eingabe ="";
             }
             else if(eingabe.equalsIgnoreCase("current date")){
                 date();
+                eingabe ="";
             }
         }
         // "close" -> While schleife endet, constructor endet, client ist beendet
@@ -95,6 +107,7 @@ public class Client {
             zumServer.write("PING\n");
             zumServer.flush();
             System.out.println(vomServer.readLine());   // Gibt Antwort des Servers direkt aus
+            return;
         } catch (IOException e) {
             System.err.println("Probleme bei void Ping() (IOException)");
             return;
@@ -104,7 +117,8 @@ public class Client {
     // Aufruf "echo":
     private void echo(String eingabe) throws IOException{
         try {
-            zumServer.write(eingabe);   // Ganze Botschaft samt ECHO an Server geben
+            zumServer.write(eingabe+"\n");
+            zumServer.flush();   // Ganze Botschaft samt ECHO an Server geben
             System.out.println(kuerzen(vomServer.readLine()));  // Entfernen von "ECHO "
         } catch (IOException e) {
             System.err.println("Probleme bei void Echo() (IOException)");
@@ -115,8 +129,11 @@ public class Client {
     // Aufruf "current time":
     private void time() throws IOException{
         try {
-            zumServer.write("CURRENT TIME");
-            System.out.println(kuerzen(vomServer.readLine()));  // Entfernen von "TIME "
+            zumServer.write("CURRENT TIME\n");
+            zumServer.flush();  
+            
+            
+            System.out.println(vomServer.readLine());  // Entfernen von "TIME "
         } catch (IOException e) {
             System.err.println("Probleme bei void Time() (IOException)");
             return;
@@ -126,7 +143,8 @@ public class Client {
     // Aufruf "current date":
     private void date() throws IOException{
         try {
-            zumServer.write("CURRENT DATE");
+            zumServer.write("CURRENT DATE\n");
+            zumServer.flush();  
             System.out.println(kuerzen(vomServer.readLine()));  // Entfernen von "DATE "
         } catch (IOException e) {
             System.err.println("Probleme bei void Date() (IOException)");
diff --git a/AufgabenStuff/ServerAnwendung/src/heimaufgaben/Server.java b/AufgabenStuff/ServerAnwendung/src/heimaufgaben/Server.java
index b2ec2fc..5347160 100644
--- a/AufgabenStuff/ServerAnwendung/src/heimaufgaben/Server.java
+++ b/AufgabenStuff/ServerAnwendung/src/heimaufgaben/Server.java
@@ -14,12 +14,12 @@ public class Server{
   private ServerSocket serverSocket;
   private BufferedReader reader;
   private BufferedWriter writer;
-  private  ArrayList<String> historyList = new ArrayList<String>();
+  private ArrayList<String> historyList = new ArrayList<String>();
 
     public Server() throws IOException
     {
       BufferedReader portinput = new BufferedReader(new InputStreamReader(System.in));
-      System.out.print("Server möchte den port:");
+      System.out.print("Serverport: ");
       String givenPort = portinput.readLine();
       portinput.close();
       
@@ -44,43 +44,79 @@ public class Server{
             System.out.println("Server has accepted Connection");
             reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
-           
+            
+            
+            String command = "";
             while (true) {
               // Warte auf eine Nachricht vom Client:
-              String command = reader.readLine();
-            
+              while(command == ""){
+              command = reader.readLine();
+              }
               // Verarbeite den Befehl:
               try {
-                if (command.equalsIgnoreCase("ping")) {
+                String[] words = command.split(" "); 
+                
+                switch (words[0].toLowerCase())
+                {
+                  case "ping":
                   historyList.add(command);
                   PingPong();
-                } else if (command.substring(0, 4).equalsIgnoreCase("echo")) {
+                  command ="";
+                  break;
+
+                  case "echo":
                   historyList.add(command);
                   Echo(command);
-                } else if (command.equalsIgnoreCase("current time")) {
-                  historyList.add(command);
-                  sendTime("time");
-                } else if (command.equalsIgnoreCase("current date")) {
-                  historyList.add(command);
-                  sendTime("date");
-                } else if (command.equalsIgnoreCase("close")) {
-                  historyList.removeAll(historyList);
-                  socket.close();
-                  serverSocket.close();
-                } else if (command.equalsIgnoreCase("history")) {
+                  command ="";
+                  break;
+                  
+                  case "current":
+                    switch(words[1].toLowerCase())
+                    {
+                        case "time":
+                        historyList.add(command);
+                        sendTime(words[1]);
+                        command ="";
+                        break;
+                      
+                        case "date":
+                        historyList.add(command);
+                        sendTime(words[1]);
+                        command ="";
+                        break;
+                      
+                        default:
+                        historyList.add(command);
+                        writer.write(words[0] + "Err Unsuported Format!\n");
+                        writer.flush();
+                        command ="";
+                        break;
+                    }
+                  case "history":
                   historyList.add(command);
                   History();
+                  command ="";
+                  break;
+
+                  case "close":
+                  
+                  break;
+
+
+
+                  default:
+                    writer.write("Err Wrong or No command\n");
+                    System.out.println("Err Wrong or No command");
+                    writer.flush();
+                    break;    
                 }
+            
               } catch (IOException Re) {
-                System.err.println("Error with commandsidk");
+                System.err.println("Error with commands");
               }
             }
-            
-          
-            
-        }
-
 
+        }
 
     private void History()
     {
@@ -88,8 +124,10 @@ public class Server{
         for(int i = 0;i < historyList.size();i++)
         {
           writer.write(i + ": " + historyList.get(i));
+          
         }
         writer.write("HstrLstDone");
+        writer.flush();
       }catch (IOException err)
       {
         System.err.println("History Error");
@@ -106,13 +144,16 @@ public class Server{
           case("time"):
             SimpleDateFormat timeDateFormat = new SimpleDateFormat("hh:mm:ss");
             String formatedStringTime = timeDateFormat.format(new Date());
-            writer.write(format.toUpperCase()+" "+formatedStringTime);
+            writer.write(format.toUpperCase()+" "+formatedStringTime+"\n");
+            writer.flush();
             break;
 
           case("date"):
+            
             SimpleDateFormat dateDateFormat = new SimpleDateFormat("dd.MM.yyyy");
             String formatedStringDate = dateDateFormat.format(new Date());
-            writer.write(format.toUpperCase()+" "+formatedStringDate);
+            writer.write(format.toUpperCase()+" "+formatedStringDate+"\n");
+            writer.flush();
             break;
 
           default:
@@ -123,7 +164,6 @@ public class Server{
       }catch (IOException err)
       {
         System.err.println("ServerErr sendTime");
-
       }
     }
     
@@ -131,7 +171,9 @@ public class Server{
     private void Echo(String msg) throws IOException
     {
       try{  
-        writer.write(msg);
+        System.out.println("received and sending back: " +msg);
+        writer.write(msg+"\n");
+        writer.flush();
         return;
       }catch (IOException err)
       {
@@ -144,9 +186,11 @@ public class Server{
     private void PingPong() throws IOException {
       try {
         // Sende "Pong" an den Client:
-        System.out.println("wir haben ping Empfangen sende pong:");
+        System.out.println("received: PING sending: PONG");
         writer.write("PONG\n");
         writer.flush();
+        
+        return;
       } catch (IOException e) {
         System.err.println("Problem bei void PingPong() (IOException)");
         return;
-- 
GitLab