Fix warnings / close scanners

This commit is contained in:
Louis Royer 2020-03-20 11:27:57 +01:00
parent a2f505c167
commit 9b6258afd3
34 changed files with 95 additions and 79 deletions

View File

@ -5,7 +5,6 @@ import java.net.Socket;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import protocolP2P.ProtocolP2PPacketTCP;
import protocolP2P.Payload;
import protocolP2P.LoadRequest;
@ -22,6 +21,7 @@ import remoteException.InternalRemoteError;
import remoteException.VersionRemoteError;
import remoteException.ProtocolRemoteError;
import remoteException.NotFound;
import remoteException.NotATracker;
import tools.Logger;
import tools.LogLevel;
@ -351,6 +351,10 @@ public class ClientDownloadPartTCP implements Runnable {
System.err.println("Error: downloadPart SocketClosed");
logger.writeTCP("downloadPart SocketClosed", LogLevel.Error);
return true;
} catch (NotATracker e) {
System.err.println("Error: downloadPart notATracker");
logger.writeTCP("downloadPart notATracker", LogLevel.Error);
return true;
}
return false;
}

View File

@ -17,9 +17,9 @@ import remoteException.ProtocolRemoteError;
import localException.VersionError;
import localException.SizeError;
import remoteException.NotFound;
import remoteException.NotATracker;
import java.nio.file.Files;
import java.io.File;
import java.nio.file.Paths;
import java.io.IOException;
import tools.Logger;
import tools.LogLevel;
@ -336,6 +336,10 @@ public class ClientDownloadPartUDP implements Runnable {
System.err.println("Error: downloadPart internalError");
logger.writeUDP("downloadPart internalError", LogLevel.Error);
return true;
} catch (NotATracker e) {
System.err.println("Error: downloadPart notATracker");
logger.writeUDP("downloadPart notATracker", LogLevel.Error);
return true;
}
return false;
}

View File

@ -7,7 +7,6 @@ import java.util.Random;
import java.io.IOException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.StandardCopyOption;
import remoteException.EmptyDirectory;
@ -16,6 +15,7 @@ import remoteException.VersionRemoteError;
import remoteException.ProtocolRemoteError;
import remoteException.NotFound;
import remoteException.InternalRemoteError;
import remoteException.NotATracker;
import localException.ProtocolError;
import localException.InternalError;
import localException.TransmissionError;
@ -239,6 +239,9 @@ public class ClientDownloadTCP implements Runnable {
} catch (SizeError e) {
logger.writeTCP(e, LogLevel.Error);
throw new InternalError();
} catch (NotATracker e) {
logger.writeTCP(e, LogLevel.Error);
throw new InternalError();
}
return hash;
} catch (IOException e) {
@ -382,6 +385,9 @@ public class ClientDownloadTCP implements Runnable {
} catch (NotFound e) {
logger.writeTCP(e, LogLevel.Error);
throw new InternalError();
} catch (NotATracker e) {
logger.writeTCP(e, LogLevel.Error);
throw new InternalError();
}
} catch (IOException e) {
logger.writeTCP(e, LogLevel.Error);

View File

@ -7,7 +7,6 @@ import java.util.Random;
import java.io.IOException;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.StandardCopyOption;
import localException.ProtocolError;
@ -21,6 +20,7 @@ import remoteException.VersionRemoteError;
import remoteException.ProtocolRemoteError;
import remoteException.NotFound;
import remoteException.InternalRemoteError;
import remoteException.NotATracker;
import protocolP2P.HashAlgorithm;
import protocolP2P.HashResponse;
import protocolP2P.HashRequest;
@ -239,6 +239,9 @@ public class ClientDownloadUDP implements Runnable {
} catch (SizeError e) {
logger.writeUDP(e, LogLevel.Error);
throw new InternalError();
} catch (NotATracker e) {
logger.writeUDP(e, LogLevel.Error);
throw new InternalError();
}
return hash;
} catch (IOException e) {
@ -378,6 +381,9 @@ public class ClientDownloadUDP implements Runnable {
} catch (NotFound e) {
logger.writeUDP(e, LogLevel.Error);
throw new InternalError();
} catch (NotATracker e) {
logger.writeUDP(e, LogLevel.Error);
throw new InternalError();
}
} catch (IOException e) {
logger.writeUDP(e, LogLevel.Error);

View File

@ -4,9 +4,7 @@ import java.net.UnknownHostException;
import java.util.Scanner;
import java.io.IOException;
import java.nio.file.Files;
import java.io.File;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.security.MessageDigest;
@ -23,15 +21,12 @@ import remoteException.InternalRemoteError;
import remoteException.NotFound;
import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError;
import remoteException.NotATracker;
import protocolP2P.ProtocolP2PPacketTCP;
import protocolP2P.Payload;
import protocolP2P.RequestResponseCode;
import protocolP2P.FileList;
import protocolP2P.FilePart;
import protocolP2P.LoadRequest;
import protocolP2P.HashAlgorithm;
import protocolP2P.HashRequest;
import protocolP2P.HashResponse;
import clientP2P.ClientDownloadTCP;
import tools.HostItem;
import tools.Logger;
@ -74,6 +69,7 @@ public class ClientManagementTCP implements Runnable {
}
System.out.println("Name of the file to download:");
String f = scanner.nextLine();
scanner.close();
download(f);
System.out.println("File " + f + " sucessfully downloaded");
logger.writeTCP("File " + f + " sucessfully downloaded", LogLevel.Info);
@ -203,6 +199,9 @@ public class ClientManagementTCP implements Runnable {
System.err.println("listDirectory : SocketClosed");
logger.writeTCP("listDirectory : SocketClosed", LogLevel.Error);
throw new ProtocolError();
} catch (NotATracker e) {
logger.writeTCP(e, LogLevel.Error);
throw new ProtocolError();
}
}

