NetworkUser.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package server;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.net.Socket;
  6. import server.match.Match;
  7. public class NetworkUser {
  8. private String name;
  9. private Socket client;
  10. private DataOutputStream dout;
  11. private DataInputStream din;
  12. private Thread receivethread;
  13. private Match match;
  14. private int pid;
  15. public NetworkUser(Socket client) {
  16. this.client = client;
  17. try {
  18. dout = new DataOutputStream(client.getOutputStream());
  19. din = new DataInputStream(client.getInputStream());
  20. } catch (IOException e) {
  21. match.stopMatch();
  22. }
  23. try {
  24. client.setSoTimeout(5000);
  25. name = din.readUTF();
  26. System.out.println(name);
  27. client.setSoTimeout(0);
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. receivethread = new Thread(new Runnable() {
  32. @Override
  33. public void run() {
  34. while (true) {
  35. receiveMessage();
  36. }
  37. }
  38. });
  39. }
  40. /* NETWORK CONTROL METHODS */
  41. public boolean checkConnection() {
  42. try {
  43. dout.writeUTF("0");
  44. } catch (IOException e) {
  45. return false;
  46. }
  47. return true;
  48. }
  49. public void startMatch(String p1, String p2, int pid, Match match) {
  50. sendMessage("1|" + p1 + "|" + p2);
  51. this.pid = pid;
  52. this.match = match;
  53. receivethread.start();
  54. }
  55. public void sendOverlayText(String text) {
  56. sendMessage("3|" + text + " ");
  57. }
  58. public void sendUpdateMessage(String m) {
  59. sendMessage("2|" + m);
  60. }
  61. public void sendChatMessage(String m) {
  62. sendMessage("4|" + m);
  63. }
  64. private void sendMessage(String m) {
  65. try {
  66. dout.writeUTF(m);
  67. } catch (IOException e) {
  68. System.err.println("User disconnected");
  69. match.stopMatch();
  70. }
  71. }
  72. public void receiveMessage() {
  73. try {
  74. String message = din.readUTF();
  75. String[] messagesplit = message.split("\\|");
  76. switch (messagesplit[0]) {
  77. case "1": // Player input information
  78. match.setPlayerDirection(pid, Integer.parseInt(messagesplit[1]));
  79. if (Integer.parseInt(messagesplit[2]) == 1) {
  80. match.playerShoot(pid);
  81. }
  82. break;
  83. case "2":
  84. match.sendChatMessage("[" + name + "] " + messagesplit[1]);
  85. break;
  86. }
  87. } catch (IOException e) {
  88. System.err.println("User disconnected");
  89. match.stopMatch();
  90. }
  91. }
  92. public void close() throws IOException{
  93. receivethread.stop();
  94. din.close();
  95. dout.close();
  96. client.close();
  97. }
  98. /* GETTERS AND SETTERS */
  99. /**
  100. * @return the name of the user
  101. */
  102. public String getName() {
  103. return name;
  104. }
  105. }