gvsu/cs654/proj1/KaZaServer.java
josh c6534cefca KaZaServer progress, working on search functions
git-svn-id: svn://anubis/gvsu@37 45c1a28c-8058-47b2-ae61-ca45b979098e
2008-03-08 22:31:49 +00:00

169 lines
5.2 KiB
Java

import java.io.*;
import java.net.*;
import java.util.*;
public class KaZaServer implements Runnable
{
public static final int LISTEN_PORT = 3442;
private ServerSocket m_serverSocket;
private HashMap<String, ClientInfo> m_clientData;
private Vector<Socket> m_peerGroupLeaders;
public void run()
{
try {
m_serverSocket = new ServerSocket(LISTEN_PORT);
/* porcess connection requests */
for (;;)
{
Socket clientSocket = m_serverSocket.accept();
Thread thread = new Thread(new ClientHandler(clientSocket));
thread.start();
}
} catch (IOException ioe) {
System.out.println("KaZaServer: IO Exception!");
ioe.printStackTrace();
return;
}
}
public void close()
{
}
public String[] getClientList()
{
Set<String> s;
synchronized (m_clientData)
{
s = m_clientData.keySet();
}
return (String[]) s.toArray();
}
private class ClientInfo
{
String userName = "Anonymous";
HashMap<String, String> files = new HashMap<String, String>();
}
private class ClientHandler implements Runnable
{
private Socket m_socket;
private String m_clientIP;
ClientInfo m_clientInfo;
public ClientHandler(Socket socket)
{
m_socket = socket;
m_clientIP = m_socket.getInetAddress().getHostAddress();
synchronized (m_clientData)
{
if (!m_clientData.containsKey(m_clientIP))
{
m_clientData.put(socket.getInetAddress().getHostAddress(),
new ClientInfo());
}
m_clientInfo = m_clientData.get(m_clientIP);
}
}
public void run()
{
try
{
BufferedReader br = new BufferedReader(
new InputStreamReader(
m_socket.getInputStream()));
DataOutputStream os = new DataOutputStream(socket.getOutputStream);
/* loop processing client messages */
for (;;)
{
String inLine = br.readLine();
StringTokenizer tokens = new StringTokenizer(inLine);
if (!tokens.hasMoreTokens())
continue;
String opCode = tokens.nextToken();
opCode.toUpperCase();
if (opCode.equals("HELO"))
{
/* user is announcing his or her username */
synchronized (m_clientData)
{
m_clientInfo.userName = inLine.substring(5);
}
}
else if (opCode.equals("DESC"))
{
/* user is giving us a description of a file */
String fileName = inLine.substring(5);
String fileDesc = br.readLine();
synchronized (m_clientData)
{
m_clientInfo.files.put(fileName, fileDesc);
}
}
else if (opCode.equals("SRCH"))
{
/* user is requesting a search */
if (tokens.hasMoreTokens())
{
int depth = Integer.parseInt(tokens.nextToken());
if (tokens.hasMoreTokens())
{
String query = tokens.nextToken();
String searchResults = performSearch(depth, query);
os.write(searchResults.getBytes();
}
}
}
}
} catch (Exception e) { }
}
}
private String performSearch(int depth, String query)
{
String results = "";
if (depth > 0)
{
synchronized (m_peerGroupLeaders)
{
for (Socket s : m_peerGroupLeaders)
{
String peerResults =
getSearchResultsFromPeerGroupLeader(s, depth-1, query);
results += peerResults;
}
}
}
/* now search my own file list */
}
private String getSearchResultsFromPeerGroupLeader(Socket s, int depth, String query)
{
BufferedReader br = new BufferedReader(
new InputStreamReader(
s.getInputStream()));
DataOutputStream os = new DataOutputStream(s.getOutputStream());
os.write("SRCH " + depth + " " + query);
String searchResults = "";
for (;;)
{
String inLine = br.readLine();
if (inLine.equals("."))
break;
searchResults += inLine + "\n";
}
return searchResults;
}
}