View File

@ -4,12 +4,9 @@ import java.util.Scanner;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.io.File;
import java.net.DatagramSocket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import localException.InternalError;
@ -23,15 +20,12 @@ import remoteException.InternalRemoteError;
import remoteException.NotFound;
import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError;
import remoteException.NotATracker;
import protocolP2P.ProtocolP2PPacketUDP;
import protocolP2P.Payload;
import protocolP2P.RequestResponseCode;
import protocolP2P.FileList;
import protocolP2P.FilePart;
import protocolP2P.LoadRequest;
import protocolP2P.HashAlgorithm;
import protocolP2P.HashRequest;
import protocolP2P.HashResponse;
import tools.HostItem;
import tools.Logger;
import tools.LogLevel;
@ -74,6 +68,7 @@ public class ClientManagementUDP implements Runnable {
}
System.out.println("Name of the file to download:");
String f = scanner.nextLine();
scanner.close();
download(f);
System.out.println("File " + f + " sucessfully downloaded");
logger.writeUDP("File " + f + " sucessfully downloaded", LogLevel.Info);
@ -199,6 +194,9 @@ public class ClientManagementUDP implements Runnable {
} catch (EmptyFile e) {
logger.writeUDP(e, LogLevel.Error);
throw new ProtocolError();
} catch (NotATracker e) {
logger.writeUDP(e, LogLevel.Error);
throw new ProtocolError();
}
}

View File

