NetworkHandler.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. private DatagramSocket udp;
  13. private String host;
  14. private int port;
  15. private boolean running;
  16. private Thread t;
  17. private byte[] send,
  18. receive;
  19. private ButtonHandler bth;
  20. private 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. try {
  51. DatagramPacket sendPacket = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName(host), 1113);
  52. udp.send(sendPacket);
  53. } catch (UnknownHostException e) {
  54. e.printStackTrace();
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. public void close()
  60. {
  61. running = false;
  62. udp.disconnect();
  63. udp.close();
  64. }
  65. @Override
  66. public void run() {
  67. while(running)
  68. {
  69. DatagramPacket receivePacket = new DatagramPacket(receive, receive.length);
  70. try {
  71. udp.receive(receivePacket);
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. String sentence = new String( receivePacket.getData());
  76. sentence = sentence.trim();
  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. ButtonHandler.getButton(i).pressed = control[i];
  85. if(control[i] == 0)
  86. bth.buttonPress(ButtonHandler.getButton(i));
  87. }
  88. }
  89. if(control[7] == 0 && control[8] == 0){
  90. if(JoystickHandler.j.getPos() != Position.UP_LEFT)
  91. {
  92. JoystickHandler.j.setPosition(Position.UP_LEFT);
  93. jth.onJoystickMoved(JoystickHandler.j);
  94. }
  95. }
  96. else if(control[7] == 0 && control[9] == 0){
  97. if(JoystickHandler.j.getPos() != Position.UP_RIGHT)
  98. {
  99. JoystickHandler.j.setPosition(Position.UP_RIGHT);
  100. jth.onJoystickMoved(JoystickHandler.j);
  101. }
  102. }
  103. else if(control[10] == 0 && control[8] == 0){
  104. if(JoystickHandler.j.getPos() != Position.DOWN_LEFT)
  105. {
  106. JoystickHandler.j.setPosition(Position.DOWN_LEFT);
  107. jth.onJoystickMoved(JoystickHandler.j);
  108. }
  109. }
  110. else if(control[10] == 0 && control[9] == 0){
  111. if(JoystickHandler.j.getPos() != Position.DOWN_RIGHT)
  112. {
  113. JoystickHandler.j.setPosition(Position.DOWN_RIGHT);
  114. jth.onJoystickMoved(JoystickHandler.j);
  115. }
  116. }
  117. else if(control[7] == 0){
  118. if(JoystickHandler.j.getPos() != Position.UP)
  119. {
  120. JoystickHandler.j.setPosition(Position.UP);
  121. jth.onJoystickMoved(JoystickHandler.j);
  122. }
  123. }
  124. else if(control[8] == 0){
  125. if(JoystickHandler.j.getPos() != Position.LEFT)
  126. {
  127. JoystickHandler.j.setPosition(Position.LEFT);
  128. jth.onJoystickMoved(JoystickHandler.j);
  129. }
  130. }
  131. else if(control[9] == 0){
  132. if(JoystickHandler.j.getPos() != Position.RIGHT)
  133. {
  134. JoystickHandler.j.setPosition(Position.RIGHT);
  135. jth.onJoystickMoved(JoystickHandler.j);
  136. }
  137. }
  138. else if(control[10] == 0){
  139. if(JoystickHandler.j.getPos() != Position.DOWN)
  140. {
  141. JoystickHandler.j.setPosition(Position.DOWN);
  142. jth.onJoystickMoved(JoystickHandler.j);
  143. }
  144. }else {
  145. if(JoystickHandler.j.getPos() != Position.CENTER){
  146. JoystickHandler.j.setPosition(Position.CENTER);
  147. jth.onJoystickMoved(JoystickHandler.j);
  148. }
  149. }
  150. }
  151. }
  152. }