p2p.server.P2PServer

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 want 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();
  }

 }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值