NetworkHandler.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 long fistpressed;
  16. private boolean running;
  17. private Thread t;
  18. private byte[] send,
  19. receive;
  20. private ButtonHandler bth;
  21. private JoystickHandler jth;
  22. public NetworkHandler(String host, int port, ButtonHandler bth, JoystickHandler jth)
  23. {
  24. this.host = host;
  25. this.port = port;
  26. this.bth = bth;
  27. this.jth = jth;
  28. udp = null;
  29. send = new byte[1024];
  30. receive = new byte[1024];
  31. try {
  32. udp = new DatagramSocket(1113);
  33. } catch (SocketException e) {
  34. e.printStackTrace();
  35. }
  36. running = true;
  37. t = new Thread(this);
  38. t.start();
  39. }
  40. public void setLed(int led, int r, int g , int b)
  41. {
  42. String cmd = "1|" + led + "|" + r + "|" + g + "|" + b + "\n";
  43. send(cmd);
  44. }
  45. public void show(){
  46. String cmd = "0\n";
  47. send(cmd);
  48. }
  49. private void send(String str)
  50. {
  51. try {
  52. DatagramPacket sendPacket = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName(host), 1113);
  53. udp.send(sendPacket);
  54. } catch (UnknownHostException e) {
  55. e.printStackTrace();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. public void close()
  61. {
  62. running = false;
  63. udp.disconnect();
  64. udp.close();
  65. }
  66. @Override
  67. public void run() {
  68. while(running)
  69. {
  70. DatagramPacket receivePacket = new DatagramPacket(receive, receive.length);
  71. try {
  72. udp.receive(receivePacket);
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. String sentence = new String( receivePacket.getData());
  77. sentence = sentence.trim();
  78. String[] controls = sentence.split("\\|");
  79. int[] control = new int[controls.length];
  80. for(int i=0; i<controls.length; i++)
  81. control[i] = Integer.parseInt(controls[i]);
  82. for(int i = 0; i < 7; i++){
  83. if(control[i] != ButtonHandler.getButton(i).pressed)
  84. {
  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 || JoystickHandler.REPEAT)
  120. {
  121. if(JoystickHandler.j.getPos() != Position.UP)
  122. {
  123. fistpressed = System.currentTimeMillis();
  124. JoystickHandler.j.setPosition(Position.UP);
  125. jth.onJoystickMoved(JoystickHandler.j);
  126. }
  127. if((fistpressed + 500) < System.currentTimeMillis()){
  128. JoystickHandler.j.setPosition(Position.UP);
  129. jth.onJoystickMoved(JoystickHandler.j);
  130. }
  131. }
  132. }
  133. else if(control[8] == 0){
  134. if(JoystickHandler.j.getPos() != Position.LEFT)
  135. {
  136. JoystickHandler.j.setPosition(Position.LEFT);
  137. jth.onJoystickMoved(JoystickHandler.j);
  138. }
  139. }
  140. else if(control[9] == 0){
  141. if(JoystickHandler.j.getPos() != Position.RIGHT)
  142. {
  143. JoystickHandler.j.setPosition(Position.RIGHT);
  144. jth.onJoystickMoved(JoystickHandler.j);
  145. }
  146. }
  147. else if(control[10] == 0){
  148. if(JoystickHandler.j.getPos() != Position.DOWN || JoystickHandler.REPEAT)
  149. {
  150. if(JoystickHandler.j.getPos() != Position.DOWN){
  151. fistpressed = System.currentTimeMillis();
  152. JoystickHandler.j.setPosition(Position.DOWN);
  153. jth.onJoystickMoved(JoystickHandler.j);
  154. }
  155. if((fistpressed + 500) < System.currentTimeMillis()){
  156. JoystickHandler.j.setPosition(Position.DOWN);
  157. jth.onJoystickMoved(JoystickHandler.j);
  158. }
  159. }
  160. }else {
  161. if(JoystickHandler.j.getPos() != Position.CENTER){
  162. fistpressed = 0;
  163. JoystickHandler.j.setPosition(Position.CENTER);
  164. jth.onJoystickMoved(JoystickHandler.j);
  165. }
  166. }
  167. }
  168. }
  169. }