NetworkHandler.java 3.8 KB

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