Merge branch 'WIP-end-etape2' of flavien/Projet_JAVA_P2P_STRI2A into master
commit
3be723f90b
@ -0,0 +1,88 @@
|
||||
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;
|
||||
}
|
||||
public void closeUDPSocket() {
|
||||
if (udpSocket != null) {
|
||||
udpSocket.close();
|
||||
}
|
||||
udpSocket = null;
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package tools;
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.InputMismatchException;
|
||||
import tools.HostItem;
|
||||
|
||||
/** Helper to get the server list from the user
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class HostList {
|
||||
/**
|
||||
* Let the user enter all server and puts it in a list
|
||||
* @return list of servers
|
||||
*/
|
||||
public static List<HostItem> getServList() {
|
||||
List<HostItem> serverList = new ArrayList<HostItem>();
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String servName;
|
||||
int port = 0;
|
||||
do {
|
||||
System.out.println("Enter hostname of next server: (or \"stop\" when finished, default: localhost).");
|
||||
servName = scanner.nextLine();
|
||||
if (servName.equals("")) {
|
||||
servName = "localhost";
|
||||
}
|
||||
if (!servName.equals("stop")) {
|
||||
boolean err = false;
|
||||
do {
|
||||
System.out.println("Enter port for this server");
|
||||
try {
|
||||
port = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
if (port > 65535 || port <= 0) {
|
||||
err = true;
|
||||
System.out.println("Port number must be in 1-65535 range. Try again.");
|
||||
} else {
|
||||
err = false;
|
||||
}
|
||||
} catch (InputMismatchException e) {
|
||||
System.out.println("Invalid number. Try again.");
|
||||
err = true;
|
||||
}
|
||||
} while (err);
|
||||
serverList.add(new HostItem(servName, port));
|
||||
}
|
||||
|
||||
} while (!servName.equals("stop"));
|
||||
return serverList;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue