search functionality for server implemented

git-svn-id: svn://anubis/gvsu@38 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-03-08 22:45:37 +00:00
parent c6534cefca
commit ed4ccea45c

View File

@ -78,7 +78,8 @@ public class KaZaServer implements Runnable
BufferedReader br = new BufferedReader(
new InputStreamReader(
m_socket.getInputStream()));
DataOutputStream os = new DataOutputStream(socket.getOutputStream);
DataOutputStream os = new DataOutputStream(
m_socket.getOutputStream());
/* loop processing client messages */
for (;;)
@ -90,7 +91,7 @@ public class KaZaServer implements Runnable
continue;
String opCode = tokens.nextToken();
opCode.toUpperCase();
opCode = opCode.toUpperCase();
if (opCode.equals("HELO"))
{
/* user is announcing his or her username */
@ -119,7 +120,8 @@ public class KaZaServer implements Runnable
{
String query = tokens.nextToken();
String searchResults = performSearch(depth, query);
os.write(searchResults.getBytes();
os.writeBytes(searchResults);
os.writeBytes(".\n");
}
}
}
@ -131,6 +133,7 @@ public class KaZaServer implements Runnable
private String performSearch(int depth, String query)
{
String results = "";
String querylc = query.toLowerCase();
if (depth > 0)
{
synchronized (m_peerGroupLeaders)
@ -145,17 +148,42 @@ public class KaZaServer implements Runnable
}
/* now search my own file list */
synchronized (m_clientData)
{
Set<String> clients = m_clientData.keySet();
for (String clientIP : clients)
{
HashMap<String, String> files = m_clientData.get(clientIP).files;
Set<String> clientFiles = files.keySet();
for (String fileName : clientFiles)
{
String fnamelc = fileName.toLowerCase();
if ( (fnamelc.indexOf(querylc) >= 0) ||
(files.get(fileName).toLowerCase().indexOf(querylc) >= 0) )
{
results += clientIP + "|" +
m_clientData.get(clientIP).userName + "|" +
fileName + "|" +
files.get(fileName) + "\n";
}
}
}
}
return results;
}
private String getSearchResultsFromPeerGroupLeader(Socket s, int depth, String query)
private String getSearchResultsFromPeerGroupLeader(Socket s, int depth,
String query)
{
String searchResults = "";
try
{
BufferedReader br = new BufferedReader(
new InputStreamReader(
s.getInputStream()));
DataOutputStream os = new DataOutputStream(s.getOutputStream());
os.write("SRCH " + depth + " " + query);
String searchResults = "";
os.writeBytes("SRCH " + depth + " " + query);
for (;;)
{
String inLine = br.readLine();
@ -163,6 +191,11 @@ public class KaZaServer implements Runnable
break;
searchResults += inLine + "\n";
}
}
catch (Exception e)
{
return "";
}
return searchResults;
}
}