NetworkHandler.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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(1113);
  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. }
  51. public void close()
  52. {
  53. running = false;
  54. udp.disconnect();
  55. udp.close();
  56. }
  57. @Override
  58. public void run() {
  59. while(running)
  60. {
  61. DatagramPacket receivePacket = new DatagramPacket(receive, receive.length);
  62. try {
  63. udp.receive(receivePacket);
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. String sentence = new String( receivePacket.getData());
  68. sentence = sentence.trim();
  69. String[] controls = sentence.split("\\|");
  70. int[] control = new int[controls.length];
  71. for(int i=0; i<controls.length; i++)
  72. control[i] = Integer.parseInt(controls[i]);
  73. for(int i = 0; i < 7; i++){
  74. if(control[i] != ButtonHandler.getButton(i).pressed)
  75. {
  76. ButtonHandler.getButton(i).pressed = control[i];
  77. if(control[i] == 0)
  78. bth.buttonPress(ButtonHandler.getButton(i));
  79. }
  80. }
  81. if(control[7] == 0 && control[8] == 0){
  82. if(JoystickHandler.j.getPos() != Position.UP_LEFT)
  83. {
  84. JoystickHandler.j.setPosition(Position.UP_LEFT);
  85. jth.onJoystickMoved(JoystickHandler.j);
  86. }
  87. }
  88. else if(control[7] == 0 && control[9] == 0){
  89. if(JoystickHandler.j.getPos() != Position.UP_RIGHT)
  90. {
  91. JoystickHandler.j.setPosition(Position.UP_RIGHT);
  92. jth.onJoystickMoved(JoystickHandler.j);
  93. }
  94. }
  95. else if(control[10] == 0 && control[8] == 0){
  96. if(JoystickHandler.j.getPos() != Position.DOWN_LEFT)
  97. {
  98. JoystickHandler.j.setPosition(Position.DOWN_LEFT);
  99. jth.onJoystickMoved(JoystickHandler.j);
  100. }
  101. }
  102. else if(control[10] == 0 && control[9] == 0){
  103. if(JoystickHandler.j.getPos() != Position.DOWN_RIGHT)
  104. {
  105. JoystickHandler.j.setPosition(Position.DOWN_RIGHT);
  106. jth.onJoystickMoved(JoystickHandler.j);
  107. }
  108. }
  109. else if(control[7] == 0){
  110. if(JoystickHandler.j.getPos() != Position.UP)
  111. {
  112. JoystickHandler.j.setPosition(Position.UP);
  113. jth.onJoystickMoved(JoystickHandler.j);
  114. }
  115. }
  116. else if(control[8] == 0){
  117. if(JoystickHandler.j.getPos() != Position.LEFT)
  118. {
  119. JoystickHandler.j.setPosition(Position.LEFT);
  120. jth.onJoystickMoved(JoystickHandler.j);
  121. }
  122. }
  123. else if(control[9] == 0){
  124. if(JoystickHandler.j.getPos() != Position.RIGHT)
  125. {
  126. JoystickHandler.j.setPosition(Position.RIGHT);
  127. jth.onJoystickMoved(JoystickHandler.j);
  128. }
  129. }
  130. else if(control[10] == 0){
  131. if(JoystickHandler.j.getPos() != Position.DOWN)
  132. {
  133. JoystickHandler.j.setPosition(Position.DOWN);
  134. jth.onJoystickMoved(JoystickHandler.j);
  135. }
  136. }else {
  137. JoystickHandler.j.setPosition(Position.CENTER);
  138. }
  139. }
  140. }
  141. }