Fix warnings / close scanners

pull/48/head
Louis Royer 5 years ago
parent a2f505c167
commit 9b6258afd3

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

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

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

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

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

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

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

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

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

@ -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. */ /** 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; private static final long serialVersionUID = 12L;
} }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@ -22,12 +22,6 @@ import localException.SizeError;
import localException.TransmissionError; import localException.TransmissionError;
import localException.VersionError; import localException.VersionError;
import localException.SocketClosed; 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 java.util.Arrays;
import tools.Logger; import tools.Logger;
import tools.LogLevel; import tools.LogLevel;
@ -86,10 +80,10 @@ public class ServerManagementUDP implements Runnable {
Payload p = pd.getPayload(); Payload p = pd.getPayload();
switch (p.getRequestResponseCode()) { switch (p.getRequestResponseCode()) {
case LOAD_REQUEST: case LOAD_REQUEST:
LoadRequestManagement(logger, p, pd); loadRequestManagement(p, pd);
break; break;
case LIST_REQUEST: case LIST_REQUEST:
ListRequestManagement(logger, pd); listRequestManagement(pd);
break; break;
case HASH_REQUEST: case HASH_REQUEST:
logger.writeUDP("Received HASH_REQUEST", LogLevel.Action); 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); logger.writeUDP("Received LOAD_REQUEST", LogLevel.Action);
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)) {
@ -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); logger.writeUDP("Received LIST_REQUEST", LogLevel.Action);
try { try {
if (fileList.length == 0) { 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))); pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
} else { } 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)))); pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)(new FileList(fileList))));
} }
} catch (Exception e2) { } catch (Exception e2) {

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

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

@ -189,7 +189,7 @@ public class TrackerManagementTCP implements Runnable {
// add host to known host list // add host to known host list
HostItem host = ((Register)p).getHostItem(); HostItem host = ((Register)p).getHostItem();
if (!hostList.contains(host)) { if (!hostList.contains(host)) {
hostList.add(pd.getHostItem()); hostList.add(host);
} }
// send a list request // send a list request
try { try {
@ -197,7 +197,7 @@ public class TrackerManagementTCP implements Runnable {
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);
host.closeTCPSocket(); //host.closeTCPSocket();
} catch (Exception e) { } catch (Exception e) {
// remove from list because list request could not be send // remove from list because list request could not be send
hostList.remove(host); hostList.remove(host);
@ -253,7 +253,7 @@ public class TrackerManagementTCP implements Runnable {
* @throws InternalError * @throws InternalError
*/ */
private void handleListResponse(ProtocolP2PPacketTCP pd, HostItem host) 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(); 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)) {

@ -21,6 +21,7 @@ import protocolP2P.DiscoverResponse;
import protocolP2P.FileList; import protocolP2P.FileList;
import protocolP2P.HashRequest; import protocolP2P.HashRequest;
import localException.InternalError; import localException.InternalError;
import remoteException.EmptyDirectory;
/** Tracker management implementation with udp /** Tracker management implementation with udp
* @author Louis Royer * @author Louis Royer
@ -142,15 +143,21 @@ public class TrackerManagementUDP implements Runnable {
// add host to known host list // add host to known host list
HostItem host = ((Register)p).getHostItem(); HostItem host = ((Register)p).getHostItem();
if (!hostList.contains(host)) { if (!hostList.contains(host)) {
hostList.add(pd.getHostItem()); hostList.add(host);
} }
// send a list request // send a list request
try { 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()); 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);
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(); 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) { } catch (Exception e) {
// remove from list because list request could not be send // remove from list because list request could not be send
hostList.remove(host); hostList.remove(host);

Loading…
Cancel
Save