mirror of
https://github.com/binlaab/nanofiles.git
synced 2026-07-01 11:10:48 +02:00
77 lines
2.3 KiB
Java
77 lines
2.3 KiB
Java
package es.um.redes.nanoFiles.tcp.message;
|
|
|
|
import java.util.Map;
|
|
import java.util.TreeMap;
|
|
|
|
public class PeerMessageOps {
|
|
|
|
public static final byte OPCODE_INVALID_CODE = 0;
|
|
|
|
/*
|
|
* TODO: (Boletín MensajesBinarios) Añadir aquí todas las constantes que definen
|
|
* los diferentes tipos de mensajes del protocolo de comunicación con un par
|
|
* servidor de ficheros (valores posibles del campo "operation").
|
|
*/
|
|
public static final byte OPCODE_REQUEST_PEER_FILES = 1;
|
|
public static final byte OPCODE_PEER_FILE = 2;
|
|
public static final byte OPCODE_PEER_FILES_ERROR = 3;
|
|
|
|
public static final byte OPCODE_REQUEST_PEER_DL = 4;
|
|
public static final byte OPCODE_FILE_NOT_FOUND = 5;
|
|
public static final byte OPCODE_AMBIGUOUS = 6;
|
|
public static final byte OPCODE_PEER_DL = 7;
|
|
|
|
|
|
|
|
|
|
/*
|
|
* TODO: (Boletín MensajesBinarios) Definir constantes con nuevos opcodes de
|
|
* mensajes definidos anteriormente, añadirlos al array "valid_opcodes" y añadir
|
|
* su representación textual a "valid_operations_str" EN EL MISMO ORDEN.
|
|
*/
|
|
private static final Byte[] _valid_opcodes = { OPCODE_INVALID_CODE,
|
|
OPCODE_REQUEST_PEER_FILES,
|
|
OPCODE_PEER_FILE,
|
|
OPCODE_PEER_FILES_ERROR,
|
|
OPCODE_REQUEST_PEER_DL,
|
|
OPCODE_FILE_NOT_FOUND,
|
|
OPCODE_AMBIGUOUS,
|
|
OPCODE_PEER_DL
|
|
};
|
|
private static final String[] _valid_operations_str = { "INVALID_OPCODE",
|
|
"REQUEST_PEER_FILES",
|
|
"PEER_FILE",
|
|
"PEER_FILES_ERROR",
|
|
"REQUEST_PEER_DL",
|
|
"FILE_NOT_FOUND",
|
|
"FILE_AMBIGUOUS",
|
|
"PEER_DL"
|
|
};
|
|
|
|
private static Map<String, Byte> _operation_to_opcode;
|
|
private static Map<Byte, String> _opcode_to_operation;
|
|
|
|
static {
|
|
_operation_to_opcode = new TreeMap<>();
|
|
_opcode_to_operation = new TreeMap<>();
|
|
for (int i = 0; i < _valid_operations_str.length; ++i) {
|
|
_operation_to_opcode.put(_valid_operations_str[i].toLowerCase(), _valid_opcodes[i]);
|
|
_opcode_to_operation.put(_valid_opcodes[i], _valid_operations_str[i]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Transforma una cadena en el opcode correspondiente
|
|
*/
|
|
protected static byte operationToOpcode(String opStr) {
|
|
return _operation_to_opcode.getOrDefault(opStr.toLowerCase(), OPCODE_INVALID_CODE);
|
|
}
|
|
|
|
/**
|
|
* Transforma un opcode en la cadena correspondiente
|
|
*/
|
|
public static String opcodeToOperation(byte opcode) {
|
|
return _opcode_to_operation.getOrDefault(opcode, null);
|
|
}
|
|
}
|