Moved getSocket from Main to NetworkController.
This commit is contained in:
@@ -8,6 +8,11 @@ import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Scanner;
|
||||
|
||||
import vslab1.src.FileReaderWriter.FileReaderWriter;
|
||||
import vslab1.src.Peers.EOnlineState;
|
||||
import vslab1.src.Peers.Peer;
|
||||
|
||||
public class NetworkController {
|
||||
public static boolean hasNetworkInterfaceWithIP(String ipAddressAsString) {
|
||||
@@ -49,4 +54,39 @@ public class NetworkController {
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks user to enter ip and port until those are valid and applicable and creates a DatagramSocket with given values.
|
||||
* @return a DatagramSocket with ip and port specified by user input.
|
||||
*/
|
||||
public static DatagramSocket getSocket(Scanner inputScanner) {
|
||||
DatagramSocket datagramSocket = null;
|
||||
boolean receivedValidIP = false;
|
||||
boolean receivedValidAndFreePort = false;
|
||||
String localIpAddress = null;
|
||||
int localPort = -1;
|
||||
// User has to enter a valid ip.
|
||||
while (!receivedValidIP) {
|
||||
System.out.println("Enter ip:");
|
||||
localIpAddress = inputScanner.nextLine();
|
||||
receivedValidIP = NetworkController.hasNetworkInterfaceWithIP(localIpAddress);
|
||||
if (!receivedValidIP) {
|
||||
System.err.println("Error: IP is not valid for this system. Please re-enter.");
|
||||
}
|
||||
}
|
||||
// User has to enter a valid port.
|
||||
while (!receivedValidAndFreePort) {
|
||||
System.out.println("Enter port:");
|
||||
String localPortAsString = inputScanner.nextLine();
|
||||
localPort = Integer.valueOf(localPortAsString);
|
||||
datagramSocket = NetworkController.tryBindToPort(localIpAddress, localPort);
|
||||
if (datagramSocket == null) {
|
||||
System.err.println("Error: Port is not available for this ip. Please re-enter.");
|
||||
} else {
|
||||
receivedValidAndFreePort = true;
|
||||
}
|
||||
}
|
||||
FileReaderWriter.updatePeer(new Peer(localIpAddress, localPort, null, EOnlineState.Online));
|
||||
return datagramSocket;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ import java.util.Scanner;
|
||||
import vslab1.src.FileReaderWriter.FileReaderWriter;
|
||||
import vslab1.src.Input.InputThread;
|
||||
import vslab1.src.Input.NetworkController;
|
||||
import vslab1.src.Peers.EOnlineState;
|
||||
import vslab1.src.Peers.Peer;
|
||||
import vslab1.src.Receiving.ReceiverThread;
|
||||
import vslab1.src.Receiving.ReceivingQueue;
|
||||
import vslab1.src.Request.RequestExecuterThread;
|
||||
@@ -27,7 +25,7 @@ public class Main {
|
||||
|
||||
FileReaderWriter.createInfoFilesIfNotExisting(Constants.PEERCONFIGFILEPATH);
|
||||
|
||||
DatagramSocket datagramSocket = getSocket(inputScanner);
|
||||
DatagramSocket datagramSocket = NetworkController.getSocket(inputScanner);
|
||||
|
||||
SendingQueue sendingQueue = new SendingQueue();
|
||||
ReceivingQueue receivingQueue = new ReceivingQueue();
|
||||
@@ -57,39 +55,4 @@ public class Main {
|
||||
datagramSocket.close();
|
||||
inputScanner.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Asks user to enter ip and port until those are valid and applicable and creates a DatagramSocket with given values.
|
||||
* @return a DatagramSocket with ip and port specified by user input.
|
||||
*/
|
||||
public static DatagramSocket getSocket(Scanner inputScanner) {
|
||||
DatagramSocket datagramSocket = null;
|
||||
boolean receivedValidIP = false;
|
||||
boolean receivedValidAndFreePort = false;
|
||||
String localIpAddress = null;
|
||||
int localPort = -1;
|
||||
// User has to enter a valid ip.
|
||||
while (!receivedValidIP) {
|
||||
System.out.println("Enter ip:");
|
||||
localIpAddress = inputScanner.nextLine();
|
||||
receivedValidIP = NetworkController.hasNetworkInterfaceWithIP(localIpAddress);
|
||||
if (!receivedValidIP) {
|
||||
System.err.println("Error: IP is not valid for this system. Please re-enter.");
|
||||
}
|
||||
}
|
||||
// User has to enter a valid port.
|
||||
while (!receivedValidAndFreePort) {
|
||||
System.out.println("Enter port:");
|
||||
String localPortAsString = inputScanner.nextLine();
|
||||
localPort = Integer.valueOf(localPortAsString);
|
||||
datagramSocket = NetworkController.tryBindToPort(localIpAddress, localPort);
|
||||
if (datagramSocket == null) {
|
||||
System.err.println("Error: Port is not available for this ip. Please re-enter.");
|
||||
} else {
|
||||
receivedValidAndFreePort = true;
|
||||
}
|
||||
}
|
||||
FileReaderWriter.updatePeer(new Peer(localIpAddress, localPort, null, EOnlineState.Online));
|
||||
return datagramSocket;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user