diff --git a/cs654/proj1/KaZaServer.java b/cs654/proj1/KaZaServer.java index fd68555..e3e62c8 100644 --- a/cs654/proj1/KaZaServer.java +++ b/cs654/proj1/KaZaServer.java @@ -192,7 +192,7 @@ public class KaZaServer implements Runnable if (tokens.hasMoreTokens()) { String query = tokens.nextToken(); - String searchResults = performSearch(depth, query); + String searchResults = performSearch(depth, query, m_socket); os.writeBytes(searchResults); os.writeBytes(".\n"); // System.out.println("Got SRCH for " + query + @@ -227,11 +227,13 @@ public class KaZaServer implements Runnable } } - private String performSearch(int depth, String query) + private String performSearch(int depth, String query, Socket clientSocket) { String results = ""; String querylc = query.toLowerCase(); + /* don't perform a search that we are already performing + * (avoid duplicate results */ synchronized (m_currentSearches) { if (m_currentSearches.containsKey(querylc)) @@ -276,7 +278,17 @@ public class KaZaServer implements Runnable if ( (fnamelc.indexOf(querylc) >= 0) || (files.get(fileName).toLowerCase().indexOf(querylc) >= 0) ) { - results += clientIP + "|" + + /* clientIPToReturn will hold our MNH server's opinion + * of the client IP address which has the file being + * searched for *unless* it starts with "127.", in which + * case it is a local connection and the client performing + * the search should receive back whichever IP it is using + * to talk to the MNH server */ + String clientIPToReturn = + clientIP.startsWith("127.") + ? clientSocket.getLocalAddress().getHostAddress() + : clientIP; + results += clientIPToReturn + "|" + m_clientData.get(clientIP).userName + "|" + m_clientData.get(clientIP).speed + "|" + fileName + "|" +