Advertisement
Guest User

Untitled

a guest
Oct 27th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.*;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.util.concurrent.CopyOnWriteArrayList;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. public class MulticastServer extends Thread {
  9. private static String MULTICAST_ADDRESS = "224.1.224.0";
  10. public static int PORT = 6666;
  11. public static int PINGPORT = 8888;
  12. private Connection c;
  13. private MulticastSocket socket = null;
  14. public static MulticastSocket secundarySocket = null;
  15. public static MulticastSocket pingSocket = null;
  16. public static MulticastSocket s = null;
  17. public static int n;
  18. public static InetAddress group;
  19. public static CopyOnWriteArrayList<Integer> messages = new CopyOnWriteArrayList<>();
  20. public static CopyOnWriteArrayList<Integer> serverList;
  21.  
  22. public MulticastServer() {
  23. super("Server " + (long) (Math.random() * 1000));
  24. serverList = new CopyOnWriteArrayList<Integer>();
  25. dbConnect();
  26. try {
  27. socket = new MulticastSocket(PORT);
  28. s = new MulticastSocket(PORT);
  29. secundarySocket = new MulticastSocket(PORT);
  30. pingSocket = new MulticastSocket(PINGPORT);
  31. group = InetAddress.getByName(MULTICAST_ADDRESS);
  32. pingSocket.joinGroup(group);
  33. secundarySocket.joinGroup(group);
  34. s.joinGroup(group);
  35.  
  36. }
  37. catch(IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. private void dbConnect() {
  43. try {
  44. Class.forName("org.postgresql.Driver");
  45. c = DriverManager.getConnection("jdbc:postgresql://localhost:5433/dropmusic", "postgres", "Almofadex123");
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. System.err.println(e.getClass().getName()+": "+e.getMessage());
  49. System.exit(0);
  50. }
  51. System.out.println("Opened database successfully");
  52. }
  53.  
  54. private static void getN() {
  55. byte[] b = "2".getBytes();
  56. byte[] receivedBytes = new byte[100];
  57. try {
  58. DatagramPacket dp = new DatagramPacket(receivedBytes, receivedBytes.length);
  59. System.out.println("tentando 2");
  60. pingSocket.send(new DatagramPacket(b, b.length, group, PINGPORT));
  61. pingSocket.setSoTimeout(1000);
  62. while(!new String(dp.getData(), 0, dp.getLength()).equals("hello"))
  63. pingSocket.receive(dp);
  64. //System.out.println(new String(dp.getData(), 0, dp.getLength()));
  65. n = 3;
  66. } catch (SocketException e) {
  67. System.out.println("AQUI");
  68. e.printStackTrace();
  69. } catch (SocketTimeoutException e) {
  70. System.out.println("timeout1!");
  71. b = "1".getBytes();
  72. receivedBytes = new byte[100];
  73. try {
  74. DatagramPacket dp = new DatagramPacket(receivedBytes, receivedBytes.length);
  75. System.out.println("tentando 1");
  76. pingSocket.send(new DatagramPacket(b, b.length, group, PINGPORT));
  77. pingSocket.setSoTimeout(1000);
  78. while(!new String(dp.getData(), 0, dp.getLength()).equals("hello"))
  79. pingSocket.receive(dp);
  80. n = 2;
  81. }
  82. catch (SocketTimeoutException e1) {
  83. System.out.println("timeout1!");
  84. n = 1;
  85. }
  86. catch (SocketException e1) {
  87. e1.printStackTrace();
  88. }
  89. catch (IOException e1) {
  90. e1.printStackTrace();
  91. }
  92. } catch (IOException e) {
  93. System.out.println("AQUI2");
  94. e.printStackTrace();
  95. }
  96. }
  97. public static void main(String args[]) {
  98. MulticastServer server = new MulticastServer();
  99. getN();
  100. new ImHereThread("1");
  101. System.out.println(n);
  102. server.start();
  103. System.out.println("FIM DO MAIN");
  104.  
  105.  
  106. }
  107. public void run() {
  108. int count = 2;
  109. System.out.println(this.getName() + " running...");
  110. try {
  111. socket.joinGroup(group);
  112. while (true) {
  113. byte[] buffer = new byte[256];
  114. DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  115. socket.receive(packet);
  116. String messageReceived = new String(packet.getData(), 0, packet.getLength());
  117. System.out.println("Received packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + " with message:" + messageReceived);
  118. ReadMessageThread t = new ReadMessageThread("" + count, messageReceived, c, s, packet.getAddress().getHostAddress());
  119. count++;
  120. }
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement