NetworkHandler.java 4.2 KB

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