@ -6,7 +6,6 @@ import clientP2P.ClientManagementUDP;
import clientP2P.ClientManagementTCP;
import serverP2P.ServerManagementUDP;
import serverP2P.ServerManagementTCP;
import tools.Directories;
import tools.Logger;
import tools.LogLevel;
import tools.Directories;
@ -21,8 +20,8 @@ import tools.HostList;
*/
public class ClientP2P {
static private final String subdir = "seeded/";
static private String parts = ".parts";
private String subdir = "seeded/";
private String parts = ".parts";
private Logger loggerServer;
private Logger loggerClient;
private String host;
@ -86,8 +85,8 @@ public class ClientP2P {
}
// Server threads
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer, c.tracker);
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer, c.tracker);
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + c.subdir, c.port, c.loggerServer, c.tracker);
ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + c.subdir, c.port, c.loggerServer, c.tracker);
Thread tudp = new Thread(smudp);
tudp.setName("server UDP P2P-JAVA-PROJECT (port: " + c.port + ")");
tudp.start();
@ -108,6 +107,7 @@ public class ClientP2P {
System.out.println("Client : Which transport protocol do you want to use? [TCP/udp]");
Scanner sc = new Scanner(System.in);
String transportchoosen = sc.nextLine();
sc.close();
Thread t;
switch(transportchoosen){
case "UDP":
@ -127,7 +127,6 @@ public class ClientP2P {
t = new Thread(cmtcp);
break;
}
t.setName("client P2P-JAVA-PROJECT");
t.start();
}

View File

@ -2,6 +2,6 @@ package localException;
import exception.LocalException;
public class InternalError extends exception.LocalException {
public class InternalError extends LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package localException;
import exception.LocalException;
public class ProtocolError extends exception.LocalException {
public class ProtocolError extends LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -4,6 +4,6 @@ import exception.LocalException;
/** Used on reception side when size as set in Packet is too big, and we cant store this in a int/long as usual. */
public class SizeError extends exception.LocalException {
public class SizeError extends LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package localException;
import exception.LocalException;
public class SocketClosed extends exception.LocalException {
public class SocketClosed extends LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package localException;
import exception.LocalException;
public class TransmissionError extends exception.LocalException {
public class TransmissionError extends LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package localException;
import exception.LocalException;
public class VersionError extends exception.LocalException {
public class VersionError extends LocalException {
private static final long serialVersionUID = 12L;
}

View File

@ -1,12 +1,10 @@
package protocolP2P;
import java.util.Arrays;
import protocolP2P.Payload;
import protocolP2P.RequestResponseCode;
import localException.TransmissionError;
import localException.ProtocolError;
import localException.InternalError;
import localException.SizeError;
import java.io.UnsupportedEncodingException;
import tools.BytesArrayTools;
/** Representation of payload for list response.

View File

@ -6,8 +6,6 @@ import localException.InternalError;
import localException.SizeError;
import localException.TransmissionError;
import tools.BytesArrayTools;
import java.util.Arrays;
import java.io.UnsupportedEncodingException;
/** Representation of payload for load response.
* @author Louis Royer

View File

@ -1,7 +1,6 @@
package protocolP2P;
import protocolP2P.Payload;
import protocolP2P.HashAlgorithm;
import java.io.UnsupportedEncodingException;
import localException.TransmissionError;
import localException.SizeError;
import localException.ProtocolError;

View File

@ -2,7 +2,6 @@ package protocolP2P;
import protocolP2P.Payload;
import java.util.HashMap;
import java.util.Map;
import java.io.UnsupportedEncodingException;
import localException.TransmissionError;
import localException.SizeError;
import localException.ProtocolError;

View File

@ -6,7 +6,6 @@ import localException.ProtocolError;
import localException.InternalError;
import localException.SizeError;
import tools.BytesArrayTools;
import java.io.UnsupportedEncodingException;
/** Representation of payload for load request.
* @author Louis Royer

View File

@ -11,7 +11,7 @@ import remoteException.NotFound;
import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError;
import remoteException.EmptyFile;
import java.net.InetAddress;
import remoteException.NotATracker;
import java.io.IOException;
import tools.HostItem;
@ -60,6 +60,7 @@ public abstract class ProtocolP2PPacket {
/** Receive a response
* @throws EmptyFile
* @throws NotFound
* @throws NotATracker
* @throws EmptyDirectory
* @throws InternalRemoteError
* @throws VersionRemoteError
@ -72,7 +73,7 @@ public abstract class ProtocolP2PPacket {
* @throws IOException
* @throws SocketClosed
*/
public abstract ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, 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
@ -108,6 +109,7 @@ public abstract class ProtocolP2PPacket {
*/
protected void checkProtocolVersion() throws VersionError {
if (PROTOCOL_VERSION != version) {
System.err.println("Error: wrong version in packet:" + version);
throw new VersionError();
}
}

View File

@ -5,21 +5,19 @@ import localException.SizeError;
import localException.TransmissionError;
import localException.VersionError;
import localException.SocketClosed;
import remoteException.NotATracker;
import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError;
import remoteException.NotFound;
import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError;
import remoteException.EmptyFile;
import tools.BytesArrayTools;
import tools.HostItem;
import protocolP2P.Payload;
import protocolP2P.RequestResponseCode;
import protocolP2P.LoadRequest;
import protocolP2P.FileList;
import protocolP2P.FilePart;
import java.util.ArrayList;
import java.lang.Byte;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
@ -68,9 +66,9 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
socket.close();
} catch (IOException e2) {
System.err.println("Cannot close socket");
} finally {
throw new SocketClosed();
}
}
}
@ -117,7 +115,6 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
ss.close();
} catch (IOException e2) {
System.err.println("Cannot close socket");
} finally {
throw new SocketClosed();
}
}
@ -139,6 +136,8 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
case LOAD_RESPONSE:
case LIST_RESPONSE:
case HASH_RESPONSE:
case DISCOVER_RESPONSE:
case NOT_A_TRACKER:
// we were expecting a request, but we are receiving a response
throw new ProtocolError();
default :
@ -189,6 +188,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
* @return ProtocolP2PPacket received
* @throws EmptyFile
* @throws NotFound
* @throws NotATracker
* @throws EmptyDirectory
* @throws InternalRemoteError
* @throws VersionRemoteError
@ -201,7 +201,7 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
* @throws IOException
* @throws SocketClosed
*/
public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, 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();
@ -226,6 +226,8 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
throw new NotFound();
case EMPTY_FILE:
throw new EmptyFile();
case NOT_A_TRACKER:
throw new NotATracker();
default :
return (ProtocolP2PPacket)p;
}

View File

@ -5,6 +5,7 @@ import localException.SizeError;
import localException.TransmissionError;
import localException.VersionError;
import localException.SocketClosed;
import remoteException.NotATracker;
import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError;
import remoteException.NotFound;
@ -18,8 +19,6 @@ import protocolP2P.RequestResponseCode;
import protocolP2P.LoadRequest;
import protocolP2P.FileList;
import protocolP2P.FilePart;
import java.util.ArrayList;
import java.lang.Byte;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
@ -133,6 +132,8 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
case LOAD_RESPONSE:
case LIST_RESPONSE:
case HASH_RESPONSE:
case DISCOVER_RESPONSE:
case NOT_A_TRACKER:
// we were expecting a request, but we are receiving a response
throw new ProtocolError();
default :
@ -186,6 +187,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
* @return ProtocolP2PPacket received
* @throws EmptyFile
* @throws NotFound
* @throws NotATracker
* @throws EmptyDirectory
* @throws InternalRemoteError
* @throws VersionRemoteError
@ -197,7 +199,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
* @throws SizeError
* @throws IOException
*/
public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, 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();
@ -206,6 +208,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
byte[] packet = new byte[8192];
DatagramPacket reception = new DatagramPacket(packet, packet.length);
requestSocket.receive(reception);
// contruction
try {
ProtocolP2PPacketUDP p = new ProtocolP2PPacketUDP(packet);
@ -223,6 +226,8 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
throw new NotFound();
case EMPTY_FILE:
throw new EmptyFile();
case NOT_A_TRACKER:
throw new NotATracker();
default :
return (ProtocolP2PPacket)p;
}

View File

@ -2,6 +2,6 @@ package remoteException;
import exception.RemoteException;
public class EmptyDirectory extends exception.RemoteException {
public class EmptyDirectory extends RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package remoteException;
import exception.RemoteException;
public class EmptyFile extends exception.RemoteException {
public class EmptyFile extends RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package remoteException;
import exception.RemoteException;
public class InternalRemoteError extends exception.RemoteException {
public class InternalRemoteError extends RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package remoteException;
import exception.RemoteException;
public class NotATracker extends exception.RemoteException {
public class NotATracker extends RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package remoteException;
import exception.RemoteException;
public class NotFound extends exception.RemoteException {
public class NotFound extends RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package remoteException;
import exception.RemoteException;
public class ProtocolRemoteError extends exception.RemoteException {
public class ProtocolRemoteError extends RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -2,6 +2,6 @@ package remoteException;
import exception.RemoteException;
public class VersionRemoteError extends exception.RemoteException {
public class VersionRemoteError extends RemoteException {
private static final long serialVersionUID = 12L;
}

View File

@ -23,12 +23,6 @@ import localException.SizeError;
import localException.TransmissionError;
import localException.VersionError;
import localException.SocketClosed;
import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError;
import remoteException.NotFound;
import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError;
import remoteException.EmptyFile;
import java.util.Arrays;
import tools.Logger;
import tools.LogLevel;
@ -360,11 +354,12 @@ public class ServerManagementTCP implements Runnable {
logger.writeTCP("Unregistering from tracker", LogLevel.Info);
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Unregister(host));
p.sendRequest((Object)tracker.getTCPSocket());
//tracker.closeTCPSocket();
logger.writeTCP("Registering into tracker", LogLevel.Info);
p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(host));
p.sendRequest((Object)tracker.getTCPSocket());
logger.writeTCP("Registering completed", LogLevel.Debug);
tracker.closeTCPSocket();
//tracker.closeTCPSocket();
}
}

View File

@ -22,12 +22,6 @@ import localException.SizeError;
import localException.TransmissionError;
import localException.VersionError;
import localException.SocketClosed;
import remoteException.EmptyDirectory;
import remoteException.InternalRemoteError;
import remoteException.NotFound;
import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError;
import remoteException.EmptyFile;
import java.util.Arrays;
import tools.Logger;
import tools.LogLevel;
@ -86,10 +80,10 @@ public class ServerManagementUDP implements Runnable {
Payload p = pd.getPayload();
switch (p.getRequestResponseCode()) {
case LOAD_REQUEST:
LoadRequestManagement(logger, p, pd);
loadRequestManagement(p, pd);
break;
case LIST_REQUEST:
ListRequestManagement(logger, pd);
listRequestManagement(pd);
break;
case HASH_REQUEST:
logger.writeUDP("Received HASH_REQUEST", LogLevel.Action);
@ -160,7 +154,7 @@ public class ServerManagementUDP implements Runnable {
}
}
public void LoadRequestManagement(Logger logger, Payload p, ProtocolP2PPacketUDP pd){
public void loadRequestManagement(Payload p, ProtocolP2PPacketUDP pd){
logger.writeUDP("Received LOAD_REQUEST", LogLevel.Action);
assert p instanceof LoadRequest : "payload must be an instance of LoadRequest";
if (!(p instanceof LoadRequest)) {
@ -210,14 +204,14 @@ public class ServerManagementUDP implements Runnable {
}
}
public void ListRequestManagement(Logger logger, ProtocolP2PPacketUDP pd){
public void listRequestManagement(ProtocolP2PPacketUDP pd) {
logger.writeUDP("Received LIST_REQUEST", LogLevel.Action);
try {
if (fileList.length == 0) {
logger.writeUDP("Sending EMPTY_DIRECTORY", LogLevel.Action);
logger.writeUDP("Sending EMPTY_DIRECTORY to host " + pd.getHostItem(), LogLevel.Action);
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
} else {
logger.writeUDP("Sending LIST_RESPONSE", LogLevel.Action);
logger.writeUDP("Sending LIST_RESPONSE to host " + pd.getHostItem(), LogLevel.Action);
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FileList(fileList))));
}
} catch (Exception e2) {

View File

@ -99,6 +99,7 @@ public class Directories {
System.out.println("Do you want to open this directory? (y/N)");
Scanner scanner = new Scanner(System.in);
String resp = scanner.nextLine();
scanner.close();
if (resp.equals("y") || resp.equals("Y")) {
System.out.println("Openning");
openDataHomeDirectory(subdir);

View File

@ -50,6 +50,7 @@ public class HostList {
}
} while (!servName.equals("stop"));
scanner.close();
return serverList;
}
}

View File

@ -189,7 +189,7 @@ public class TrackerManagementTCP implements Runnable {
// add host to known host list
HostItem host = ((Register)p).getHostItem();
if (!hostList.contains(host)) {
hostList.add(pd.getHostItem());
hostList.add(host);
}
// send a list request
try {
@ -197,7 +197,7 @@ public class TrackerManagementTCP implements Runnable {
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);
host.closeTCPSocket();
//host.closeTCPSocket();
} catch (Exception e) {
// remove from list because list request could not be send
hostList.remove(host);
@ -253,7 +253,7 @@ public class TrackerManagementTCP implements Runnable {
* @throws InternalError
*/
private void handleListResponse(ProtocolP2PPacketTCP pd, HostItem host) throws InternalError {
logger.writeTCP("Received LIST RESPONSE from host " + host + ": registered host.", LogLevel.Action);
logger.writeTCP("Received LIST RESPONSE from host " + host, LogLevel.Action);
Payload p = pd.getPayload();
assert p instanceof FileList: "payload must be an instance of FileList";
if (!(p instanceof FileList)) {

View File

@ -21,6 +21,7 @@ import protocolP2P.DiscoverResponse;
import protocolP2P.FileList;
import protocolP2P.HashRequest;
import localException.InternalError;
import remoteException.EmptyDirectory;
/** Tracker management implementation with udp
* @author Louis Royer
@ -142,15 +143,21 @@ public class TrackerManagementUDP implements Runnable {
// add host to known host list
HostItem host = ((Register)p).getHostItem();
if (!hostList.contains(host)) {
hostList.add(pd.getHostItem());
hostList.add(host);
}
// send a list request
try {
ProtocolP2PPacket pLReq =(ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST));
ProtocolP2PPacket pLReq = (ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST));
pLReq.sendRequest((Object)host.getUDPSocket());
logger.writeUDP("Received REGISTER from host " + pd.getHostItem() + ". Adding host " + host + " to list. Sending List request", LogLevel.Action);
handleListResponse((ProtocolP2PPacketUDP)pLReq.receiveResponse(), host);
ProtocolP2PPacket resp = pLReq.receiveResponse();
handleListResponse((ProtocolP2PPacketUDP)resp, host);
logger.writeUDP("Received LIST RESPONSE from host " + pd.getHostItem(), LogLevel.Action);
host.closeUDPSocket();
} catch (EmptyDirectory e) {
logger.writeUDP("Empty Directory", LogLevel.Debug);
hostList.remove(host);
logger.writeUDP("Received EMPTY DIRECTORY from host " + pd.getHostItem() + ". Aborting.", LogLevel.Action);
} catch (Exception e) {
// remove from list because list request could not be send
hostList.remove(host);