Coverage Summary for Class: CLI (it.polimi.ingsw.Client.CLI)

Class Class, % Method, % Branch, % Line, %
CLI 0% (0/1) 0% (0/5) 0% (0/26) 0% (0/46)


1 package it.polimi.ingsw.Client.CLI; 2  3  4 import it.polimi.ingsw.Network.KeepAliveSocketWrapper; 5 import it.polimi.ingsw.Network.SocketWrapper; 6  7 import java.io.BufferedReader; 8 import java.io.IOException; 9 import java.io.InputStreamReader; 10 import java.net.ConnectException; 11 import java.net.Socket; 12 import java.util.Arrays; 13 import java.util.concurrent.CyclicBarrier; 14  15 /** 16  * This class runs game's cli version and to do that it initializes client's view and runs 2 different threads: 17  * One for writing elements on CLI and send message to Server (CliWriter class) 18  * One for receiving responses from Server and update Client's view (ClientReader class) 19  */ 20 public class CLI implements Runnable { 21  /** 22  * Run Thread responsible for asking User which server wants to connect to 23  */ 24  public void run() { 25  //Create and Initialize BufferedReader 26  BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); 27  Socket connection; 28  try { 29  System.out.println("Type server's ip address"); 30  String ipAddress; 31  boolean validate; 32  do { 33  ipAddress = stdIn.readLine(); //get input from stdIn 34  validate = isIp(ipAddress); //check if is a valid ip address 35  if (!validate) System.out.println("Ip address not valid"); 36  else break; 37  //repeat until the user types a valid ip address 38  } while (true); 39  System.out.println("Type server's port"); 40  int port; 41  do { 42  while (true) { 43  try { 44  //get input from stdIN 45  String portString = stdIn.readLine(); 46  if (portString == null) 47  throw new IOException(); 48  //get int value from int 49  port = Integer.parseInt(portString); 50  break; 51  } catch (NumberFormatException ex) { 52  System.out.println("This string is not a number, retry."); 53  } 54  //repeat until the user types a number 55  } 56  if (port < 1024 || port > 65535) System.out.println("Port number not valid, try again"); 57  else break; 58  //repeat until the user types a valid port number 59  } while (true); 60  61  try { 62  //try to open connection with given parameters 63  connection = new Socket(ipAddress, port); 64  } catch (ConnectException connectException) { 65  System.out.println("No server listening in this socket, quitting the game..."); 66  return; 67  } 68  //create and initialize SocketWrapper 69  SocketWrapper socketWrapper = new KeepAliveSocketWrapper(connection, 5000, true); 70  OpenCLI(socketWrapper, stdIn); 71  } catch (Exception e) { 72  e.printStackTrace(); 73  } 74  } 75  76  /** 77  * used to verify that the string entered by the user is an ip address 78  * 79  * @param string String typed by user 80  * @return true if the string is a valid ip address, false otherwise 81  */ 82  private boolean isIp(String string) { 83  //divided string in parts using '\\' as limiter 84  String[] parts = string.split("\\.", -1); 85  return parts.length == 4 // 4 parts 86  && Arrays.stream(parts) 87  .filter(this::isDecimal) // Only decimal numbers 88  .map(Integer::parseInt) 89  .filter(i -> i <= 255 && i >= 0) // Must be inside [0, 255] 90  .count() == 4; // 4 numerical parts inside [0, 255] 91  } 92  93  /** 94  * Support method to initialize CliWriter and ClientReader threads, it also creates and initialize Client's view 95  * 96  * @param socket SocketWrapper used to wrap the socket used from Client and Server 97  * @param bufferedReader BufferedReader used to acquire ip address and port number will be used to acquire commands during the game 98  */ 99  private static void OpenCLI(SocketWrapper socket, BufferedReader bufferedReader) { 100  //initialize cyclic barrier shared by CliWriter and CliReader 101  CyclicBarrier cyclicBarrier = new CyclicBarrier(2); 102  //initialize Client's view 103  ClientView clientView = new ClientView(); 104  //initialize cliWriter 105  CliWriter cliWriter = new CliWriter(socket, clientView, bufferedReader, cyclicBarrier); 106  //start cliWriter's thread 107  Thread writerThread = new Thread(cliWriter); 108  writerThread.start(); 109  //initialize ClientReader 110  ClientReader ClientReader = new ClientReader(socket, clientView, cyclicBarrier); 111  //start ClientReader's thread 112  Thread readerThread = new Thread(ClientReader); 113  readerThread.start(); 114  115  } 116  117  /** 118  * Check that a string represents a decimal number 119  * 120  * @param string The string to check 121  * @return true if string consists of only numbers without leading zeroes, false otherwise 122  */ 123  private boolean isDecimal(String string) { 124  // Check whether string has a leading zero but is not "0" 125  if (string.startsWith("0")) { 126  return string.length() == 1; 127  } 128  for (char c : string.toCharArray()) { 129  if (c < '0' || c > '9') { 130  return false; 131  } 132  } 133  return true; 134  } 135 }