Projet_JAVA_P2P_STRI2A/src/tools/HostItem.java
Louis 07d9276232 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
2020-03-20 18:23:59 +01:00

129 lines
2.8 KiB
Java

package tools;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.Socket;
import java.net.DatagramSocket;
import java.net.UnknownHostException;
import java.io.IOException;
/** Class to store hostnames + ports
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
*/
public class HostItem {
private String hostname;
private int port;
private Socket tcpSocket;
private DatagramSocket udpSocket;
/** Constructor with hostname and port
* @param hostname Hostname
* @param port Port
*/
public HostItem(String hostname, int port) {
this.port = port;
this.hostname = hostname;
}
/** Get TCP Socket.
* @return TCP Socket
*/
public Socket getTCPSocket() {
if (tcpSocket == null) {
try {
tcpSocket = new Socket(InetAddress.getByName(hostname), port);
} catch (SocketException e) {
System.err.println("Error: No TCP socket available.");
System.exit(-1);
} catch (UnknownHostException e) {
System.err.println("Error: Unknown host.");
System.exit(-1);
} catch (IOException e) {
System.err.println("Error: Cannot create TCP socket");
System.exit(-1);
}
}
return tcpSocket;
}
/** Closes tcp socket
*/
public void closeTCPSocket() {
if (tcpSocket != null) {
try {
tcpSocket.close();
} catch (IOException e2) {
System.err.println("Error: cannot close socket");
}
}
tcpSocket = null;
}
/** Get UDP Socket
* return UDP Socket
*/
public DatagramSocket getUDPSocket() {
if (udpSocket == null) {
try {
udpSocket = new DatagramSocket();
udpSocket.connect(InetAddress.getByName(hostname), port);
} catch (SocketException e) {
System.err.println("Error: No UDP socket available.");
System.exit(-1);
} catch (UnknownHostException e) {
System.err.println("Error: Unknown host.");
System.exit(-1);
}
}
return udpSocket;
}
/** Closes udp socket
*/
public void closeUDPSocket() {
if (udpSocket != null) {
udpSocket.close();
}
udpSocket = null;
}
/** Getter for hostname */
public String getHostname() {
return hostname;
}
/** Getter for port */
public int getPort() {
return port;
}
/** To string
* @return String representation
*/
public String toString() {
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;
}
}