NetworkHandler.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package control;
  2. import java.io.IOException;
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6. import java.net.SocketException;
  7. import java.net.UnknownHostException;
  8. import control.button.ButtonHandler;
  9. import control.joystick.Joystick.Position;
  10. import control.joystick.JoystickHandler;
  11. public class NetworkHandler implements Runnable{
  12. DatagramSocket udp;
  13. String host;
  14. int port;
  15. boolean running;
  16. Thread t;
  17. byte[] send;
  18. byte[] receive;
  19. ButtonHandler bth;
  20. JoystickHandler jth;
  21. public NetworkHandler(String host, int port, ButtonHandler bth, JoystickHandler jth)
  22. {
  23. this.host = host;
  24. this.port = port;
  25. this.bth = bth;
  26. this.jth = jth;
  27. udp = null;
  28. send = new byte[1024];
  29. receive = new byte[1024];
  30. try {
  31. udp = new DatagramSocket(1112);
  32. } catch (SocketException e) {
  33. e.printStackTrace();
  34. }
  35. running = true;
  36. t = new Thread(this);
  37. t.start();
  38. }
  39. public void setLed(int led, int r, int g , int b)
  40. {
  41. String cmd = "1|" + led + "|" + r + "|" + g + "|" + b + "\n";
  42. send(cmd);
  43. }
  44. public void show(){
  45. String cmd = "0\n";
  46. send(cmd);
  47. }
  48. private void send(String str)
  49. {
  50. send = str.getBytes();
  51. try {
  52. udp.send(new DatagramPacket(send, send.length));
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. public void close()
  58. {
  59. running = false;
  60. udp.disconnect();
  61. udp.close();
  62. }
  63. @Override
  64. public void run() {
  65. while(running)
  66. {
  67. DatagramPacket receivePacket = new DatagramPacket(receive, receive.length);
  68. try {
  69. udp.receive(receivePacket);
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. String sentence = new String( receivePacket.getData());
  74. if(sentence.length() != 21)
  75. return;
  76. System.out.println("RECEIVED: " + sentence);
  77. String[] controls = sentence.split("\\|");
  78. int[] control = new int[controls.length];
  79. for(int i=0; i<controls.length; i++)
  80. control[i] = Integer.parseInt(controls[i]);
  81. for(int i = 0; i < 7; i++){
  82. if(control[i] != ButtonHandler.getButton(i).pressed)
  83. {
  84. System.out.println("PRESS BITCH " + controls[i]);
  85. ButtonHandler.getButton(i).pressed = control[i];
  86. if(control[i] == 0)
  87. bth.buttonPress(ButtonHandler.getButton(i));
  88. }
  89. }
  90. if(control[7] == 0 && control[8] == 0){
  91. if(JoystickHandler.j.getPos() != Position.UP_LEFT)
  92. {
  93. JoystickHandler.j.setPosition(Position.UP_LEFT);
  94. jth.onJoystickMoved(JoystickHandler.j);
  95. }
  96. }
  97. else if(control[7] == 0 && control[9] == 0){
  98. if(JoystickHandler.j.getPos() != Position.UP_RIGHT)
  99. {
  100. JoystickHandler.j.setPosition(Position.UP_RIGHT);
  101. jth.onJoystickMoved(JoystickHandler.j);
  102. }
  103. }
  104. else if(control[10] == 0 && control[8] == 0){
  105. if(JoystickHandler.j.getPos() != Position.DOWN_LEFT)
  106. {
  107. JoystickHandler.j.setPosition(Position.DOWN_LEFT);
  108. jth.onJoystickMoved(JoystickHandler.j);
  109. }
  110. }
  111. else if(control[10] == 0 && control[9] == 0){
  112. if(JoystickHandler.j.getPos() != Position.DOWN_RIGHT)
  113. {
  114. JoystickHandler.j.setPosition(Position.DOWN_RIGHT);
  115. jth.onJoystickMoved(JoystickHandler.j);
  116. }
  117. }
  118. else if(control[7] == 0){
  119. if(JoystickHandler.j.getPos() != Position.UP)
  120. {
  121. JoystickHandler.j.setPosition(Position.UP);
  122. jth.onJoystickMoved(JoystickHandler.j);
  123. }
  124. }
  125. else if(control[8] == 0){
  126. if(JoystickHandler.j.getPos() != Position.LEFT)
  127. {
  128. JoystickHandler.j.setPosition(Position.LEFT);
  129. jth.onJoystickMoved(JoystickHandler.j);
  130. }
  131. }
  132. else if(control[9] == 0){
  133. if(JoystickHandler.j.getPos() != Position.RIGHT)
  134. {
  135. JoystickHandler.j.setPosition(Position.RIGHT);
  136. jth.onJoystickMoved(JoystickHandler.j);
  137. }
  138. }
  139. else if(control[10] == 0){
  140. if(JoystickHandler.j.getPos() != Position.DOWN)
  141. {
  142. JoystickHandler.j.setPosition(Position.DOWN);
  143. jth.onJoystickMoved(JoystickHandler.j);
  144. }
  145. }
  146. }
  147. }
  148. }