Merge pull request 'tracker' (#48) from tracker into etape4
Ajout du tracker, correction de bugs, et documentation.pull/64/head
commit
e0dca481fb
@ -0,0 +1 @@
|
||||
<mxfile host="app.diagrams.net" modified="2020-03-19T14:50:01.302Z" agent="Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0" etag="dOXguTcOAZ-R9XuIfENW" version="12.8.8" type="device"><diagram name="Page-1" id="13e1069c-82ec-6db2-03f1-153e76fe0fe0">7Vtfc+I2EP80zFwfkrEt28BjQkh60+tdEtJrp2/CFqBGWFSI/LlP3xWWDZbAEJ8hZpo8MNJqvZJ2f1rtSkoL9aYvNwLPJr/zmLCW58QvLXTV8rxO2IFfRXhNCX63mxLGgsYpyV0RBvQH0URHUxc0JvMCo+ScSTorEiOeJCSSBRoWgj8X2UacFXud4TGxCIMIM5v6J43lRFNdx1k1/EroeKK77gS6YYijx7Hgi0T31/LQaPmXNk9xJkvzzyc45s9rJNRvoZ7gXKal6UuPMKXaTG3pd9dbWvNxC5LIfT5oD/EQu5E/jLz2yB86Z14q4QmzhdbFg4A5EaHHK18zHcHQZ6q4mLIvdEQYTaB2OSOCTokEfnTFNPl2Rbt8nlBJBjMcqU+fATxAm8gpg5oLRTCoxPCJyOuM4dmcDpe9OkARJFqIOX0i92Se4kZR+UKqnno5Hpasyg4k1qJyVTtLuVMa6TLDQ8Iuc8P1OOOq+4QvJzSXgj/mKFCCRjDGazylTIH7OxExTrAmayS7YMdLzOg4gUoEplhO3baNNtcTEZK8rJG0rW4IB7WJV2DRrZ6vcaPXVUdXn9dAmmF0sobPIGPEemGMc9ErcEBB42NPrCALKz1GYXLuB1YagBXfaRJW2hZWWuji2/AfZYEPtLw/Wrphk9DSsdAyIAIm8+FZmoAVt1GexQ8tsHzlkmxFSqIad0JAW9X1oaw0QyE4vNC6lHy2pllGRlJ9AKJoMn5QbVdnYcHwztENX79DQHsaGQU12Lj9RzIVf9Hf/h7Iq3+vF/0wnm6ITO/7N58HD/37T79Yxl6zpGm9IZeST6GBJPGFyhUUjfHosWxxSCzGRG5BoB4ZiQtJha3ZNdVlK0EQhiW4kUJnm1Snpd1yCiNbrcSgaCQ3NFbYnC9ERPRX63mAISg37zZBqQIsQUtL5lOsblzfMu4XMC1Q7vt3lYw7hwHLzLwc5Ga0a8qyjzcAYMmil1GnMiA6bwdEWA8gvG7Rjp5TERAo2CFoCyBAofh1jW2mGOYlA+5s7meFr1RirWgLtqNtcPtpRBkBxffUwQEUvWre5V0BuK9H0kZwzl2/bbn3miBp+qg8h32zj3J3CNoCyZWgjJGPRnNyED/WtZB19XnQ+/a9f5+ha54FsgpfacVbr6Bmwi01UjncSiGJ3s0nItMnmmHovgD0TSSbgmryiabv9dERfGK2uKycy7Pw+JFzHT/nCoL3zLk2Asa1APOTAflJbJn1+6fQqSmID8P3DeKzvPNEo/gyr9iEUL4qLKxQ/siw2HD064VMnZnE9AmKY1UshEl3yoGkLNDjGlcjQVQaGqFGux4roulWxFhgxuamoJpCI98r9oPaTum4svYt/AcKpeyLMTO/9Fb55SkE/GWusanQNnbVymmnH+4QVBO0A/PA1by6N8eFSvkPBO3/50GdVvmZc+6gUAs+AGSN232/sjd2dghqwEmJax/CpfkmsmD0kW8eP98MjY2zAfmmfc/X8Hxz9xYanNQWmu0wP72FWoJq2kJDw4G63WNsifZjhVPaEstw2YSstyrmrKx3T8zVBgv7KqDkkulkk4APD1ZvEmAG9f6O/LZTyn8Yj5fNfb/8tqH3p6WnNp09UL3vhVZ+x+o4CBW3JzumquvO1cxZg4q497rd86BcVF0PAYwTpCyQ3Dqydil/Efm2gopzOFyak0feW26E72C5RBNKxCkuk4YfbppZsFuT87cE1XXvaxxuhsc40fE8C6BfASpg1QmB32j5oh8Kj4n6r5rslYKrppXEeR2pNBA/Ee3zAXUXUIQsOGEcK7YIJ/A7JGOanFtAP/rTzK1wf8MjWMP/hBsy5MyAhQy5bYCnvgeS9rHzHo9PTiHyPK33JqbfCaq+wTNPeMMDbb3muWN2j1zR70B19Q9sKfvqnwRR/z8=</diagram></mxfile>
|
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 29 KiB |
@ -0,0 +1,71 @@
|
||||
package protocolP2P;
|
||||
import protocolP2P.Payload;
|
||||
import tools.BytesArrayTools;
|
||||
import localException.InternalError;
|
||||
import localException.SizeError;
|
||||
import localException.ProtocolError;
|
||||
import localException.TransmissionError;
|
||||
|
||||
/** Representation of payload for discover request.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class DiscoverRequest extends Payload {
|
||||
|
||||
private String filename;
|
||||
|
||||
/** Constructor with filename (typically used by client). If filename is null, it is initialized with "".
|
||||
* @param filename Name of the file you want a server list of.
|
||||
* @throws InternalError
|
||||
*/
|
||||
public DiscoverRequest(String filename) throws InternalError {
|
||||
super(RequestResponseCode.DISCOVER_REQUEST);
|
||||
if (filename == null) {
|
||||
this.filename = "";
|
||||
} else {
|
||||
this.filename = filename;
|
||||
}
|
||||
}
|
||||
|
||||
/** Constructor (typically used by server) with a byte[] parameter containing the Packet received.
|
||||
* @param packet the full Packet received
|
||||
* @throws SizeError
|
||||
* @throws InternalError
|
||||
* @throws ProtocolError
|
||||
* @throws TransmissionError
|
||||
*/
|
||||
protected DiscoverRequest(byte[] packet) throws SizeError, ProtocolError, InternalError, TransmissionError {
|
||||
super(packet);
|
||||
int size = getPayloadSize(packet);
|
||||
filename = BytesArrayTools.readString(packet, Payload.PAYLOAD_START_POSITION, size);
|
||||
}
|
||||
|
||||
/** Returns a byte[] containing Packet with padding.
|
||||
* This Packet is still incomplete and should not be send directly.
|
||||
* ProtocolP2PPacket will use this method to generate the complete Packet.
|
||||
* @return Packet with padding
|
||||
* @throws InternalError
|
||||
*/
|
||||
protected byte[] toPacket() throws InternalError {
|
||||
// compute total size
|
||||
int size = PAYLOAD_START_POSITION + filename.length();
|
||||
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
||||
// set request/response code
|
||||
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||
// set Payload size
|
||||
setPayloadSize(size - PAYLOAD_START_POSITION, packet);
|
||||
// write filename to Packet
|
||||
BytesArrayTools.write(packet, filename, PAYLOAD_START_POSITION);
|
||||
return packet;
|
||||
}
|
||||
|
||||
/** Filename getter.
|
||||
* @return filename
|
||||
*/
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,117 @@
|
||||
package protocolP2P;
|
||||
import protocolP2P.Payload;
|
||||
import tools.HostItem;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import localException.InternalError;
|
||||
import localException.SizeError;
|
||||
import localException.ProtocolError;
|
||||
import localException.TransmissionError;
|
||||
import tools.BytesArrayTools;
|
||||
|
||||
/** Representation of payload for discover response.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class DiscoverResponse extends Payload {
|
||||
|
||||
private List<HostItem> hostList;
|
||||
private String filename;
|
||||
private static final int FILENAME_SIZE_POSITION = PAYLOAD_START_POSITION;
|
||||
private static final int FILENAME_POSITION = FILENAME_SIZE_POSITION + 4;
|
||||
|
||||
/** Constructor with filename (typically used by tracker). If filename is null, it is initialized with "".
|
||||
* @param filename Name of the file related to the server list.
|
||||
* @param hostList List of servers
|
||||
* @throws InternalError
|
||||
*/
|
||||
public DiscoverResponse(String filename, List<HostItem> hostList) throws InternalError {
|
||||
super(RequestResponseCode.DISCOVER_RESPONSE);
|
||||
this.filename = filename;
|
||||
this.hostList = hostList;
|
||||
}
|
||||
|
||||
/** Constructor (typically used by server) with a byte[] parameter containing the Packet received.
|
||||
* @param packet the full Packet received
|
||||
* @throws SizeError
|
||||
* @throws InternalError
|
||||
* @throws ProtocolError
|
||||
* @throws TransmissionError
|
||||
*/
|
||||
protected DiscoverResponse(byte[] packet) throws SizeError, ProtocolError, InternalError, TransmissionError {
|
||||
super(packet);
|
||||
int size = getPayloadSize(packet);
|
||||
/* Read filename size */
|
||||
int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
|
||||
|
||||
/* Read filename */
|
||||
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize);
|
||||
|
||||
// TODO
|
||||
hostList = new ArrayList<>();
|
||||
int i = FILENAME_POSITION + filenameSize;
|
||||
while(i<size) {
|
||||
int port = BytesArrayTools.readInt16Bits(packet, i);
|
||||
i += 2;
|
||||
String hostname = BytesArrayTools.readString(packet, i, "\n");
|
||||
i += hostname.length();
|
||||
hostList.add(new HostItem(hostname, port));
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a byte[] containing Packet with padding.
|
||||
* This Packet is still incomplete and should not be send directly.
|
||||
* ProtocolP2PPacket will use this method to generate the complete Packet.
|
||||
* @return Packet with padding
|
||||
* @throws InternalError
|
||||
*/
|
||||
protected byte[] toPacket() throws InternalError {
|
||||
int filenameSize = filename.length();
|
||||
int hostListSize = 0;
|
||||
for (HostItem hostItem: hostList) {
|
||||
hostListSize += (2 + hostItem.getHostname().length() + 1);
|
||||
}
|
||||
// compute total size
|
||||
int size = FILENAME_POSITION + filename.length() + hostListSize;
|
||||
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
||||
// set request/response code
|
||||
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||
// set Payload size
|
||||
setPayloadSize(size - PAYLOAD_START_POSITION, packet);
|
||||
// write filename size
|
||||
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
|
||||
// write filename
|
||||
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
|
||||
|
||||
int i = FILENAME_POSITION + filename.length();
|
||||
// write hostList
|
||||
for(HostItem hostItem: hostList) {
|
||||
try {
|
||||
BytesArrayTools.write16Bits(packet, i, hostItem.getPort());
|
||||
i+=2;
|
||||
String hostname = hostItem.getHostname() + "\n";
|
||||
BytesArrayTools.write(packet, hostname, i);
|
||||
i+=hostname.length();
|
||||
} catch (SizeError e) {
|
||||
throw new InternalError();
|
||||
}
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
|
||||
/** HostList getter.
|
||||
* @return hostList
|
||||
*/
|
||||
public List<HostItem> getHostList() {
|
||||
return hostList;
|
||||
}
|
||||
|
||||
/** Filename getter.
|
||||
* @return filename
|
||||
*/
|
||||
public String getFilename() {
|
||||
return filename;
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package protocolP2P;
|
||||
import protocolP2P.Payload;
|
||||
import tools.HostItem;
|
||||
import tools.BytesArrayTools;
|
||||
import localException.SizeError;
|
||||
import localException.InternalError;
|
||||
import localException.TransmissionError;
|
||||
import localException.ProtocolError;
|
||||
|
||||
/** Representation of payload for unregister.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Register extends Payload {
|
||||
private HostItem hostItem;
|
||||
private static final int HOSTNAME_START_POSITION = PAYLOAD_START_POSITION + 2;
|
||||
|
||||
/** Constructor with hostItem (typically used by client)
|
||||
* @param hostItem Host you want to register.
|
||||
* @throws InternalError
|
||||
*/
|
||||
public Register(HostItem hostItem) throws InternalError {
|
||||
super(RequestResponseCode.REGISTER);
|
||||
this.hostItem = hostItem;
|
||||
}
|
||||
|
||||
/** Constructor (typically used by server) with a byte[] parameter containing the Packet received.
|
||||
* @param packet the full Packet received
|
||||
* @throws SizeError
|
||||
* @throws InternalError
|
||||
* @throws ProtocolError
|
||||
* @throws TransmissionError
|
||||
*/
|
||||
protected Register(byte[] packet) throws SizeError, ProtocolError, InternalError, TransmissionError {
|
||||
super(packet);
|
||||
int size = getPayloadSize(packet);
|
||||
int port = BytesArrayTools.readInt16Bits(packet, PAYLOAD_START_POSITION);
|
||||
String hostname = BytesArrayTools.readString(packet, HOSTNAME_START_POSITION, size - HOSTNAME_START_POSITION + PAYLOAD_START_POSITION);
|
||||
hostItem = new HostItem(hostname, port);
|
||||
}
|
||||
|
||||
/** Returns a byte[] containing Packet with padding.
|
||||
* This Packet is still incomplete and should not be send directly.
|
||||
* ProtocolP2PPacket will use this method to generate the complete Packet.
|
||||
* @return Packet with padding
|
||||
* @throws InternalError
|
||||
*/
|
||||
protected byte[] toPacket() throws InternalError {
|
||||
String hostname = hostItem.getHostname();
|
||||
// compute total size
|
||||
int size = HOSTNAME_START_POSITION + hostname.length();
|
||||
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
||||
// set request/response code
|
||||
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||
// set Payload size
|
||||
setPayloadSize(size - PAYLOAD_START_POSITION, packet);
|
||||
// write port to Packet
|
||||
try {
|
||||
BytesArrayTools.write16Bits(packet, PAYLOAD_START_POSITION, hostItem.getPort());
|
||||
} catch (SizeError e) {
|
||||
throw new InternalError();
|
||||
}
|
||||
// write hostname to Packet
|
||||
BytesArrayTools.write(packet, hostname, HOSTNAME_START_POSITION);
|
||||
return packet;
|
||||
}
|
||||
|
||||
/** HostItem getter.
|
||||
* @return hostItem
|
||||
*/
|
||||
public HostItem getHostItem() {
|
||||
return hostItem;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package protocolP2P;
|
||||
import protocolP2P.Payload;
|
||||
import tools.HostItem;
|
||||
import tools.BytesArrayTools;
|
||||
import localException.SizeError;
|
||||
import localException.InternalError;
|
||||
import localException.TransmissionError;
|
||||
import localException.ProtocolError;
|
||||
|
||||
/** Representation of payload for unregister.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Unregister extends Payload {
|
||||
private HostItem hostItem;
|
||||
private static final int HOSTNAME_START_POSITION = PAYLOAD_START_POSITION + 2;
|
||||
|
||||
/** Constructor with hostItem (typically used by client)
|
||||
* @param hostItem Host you want to register.
|
||||
* @throws InternalError
|
||||
*/
|
||||
public Unregister(HostItem hostItem) throws InternalError {
|
||||
super(RequestResponseCode.UNREGISTER);
|
||||
this.hostItem = hostItem;
|
||||
}
|
||||
|
||||
/** Constructor (typically used by server) with a byte[] parameter containing the Packet received.
|
||||
* @param packet the full Packet received
|
||||
* @throws SizeError
|
||||
* @throws InternalError
|
||||
* @throws ProtocolError
|
||||
* @throws TransmissionError
|
||||
*/
|
||||
protected Unregister(byte[] packet) throws SizeError, ProtocolError, InternalError, TransmissionError {
|
||||
super(packet);
|
||||
int size = getPayloadSize(packet);
|
||||
int port = BytesArrayTools.readInt16Bits(packet, PAYLOAD_START_POSITION);
|
||||
String hostname = BytesArrayTools.readString(packet, HOSTNAME_START_POSITION, size - HOSTNAME_START_POSITION + PAYLOAD_START_POSITION);
|
||||
hostItem = new HostItem(hostname, port);
|
||||
}
|
||||
|
||||
/** Returns a byte[] containing Packet with padding.
|
||||
* This Packet is still incomplete and should not be send directly.
|
||||
* ProtocolP2PPacket will use this method to generate the complete Packet.
|
||||
* @return Packet with padding
|
||||
* @throws InternalError
|
||||
*/
|
||||
protected byte[] toPacket() throws InternalError {
|
||||
String hostname = hostItem.getHostname();
|
||||
// compute total size
|
||||
int size = HOSTNAME_START_POSITION + hostname.length();
|
||||
byte[] packet = new byte[size + 1]; // java initialize all to zero
|
||||
// set request/response code
|
||||
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
|
||||
// set Payload size
|
||||
setPayloadSize(size - PAYLOAD_START_POSITION, packet);
|
||||
// write port to Packet
|
||||
try {
|
||||
BytesArrayTools.write16Bits(packet, PAYLOAD_START_POSITION, hostItem.getPort());
|
||||
} catch (SizeError e) {
|
||||
throw new InternalError();
|
||||
}
|
||||
// write hostname to Packet
|
||||
BytesArrayTools.write(packet, hostname, HOSTNAME_START_POSITION);
|
||||
return packet;
|
||||
}
|
||||
|
||||
/** HostItem getter.
|
||||
* @return hostItem
|
||||
*/
|
||||
public HostItem getHostItem() {
|
||||
return hostItem;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package remoteException;
|
||||
|
||||
import exception.RemoteException;
|
||||
|
||||
public class NotATracker extends RemoteException {
|
||||
private static final long serialVersionUID = 12L;
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package tools;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.InputMismatchException;
|
||||
import tools.HostItem;
|
||||
|
||||
/** Helper to get the server list from the user
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class HostList {
|
||||
/**
|
||||
* Let the user enter all server and puts it in a list
|
||||
* @return list of servers
|
||||
*/
|
||||
public static List<HostItem> getServList() {
|
||||
List<HostItem> serverList = new ArrayList<HostItem>();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String servName;
|
||||
int port = 0;
|
||||
do {
|
||||
System.out.println("Enter hostname of next server: (or \"stop\" when finished, default: localhost).");
|
||||
servName = scanner.nextLine();
|
||||
if (servName.equals("")) {
|
||||
servName = "localhost";
|
||||
}
|
||||
if (!servName.equals("stop")) {
|
||||
boolean err = false;
|
||||
do {
|
||||
System.out.println("Enter port for this server");
|
||||
try {
|
||||
port = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
if (port > 65535 || port <= 0) {
|
||||
err = true;
|
||||
System.out.println("Port number must be in 1-65535 range. Try again.");
|
||||
} else {
|
||||
err = false;
|
||||
}
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Invalid number. Try again.");
|
||||
err = true;
|
||||
}
|
||||
} while (err);
|
||||
serverList.add(new HostItem(servName, port));
|
||||
}
|
||||
|
||||
} while (!servName.equals("stop"));
|
||||
return serverList;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package tracker;
|
||||
import tracker.TrackerManagementTCP;
|
||||
import tracker.TrackerManagementUDP;
|
||||
import tools.Directories;
|
||||
import tools.Logger;
|
||||
import java.util.Scanner;
|
||||
|
||||
/** Tracker implementation
|
||||
* First argument of main method is port listened by the tracker, and is mandatory.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Tracker {
|
||||
private int port;
|
||||
private Directories directories;
|
||||
private Logger logger;
|
||||
|
||||
/** Constructor with portStr containing a port number.
|
||||
* @param portStr String containing port number of listening.
|
||||
*/
|
||||
public Tracker(String portStr) {
|
||||
port = Integer.valueOf(Integer.parseInt(portStr));
|
||||
directories = new Directories("P2P_JAVA_PROJECT_TRACKER_" + port);
|
||||
logger = new Logger(directories.getDataHomeDirectory() + "tracker.log");
|
||||
System.out.println("Tracker will listen on port " + port + " and write logs into " + directories.getDataHomeDirectory());
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
directories.askOpenDataHomeDirectory(null, scanner);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
/** Main program entry point
|
||||
* first parameter is port number and is mandatory
|
||||
* to test, run with: java -ea serverP2P.ServerP2P -- <portNumber>
|
||||
* @param args parameters
|
||||
*/
|
||||
public static void main(String [] args) {
|
||||
Tracker t = new Tracker(args[1]);
|
||||
TrackerManagementUDP tmudp = new TrackerManagementUDP(t.port, t.logger);
|
||||
TrackerManagementTCP tmtcp = new TrackerManagementTCP(t.port, t.logger);
|
||||
Thread tudp = new Thread(tmudp);
|
||||
tudp.setName("Tracker UDP P2P-JAVA-PROJECT");
|
||||
tudp.start();
|
||||
Thread ttcp = new Thread(tmtcp);
|
||||
ttcp.setName("Tracker TCP P2P-JAVA-PROJECT");
|
||||
ttcp.start();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,287 @@
|
||||
package tracker;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import protocolP2P.ProtocolP2PPacketTCP;
|
||||
import protocolP2P.ProtocolP2PPacket;
|
||||
import protocolP2P.RequestResponseCode;
|
||||
import protocolP2P.Payload;
|
||||
import protocolP2P.Register;
|
||||
import protocolP2P.Unregister;
|
||||
import tools.HostItem;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import exception.LocalException;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import protocolP2P.DiscoverRequest;
|
||||
import protocolP2P.DiscoverResponse;
|
||||
import protocolP2P.FileList;
|
||||
import localException.InternalError;
|
||||
import remoteException.EmptyDirectory;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.InetAddress;
|
||||
import localException.SocketClosed;
|
||||
|
||||
|
||||
/** Tracker management implementation with tcp
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class TrackerManagementTCP implements Runnable {
|
||||
|
||||
private HostItem tracker;
|
||||
private Logger logger;
|
||||
private ServerSocket socket;
|
||||
private List<HostItem> hostList = new ArrayList<>();
|
||||
private Map<String, List<HostItem>> fileList = new HashMap<>();
|
||||
|
||||
/** Constructor with port and logger.
|
||||
* @param port Port used to listen.
|
||||
* @param logger Logger object
|
||||
*/
|
||||
public TrackerManagementTCP(int port, Logger logger) {
|
||||
tracker = new HostItem("localhost", port);
|
||||
this.logger = logger;
|
||||
try {
|
||||
socket = new ServerSocket(tracker.getPort(), 10, tracker.getInetAddress());
|
||||
} catch (SocketException e) {
|
||||
logger.writeTCP("Error: cannot listen on" + tracker, LogLevel.Error);
|
||||
System.exit(-1);
|
||||
} catch (IOException e) {
|
||||
logger.writeTCP("Error: cannot open socket", LogLevel.Error);
|
||||
System.exit(-2);
|
||||
}
|
||||
}
|
||||
|
||||
/** Implementation of runnable. This methods allows to run the server.
|
||||
*/
|
||||
public void run() {
|
||||
logger.writeTCP("Tracker sucessfully started", LogLevel.Info);
|
||||
do {
|
||||
try {
|
||||
Socket s = socket.accept();
|
||||
ClientHandler c = new ClientHandler(s);
|
||||
(new Thread(c)).start();
|
||||
} catch (IOException e) {
|
||||
logger.writeTCP("Error while accepting new connection", LogLevel.Warning);
|
||||
}
|
||||
} while(true);
|
||||
}
|
||||
|
||||
|
||||
/** Private runnable class allowing to serve one client.
|
||||
*/
|
||||
private class ClientHandler implements Runnable {
|
||||
private HostItem addr;
|
||||
/** Constructor with a socket.
|
||||
* @param s Socket of this client
|
||||
*/
|
||||
public ClientHandler(Socket s) {
|
||||
this.addr = new HostItem(s);
|
||||
}
|
||||
|
||||
/** Implementation of runnable. This method allow to serve one client.
|
||||
*/
|
||||
public void run() {
|
||||
|
||||
boolean end = false;
|
||||
logger.writeTCP("[ " + addr + "] New connection", LogLevel.Action);
|
||||
do {
|
||||
end = handleRequest();
|
||||
} while(!end);
|
||||
logger.writeTCP("[ " + addr + "] End of connection", LogLevel.Action);
|
||||
}
|
||||
|
||||
/** Respond to next request incomming on socket s.
|
||||
* @param s Socket used to read request and send response
|
||||
* @return true if cannot expect another request (ie, socket is closed)
|
||||
*/
|
||||
private boolean handleRequest() {
|
||||
try {
|
||||
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket());
|
||||
Payload p = pd.getPayload();
|
||||
switch (p.getRequestResponseCode()) {
|
||||
case LOAD_REQUEST:
|
||||
logger.writeTCP("Received LOAD_REQUEST from host " + pd.getHostItem() + ", sending NOT_FOUND", LogLevel.Action);
|
||||
sendNotFound(pd);
|
||||
break;
|
||||
case LIST_REQUEST:
|
||||
logger.writeTCP("Received LIST_REQUEST from host " + pd.getHostItem() + ", sending EMPTY_DIRECTORY", LogLevel.Action);
|
||||
sendEmptyDirectory(pd);
|
||||
break;
|
||||
case HASH_REQUEST:
|
||||
logger.writeTCP("Received HASH_REQUEST from host " + pd.getHostItem() + ", sending NOT_FOUND", LogLevel.Action);
|
||||
sendNotFound(pd);
|
||||
break;
|
||||
case REGISTER:
|
||||
logger.writeTCP("Received REGISTER from host " + pd.getHostItem(), LogLevel.Debug);
|
||||
handleRegister(pd);
|
||||
break;
|
||||
case UNREGISTER:
|
||||
logger.writeTCP("Received UNREGISTER from host " + pd.getHostItem(), LogLevel.Debug);
|
||||
handleUnregister(pd);
|
||||
break;
|
||||
case DISCOVER_REQUEST:
|
||||
handleDiscover(pd);
|
||||
break;
|
||||
default:
|
||||
logger.writeTCP("Received grabbage from host " + pd.getHostItem(), LogLevel.Action);
|
||||
sendInternalError(pd);
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.writeTCP(e, LogLevel.Warning);
|
||||
return true;
|
||||
} catch (SocketClosed e) {
|
||||
return true;
|
||||
} catch (LocalException e) {
|
||||
logger.writeTCP(e, LogLevel.Warning);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Send a not found message.
|
||||
* @param pd ProtocolP2PPacketTCP to respond
|
||||
*/
|
||||
private void sendNotFound(ProtocolP2PPacketTCP pd) {
|
||||
try {
|
||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.NOT_FOUND)));
|
||||
} catch (Exception e) {
|
||||
logger.writeTCP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** Send an empty directory message.
|
||||
* @param pd ProtocolP2PPacketTCP to respond
|
||||
*/
|
||||
private void sendEmptyDirectory(ProtocolP2PPacketTCP pd) {
|
||||
try {
|
||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
||||
} catch (Exception e) {
|
||||
logger.writeTCP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** Send an internal error message.
|
||||
* @param pd ProtocolP2PPacketTCP to respond
|
||||
*/
|
||||
private void sendInternalError(ProtocolP2PPacketTCP pd) {
|
||||
logger.writeTCP("Internal Error", LogLevel.Warning);
|
||||
try {
|
||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
||||
} catch (Exception e) {
|
||||
logger.writeTCP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle Registering
|
||||
* @param pd ProtocolP2PPacketTCP to respond
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void handleRegister(ProtocolP2PPacketTCP pd) throws InternalError {
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof Register : "payload must be an instance of Register";
|
||||
if (!(p instanceof Register)) {
|
||||
throw new InternalError();
|
||||
}
|
||||
// add host to known host list
|
||||
HostItem host = ((Register)p).getHostItem();
|
||||
if (!hostList.contains(host)) {
|
||||
hostList.add(host);
|
||||
}
|
||||
// send a list request
|
||||
try {
|
||||
ProtocolP2PPacket pLReq = (ProtocolP2PPacket) new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST));
|
||||
pLReq.sendRequest((Object)host.getTCPSocket());
|
||||
logger.writeTCP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action);
|
||||
handleListResponse((ProtocolP2PPacketTCP)pLReq.receiveResponse(), host);
|
||||
logger.writeTCP("Received LIST RESPONSE from host " + pd.getHostItem(), LogLevel.Action);
|
||||
host.closeTCPSocket();
|
||||
} catch (EmptyDirectory e) {
|
||||
logger.writeTCP("Empty Directory", LogLevel.Debug);
|
||||
hostList.remove(host);
|
||||
logger.writeTCP("Received EMPTY DIRECTORY from host " + pd.getHostItem() + ". Aborting.", LogLevel.Action);
|
||||
} catch (Exception e) {
|
||||
// remove from list because list request could not be send
|
||||
hostList.remove(host);
|
||||
logger.writeTCP("Aborting the add of host " + host, LogLevel.Action);
|
||||
logger.writeTCP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle Unregistering
|
||||
* @param pd ProtocolP2PPacketTCP to respond
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void handleUnregister(ProtocolP2PPacketTCP pd) throws InternalError {
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof Unregister : "payload must be an instance of Unregister";
|
||||
if (!(p instanceof Unregister)) {
|
||||
sendInternalError(pd);
|
||||
throw new InternalError();
|
||||
}
|
||||
HostItem host = ((Unregister)p).getHostItem();
|
||||
logger.writeTCP("Received UNREGISTER from host " + pd.getHostItem() + ". Removing host " + host, LogLevel.Action);
|
||||
hostList.remove(host);
|
||||
for(String f: fileList.keySet()) {
|
||||
fileList.get(f).remove(host);
|
||||
if(fileList.get(f).isEmpty()) {
|
||||
fileList.remove(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle Discover request
|
||||
* @param pd ProtocolP2PPacketTCP to respond
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void handleDiscover(ProtocolP2PPacketTCP pd) throws InternalError {
|
||||
logger.writeTCP("Received DISCOVER REQUEST from host " + pd.getHostItem(), LogLevel.Action);
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof DiscoverRequest : "payload must be an instance of DiscoverRequest";
|
||||
if (!(p instanceof DiscoverRequest)) {
|
||||
sendInternalError(pd);
|
||||
} else {
|
||||
String filename = ((DiscoverRequest)p).getFilename();
|
||||
try {
|
||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList))));
|
||||
} catch (Exception e) {
|
||||
logger.writeTCP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle List Responses
|
||||
* @param pd ProtocolP2PPacketTCP response
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void handleListResponse(ProtocolP2PPacketTCP pd, HostItem host) throws InternalError {
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof FileList: "payload must be an instance of FileList";
|
||||
if (!(p instanceof FileList)) {
|
||||
throw new InternalError();
|
||||
} else {
|
||||
String[] f = ((FileList)p).getFileList();
|
||||
for (String file: f) {
|
||||
List<HostItem> h = fileList.get(file);
|
||||
if (h != null) {
|
||||
if (!h.contains(host)) {
|
||||
h.add(host);
|
||||
}
|
||||
} else {
|
||||
List<HostItem> emptyH = new ArrayList<>();
|
||||
emptyH.add(host);
|
||||
fileList.put(file, emptyH);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,237 @@
|
||||
package tracker;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
import java.net.DatagramSocket;
|
||||
import protocolP2P.ProtocolP2PPacketUDP;
|
||||
import protocolP2P.ProtocolP2PPacket;
|
||||
import protocolP2P.RequestResponseCode;
|
||||
import protocolP2P.Payload;
|
||||
import protocolP2P.Register;
|
||||
import protocolP2P.Unregister;
|
||||
import tools.HostItem;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import java.net.SocketException;
|
||||
import exception.LocalException;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import protocolP2P.DiscoverRequest;
|
||||
import protocolP2P.DiscoverResponse;
|
||||
import protocolP2P.FileList;
|
||||
import localException.InternalError;
|
||||
import remoteException.EmptyDirectory;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/** Tracker management implementation with udp
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class TrackerManagementUDP implements Runnable {
|
||||
|
||||
private HostItem tracker;
|
||||
private Logger logger;
|
||||
private DatagramSocket socket;
|
||||
private List<HostItem> hostList = new ArrayList<>();
|
||||
private Map<String, List<HostItem>> fileList = new HashMap<>();
|
||||
|
||||
/** Constructor with port and logger.
|
||||
* @param port Port used to listen.
|
||||
* @param logger Logger object
|
||||
*/
|
||||
public TrackerManagementUDP(int port, Logger logger) {
|
||||
tracker = new HostItem("localhost", port);
|
||||
this.logger = logger;
|
||||
try {
|
||||
socket = new DatagramSocket(tracker.getPort(), tracker.getInetAddress());
|
||||
} catch (SocketException e) {
|
||||
logger.writeUDP("Error: cannot listen on port " + port, LogLevel.Error);
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
/** Implementation of runnable. This methods allows to run the tracker.
|
||||
*/
|
||||
public void run() {
|
||||
logger.writeUDP("Tracker successfully started", LogLevel.Info);
|
||||
while(true) {
|
||||
try {
|
||||
ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket);
|
||||
Payload p = pd.getPayload();
|
||||
switch (p.getRequestResponseCode()) {
|
||||
case LOAD_REQUEST:
|
||||
logger.writeUDP("Received LOAD_REQUEST from host " + pd.getHostItem() + ", sending NOT_FOUND", LogLevel.Action);
|
||||
sendNotFound(pd);
|
||||
break;
|
||||
case LIST_REQUEST:
|
||||
logger.writeUDP("Received LIST_REQUEST from host " + pd.getHostItem() + ", sending EMPTY_DIRECTORY", LogLevel.Action);
|
||||
sendEmptyDirectory(pd);
|
||||
break;
|
||||
case HASH_REQUEST:
|
||||
logger.writeUDP("Received HASH_REQUEST from host " + pd.getHostItem() + ", sending NOT_FOUND", LogLevel.Action);
|
||||
sendNotFound(pd);
|
||||
break;
|
||||
case REGISTER:
|
||||
handleRegister(pd);
|
||||
break;
|
||||
case UNREGISTER:
|
||||
handleUnregister(pd);
|
||||
break;
|
||||
case DISCOVER_REQUEST:
|
||||
handleDiscover(pd);
|
||||
break;
|
||||
default:
|
||||
logger.writeUDP("Received grabbage from host " + pd.getHostItem(), LogLevel.Action);
|
||||
sendInternalError(pd);
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.writeUDP(e, LogLevel.Warning);
|
||||
} catch (LocalException e) {
|
||||
logger.writeUDP(e, LogLevel.Warning);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Send a not found message.
|
||||
* @param pd ProtocolP2PPacketUDP to respond
|
||||
*/
|
||||
private void sendNotFound(ProtocolP2PPacketUDP pd) {
|
||||
try {
|
||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.NOT_FOUND)));
|
||||
} catch (Exception e) {
|
||||
logger.writeUDP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** Send an empty directory message.
|
||||
* @param pd ProtocolP2PPacketUDP to respond
|
||||
*/
|
||||
private void sendEmptyDirectory(ProtocolP2PPacketUDP pd) {
|
||||
try {
|
||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
||||
} catch (Exception e) {
|
||||
logger.writeUDP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** Send an internal error message.
|
||||
* @param pd ProtocolP2PPacketUDP to respond
|
||||
*/
|
||||
private void sendInternalError(ProtocolP2PPacketUDP pd) {
|
||||
logger.writeUDP("Internal Error", LogLevel.Warning);
|
||||
try {
|
||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR)));
|
||||
} catch (Exception e) {
|
||||
logger.writeUDP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle Registering
|
||||
* @param pd ProtocolP2PPacketUDP to respond
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void handleRegister(ProtocolP2PPacketUDP pd) throws InternalError {
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof Register : "payload must be an instance of Register";
|
||||
if (!(p instanceof Register)) {
|
||||
sendInternalError(pd);
|
||||
throw new InternalError();
|
||||
}
|
||||
// add host to known host list
|
||||
HostItem host = ((Register)p).getHostItem();
|
||||
if (!hostList.contains(host)) {
|
||||
hostList.add(host);
|
||||
}
|
||||
// send a list request
|
||||
try {
|
||||
ProtocolP2PPacket pLReq = (ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST));
|
||||
pLReq.sendRequest((Object)host.getUDPSocket());
|
||||
logger.writeUDP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action);
|
||||
ProtocolP2PPacket resp = pLReq.receiveResponse();
|
||||
handleListResponse((ProtocolP2PPacketUDP)resp, host);
|
||||
logger.writeUDP("Received LIST RESPONSE from host " + pd.getHostItem(), LogLevel.Action);
|
||||
host.closeUDPSocket();
|
||||
} catch (EmptyDirectory e) {
|
||||
logger.writeUDP("Empty Directory", LogLevel.Debug);
|
||||
hostList.remove(host);
|
||||
logger.writeUDP("Received EMPTY DIRECTORY from host " + pd.getHostItem() + ". Aborting.", LogLevel.Action);
|
||||
} catch (Exception e) {
|
||||
// remove from list because list request could not be send
|
||||
hostList.remove(host);
|
||||
logger.writeUDP("Aborting the add of host " + host, LogLevel.Action);
|
||||
logger.writeUDP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle Unregistering
|
||||
* @param pd ProtocolP2PPacketUDP to respond
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void handleUnregister(ProtocolP2PPacketUDP pd) throws InternalError {
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof Unregister : "payload must be an instance of Unregister";
|
||||
if (!(p instanceof Unregister)) {
|
||||
sendInternalError(pd);
|
||||
throw new InternalError();
|
||||
}
|
||||
HostItem host = ((Unregister)p).getHostItem();
|
||||
logger.writeUDP("Received UNREGISTER from host " + pd.getHostItem() + ". Removing host " + host, LogLevel.Action);
|
||||
hostList.remove(host);
|
||||
for(String f: fileList.keySet()) {
|
||||
fileList.get(f).remove(host);
|
||||
if(fileList.get(f).isEmpty()) {
|
||||
fileList.remove(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle Discover request
|
||||
* @param pd ProtocolP2PPacketUDP to respond
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void handleDiscover(ProtocolP2PPacketUDP pd) throws InternalError {
|
||||
logger.writeUDP("Received DISCOVER REQUEST from host " + pd.getHostItem(), LogLevel.Action);
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof DiscoverRequest : "payload must be an instance of DiscoverRequest";
|
||||
if (!(p instanceof DiscoverRequest)) {
|
||||
sendInternalError(pd);
|
||||
} else {
|
||||
String filename = ((DiscoverRequest)p).getFilename();
|
||||
try {
|
||||
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList))));
|
||||
} catch (Exception e) {
|
||||
logger.writeUDP(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Handle List Responses
|
||||
* @param pd ProtocolP2PPacketUDP response
|
||||
* @throws InternalError
|
||||
*/
|
||||
private void handleListResponse(ProtocolP2PPacketUDP pd, HostItem host) throws InternalError {
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof FileList: "payload must be an instance of FileList";
|
||||
if (!(p instanceof FileList)) {
|
||||
throw new InternalError();
|
||||
} else {
|
||||
String[] f = ((FileList)p).getFileList();
|
||||
for (String file: f) {
|
||||
List<HostItem> h = fileList.get(file);
|
||||
if (h != null) {
|
||||
if (!h.contains(host)) {
|
||||
h.add(host);
|
||||
}
|
||||
} else {
|
||||
List<HostItem> emptyH = new ArrayList<>();
|
||||
emptyH.add(host);
|
||||
fileList.put(file, emptyH);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue