Use generics

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

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

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

@ -197,7 +197,7 @@ public class ClientDownloadTCP implements Runnable {
byte[] hash;
HashAlgorithm[] hashesAlgo = new HashAlgorithm[1];
hashesAlgo[0] = HashAlgorithm.SHA512;
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new HashRequest(filename, hashesAlgo));
ProtocolP2PPacketTCP<HashRequest> d = new ProtocolP2PPacketTCP<>(new HashRequest(filename, hashesAlgo));
try {
d.sendRequest((Object)hostItem.getTCPSocket());
try {
@ -319,7 +319,7 @@ public class ClientDownloadTCP implements Runnable {
* @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 {
d.sendRequest((Object)hostList.get(0).getTCPSocket());
try {

@ -197,7 +197,7 @@ public class ClientDownloadUDP implements Runnable {
byte[] hash;
HashAlgorithm[] hashesAlgo = new HashAlgorithm[1];
hashesAlgo[0] = HashAlgorithm.SHA512;
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new HashRequest(filename, hashesAlgo));
ProtocolP2PPacketUDP<HashRequest> d = new ProtocolP2PPacketUDP<>(new HashRequest(filename, hashesAlgo));
try {
d.sendRequest((Object)hostItem.getUDPSocket());
try {
@ -315,7 +315,7 @@ public class ClientDownloadUDP implements Runnable {
* @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 {
d.sendRequest((Object)hostList.get(0).getUDPSocket());
try {

@ -174,7 +174,7 @@ public class ClientManagementTCP implements Runnable {
* @throws 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 {
d.sendRequest((Object)hostList.get(0).getTCPSocket());
Payload p = d.receiveResponse().getPayload();
@ -220,7 +220,7 @@ public class ClientManagementTCP implements Runnable {
* @throws InternalError
*/
private void initHostList() throws ProtocolError, InternalError {
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new DiscoverRequest(null));
ProtocolP2PPacketTCP<DiscoverRequest> d = new ProtocolP2PPacketTCP<>(new DiscoverRequest(null));
try {
d.sendRequest((Object)tracker.getTCPSocket());
Payload p = d.receiveResponse().getPayload();

@ -189,7 +189,7 @@ public class ClientManagementUDP implements Runnable {
* @throws 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());
try {
Payload p = d.receiveResponse().getPayload();
@ -234,7 +234,7 @@ public class ClientManagementUDP implements Runnable {
* @throws InternalError
*/
private void initHostList() throws ProtocolError, InternalError {
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new DiscoverRequest(null));
ProtocolP2PPacketUDP<DiscoverRequest> d = new ProtocolP2PPacketUDP<>(new DiscoverRequest(null));
try {
d.sendRequest((Object)tracker.getUDPSocket());
Payload p = d.receiveResponse().getPayload();

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

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

@ -21,7 +21,7 @@ import tools.HostItem;
* @author JS Auge
* @version 1.0
*/
public abstract class ProtocolP2PPacket {
public abstract class ProtocolP2PPacket < T extends Payload>{
private final static byte PROTOCOL_VERSION = 0x12;
protected final static int VERSION_POSITION = 0;
protected byte version;
@ -30,9 +30,9 @@ public abstract class ProtocolP2PPacket {
/** Constructor with payload parameter (typically used when sending Packet).
* @param payload the payload associated with the Packet to send
*/
public ProtocolP2PPacket(Payload payload) {
public ProtocolP2PPacket (T payload) {
version = PROTOCOL_VERSION;
this.payload = payload;
this.payload = (Payload)payload;
}
/** Send a request
@ -49,7 +49,7 @@ public abstract class ProtocolP2PPacket {
* @throws IOException
* @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
* @return hostItem of the sender
@ -73,7 +73,7 @@ public abstract class ProtocolP2PPacket {
* @throws IOException
* @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.
* @param socket socket used to get the request

@ -28,7 +28,7 @@ import java.net.Socket;
* @author JS Auge
* @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 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).
* @param payload the payload associated with the packet to send
*/
public ProtocolP2PPacketTCP(Payload payload) {
public ProtocolP2PPacketTCP(T payload) {
super(payload);
}
@ -147,19 +147,19 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
break;
}
} catch (TransmissionError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss);
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss);
throw 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;
} catch (VersionError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss);
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.VERSION_ERROR))).send(ss);
throw 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;
} catch (SizeError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss);
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(ss);
throw e;
}
if (protocolError) {
@ -173,10 +173,10 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
* @throws IOException
* @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";
if (response instanceof ProtocolP2PPacketTCP) {
ProtocolP2PPacketTCP r = (ProtocolP2PPacketTCP) response;
ProtocolP2PPacketTCP<?> r = (ProtocolP2PPacketTCP<?>) response;
assert responseSocket != null : "Cannot send response to a packet not received";
if (responseSocket == null) {
throw new InternalError();
@ -204,7 +204,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
* @throws IOException
* @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.";
if (requestSocket == null) {
throw new InternalError();
@ -227,7 +227,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
}
// contruction
try {
ProtocolP2PPacketTCP p = new ProtocolP2PPacketTCP(packet);
ProtocolP2PPacketTCP<?> p = new ProtocolP2PPacketTCP<>(packet);
Payload payload = p.getPayload();
switch (payload.getRequestResponseCode()) {
case PROTOCOL_ERROR :
@ -248,19 +248,19 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
return (ProtocolP2PPacket)p;
}
} catch (TransmissionError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw 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;
} catch (VersionError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket);
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket);
throw 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;
} catch (SizeError e) {
(new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e;
}
}

@ -30,7 +30,7 @@ import java.io.IOException;
* @author JS Auge
* @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 HostItem remoteHost;
@ -41,7 +41,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
/** Constructor with payload parameter (typically used when sending packet).
* @param payload the payload associated with the packet to send
*/
public ProtocolP2PPacketUDP(Payload payload) {
public ProtocolP2PPacketUDP(T payload) {
super(payload);
}
@ -140,19 +140,19 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
break;
}
} 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;
} 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;
} 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;
} 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;
} 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;
}
if (protocolError) {
@ -165,10 +165,10 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
* @throws InternalError
* @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";
if (response instanceof ProtocolP2PPacketUDP) {
ProtocolP2PPacketUDP r = (ProtocolP2PPacketUDP) response;
ProtocolP2PPacketUDP<?> r = (ProtocolP2PPacketUDP<?>) response;
assert responseSocket != null : "Cannot send response to a packet not received";
if (responseSocket == null) {
throw new InternalError();
@ -199,7 +199,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
* @throws SizeError
* @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.";
if (requestSocket == null) {
throw new InternalError();
@ -211,7 +211,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
// contruction
try {
ProtocolP2PPacketUDP p = new ProtocolP2PPacketUDP(packet);
ProtocolP2PPacketUDP<?> p = new ProtocolP2PPacketUDP<>(packet);
Payload payload = p.getPayload();
switch (payload.getRequestResponseCode()) {
case PROTOCOL_ERROR :
@ -232,19 +232,19 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
return (ProtocolP2PPacket)p;
}
} catch (TransmissionError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
(new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw 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;
} catch (VersionError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket);
(new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.VERSION_ERROR))).send(requestSocket);
throw 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;
} catch (SizeError e) {
(new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
(new ProtocolP2PPacketUDP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
throw e;
}
}

@ -51,7 +51,7 @@ public class FileWatcherTCP extends FileWatcher {
protected void registerTracker() {
try {
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());
writeLog("Register request sent.", LogLevel.Debug);
tracker.closeTCPSocket();

@ -51,7 +51,7 @@ public class FileWatcherUDP extends FileWatcher {
protected void registerTracker() {
try {
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());
writeLog("Register request sent (but cannot ensure reception).", LogLevel.Debug);
tracker.closeUDPSocket();

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

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

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

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

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

Loading…
Cancel
Save