Changes in protocol

À implémenter: Register + Unregister dans protocolP2P.

Encore des erreurs:
- [UDP] ICMP Port unreachable, c’est parce qu’on essaie de
se connecter à un port qui est fermé. c’est pour ça qu’il faut donner le
port dans register et dans unregister (sinon on a le mauvais HostItem).
- [TCP] une autre erreur que je corrigerai demain
pull/48/head
Louis Royer 5 years ago
parent 6d7fabaf1c
commit 07d9276232

@ -106,11 +106,21 @@ A algo hash bloc contains:
#### Register #### Register
Used by a server to register itself on a tracker. Used by a server to register itself on a tracker.
Server may want to do a free `DISCOVER` to check if they have been registered. Server may want to do a free `DISCOVER` to check if they have been registered.
Payload contains:
```
2 bytes: [<PORT NUMBER>]
```
#### Unregister #### Unregister
Used by a server to unregister itself from a tracker. Used by a server to unregister itself from a tracker.
No error is raised if the server was not registered. No error is raised if the server was not registered.
Server may want to do a free `DISCOVER` to check if they have been unregistered. Server may want to do a free `DISCOVER` to check if they have been unregistered.
Payload contains:
```
2 bytes: [<PORT NUMBER>]
```
#### Discover request #### Discover request
If payload size is null, lists all servers registered. If payload size is null, lists all servers registered.

