Coverage Summary for Class: Connection (it.polimi.ingsw.server)

Class Class, % Method, % Line, %
Connection 0% (0/ 1) 0% (0/ 8) 0% (0/ 54)


1 package it.polimi.ingsw.server; 2  3 import com.google.gson.Gson; 4 import it.polimi.ingsw.model.GameMode; 5 import it.polimi.ingsw.utils.Observable; 6 import it.polimi.ingsw.utils.Observer; 7 import it.polimi.ingsw.utils.Pinger; 8 import it.polimi.ingsw.utils.model.Command; 9 import it.polimi.ingsw.utils.model.Notification; 10  11 import java.io.Closeable; 12 import java.io.IOException; 13 import java.io.PrintWriter; 14 import java.net.Socket; 15 import java.util.Scanner; 16  17 /** 18  * Server Side socket connection 19  */ 20 class Connection extends Observable<Notification> implements Runnable, Observer<String>, Closeable { 21  /** 22  * Socket instance of Client 23  */ 24  private Socket socket; 25  26  /** 27  * Send the message 28  */ 29  private PrintWriter sender; 30  31  /** 32  * Player's username 33  */ 34  private String username; 35  36  /** 37  * Connection's activation 38  */ 39  private Boolean active = true; 40  41  /** 42  * Game mode chosen by player 43  */ 44  private GameMode mode; 45  46  /** 47  * Connections'ping 48  */ 49  private final Pinger pinger; 50  51  /** 52  * Set the connection 53  * 54  * @param socket socket 55  * @param server server 56  */ 57  58  public Connection(Socket socket, Server server) { 59  this.socket = socket; 60  pinger = new Pinger(this); 61  } 62  63  /** 64  * Get Connection User username 65  * 66  * @return connection username 67  */ 68  public String getUsername() { 69  return username; 70  } 71  72  /** 73  * Get current connection status 74  * 75  * @return connection status 76  */ 77  78  public boolean isActive() { 79  return active; 80  } 81  82  /** 83  * Send messages 84  * 85  * @param message message to send 86  */ 87  88  public synchronized void send(String message) { 89  if (Boolean.FALSE.equals(active)) 90  return; 91  sender.println(message); 92  sender.flush(); 93  } 94  95  /** 96  * Close the current connection 97  * 98  */ 99  100  private void closeConnection() { 101  try { 102  socket.close(); 103  } catch (IOException ex) { 104  // Ex on close 105  } 106  } 107  108  /** 109  * Close all with a quit to Game 110  */ 111  112  @Override 113  public void close() { 114  if (Boolean.FALSE.equals(active)) 115  return; 116  active = false; 117  notify(new Notification(username, new Gson().toJson(new Command("quitPlayer", "quitPlayer", null, null)))); 118  closeConnection(); 119  pinger.stop(); 120  } 121  122  /** 123  * Socket run 124  */ 125  @Override 126  public void run() { 127  try { 128  129  Scanner receiver = new Scanner(socket.getInputStream()); 130  sender = new PrintWriter(socket.getOutputStream()); 131  132  socket.setSoTimeout(30000); 133  new Thread(pinger).start(); 134  // Set game mode 135  while (true) { 136  String input = receiver.nextLine().trim(); 137  if (input.equals("")) 138  continue; 139  else if (GameMode.strConverter(input) == null) { 140  send("ko"); 141  } 142  this.mode = GameMode.strConverter((input)); 143  break; 144  } 145  send("ok"); 146  // Set username 147  while (true) { 148  username = receiver.nextLine().trim(); 149  if (username.equals("")) 150  continue; 151  // Verify username 152  if (Lobby.getInstance().putOnWaiting(this, username, mode)) 153  break; 154  send("ko"); 155  } 156  send("ok"); 157  // Game 158  while (isActive()) { 159  // Action from player 160  String clientInput = receiver.nextLine(); 161  Notification notification = new Notification(username, clientInput); 162  notify(notification); 163  } 164  } catch (Exception e) { 165  System.out.println("Connection lost: " + username); 166  } finally { 167  close(); 168  } 169  } 170  171  /** 172  * Send Message to Client 173  * 174  * @param message message to send 175  */ 176  @Override 177  public void update(String message) { 178  send(message); 179  } 180  181 }