Use generics

pull/76/head
Louis Royer 4 years ago
parent ef5484566e
commit d1d7993864

@ -172,7 +172,7 @@ public class ClientDownloadPartTCP implements Runnable {
if (!stop) { if (!stop) {
try { try {
Long offset = toDoTasks.get(0); Long offset = toDoTasks.get(0);
ProtocolP2PPacketTCP p = reqPart(offset); ProtocolP2PPacketTCP<LoadRequest> p = reqPart(offset);
if (p == null) { if (p == null) {
stop = true; stop = true;
} }
@ -197,7 +197,7 @@ public class ClientDownloadPartTCP implements Runnable {
* @param offset Offset of the file part to download * @param offset Offset of the file part to download
* @return ProtocolP2PPacketTCP used to send request * @return ProtocolP2PPacketTCP used to send request
*/ */
private ProtocolP2PPacketTCP reqPart(Long offset) { private ProtocolP2PPacketTCP<LoadRequest> reqPart(Long offset) {
System.err.println("New request: " + offset); System.err.println("New request: " + offset);
logger.writeTCP("New request: " + offset, LogLevel.Info); logger.writeTCP("New request: " + offset, LogLevel.Info);
// maintain tracking of tasks // maintain tracking of tasks
@ -225,7 +225,7 @@ public class ClientDownloadPartTCP implements Runnable {
} }
// send request // send request
try { try {
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, offset.longValue(), MAX_PARTIAL_SIZE)); ProtocolP2PPacketTCP<LoadRequest> d = new ProtocolP2PPacketTCP<>(new LoadRequest(filename, offset.longValue(), MAX_PARTIAL_SIZE));
d.sendRequest((Object)socket); d.sendRequest((Object)socket);
return d; return d;
} catch (InternalError e) { } catch (InternalError e) {
@ -248,7 +248,7 @@ public class ClientDownloadPartTCP implements Runnable {
* @param d request packet * @param d request packet
* @return true on failure, else false * @return true on failure, else false
*/ */
public boolean downloadPart(ProtocolP2PPacketTCP d) { public boolean downloadPart(ProtocolP2PPacketTCP<LoadRequest> d) {
if (d == null) { if (d == null) {
System.err.println("Error: downloadPart -> d is null."); System.err.println("Error: downloadPart -> d is null.");
logger.writeTCP("downloadPart -> d is null.", LogLevel.Error); logger.writeTCP("downloadPart -> d is null.", LogLevel.Error);

@ -166,7 +166,7 @@ public class ClientDownloadPartUDP implements Runnable {
if (!stop) { if (!stop) {
try { try {
Long offset = toDoTasks.get(0); Long offset = toDoTasks.get(0);
ProtocolP2PPacketUDP p = reqPart(offset); ProtocolP2PPacketUDP<LoadRequest> p = reqPart(offset);
if (p == null) { if (p == null) {
stop = true; stop = true;
} }
@ -190,7 +190,7 @@ public class ClientDownloadPartUDP implements Runnable {
* @param offset Offset of the file part to download * @param offset Offset of the file part to download
* @return ProtocolP2PPacketTCP used to send request * @return ProtocolP2PPacketTCP used to send request
*/ */
private ProtocolP2PPacketUDP reqPart(Long offset) { private ProtocolP2PPacketUDP<LoadRequest> reqPart(Long offset) {
System.err.println("New request: "+ offset); System.err.println("New request: "+ offset);
logger.writeUDP("New request: "+ offset, LogLevel.Info); logger.writeUDP("New request: "+ offset, LogLevel.Info);
// maintain tracking of tasks // maintain tracking of tasks
@ -218,7 +218,7 @@ public class ClientDownloadPartUDP implements Runnable {
} }
// send request // send request
try { try {
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, offset.longValue(), MAX_PARTIAL_SIZE)); ProtocolP2PPacketUDP<LoadRequest> d = new ProtocolP2PPacketUDP<>(new LoadRequest(filename, offset.longValue(), MAX_PARTIAL_SIZE));
d.sendRequest((Object)socket); d.sendRequest((Object)socket);
return d; return d;
} catch (InternalError e) { } catch (InternalError e) {
@ -237,7 +237,7 @@ public class ClientDownloadPartUDP implements Runnable {
* @param d request packet * @param d request packet
* @return true on failure, else false * @return true on failure, else false
*/ */
public boolean downloadPart(ProtocolP2PPacketUDP d) { public boolean downloadPart(ProtocolP2PPacketUDP<LoadRequest> d) {
if (d == null) { if (d == null) {
System.err.println("Error: downloadPart -> d is null."); System.err.println("Error: downloadPart -> d is null.");
logger.writeUDP("downloadPart -> d is null.", LogLevel.Error); logger.writeUDP("downloadPart -> d is null.", LogLevel.Error);

@ -197,7 +197,7 @@ public class ClientDownloadTCP implements Runnable {
byte[] hash; byte[] hash;
HashAlgorithm[] hashesAlgo = new HashAlgorithm[1]; HashAlgorithm[] hashesAlgo = new HashAlgorithm[1];
hashesAlgo[0] = HashAlgorithm.SHA512; hashesAlgo[0] = HashAlgorithm.SHA512;
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new HashRequest(filename, hashesAlgo)); ProtocolP2PPacketTCP<HashRequest> d = new ProtocolP2PPacketTCP<>(new HashRequest(filename, hashesAlgo));
try { try {
d.sendRequest((Object)hostItem.getTCPSocket()); d.sendRequest((Object)hostItem.getTCPSocket());
try { try {
@ -319,7 +319,7 @@ public class ClientDownloadTCP implements Runnable {
* @throws InternalError * @throws InternalError
*/ */
private void setSize() throws InternalError { private void setSize() throws InternalError {
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); ProtocolP2PPacketTCP<LoadRequest> d = new ProtocolP2PPacketTCP<>(new LoadRequest(filename, 0, MAX_PARTIAL_SIZE));
try { try {
d.sendRequest((Object)hostList.get(0).getTCPSocket()); d.sendRequest((Object)hostList.get(0).getTCPSocket());
try { try {

@ -197,7 +197,7 @@ public class ClientDownloadUDP implements Runnable {
byte[] hash; byte[] hash;
HashAlgorithm[] hashesAlgo = new HashAlgorithm[1]; HashAlgorithm[] hashesAlgo = new HashAlgorithm[1];
hashesAlgo[0] = HashAlgorithm.SHA512; hashesAlgo[0] = HashAlgorithm.SHA512;
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new HashRequest(filename, hashesAlgo)); ProtocolP2PPacketUDP<HashRequest> d = new ProtocolP2PPacketUDP<>(new HashRequest(filename, hashesAlgo));
try { try {
d.sendRequest((Object)hostItem.getUDPSocket()); d.sendRequest((Object)hostItem.getUDPSocket());
try { try {
@ -315,7 +315,7 @@ public class ClientDownloadUDP implements Runnable {
* @throws InternalError * @throws InternalError
*/ */
private void setSize() throws InternalError { private void setSize() throws InternalError {
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); ProtocolP2PPacketUDP<LoadRequest> d = new ProtocolP2PPacketUDP<>(new LoadRequest(filename, 0, MAX_PARTIAL_SIZE));
try { try {
d.sendRequest((Object)hostList.get(0).getUDPSocket()); d.sendRequest((Object)hostList.get(0).getUDPSocket());
try { try {

@ -174,7 +174,7 @@ public class ClientManagementTCP implements Runnable {
* @throws VersionRemoteError * @throws VersionRemoteError
*/ */
private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST)); ProtocolP2PPacketTCP<Payload> d = new ProtocolP2PPacketTCP<>(new Payload(RequestResponseCode.LIST_REQUEST));
try { try {
d.sendRequest((Object)hostList.get(0).getTCPSocket()); d.sendRequest((Object)hostList.get(0).getTCPSocket());
Payload p = d.receiveResponse().getPayload(); Payload p = d.receiveResponse().getPayload();
@ -220,7 +220,7 @@ public class ClientManagementTCP implements Runnable {
* @throws InternalError * @throws InternalError
*/ */
private void initHostList() throws ProtocolError, InternalError { private void initHostList() throws ProtocolError, InternalError {
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new DiscoverRequest(null)); ProtocolP2PPacketTCP<DiscoverRequest> d = new ProtocolP2PPacketTCP<>(new DiscoverRequest(null));
try { try {
d.sendRequest((Object)tracker.getTCPSocket()); d.sendRequest((Object)tracker.getTCPSocket());
Payload p = d.receiveResponse().getPayload(); Payload p = d.receiveResponse().getPayload();

@ -189,7 +189,7 @@ public class ClientManagementUDP implements Runnable {
* @throws VersionRemoteError * @throws VersionRemoteError
*/ */
private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST)); ProtocolP2PPacketUDP<Payload> d = new ProtocolP2PPacketUDP<>(new Payload(RequestResponseCode.LIST_REQUEST));
d.sendRequest((Object)hostList.get(0).getUDPSocket()); d.sendRequest((Object)hostList.get(0).getUDPSocket());
try { try {
Payload p = d.receiveResponse().getPayload(); Payload p = d.receiveResponse().getPayload();
@ -234,7 +234,7 @@ public class ClientManagementUDP implements Runnable {
* @throws InternalError * @throws InternalError
*/ */
private void initHostList() throws ProtocolError, InternalError { private void initHostList() throws ProtocolError, InternalError {
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new DiscoverRequest(null)); ProtocolP2PPacketUDP<DiscoverRequest> d = new ProtocolP2PPacketUDP<>(new DiscoverRequest(null));
try { try {
d.sendRequest((Object)tracker.getUDPSocket()); d.sendRequest((Object)tracker.getUDPSocket());
Payload p = d.receiveResponse().getPayload(); Payload p = d.receiveResponse().getPayload();

@ -111,14 +111,14 @@ public class HashResponse extends Payload {
int bCount = FILENAME_POSITION + filename.length(); int bCount = FILENAME_POSITION + filename.length();
for(HashAlgorithm h : hashes.keySet()) { for(HashAlgorithm h : hashes.keySet()) {
String s = h.getName(); String s = h.getName();
BytesArrayTools.write(packet, bCount, (int)s.length()); BytesArrayTools.write(packet, bCount, s.length());
bCount += 4; bCount += 4;
// write algoname // write algoname
BytesArrayTools.write(packet, s, bCount); BytesArrayTools.write(packet, s, bCount);
bCount += s.length(); bCount += s.length();
// write hash size // write hash size
byte[] hashb = hashes.get(HashAlgorithm.fromName(s)); byte[] hashb = hashes.get(HashAlgorithm.fromName(s));
BytesArrayTools.write(packet, bCount, (int)hashb.length); BytesArrayTools.write(packet, bCount, hashb.length);
bCount += 4; bCount += 4;
if (hashb.length != 0) { if (hashb.length != 0) {
// write hash // write hash

@ -82,11 +82,11 @@ public class LoadRequest extends Payload {
// set Payload size // set Payload size
setPayloadSize(size - OFFSET_POSITION, packet); setPayloadSize(size - OFFSET_POSITION, packet);
// Write offset // Write offset
BytesArrayTools.write(packet, OFFSET_POSITION, (long)offset); BytesArrayTools.write(packet, OFFSET_POSITION, offset);
// Write maxSizePartialContent // Write maxSizePartialContent
BytesArrayTools.write(packet, MAX_SIZE_PARTIAL_CONTENT_POSITION, (long)maxSizePartialContent); BytesArrayTools.write(packet, MAX_SIZE_PARTIAL_CONTENT_POSITION, maxSizePartialContent);
// Write filenameSize // Write filenameSize
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, (int)filenameSize); BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
// Write filename // Write filename
BytesArrayTools.write(packet, filename, FILENAME_POSITION); BytesArrayTools.write(packet, filename, FILENAME_POSITION);
return packet; return packet;

@ -21,7 +21,7 @@ import tools.HostItem;
* @author JS Auge * @author JS Auge
* @version 1.0 * @version 1.0
*/ */
public abstract class ProtocolP2PPacket { public abstract class ProtocolP2PPacket < T extends Payload>{
private final static byte PROTOCOL_VERSION = 0x12; private final static byte PROTOCOL_VERSION = 0x12;
protected final static int VERSION_POSITION = 0; protected final static int VERSION_POSITION = 0;
protected byte version; protected byte version;
@ -30,9 +30,9 @@ public abstract class ProtocolP2PPacket {
/** Constructor with payload parameter (typically used when sending Packet). /** Constructor with payload parameter (typically used when sending Packet).
* @param payload the payload associated with the Packet to send * @param payload the payload associated with the Packet to send
*/ */
public ProtocolP2PPacket(Payload payload) { public ProtocolP2PPacket (T payload) {
version = PROTOCOL_VERSION; version = PROTOCOL_VERSION;
this.payload = payload; this.payload = (Payload)payload;
} }
/** Send a request /** Send a request
@ -49,7 +49,7 @@ public abstract class ProtocolP2PPacket {
* @throws IOException * @throws IOException
* @throws SocketClosed * @throws SocketClosed
*/ */
public abstract <T extends ProtocolP2PPacket> void sendResponse(T response) throws InternalError, IOException, SocketClosed; public abstract <U extends ProtocolP2PPacket<?>> void sendResponse(U response) throws InternalError, IOException, SocketClosed;
/** Get hostItem of the sender /** Get hostItem of the sender
* @return hostItem of the sender * @return hostItem of the sender
@ -73,7 +73,7 @@ public abstract class ProtocolP2PPacket {
* @throws IOException * @throws IOException
* @throws SocketClosed * @throws SocketClosed
*/ */
public abstract ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed; public abstract ProtocolP2PPacket<?> receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed;
/** Receive a request, subclasses must overwrite this constructor. /** Receive a request, subclasses must overwrite this constructor.
* @param socket socket used to get the request * @param socket socket used to get the request

@ -28,7 +28,7 @@ import java.net.Socket;
* @author JS Auge * @author JS Auge
* @version 1.0 * @version 1.0
*/ */
public class ProtocolP2PPacketTCP extends ProtocolP2PPacket { public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacket < T > {
private Socket responseSocket; // socket used to recept request and send response private Socket responseSocket; // socket used to recept request and send response
private Socket requestSocket; // socket used to send request and to reception response private Socket requestSocket; // socket used to send request and to reception response
@ -36,7 +36,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
/** Constructor with payload parameter (typically used when sending packet). /** Constructor with payload parameter (typically used when sending packet).
* @param payload the payload associated with the packet to send * @param payload the payload associated with the packet to send
*/ */
public ProtocolP2PPacketTCP(Payload payload) { public ProtocolP2PPacketTCP(T payload) {
super(payload); super(payload);
} }
@ -147,19 +147,19 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
break; break;
} }
} catch (TransmissionError e) { } catch (TransmissionError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss);
throw e; throw e;
} catch (ProtocolError e) { } catch (ProtocolError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss);
throw e; throw e;
} catch (VersionError e) { } catch (VersionError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss);
throw e; throw e;
} catch (InternalError e) { } catch (InternalError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss);
throw e; throw e;
} catch (SizeError e) { } catch (SizeError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss);
throw e; throw e;
} }
if (protocolError) { if (protocolError) {
@ -173,10 +173,10 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
* @throws IOException * @throws IOException
* @throws SocketClosed * @throws SocketClosed
*/ */
public void sendResponse(ProtocolP2PPacket response) throws InternalError, IOException, SocketClosed { public <U extends ProtocolP2PPacket<?>> void sendResponse(U response) throws InternalError, IOException, SocketClosed {
assert response instanceof ProtocolP2PPacketTCP: "Wrong Packet type"; assert response instanceof ProtocolP2PPacketTCP: "Wrong Packet type";
if (response instanceof ProtocolP2PPacketTCP) { if (response instanceof ProtocolP2PPacketTCP) {
ProtocolP2PPacketTCP r = (ProtocolP2PPacketTCP) response; ProtocolP2PPacketTCP<?> r = (ProtocolP2PPacketTCP<?>) response;
assert responseSocket != null : "Cannot send response to a packet not received"; assert responseSocket != null : "Cannot send response to a packet not received";
if (responseSocket == null) { if (responseSocket == null) {
throw new InternalError(); throw new InternalError();
@ -204,7 +204,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
* @throws IOException * @throws IOException
* @throws SocketClosed * @throws SocketClosed
*/ */
public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed { public ProtocolP2PPacket<?> receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed {
assert requestSocket != null : "Cannot receive response because request packet not sent."; assert requestSocket != null : "Cannot receive response because request packet not sent.";
if (requestSocket == null) { if (requestSocket == null) {
throw new InternalError(); throw new InternalError();
@ -227,7 +227,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
} }
// contruction // contruction
try { try {
ProtocolP2PPacketTCP p = new ProtocolP2PPacketTCP(packet); ProtocolP2PPacketTCP<?> p = new ProtocolP2PPacketTCP<>(packet);
Payload payload = p.getPayload(); Payload payload = p.getPayload();
switch (payload.getRequestResponseCode()) { switch (payload.getRequestResponseCode()) {
case PROTOCOL_ERROR : case PROTOCOL_ERROR :
@ -248,19 +248,19 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
return (ProtocolP2PPacket)p; return (ProtocolP2PPacket)p;
} }
} catch (TransmissionError e) { } catch (TransmissionError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e; throw e;
} catch (ProtocolError e) { } catch (ProtocolError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket);
throw e; throw e;
} catch (VersionError e) { } catch (VersionError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket);
throw e; throw e;
} catch (InternalError e) { } catch (InternalError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e; throw e;
} catch (SizeError e) { } catch (SizeError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); (new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e; throw e;
} }
} }

@ -30,7 +30,7 @@ import java.io.IOException;
* @author JS Auge * @author JS Auge
* @version 1.0 * @version 1.0
*/ */
public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket < T > {
private final static int CHECKSUM_POSITION = 2; private final static int CHECKSUM_POSITION = 2;
private HostItem remoteHost; private HostItem remoteHost;
@ -41,7 +41,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
/** Constructor with payload parameter (typically used when sending packet). /** Constructor with payload parameter (typically used when sending packet).
* @param payload the payload associated with the packet to send * @param payload the payload associated with the packet to send
*/ */
public ProtocolP2PPacketUDP(Payload payload) { public ProtocolP2PPacketUDP(T payload) {
super(payload); super(payload);
} }
@ -140,19 +140,19 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
break; break;
} }
} catch (TransmissionError e) { } catch (TransmissionError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress);
throw e; throw e;
} catch (ProtocolError e) { } catch (ProtocolError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss, responseSocketAddress); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(ss, responseSocketAddress);
throw e; throw e;
} catch (VersionError e) { } catch (VersionError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss, responseSocketAddress); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss, responseSocketAddress);
throw e; throw e;
} catch (InternalError e) { } catch (InternalError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress);
throw e; throw e;
} catch (SizeError e) { } catch (SizeError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss, responseSocketAddress);
throw e; throw e;
} }
if (protocolError) { if (protocolError) {
@ -165,10 +165,10 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
* @throws InternalError * @throws InternalError
* @throws IOException * @throws IOException
*/ */
public void sendResponse(ProtocolP2PPacket response) throws InternalError, IOException { public <U extends ProtocolP2PPacket<?>>void sendResponse(U response) throws InternalError, IOException {
assert response instanceof ProtocolP2PPacketUDP: "Wrong Packet type"; assert response instanceof ProtocolP2PPacketUDP: "Wrong Packet type";
if (response instanceof ProtocolP2PPacketUDP) { if (response instanceof ProtocolP2PPacketUDP) {
ProtocolP2PPacketUDP r = (ProtocolP2PPacketUDP) response; ProtocolP2PPacketUDP<?> r = (ProtocolP2PPacketUDP<?>) response;
assert responseSocket != null : "Cannot send response to a packet not received"; assert responseSocket != null : "Cannot send response to a packet not received";
if (responseSocket == null) { if (responseSocket == null) {
throw new InternalError(); throw new InternalError();
@ -199,7 +199,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
* @throws SizeError * @throws SizeError
* @throws IOException * @throws IOException
*/ */
public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException { public ProtocolP2PPacket<?> receiveResponse() throws EmptyFile, NotFound, NotATracker, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException {
assert requestSocket != null : "Cannot receive response because request packet not sent."; assert requestSocket != null : "Cannot receive response because request packet not sent.";
if (requestSocket == null) { if (requestSocket == null) {
throw new InternalError(); throw new InternalError();
@ -211,7 +211,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
// contruction // contruction
try { try {
ProtocolP2PPacketUDP p = new ProtocolP2PPacketUDP(packet); ProtocolP2PPacketUDP<?> p = new ProtocolP2PPacketUDP<>(packet);
Payload payload = p.getPayload(); Payload payload = p.getPayload();
switch (payload.getRequestResponseCode()) { switch (payload.getRequestResponseCode()) {
case PROTOCOL_ERROR : case PROTOCOL_ERROR :
@ -232,19 +232,19 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
return (ProtocolP2PPacket)p; return (ProtocolP2PPacket)p;
} }
} catch (TransmissionError e) { } catch (TransmissionError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e; throw e;
} catch (ProtocolError e) { } catch (ProtocolError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.PROTOCOL_ERROR))).send(requestSocket);
throw e; throw e;
} catch (VersionError e) { } catch (VersionError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket);
throw e; throw e;
} catch (InternalError e) { } catch (InternalError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e; throw e;
} catch (SizeError e) { } catch (SizeError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket); (new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e; throw e;
} }
} }

@ -51,7 +51,7 @@ public class FileWatcherTCP extends FileWatcher {
protected void registerTracker() { protected void registerTracker() {
try { try {
writeLog("Trying to into tracker", LogLevel.Info); writeLog("Trying to into tracker", LogLevel.Info);
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(server)); ProtocolP2PPacket<Register> p = (ProtocolP2PPacket<Register>)new ProtocolP2PPacketTCP<Register>(new Register(server));
p.sendRequest((Object)tracker.tryGetTCPSocket()); p.sendRequest((Object)tracker.tryGetTCPSocket());
writeLog("Register request sent.", LogLevel.Debug); writeLog("Register request sent.", LogLevel.Debug);
tracker.closeTCPSocket(); tracker.closeTCPSocket();

@ -51,7 +51,7 @@ public class FileWatcherUDP extends FileWatcher {
protected void registerTracker() { protected void registerTracker() {
try { try {
writeLog("Trying to register into tracker", LogLevel.Info); writeLog("Trying to register into tracker", LogLevel.Info);
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new Register(server)); ProtocolP2PPacket<Register> p = (ProtocolP2PPacket<Register>)new ProtocolP2PPacketUDP<Register>(new Register(server));
p.sendRequest((Object)tracker.getUDPSocket()); p.sendRequest((Object)tracker.getUDPSocket());
writeLog("Register request sent (but cannot ensure reception).", LogLevel.Debug); writeLog("Register request sent (but cannot ensure reception).", LogLevel.Debug);
tracker.closeUDPSocket(); tracker.closeUDPSocket();

@ -76,12 +76,12 @@ public abstract class ServerManagement implements Runnable {
/** Create packets /** Create packets
* @param payload Payload * @param payload Payload
*/ */
protected abstract < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload); protected abstract < T extends Payload > ProtocolP2PPacket<?> createProtocolP2PPacket(T payload);
/** Send response to list request /** Send response to list request
* @param pd Request received * @param pd Request received
*/ */
protected < T extends ProtocolP2PPacket > void sendListResponse(T pd) { protected < T extends ProtocolP2PPacket<?> > void sendListResponse(T pd) {
try { try {
String[] fileList = fileListWatcher.getFileList(); String[] fileList = fileListWatcher.getFileList();
if (fileList.length == 0) { if (fileList.length == 0) {
@ -99,7 +99,7 @@ public abstract class ServerManagement implements Runnable {
/** Send a NotATracker error message. /** Send a NotATracker error message.
* @param pd Request received * @param pd Request received
*/ */
protected < T extends ProtocolP2PPacket > void sendNotATracker(T pd) { protected < T extends ProtocolP2PPacket<?> > void sendNotATracker(T pd) {
try { try {
pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.NOT_A_TRACKER))); pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.NOT_A_TRACKER)));
} catch (Exception e) { } catch (Exception e) {
@ -110,7 +110,7 @@ public abstract class ServerManagement implements Runnable {
/** Send an internal error message. /** Send an internal error message.
* @param pd Request received * @param pd Request received
*/ */
protected < T extends ProtocolP2PPacket > void sendInternalError(T pd) { protected < T extends ProtocolP2PPacket<?> > void sendInternalError(T pd) {
writeLog("Internal Error", LogLevel.Warning); writeLog("Internal Error", LogLevel.Warning);
try { try {
pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.INTERNAL_ERROR))); pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.INTERNAL_ERROR)));
@ -122,7 +122,7 @@ public abstract class ServerManagement implements Runnable {
/** Send hash response to hash request /** Send hash response to hash request
* @param pd Request received * @param pd Request received
*/ */
protected < T extends ProtocolP2PPacket > void sendHashResponse(T pd) { protected < T extends ProtocolP2PPacket<?> > void sendHashResponse(T pd) {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof HashRequest : "payload must be an instance of HashRequest"; assert p instanceof HashRequest : "payload must be an instance of HashRequest";
if (!(p instanceof HashRequest)) { if (!(p instanceof HashRequest)) {
@ -161,7 +161,7 @@ public abstract class ServerManagement implements Runnable {
/** Send response to load request /** Send response to load request
* @param pd Request received * @param pd Request received
*/ */
protected < T extends ProtocolP2PPacket > void sendLoadResponse(T pd) { protected < T extends ProtocolP2PPacket<?> > void sendLoadResponse(T pd) {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof LoadRequest : "payload must be an instance of LoadRequest"; assert p instanceof LoadRequest : "payload must be an instance of LoadRequest";
if (!(p instanceof LoadRequest)) { if (!(p instanceof LoadRequest)) {
@ -223,8 +223,7 @@ public abstract class ServerManagement implements Runnable {
// unregistering from tracker // unregistering from tracker
try { try {
writeLog("Unregistering from tracker", LogLevel.Info); writeLog("Unregistering from tracker", LogLevel.Info);
ProtocolP2PPacket p = createProtocolP2PPacket((Payload)new Unregister(server)); createProtocolP2PPacket(new Unregister(server)).sendRequest(getTrackerSocket());
p.sendRequest(getTrackerSocket());
} catch (Exception e) { } catch (Exception e) {
writeLog("Cannot unregister from tracker", LogLevel.Error); writeLog("Cannot unregister from tracker", LogLevel.Error);
writeLog(e, LogLevel.Error); writeLog(e, LogLevel.Error);

@ -121,7 +121,7 @@ public class ServerManagementTCP extends ServerManagement {
*/ */
private boolean handleRequest() { private boolean handleRequest() {
try { try {
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket()); 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:
@ -185,8 +185,8 @@ public class ServerManagementTCP extends ServerManagement {
/** Create packets /** Create packets
* @param payload Payload * @param payload Payload
*/ */
protected < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload) { protected < T extends Payload > ProtocolP2PPacket<T> createProtocolP2PPacket(T payload) {
return (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload) payload); return (ProtocolP2PPacket<T>)new ProtocolP2PPacketTCP<T>(payload);
} }
/** Getter for tracker socket /** Getter for tracker socket

@ -76,7 +76,7 @@ public class ServerManagementUDP extends ServerManagement {
(new Thread(fileListWatcher)).start(); (new Thread(fileListWatcher)).start();
while(!stop) { while(!stop) {
try { try {
ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket); ProtocolP2PPacketUDP<?> pd = new ProtocolP2PPacketUDP<>((Object)socket);
Payload p = pd.getPayload(); Payload p = pd.getPayload();
switch (p.getRequestResponseCode()) { switch (p.getRequestResponseCode()) {
case LOAD_REQUEST: case LOAD_REQUEST:
@ -132,8 +132,8 @@ public class ServerManagementUDP extends ServerManagement {
/** Create packets /** Create packets
* @param payload Payload * @param payload Payload
*/ */
protected < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload) { protected < T extends Payload > ProtocolP2PPacket<T> createProtocolP2PPacket(T payload) {
return (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload) payload); return (ProtocolP2PPacket<T>)new ProtocolP2PPacketUDP<T>(payload);
} }
/** Getter for tracker socket /** Getter for tracker socket

@ -104,7 +104,7 @@ public class TrackerManagementTCP implements Runnable {
*/ */
private boolean handleRequest() { private boolean handleRequest() {
try { try {
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)addr.getTCPSocket()); 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:
@ -150,9 +150,9 @@ public class TrackerManagementTCP implements Runnable {
/** Send a not found message. /** Send a not found message.
* @param pd ProtocolP2PPacketTCP to respond * @param pd ProtocolP2PPacketTCP to respond
*/ */
private void sendNotFound(ProtocolP2PPacketTCP pd) { private void sendNotFound(ProtocolP2PPacketTCP<?> pd) {
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.NOT_FOUND))); pd.sendResponse((ProtocolP2PPacket<?>)new ProtocolP2PPacketTCP<>(new Payload(RequestResponseCode.NOT_FOUND)));
} catch (Exception e) { } catch (Exception e) {
logger.writeTCP(e, LogLevel.Error); logger.writeTCP(e, LogLevel.Error);
} }
@ -161,9 +161,9 @@ public class TrackerManagementTCP implements Runnable {
/** Send an empty directory message. /** Send an empty directory message.
* @param pd ProtocolP2PPacketTCP to respond * @param pd ProtocolP2PPacketTCP to respond
*/ */
private void sendEmptyDirectory(ProtocolP2PPacketTCP pd) { private void sendEmptyDirectory(ProtocolP2PPacketTCP<?> pd) {
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.EMPTY_DIRECTORY))); pd.sendResponse((ProtocolP2PPacket<?>)new ProtocolP2PPacketTCP<>(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
} catch (Exception e) { } catch (Exception e) {
logger.writeTCP(e, LogLevel.Error); logger.writeTCP(e, LogLevel.Error);
} }
@ -172,10 +172,10 @@ public class TrackerManagementTCP implements Runnable {
/** Send an internal error message. /** Send an internal error message.
* @param pd ProtocolP2PPacketTCP to respond * @param pd ProtocolP2PPacketTCP to respond
*/ */
private void sendInternalError(ProtocolP2PPacketTCP pd) { private void sendInternalError(ProtocolP2PPacketTCP<?> pd) {
logger.writeTCP("Internal Error", LogLevel.Warning); logger.writeTCP("Internal Error", LogLevel.Warning);
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))); pd.sendResponse((ProtocolP2PPacket<?>)new ProtocolP2PPacketTCP<>(new Payload(RequestResponseCode.INTERNAL_ERROR)));
} catch (Exception e) { } catch (Exception e) {
logger.writeTCP(e, LogLevel.Error); logger.writeTCP(e, LogLevel.Error);
} }
@ -185,7 +185,7 @@ public class TrackerManagementTCP implements Runnable {
* @param pd ProtocolP2PPacketTCP to respond * @param pd ProtocolP2PPacketTCP to respond
* @throws InternalError * @throws InternalError
*/ */
private void handleRegister(ProtocolP2PPacketTCP pd) throws InternalError { private void handleRegister(ProtocolP2PPacketTCP<?> pd) throws InternalError {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof Register : "payload must be an instance of Register"; assert p instanceof Register : "payload must be an instance of Register";
if (!(p instanceof Register)) { if (!(p instanceof Register)) {
@ -198,7 +198,7 @@ public class TrackerManagementTCP implements Runnable {
} }
// send a list request // send a list request
try { try {
ProtocolP2PPacket pLReq = (ProtocolP2PPacket) new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST)); ProtocolP2PPacket<Payload> pLReq = (ProtocolP2PPacket<Payload>) new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.LIST_REQUEST));
pLReq.sendRequest((Object)host.getTCPSocket()); pLReq.sendRequest((Object)host.getTCPSocket());
logger.writeTCP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action); logger.writeTCP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action);
handleListResponse((ProtocolP2PPacketTCP)pLReq.receiveResponse(), host); handleListResponse((ProtocolP2PPacketTCP)pLReq.receiveResponse(), host);
@ -220,7 +220,7 @@ public class TrackerManagementTCP implements Runnable {
* @param pd ProtocolP2PPacketTCP to respond * @param pd ProtocolP2PPacketTCP to respond
* @throws InternalError * @throws InternalError
*/ */
private void handleUnregister(ProtocolP2PPacketTCP pd) throws InternalError { private void handleUnregister(ProtocolP2PPacketTCP<?> pd) throws InternalError {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof Unregister : "payload must be an instance of Unregister"; assert p instanceof Unregister : "payload must be an instance of Unregister";
if (!(p instanceof Unregister)) { if (!(p instanceof Unregister)) {
@ -242,7 +242,7 @@ public class TrackerManagementTCP implements Runnable {
* @param pd ProtocolP2PPacketTCP to respond * @param pd ProtocolP2PPacketTCP to respond
* @throws InternalError * @throws InternalError
*/ */
private void handleDiscover(ProtocolP2PPacketTCP pd) throws InternalError { private void handleDiscover(ProtocolP2PPacketTCP<?> pd) throws InternalError {
logger.writeTCP("Received DISCOVER REQUEST from host " + pd.getHostItem(), LogLevel.Action); logger.writeTCP("Received DISCOVER REQUEST from host " + pd.getHostItem(), LogLevel.Action);
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof DiscoverRequest : "payload must be an instance of DiscoverRequest"; assert p instanceof DiscoverRequest : "payload must be an instance of DiscoverRequest";
@ -251,7 +251,7 @@ public class TrackerManagementTCP implements Runnable {
} else { } else {
String filename = ((DiscoverRequest)p).getFilename(); String filename = ((DiscoverRequest)p).getFilename();
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList)))); pd.sendResponse(new ProtocolP2PPacketTCP<DiscoverResponse>(new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList))));
} catch (Exception e) { } catch (Exception e) {
logger.writeTCP(e, LogLevel.Error); logger.writeTCP(e, LogLevel.Error);
} }
@ -262,7 +262,7 @@ public class TrackerManagementTCP implements Runnable {
* @param pd ProtocolP2PPacketTCP response * @param pd ProtocolP2PPacketTCP response
* @throws InternalError * @throws InternalError
*/ */
private void handleListResponse(ProtocolP2PPacketTCP pd, HostItem host) throws InternalError { private void handleListResponse(ProtocolP2PPacketTCP<?> pd, HostItem host) throws InternalError {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof FileList: "payload must be an instance of FileList"; assert p instanceof FileList: "payload must be an instance of FileList";
if (!(p instanceof FileList)) { if (!(p instanceof FileList)) {

@ -59,7 +59,7 @@ public class TrackerManagementUDP implements Runnable {
logger.writeUDP("Tracker successfully started", LogLevel.Info); logger.writeUDP("Tracker successfully started", LogLevel.Info);
while(true) { while(true) {
try { try {
ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket); ProtocolP2PPacketUDP<?> pd = new ProtocolP2PPacketUDP<>((Object)socket);
Payload p = pd.getPayload(); Payload p = pd.getPayload();
switch (p.getRequestResponseCode()) { switch (p.getRequestResponseCode()) {
case LOAD_REQUEST: case LOAD_REQUEST:
@ -101,9 +101,9 @@ public class TrackerManagementUDP implements Runnable {
/** Send a not found message. /** Send a not found message.
* @param pd ProtocolP2PPacketUDP to respond * @param pd ProtocolP2PPacketUDP to respond
*/ */
private void sendNotFound(ProtocolP2PPacketUDP pd) { private void sendNotFound(ProtocolP2PPacketUDP<?> pd) {
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.NOT_FOUND))); pd.sendResponse((ProtocolP2PPacket<?>)new ProtocolP2PPacketUDP<>(new Payload(RequestResponseCode.NOT_FOUND)));
} catch (Exception e) { } catch (Exception e) {
logger.writeUDP(e, LogLevel.Error); logger.writeUDP(e, LogLevel.Error);
} }
@ -112,9 +112,9 @@ public class TrackerManagementUDP implements Runnable {
/** Send an empty directory message. /** Send an empty directory message.
* @param pd ProtocolP2PPacketUDP to respond * @param pd ProtocolP2PPacketUDP to respond
*/ */
private void sendEmptyDirectory(ProtocolP2PPacketUDP pd) { private void sendEmptyDirectory(ProtocolP2PPacketUDP<?> pd) {
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY))); pd.sendResponse((ProtocolP2PPacket<?>)new ProtocolP2PPacketUDP<>(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
} catch (Exception e) { } catch (Exception e) {
logger.writeUDP(e, LogLevel.Error); logger.writeUDP(e, LogLevel.Error);
} }
@ -123,10 +123,10 @@ public class TrackerManagementUDP implements Runnable {
/** Send an internal error message. /** Send an internal error message.
* @param pd ProtocolP2PPacketUDP to respond * @param pd ProtocolP2PPacketUDP to respond
*/ */
private void sendInternalError(ProtocolP2PPacketUDP pd) { private void sendInternalError(ProtocolP2PPacketUDP<?> pd) {
logger.writeUDP("Internal Error", LogLevel.Warning); logger.writeUDP("Internal Error", LogLevel.Warning);
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))); pd.sendResponse((ProtocolP2PPacket<?>)new ProtocolP2PPacketUDP<>(new Payload(RequestResponseCode.INTERNAL_ERROR)));
} catch (Exception e) { } catch (Exception e) {
logger.writeUDP(e, LogLevel.Error); logger.writeUDP(e, LogLevel.Error);
} }
@ -136,7 +136,7 @@ public class TrackerManagementUDP implements Runnable {
* @param pd ProtocolP2PPacketUDP to respond * @param pd ProtocolP2PPacketUDP to respond
* @throws InternalError * @throws InternalError
*/ */
private void handleRegister(ProtocolP2PPacketUDP pd) throws InternalError { private void handleRegister(ProtocolP2PPacketUDP<?> pd) throws InternalError {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof Register : "payload must be an instance of Register"; assert p instanceof Register : "payload must be an instance of Register";
if (!(p instanceof Register)) { if (!(p instanceof Register)) {
@ -150,10 +150,10 @@ public class TrackerManagementUDP implements Runnable {
} }
// send a list request // send a list request
try { try {
ProtocolP2PPacket pLReq = (ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST)); ProtocolP2PPacket<Payload> pLReq = (ProtocolP2PPacket<Payload>)new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.LIST_REQUEST));
pLReq.sendRequest((Object)host.getUDPSocket()); pLReq.sendRequest((Object)host.getUDPSocket());
logger.writeUDP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action); logger.writeUDP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action);
ProtocolP2PPacket resp = pLReq.receiveResponse(); ProtocolP2PPacket<?> resp = pLReq.receiveResponse();
handleListResponse((ProtocolP2PPacketUDP)resp, host); handleListResponse((ProtocolP2PPacketUDP)resp, host);
logger.writeUDP("Received LIST RESPONSE from host " + pd.getHostItem(), LogLevel.Action); logger.writeUDP("Received LIST RESPONSE from host " + pd.getHostItem(), LogLevel.Action);
host.closeUDPSocket(); host.closeUDPSocket();
@ -173,7 +173,7 @@ public class TrackerManagementUDP implements Runnable {
* @param pd ProtocolP2PPacketUDP to respond * @param pd ProtocolP2PPacketUDP to respond
* @throws InternalError * @throws InternalError
*/ */
private void handleUnregister(ProtocolP2PPacketUDP pd) throws InternalError { private void handleUnregister(ProtocolP2PPacketUDP<?> pd) throws InternalError {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof Unregister : "payload must be an instance of Unregister"; assert p instanceof Unregister : "payload must be an instance of Unregister";
if (!(p instanceof Unregister)) { if (!(p instanceof Unregister)) {
@ -195,7 +195,7 @@ public class TrackerManagementUDP implements Runnable {
* @param pd ProtocolP2PPacketUDP to respond * @param pd ProtocolP2PPacketUDP to respond
* @throws InternalError * @throws InternalError
*/ */
private void handleDiscover(ProtocolP2PPacketUDP pd) throws InternalError { private void handleDiscover(ProtocolP2PPacketUDP<?> pd) throws InternalError {
logger.writeUDP("Received DISCOVER REQUEST from host " + pd.getHostItem(), LogLevel.Action); logger.writeUDP("Received DISCOVER REQUEST from host " + pd.getHostItem(), LogLevel.Action);
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof DiscoverRequest : "payload must be an instance of DiscoverRequest"; assert p instanceof DiscoverRequest : "payload must be an instance of DiscoverRequest";
@ -204,7 +204,7 @@ public class TrackerManagementUDP implements Runnable {
} else { } else {
String filename = ((DiscoverRequest)p).getFilename(); String filename = ((DiscoverRequest)p).getFilename();
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList)))); pd.sendResponse(new ProtocolP2PPacketUDP<DiscoverResponse>(new DiscoverResponse(filename, fileList.getOrDefault(filename, hostList))));
} catch (Exception e) { } catch (Exception e) {
logger.writeUDP(e, LogLevel.Error); logger.writeUDP(e, LogLevel.Error);
} }
@ -215,7 +215,7 @@ public class TrackerManagementUDP implements Runnable {
* @param pd ProtocolP2PPacketUDP response * @param pd ProtocolP2PPacketUDP response
* @throws InternalError * @throws InternalError
*/ */
private void handleListResponse(ProtocolP2PPacketUDP pd, HostItem host) throws InternalError { private void handleListResponse(ProtocolP2PPacketUDP<?> pd, HostItem host) throws InternalError {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
assert p instanceof FileList: "payload must be an instance of FileList"; assert p instanceof FileList: "payload must be an instance of FileList";
if (!(p instanceof FileList)) { if (!(p instanceof FileList)) {

Loading…
Cancel
Save