|
|
|
@ -22,6 +22,8 @@ import protocolP2P.DiscoverResponse;
|
|
|
|
|
import protocolP2P.FileList;
|
|
|
|
|
import protocolP2P.HashRequest;
|
|
|
|
|
import localException.InternalError;
|
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Tracker management implementation with tcp
|
|
|
|
@ -32,7 +34,7 @@ import localException.InternalError;
|
|
|
|
|
*/
|
|
|
|
|
public class TrackerManagementTCP implements Runnable {
|
|
|
|
|
|
|
|
|
|
private int port;
|
|
|
|
|
private HostItem tracker;
|
|
|
|
|
private Logger logger;
|
|
|
|
|
private ServerSocket socket;
|
|
|
|
|
private List<HostItem> hostList = new ArrayList<>();
|
|
|
|
@ -43,15 +45,15 @@ public class TrackerManagementTCP implements Runnable {
|
|
|
|
|
* @param logger Logger object
|
|
|
|
|
*/
|
|
|
|
|
public TrackerManagementTCP(int port, Logger logger) {
|
|
|
|
|
this.port = port;
|
|
|
|
|
tracker = new HostItem("localhost", port);
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
try {
|
|
|
|
|
socket = new ServerSocket(port);
|
|
|
|
|
socket = new ServerSocket(tracker.getPort(), 10, tracker.getInetAddress());
|
|
|
|
|
} catch (SocketException e) {
|
|
|
|
|
logger.writeTCP("Error: cannot listen on port " + port, LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Error: cannot listen on" + tracker, LogLevel.Error);
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.writeTCP("Error: cannot openning socket", LogLevel.Error);
|
|
|
|
|
logger.writeTCP("Error: cannot open socket", LogLevel.Error);
|
|
|
|
|
System.exit(-2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -76,13 +78,12 @@ public class TrackerManagementTCP implements Runnable {
|
|
|
|
|
*/
|
|
|
|
|
private class ClientHandler implements Runnable {
|
|
|
|
|
private Socket s;
|
|
|
|
|
private String addr;
|
|
|
|
|
private HostItem addr;
|
|
|
|
|
/** Constructor with a socket.
|
|
|
|
|
* @param s Socket of this client
|
|
|
|
|
*/
|
|
|
|
|
public ClientHandler(Socket s) {
|
|
|
|
|
this.s = s;
|
|
|
|
|
this.addr = "[" +s.getInetAddress().getHostAddress() + "]:" + s.getPort() + " ";
|
|
|
|
|
this.addr = new HostItem(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Implementation of runnable. This method allow to serve one client.
|
|
|
|
@ -90,11 +91,11 @@ public class TrackerManagementTCP implements Runnable {
|
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
|
|
boolean end = false;
|
|
|
|
|
logger.writeTCP(addr + "New connection", LogLevel.Action);
|
|
|
|
|
logger.writeTCP("[ " + addr + "] New connection", LogLevel.Action);
|
|
|
|
|
do {
|
|
|
|
|
end = handleRequest();
|
|
|
|
|
} while(!end);
|
|
|
|
|
logger.writeTCP(addr + "End of connection", LogLevel.Action);
|
|
|
|
|
logger.writeTCP("[ " + addr + "] End of connection", LogLevel.Action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Respond to next request incomming on socket s.
|
|
|
|
@ -103,7 +104,7 @@ public class TrackerManagementTCP implements Runnable {
|
|
|
|
|
*/
|
|
|
|
|
private boolean handleRequest() {
|
|
|
|
|
try {
|
|
|
|
|
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s);
|
|
|
|
|
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket());
|
|
|
|
|
Payload p = pd.getPayload();
|
|
|
|
|
switch (p.getRequestResponseCode()) {
|
|
|
|
|
case LOAD_REQUEST:
|
|
|
|
|