implementado quit, hay que cerrar los sockets TCP cuando terminen las cosas

This commit is contained in:
binlaab
2026-04-27 23:58:44 +02:00
parent 4e60e66926
commit c480c4a831
8 changed files with 50 additions and 16 deletions

View File

@@ -306,11 +306,6 @@ public class DirectoryConnector {
String respStr = new String(response, 0, response.length);
DirMessage respServe = DirMessage.fromString(respStr);
success = respServe.getOperation().equals(DirMessageOps.OPERATION_SERVE_OK);
if (success) {
}
return success;
}
@@ -405,9 +400,13 @@ public class DirectoryConnector {
*/
public boolean unregisterFileServer() {
boolean success = false;
DirMessage unregisterServer = new DirMessage(DirMessageOps.OPERATION_STOP_SERVE, NanoFiles.peerNickname);
byte[] unregisterBytes = unregisterServer.toString().getBytes();
byte[] response = sendAndReceiveDatagrams(unregisterBytes);
String respStr = new String(response, 0, response.length);
DirMessage respUnregister = DirMessage.fromString(respStr);
success = respUnregister.getOperation().equals(DirMessageOps.OPERATION_STOP_SERVE_OK);
return success;
}

View File

@@ -75,6 +75,11 @@ public class DirMessage {
}
public DirMessage(String op, String nick) {
this(op);
this.nick = nick;
}
public DirMessage(String op, FileInfo[] filelist) {
this(op);
this.fileList = filelist;
@@ -275,6 +280,7 @@ public class DirMessage {
break;
case DirMessageOps.OPERATION_SERVE:
case DirMessageOps.OPERATION_STOP_SERVE:
sb.append(FIELDNAME_NICK + DELIMITER + this.nick + END_LINE);
sb.append(FIELDNAME_IP + DELIMITER + this.ip + END_LINE);
sb.append(FIELDNAME_PORT + DELIMITER + this.port + END_LINE);

View File

@@ -22,6 +22,10 @@ public class DirMessageOps {
public static final String OPERATION_REQUEST_SERVER_PEERS = "peers";
public static final String OPERATION_SERVER_PEERS = "serverPeers";
public static final String OPERATION_STOP_SERVE = "stopServe";
public static final String OPERATION_STOP_SERVE_OK = "stopServeOk";
public static final String OPERATION_STOP_SERVE_ERROR = "stopServeError";
// TODO: definir las operaciones del protocolo de directorio

View File

@@ -267,6 +267,17 @@ public class NFDirectoryServer {
msgToSend.setPeers(registeredPeers);
break;
}
case DirMessageOps.OPERATION_STOP_SERVE: {
System.out.println("stop_serve - tenemos el nick " + receivedMsg.getNick());
if (registeredPeers.get(receivedMsg.getNick()) != null) {
registeredPeers.remove(receivedMsg.getNick());
msgToSend = new DirMessage(DirMessageOps.OPERATION_STOP_SERVE_OK);
} else {
msgToSend = new DirMessage(DirMessageOps.OPERATION_STOP_SERVE_ERROR);
}
break;
}
default:
System.err.println("Unexpected message operation: \"" + operation + "\"");