This commit is contained in:
Louis Royer 2020-03-20 17:41:24 +01:00
parent 3387b90b14
commit ad2b796071
3 changed files with 33 additions and 6 deletions

View File

@ -108,11 +108,14 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
Socket ss = (Socket)socket;
byte[] packet = new byte[1024];
try {
System.err.println("Reading " + ss.getInputStream().read(packet) + " bytes");
if (-1 == ss.getInputStream().read(packet)) {
throw new IOException();
}
} catch (IOException e) {
// Error: cannot read request, closing socket
try {
ss.close();
throw new SocketClosed();
} catch (IOException e2) {
System.err.println("Cannot close socket");
throw new SocketClosed();
@ -208,7 +211,20 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
}
// reception
byte[] packet = new byte[8192];
requestSocket.getInputStream().read(packet);
try {
if (-1== requestSocket.getInputStream().read(packet)) {
throw new IOException();
}
} catch (IOException e) {
// Error: cannot read request, closing socket
try {
requestSocket.close();
throw new SocketClosed();
} catch (IOException e2) {
System.err.println("Cannot close socket");
throw new SocketClosed();
}
}
// contruction
try {
ProtocolP2PPacketTCP p = new ProtocolP2PPacketTCP(packet);

View File

@ -109,11 +109,11 @@ public class ServerManagementTCP implements Runnable {
public void run() {
boolean end = false;
logger.writeTCP("[ " + addr + "] New connection", LogLevel.Action);
logger.writeTCP("[" + addr + "] New connection", LogLevel.Action);
do {
end = handleRequest();
} while(!end);
logger.writeTCP("[ " + addr + "] End of connection", LogLevel.Action);
logger.writeTCP("[" + addr + "] End of connection", LogLevel.Action);
}
/** Respond to next request incomming on socket s.
@ -354,12 +354,18 @@ public class ServerManagementTCP implements Runnable {
logger.writeTCP("Unregistering from tracker", LogLevel.Info);
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Unregister(server));
p.sendRequest((Object)tracker.getTCPSocket());
//tracker.closeTCPSocket();
// FIXME: this is a hack
// ProtocolP2PPacketTCP reads 1024 bytes but if 2 request comes at the same time InputStream is cleared fully
// and we keep waiting forever on the other side
// a fix could be to read only the header first, and read the required size after
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
logger.writeTCP("Registering into tracker", LogLevel.Info);
p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(server));
p.sendRequest((Object)tracker.getTCPSocket());
logger.writeTCP("Registering completed", LogLevel.Debug);
//tracker.closeTCPSocket();
tracker.closeTCPSocket();
}
}

View File

@ -24,6 +24,7 @@ import protocolP2P.HashRequest;
import localException.InternalError;
import java.net.UnknownHostException;
import java.net.InetAddress;
import localException.SocketClosed;
/** Tracker management implementation with tcp
@ -119,9 +120,11 @@ public class TrackerManagementTCP implements Runnable {
sendNotFound(pd);
break;
case REGISTER:
logger.writeTCP("Received REGISTER from host " + pd.getHostItem(), LogLevel.Debug);
handleRegister(pd);
break;
case UNREGISTER:
logger.writeTCP("Received UNREGISTER from host " + pd.getHostItem(), LogLevel.Debug);
handleUnregister(pd);
break;
case DISCOVER_REQUEST:
@ -135,6 +138,8 @@ public class TrackerManagementTCP implements Runnable {
} catch (IOException e) {
logger.writeTCP(e, LogLevel.Warning);
return true;
} catch (SocketClosed e) {
return true;
} catch (LocalException e) {
logger.writeTCP(e, LogLevel.Warning);
return true;