@ -30,6 +30,7 @@ public class ClientP2P {
private Directories directories; private Directories directories;
private List<HostItem> hostList; private List<HostItem> hostList;
private static final int defaultPort = 20000; private static final int defaultPort = 20000;
private HostItem tracker;
/** Initialize loggers if directories and logger are null, /** Initialize loggers if directories and logger are null,
@ -47,6 +48,7 @@ public class ClientP2P {
* @param portStr String containing port for server listenning. * @param portStr String containing port for server listenning.
*/ */
public ClientP2P(String portStr) { public ClientP2P(String portStr) {
tracker = new HostItem("localhost", 30000); // TODO : make it configurable
try{ try{
port = Integer.valueOf(Integer.parseInt(portStr)); port = Integer.valueOf(Integer.parseInt(portStr));
} catch (NumberFormatException e){ } catch (NumberFormatException e){
@ -84,8 +86,8 @@ public class ClientP2P {
} }
// Server threads // Server threads
ServerManagementUDP smudp = new ServerManagementUDP(c.directories.getDataHomeDirectory() + subdir, c.port, c.loggerServer); 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); ServerManagementTCP smtcp = new ServerManagementTCP(c.directories.getDataHomeDirectory() + 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();

@ -34,6 +34,7 @@ import java.io.IOException;
public class ProtocolP2PPacketUDP extends ProtocolP2PPacket { public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
private final static int CHECKSUM_POSITION = 2; private final static int CHECKSUM_POSITION = 2;
private HostItem remoteHost;
private SocketAddress responseSocketAddress; // socket address used when receptionning request and to sending response private SocketAddress responseSocketAddress; // socket address used when receptionning request and to sending response
private DatagramSocket responseSocket; // socket used to recept request and send response private DatagramSocket responseSocket; // socket used to recept request and send response
private DatagramSocket requestSocket; // socket used to send request and to reception response private DatagramSocket requestSocket; // socket used to send request and to reception response
@ -113,6 +114,7 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
DatagramPacket reception = new DatagramPacket(packet, packet.length); DatagramPacket reception = new DatagramPacket(packet, packet.length);
ss.receive(reception); ss.receive(reception);
responseSocketAddress = reception.getSocketAddress(); responseSocketAddress = reception.getSocketAddress();
remoteHost = new HostItem(reception.getAddress().getHostName(), reception.getPort());
// contruction // contruction
boolean protocolError = false; boolean protocolError = false;
try { try {
@ -371,9 +373,9 @@ public class ProtocolP2PPacketUDP extends ProtocolP2PPacket {
* @throws InternalError * @throws InternalError
*/ */
public HostItem getHostItem() throws InternalError { public HostItem getHostItem() throws InternalError {
if (responseSocket == null) { if (remoteHost == null) {
throw new InternalError(); throw new InternalError();
} }
return new HostItem(responseSocket.getInetAddress().getHostName(), responseSocket.getPort()); return remoteHost;
} }
} }

@ -32,6 +32,7 @@ import remoteException.EmptyFile;
import java.util.Arrays; import java.util.Arrays;
import tools.Logger; import tools.Logger;
import tools.LogLevel; import tools.LogLevel;
import tools.HostItem;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import protocolP2P.HashAlgorithm; import protocolP2P.HashAlgorithm;
@ -53,12 +54,14 @@ public class ServerManagementTCP implements Runnable {
private int TCPPort; private int TCPPort;
private ServerSocket socket; private ServerSocket socket;
private Logger logger; private Logger logger;
private HostItem tracker;
/** Constructor for TCP implementation, with baseDirectory and TCPPort parameters. /** Constructor for TCP implementation, with baseDirectory and TCPPort parameters.
* @param baseDirectory the root directory where files are stored * @param baseDirectory the root directory where files are stored
* @param TCPPort the server will listen on this port * @param TCPPort the server will listen on this port
*/ */
public ServerManagementTCP(String baseDirectory, int TCPPort, Logger logger) { public ServerManagementTCP(String baseDirectory, int TCPPort, Logger logger, HostItem tracker) {
this.tracker = tracker;
this.logger = logger; this.logger = logger;
this.baseDirectory = baseDirectory; this.baseDirectory = baseDirectory;
this.TCPPort = TCPPort; this.TCPPort = TCPPort;
@ -79,6 +82,12 @@ public class ServerManagementTCP implements Runnable {
*/ */
public void run() { public void run() {
logger.writeTCP("Server sucessfully started", LogLevel.Info); logger.writeTCP("Server sucessfully started", LogLevel.Info);
try {
registerTracker();
} catch (Exception e) {
logger.writeTCP(e, LogLevel.Error);
System.exit(-4);
}
do { do {
try { try {
Socket s = socket.accept(); Socket s = socket.accept();
@ -332,4 +341,20 @@ public class ServerManagementTCP implements Runnable {
} }
} }
/** Register server on tracker
* @throws InternalError
* @throws IOException
* @throws SocketClosed
*/
public void registerTracker() throws InternalError, IOException, SocketClosed {
logger.writeTCP("Unregistering from tracker", LogLevel.Info);
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.UNREGISTER));
p.sendRequest((Object)tracker.getTCPSocket());
logger.writeTCP("Registering into tracker", LogLevel.Info);
p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.REGISTER));
p.sendRequest((Object)tracker.getTCPSocket());
logger.writeTCP("Registering completed", LogLevel.Debug);
tracker.closeTCPSocket();
}
} }

@ -36,6 +36,7 @@ import java.util.Map;
import protocolP2P.HashAlgorithm; import protocolP2P.HashAlgorithm;
import protocolP2P.HashRequest; import protocolP2P.HashRequest;
import protocolP2P.HashResponse; import protocolP2P.HashResponse;
import tools.HostItem;
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP. /** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol for UDP.
* @author Louis Royer * @author Louis Royer
@ -51,15 +52,17 @@ public class ServerManagementUDP implements Runnable {
private int UDPPort; private int UDPPort;
private DatagramSocket socket; private DatagramSocket socket;
private Logger logger; private Logger logger;
private HostItem tracker;
/** Constructor for UDP implementation, with baseDirectory and UDPPort parameters. /** Constructor for UDP implementation, with baseDirectory and UDPPort parameters.
* @param baseDirectory the root directory where files are stored * @param baseDirectory the root directory where files are stored
* @param UDPPort the server will listen on this port * @param UDPPort the server will listen on this port
*/ */
public ServerManagementUDP(String baseDirectory, int UDPPort, Logger logger) { public ServerManagementUDP(String baseDirectory, int UDPPort, Logger logger, HostItem tracker) {
this.logger = logger; this.logger = logger;
this.baseDirectory = baseDirectory; this.baseDirectory = baseDirectory;
this.UDPPort = UDPPort; this.UDPPort = UDPPort;
this.tracker = tracker;
initFileList(); initFileList();
initSha512(); initSha512();
try { try {
@ -74,6 +77,12 @@ public class ServerManagementUDP implements Runnable {
*/ */
public void run() { public void run() {
logger.writeUDP("Server sucessfully started", LogLevel.Info); logger.writeUDP("Server sucessfully started", LogLevel.Info);
try {
registerTracker();
} catch (Exception e) {
logger.writeUDP(e, LogLevel.Error);
System.exit(-4);
}
while(true) { while(true) {
try { try {
ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket); ProtocolP2PPacketUDP pd = new ProtocolP2PPacketUDP((Object)socket);
@ -269,4 +278,19 @@ public class ServerManagementUDP implements Runnable {
} }
} }
/** Register server on tracker
* @throws InternalError
* @throws IOException
* @throws SocketClosed
*/
public void registerTracker() throws InternalError, IOException, SocketClosed {
logger.writeUDP("Unregistering from tracker", LogLevel.Info);
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.UNREGISTER));
p.sendRequest((Object)tracker.getUDPSocket());
logger.writeUDP("Registering into tracker", LogLevel.Info);
p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.REGISTER));
p.sendRequest((Object)tracker.getUDPSocket());
tracker.closeUDPSocket();
logger.writeUDP("Registering completed", LogLevel.Debug);
}
} }

@ -3,6 +3,7 @@ import serverP2P.ServerManagementUDP;
import serverP2P.ServerManagementTCP; import serverP2P.ServerManagementTCP;
import tools.Directories; import tools.Directories;
import tools.Logger; import tools.Logger;
import tools.HostItem;
/** Server only implementation /** Server only implementation
* First argument of main method is port listened by the server, and is mandatory. * First argument of main method is port listened by the server, and is mandatory.
@ -16,12 +17,14 @@ public class ServerP2P {
private Directories directories; private Directories directories;
static private final String subdir = "seeded/"; static private final String subdir = "seeded/";
private Logger logger; private Logger logger;
private HostItem tracker;
/** Constructor with portStr containing a port number. /** Constructor with portStr containing a port number.
* @param portStr String containing port number of listening. * @param portStr String containing port number of listening.
*/ */
public ServerP2P(String portStr) { public ServerP2P(String portStr, String trackerHostname, String trackerPortStr) {
port = Integer.valueOf(Integer.parseInt(portStr)); port = Integer.valueOf(Integer.parseInt(portStr));
tracker = new HostItem(trackerHostname, Integer.valueOf(Integer.parseInt(trackerPortStr)));
directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + port); directories = new Directories("P2P_JAVA_PROJECT_SERVER_" + port);
directories.createSubdir(subdir); directories.createSubdir(subdir);
logger = new Logger(directories.getDataHomeDirectory() + "server.log"); logger = new Logger(directories.getDataHomeDirectory() + "server.log");
@ -31,18 +34,17 @@ public class ServerP2P {
/** Main program entry point /** Main program entry point
* first parameter is port number and is mandatory * first parameter is port number and is mandatory
* to test, run with: java -ea serverP2P.ServerP2P -- <portNumber> * to test, run with: java -ea serverP2P.ServerP2P -- <portNumber> <TrackerHostname> <TrackerPort>
* @param args parameters * @param args parameters
*/ */
public static void main(String [] args) { public static void main(String [] args) {
if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){ if (args[1].equals("help") || args[1].equals("-h") || args[1].equals("h")){
System.out.println("usage : java -ea serveurP2P.ServeurP2P -- <serveurPORT> <trackerHOSTNAME> <trackerPORT>"); System.out.println("usage : java -ea serveurP2P.ServeurP2P -- <serveurPORT> <trackerHOSTNAME> <trackerPORT>");
} }
else{ else{
ServerP2P s = new ServerP2P(args[1]); ServerP2P s = new ServerP2P(args[1], args[2], args[3]);
ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger); ServerManagementUDP smudp = new ServerManagementUDP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger, s.tracker);
ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger); ServerManagementTCP smtcp = new ServerManagementTCP(s.directories.getDataHomeDirectory() + subdir, s.port, s.logger, s.tracker);
Thread tudp = new Thread(smudp); Thread tudp = new Thread(smudp);
tudp.setName("server UDP P2P-JAVA-PROJECT"); tudp.setName("server UDP P2P-JAVA-PROJECT");
tudp.start(); tudp.start();

@ -106,4 +106,23 @@ public class HostItem {
return getHostname() + " (port " + getPort() + ")"; return getHostname() + " (port " + getPort() + ")";
} }
/** Override of equals method
* @param other Object to test equality with
* @return true if equals
*/
public boolean equals(Object other) {
boolean result = false;
if (other instanceof HostItem) {
HostItem that = (HostItem) other;
result = this.getHostname() == that.getHostname() && this.getPort() == that.getPort();
}
return result;
}
/** Override of hashCode method
* @return a hash code for this object.
*/
public int hashCode() {
return hostname.hashCode() ^ port;
}
} }

@ -101,7 +101,7 @@ public class TrackerManagementTCP implements Runnable {
*/ */
private boolean handleRequest() { private boolean handleRequest() {
try { try {
ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)socket); ProtocolP2PPacketTCP pd = new ProtocolP2PPacketTCP((Object)s);
Payload p = pd.getPayload(); Payload p = pd.getPayload();
switch (p.getRequestResponseCode()) { switch (p.getRequestResponseCode()) {
case LOAD_REQUEST: case LOAD_REQUEST:
@ -125,9 +125,6 @@ public class TrackerManagementTCP implements Runnable {
case DISCOVER_REQUEST: case DISCOVER_REQUEST:
handleDiscover(pd); handleDiscover(pd);
break; break;
case LIST_RESPONSE:
handleListResponse(pd);
break;
default: default:
logger.writeTCP("Received grabbage from host " + pd.getHostItem(), LogLevel.Action); logger.writeTCP("Received grabbage from host " + pd.getHostItem(), LogLevel.Action);
sendInternalError(pd); sendInternalError(pd);
@ -189,8 +186,11 @@ public class TrackerManagementTCP implements Runnable {
} }
// send a list request // send a list request
try { try {
pd.sendResponse((ProtocolP2PPacket)new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST))); ProtocolP2PPacket pLReq = (ProtocolP2PPacket) new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST));
pLReq.sendRequest((Object)host.getTCPSocket());
logger.writeTCP("Received REGISTER. Adding host " + host + " to list. Sending List request", LogLevel.Action); logger.writeTCP("Received REGISTER. Adding host " + host + " to list. Sending List request", LogLevel.Action);
handleListResponse((ProtocolP2PPacketTCP)pLReq.receiveResponse());
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);

@ -79,9 +79,6 @@ public class TrackerManagementUDP implements Runnable {
case DISCOVER_REQUEST: case DISCOVER_REQUEST:
handleDiscover(pd); handleDiscover(pd);
break; break;
case LIST_RESPONSE:
handleListResponse(pd);
break;
default: default:
logger.writeUDP("Received grabbage from host " + pd.getHostItem(), LogLevel.Action); logger.writeUDP("Received grabbage from host " + pd.getHostItem(), LogLevel.Action);
sendInternalError(pd); sendInternalError(pd);
@ -141,8 +138,11 @@ public class TrackerManagementUDP implements Runnable {
} }
// send a list request // send a list request
try { try {
pd.sendResponse((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. Adding host " + host + " to list. Sending List request", LogLevel.Action); logger.writeUDP("Received REGISTER. Adding host " + host + " to list. Sending List request", LogLevel.Action);
handleListResponse((ProtocolP2PPacketUDP)pLReq.receiveResponse());
host.closeUDPSocket();
} 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