<commit_before>

import java.net.Socket;
import java.net.ServerSocket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator; 
import java.util.Map; 

public class DiscoveryServer {
    
    public static HashMap<String,String> table = new HashMap<String,String>();

    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, please enter command");

        /* read and print the client's request */
        // readLine() blocks until the server receives a new line from client
        String input;
        input = in.readLine();
        System.out.println("input is " + input);
        String a[] = input.split(" ");
        String command = a[0];
        if(command.equals("ADD") || command.equals("Add") || command.equals("add"))
        {
            //add
            String unit1 = a[1];
            String unit2 = a[2];
            String ip = a[3];
            String port = a[4];
            String key1 = unit1 + "-" + unit2;
            String value1 = ip + "-" + port;
            if(table.containsKey(key1) && table.get(key1).equals(value1)){
                out.println("FAILURE already exists");
                System.out.println("failed add");
            }
            else{
                table.put(key1,value1);
                out.println("SUCCESS");
                System.out.println("success add");
            }
            
        }
        
        if(command.equals("REMOVE") || command.equals("Remove") || command.equals("remove"))
        {
            //remove
            String ip = a[1];
            String port = a[2];
            String value1 = a[1] + "-" + a[2];
            Iterator<Map.Entry<String, String>> it = table.entrySet().iterator();  
            while(it.hasNext())
            { 
                Map.Entry<String, String> entry=it.next();  
                String value2 = entry.getValue();  
                if(value1.equals(value2)){  
                    it.remove();        //OK   
                    out.println("SUCCESS");
                    System.out.println("success remove");
                    out.close();
                    in.close();
                    clientSocket.close();
                    return;
                }  
            }
            out.println("FAILURE no such ip-port pairs");
            System.out.println("failed remove");
        }
        if(command.equals("LOOKUP") || command.equals("Lookup") || command.equals("lookup"))
        {
            String unit1 = a[1];
            String unit2 = a[2];
            String key1 = a[1] + "-" + a[2];
            if(table.containsKey(key1))
            {
                String str = table.get(key1);<commit_msg>fixed lookup command to search bi-directionally<commit_after>

import java.net.Socket;
import java.net.ServerSocket;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator; 
import java.util.Map; 

public class DiscoveryServer {
    
    public static HashMap<String,String> table = new HashMap<String,String>();

    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, please enter command");

        /* read and print the client's request */
        // readLine() blocks until the server receives a new line from client
        String input;
        input = in.readLine();
        System.out.println("input is " + input);
        String a[] = input.split(" ");
        String command = a[0];
        if(command.equals("ADD") || command.equals("Add") || command.equals("add"))
        {
            //add
            String unit1 = a[1];
            String unit2 = a[2];
            String ip = a[3];
            String port = a[4];
            String key1 = unit1 + "-" + unit2;
            String value1 = ip + "-" + port;
            if(table.containsKey(key1) && table.get(key1).equals(value1)){
                out.println("FAILURE already exists");
                System.out.println("failed add");
            }
            else{
                table.put(key1,value1);
                out.println("SUCCESS");
                System.out.println("success add");
            }
            
        }
        
        if(command.equals("REMOVE") || command.equals("Remove") || command.equals("remove"))
        {
            //remove
            String ip = a[1];
            String port = a[2];
            String value1 = a[1] + "-" + a[2];
            Iterator<Map.Entry<String, String>> it = table.entrySet().iterator();  
            while(it.hasNext())
            { 
                Map.Entry<String, String> entry=it.next();  
                String value2 = entry.getValue();  
                if(value1.equals(value2)){  
                    it.remove();        //OK   
                    out.println("SUCCESS");
                    System.out.println("success remove");
                    out.close();
                    in.close();
                    clientSocket.close();
                    return;
                }  
            }
            out.println("FAILURE no such ip-port pairs");
            System.out.println("failed remove");
        }
        if(command.equals("LOOKUP") || command.equals("Lookup") || command.equals("lookup"))
        {
            String unit1 = a[1];
            String unit2 = a[2];
            String key1 = a[1] + "-" + a[2];
            String key2 = a[2] + "-" + a[1];
            if(table.containsKey(key1))
            {
                String str = table.get(key1);
                String ss[] = str.split("-");
                out.println(ss[0] + " " + ss[1]);
                System.out.println("success lookup " + ss[0] + " " + ss[1]);
            }
            else if(table.containsKey(key2))
            {
                String str = table.get(key2);
{'fraction_non_alphanumeric': 0.1302705153392776, 'fraction_numerical': 0.01133444158984434, 'mean_word_length': 1.646141543382647, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3729780', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1717, 'n_words': 398}