|
|
|
@ -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)) {
|
|
|
|
|