/******************************************************************************
 *
 *  CS 6421 - Discovery Server
 *  implement a discoverty server store adresses information
 * 
 ******************************************************************************/

package com.discovery.server;

import java.net.*;
import java.io.*;
import java.util.*;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class DiscovServer {
    static Multimap<String, String> discovTable = ArrayListMultimap.create();
    
    public static void add(String[] msg, PrintWriter out){
        if (msg.length != 5){
            System.out.println("Error input");
            out.println("Error input");
            out.println("Please input in format of <add unit1 unit2 ip port>");
            return;
        }
        
        String key = msg[1] + " " + msg[2];
        String value = msg[3] + " " + msg[4];
        Collection<String> values = discovTable.get(key);
        if(values.contains(value)){
        	out.println("This ip and port has already exist!");
        	System.out.println("This ip and port has already exist!");
        	return;
        }else{
	        discovTable.put(key, value);
	        System.out.println("Add successfully!");
	        //System.out.println("values = " + Multimap.get(key) + "n"); 
	        out.println("Add successfully!");
	        return;
        }
    }
    
    public static void remove(String[] msg, PrintWriter out){
        if (msg.length != 3){
            System.out.println("Error input");
            out.println("Error input");
            out.println("Please input in format of <remove unit3 unit4>");
            return;
        }
        
        String values = msg[1] + " " + msg[2];
        discovTable.values().remove(values);
        //System.out.println("the keys are:");
        System.out.println("Remove successfully!");
        out.println("Remove successfully!");
        return;
    }
    
    @SuppressWarnings("unused")
	public static void lookup(String[] msg, PrintWriter out){ 
        if (msg.length != 3){
            System.out.println("Error input");
            out.println("Error input");
            return;
        }
        
        String ipPort = msg[1] + " " + msg[2];
        if (ipPort == null){
            System.out.println("No such conversion exist!");
            out.println("No such conversion exist!");
            return;
        }else{
            out.println("Ip and port number is: " + discovTable.get(ipPort));
        }
        
        return;
    }
    
    public static void process (Socket clientSocket) throws IOException {
        // open up IO streams
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

        /* Write a welcome message to the client */
        out.println("Welcome to the DiscovServer!");

        /* read and print the client's request */
        // readLine() blocks until the server receives a new line from client
        String userInput;
        if ((userInput = in.readLine()) == null) {
            System.out.println("Error reading message");
            out.close();
            in.close();
            clientSocket.close();
        }

        System.out.println("Received message: " + userInput);
        String[] msg = userInput.split(" ");
        msg[0] = msg[0].toUpperCase();
        try{
            switch(msg[0]){
                case "ADD":
                    add(msg, out);
                    break;
                case "REMOVE":
                    remove(msg, out);
                    break;
                case "LOOKUP":
                    lookup(msg, out);   
                    break;
                default:
                    System.out.println("Error input");
                    break;
            }
        }catch(Exception e){
            out.println("Error:" + e);
        }
        // close IO streams, then socket
        out.close();
        in.close();
        clientSocket.close();
    }

    public static void main(String[] args) throws Exception {

        //check if argument length is invalid
        if(args.length != 1) {
            System.err.println("Usage: java ConvServer port");
        }
        // create socket
        int port = Integer.parseInt(args[0]);
        ServerSocket serverSocket = new ServerSocket(port);
        System.err.println("Started server on port " + port);
        
        // wait for connections, and process
        try {
            while(true) {
                // a "blocking" call which waits until a connection is requested
                Socket clientSocket = serverSocket.accept();
                System.err.println("\nAccepted connection from client");
                process(clientSocket);
            }

        }catch (IOException e) {
            System.err.println("Connection Error");
        }
        System.exit(0);
    }
}

@@ -546,20 +546,17 @@
 ate();%0A 
-   
 %0A
+
     publ
@@ -598,32 +598,70 @@
 intWriter out)%7B%0A
+    %09//check if msg valuse is invalid%0A
         if (msg.
@@ -1028,16 +1028,69 @@
 t(key);%0A
+        //check if the ip and port has already exist%0A
         
@@ -1278,16 +1278,60 @@
  %7Delse%7B%0A
+        %09//add this path to discovery table%0A
 %09       
@@ -1414,79 +1414,8 @@
 %22);%0A
-%09        //System.out.println(%22values = %22 + Multimap.get(key) + %22n%22); %0A
 %09   
@@ -1549,24 +1549,62 @@
 riter out)%7B%0A
+    %09//check if msg values is invalid%0A
         if (
@@ -1809,33 +1809,24 @@
 ;%0A        %7D%0A
-        %0A
         Stri
@@ -1872,42 +1872,170 @@
     
-discovTable.values().remove(values
+//check if the ip and port exist in the discovery table%0A        if(!discovTable.values().contains(values))%7B%0A        %09out.println(%22This ip and port doesn't exist!%22
 );%0A 
@@ -2041,18 +2041,17 @@
         
-//
+%09
 System.o
@@ -2066,25 +2066,183 @@
 ln(%22
-the keys are:%22
+This ip and port doesn't exist!%22);%0A        %09return;%0A        %7Delse%7B%0A        %09//if exist, remove ip and port from discovery table%0A%09        discovTable.values().remove(values
 );%0A
+%09
     
@@ -2281,32 +2281,33 @@
 uccessfully!%22);%0A
+%09
         out.prin
@@ -2331,24 +2331,25 @@
 ssfully!%22);%0A
+%09
         retu
@@ -2344,32 +2344,42 @@
         return;%0A
+        %7D%0A
     %7D%0A    %0A    @
@@ -2465,16 +2465,50 @@
  out)%7B %0A
+%09//check if msg values is invalid%0A
         
@@ -2694,32 +2694,82 @@
 + %22 %22 + msg%5B2%5D;%0A
+        //check if there is such ip and port exit%0A
         if (ipPo
@@ -4856,33 +4856,12 @@
 t = 
-Integer.parseInt(args%5B0%5D)
+3399
 ;%0A  

Update DiscovServer.java