www.pudn.com > P2P file system.rar > P2PServer.java


package server; 
 
/** 
 *An Index server.  This contains an index of all the files that are being 
 *shared and their locations.  The key problems for this component are: 
 *How to construct a robust index that can be searched as quickly as 
 *possible. 
 */ 
import java.io.*; 
import java.net.*; 
import java.util.*; 
 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
 
import util.*; 
 
public class P2PServer extends Thread { 
	//default port number 
	public static final int DEFAULT_SERVER_PORT = 5678; 
 
	//server socket 
	private ServerSocket p2pServer; 
 
	//hash table to save the hashed data 
	public HashTable hashTableByFileName, hashTableByFileLocation; 
 
	//default constructor 
	public P2PServer() { 
 
		try { 
			//initialate the server socket 
			p2pServer = new ServerSocket(DEFAULT_SERVER_PORT); 
 
			//set up the hash table 
			hashTableByFileName = new HashTable(HashTable.HASH_BY_FILE_NAME); 
			hashTableByFileLocation = new HashTable( 
					HashTable.HASH_BY_FILE_LOCATION); 
 
			start(); 
			System.out.println("server Started!"); 
		} catch (Exception e) { 
 
		} 
 
	} 
 
	//stop the server 
	public void close() { 
		try { 
			p2pServer.close(); //close the server socket 
			System.out.println("Close"); 
 
			//clear the hash table 
			hashTableByFileName.flush(); 
			hashTableByFileLocation.flush(); 
			System.out.println("server stopped!!!"); 
 
		} catch (Exception e) { 
			System.out.println("There is something wrong"); 
		} 
	} 
 
	public void run() { 
		while (true) { 
 
			try { 
				//set up an socket to deal with the client's request 
				Socket connectionSocket = p2pServer.accept(); 
 
				//create a new thread 
				new ServerThread(connectionSocket, hashTableByFileName, 
						hashTableByFileLocation); 
			} catch (Exception e) { 
			} 
		} 
	} 
 
} 
 
 
 
 
 
 
//server Thread to dealt with the client's request 
class ServerThread extends Thread { 
	Socket socket; 
 
	ObjectOutputStream output; 
 
	ObjectInputStream input; 
 
	HashTable hashTableByFileName, hashTableByFileLocation; 
 
	//constructor 
	public ServerThread(Socket socket, HashTable hashTableByFileName, 
			HashTable hashTableByFileLocation) { 
		this.socket = socket; 
		this.hashTableByFileName = hashTableByFileName; 
		this.hashTableByFileLocation = hashTableByFileLocation; 
		start(); 
	} 
 
	public void run() { 
 
		String message; 
		try { 
 
			output = new ObjectOutputStream(socket.getOutputStream()); 
			input = new ObjectInputStream(socket.getInputStream()); 
			 
			do { 
				//read message from the client 
				message = input.readObject().toString(); 
				System.out.println("message:"+message); 
 
				//the client want to publish a shared file 
				if (message.equalsIgnoreCase("publish")) { 
					FileInformation fileInformation = (FileInformation) input 
							.readObject(); 
					System.out.println("publish"); 
 
					//insert the item to the hash table 
					hashTableByFileName.insertItem(fileInformation); 
					hashTableByFileLocation.insertItem(fileInformation); 
					//answer to the client 
					output.writeObject("success"); 
 
				} 
				//the client want to get the shared file list 
				else if (message.equalsIgnoreCase("list")) { 
					Vector list = hashTableByFileName.getFileList(); 
					int number = list.size(); 
					System.out.println("list"); 
 
 
					//send the number to the client 
					output.writeObject("" + number); 
 
					for (int i = 0; i < number; i++) { 
						output.writeObject(list.elementAt(i)); 
					} 
					System.out.println(number); 
				} 
				//the client want to get the shared file list 
				else if (message.equalsIgnoreCase("delete ")) { 
					System.out.println("delete"); 
					String fileName = message.substring(7); 
					Vector list = hashTableByFileName 
							.findFileByFileName(fileName); 
					int number = list.size(); 
 
					output.writeObject("" + number); 
 
					for (int i = 0; i < number; i++) { 
						output.writeObject(list.elementAt(i)); 
					} 
				} 
				//when the server wanto to close or update the shared file information 
				//we need to delete all the shared file information in the server 
				else if (message.startsWith("close ") 
						|| message.equalsIgnoreCase("update")) { 
					System.out.println("update"); 
					String clientAddress = message.substring(6); 
 
					Vector list = hashTableByFileLocation 
							.findFileByFileLocation(clientAddress); 
 
					for (int i = 0; i < list.size(); i++) { 
						hashTableByFileName.removeItem(list.elementAt(i)); 
						hashTableByFileLocation.removeItem(list.elementAt(i)); 
					} 
				} 
				//find method to find the needed file 
				else if (message.startsWith("find ")) { 
					System.out.println("find"); 
					String fileName = message.substring(5); 
					Vector list = hashTableByFileName 
							.findFileByFileName(fileName); 
					int number = list.size(); 
 
					output.writeObject("" + number); 
 
					for (int i = 0; i < number; i++) { 
						output.writeObject(list.elementAt(i)); 
					} 
				} else { 
					output.writeObject("error"); 
				} 
			} while (!message.equals("end") && !message.startsWith("close ")); 
		//	System.out.println("Close!!!"); 
		//	socket.close(); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
 
	} 
};