NetworkHandler.java 4.1 KB

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