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