Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.net.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.util.concurrent.CopyOnWriteArrayList;
- import java.util.concurrent.TimeUnit;
- public class MulticastServer extends Thread {
- private static String MULTICAST_ADDRESS = "224.1.224.0";
- public static int PORT = 6666;
- public static int PINGPORT = 8888;
- private Connection c;
- private MulticastSocket socket = null;
- public static MulticastSocket secundarySocket = null;
- public static MulticastSocket pingSocket = null;
- public static MulticastSocket s = null;
- public static int n;
- public static InetAddress group;
- public static CopyOnWriteArrayList<Integer> messages = new CopyOnWriteArrayList<>();
- public static CopyOnWriteArrayList<Integer> serverList;
- public MulticastServer() {
- super("Server " + (long) (Math.random() * 1000));
- serverList = new CopyOnWriteArrayList<Integer>();
- dbConnect();
- try {
- socket = new MulticastSocket(PORT);
- s = new MulticastSocket(PORT);
- secundarySocket = new MulticastSocket(PORT);
- pingSocket = new MulticastSocket(PINGPORT);
- group = InetAddress.getByName(MULTICAST_ADDRESS);
- pingSocket.joinGroup(group);
- secundarySocket.joinGroup(group);
- s.joinGroup(group);
- }
- catch(IOException e) {
- e.printStackTrace();
- }
- }
- private void dbConnect() {
- try {
- Class.forName("org.postgresql.Driver");
- c = DriverManager.getConnection("jdbc:postgresql://localhost:5433/dropmusic", "postgres", "Almofadex123");
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println(e.getClass().getName()+": "+e.getMessage());
- System.exit(0);
- }
- System.out.println("Opened database successfully");
- }
- private static void getN() {
- byte[] b = "2".getBytes();
- byte[] receivedBytes = new byte[100];
- try {
- DatagramPacket dp = new DatagramPacket(receivedBytes, receivedBytes.length);
- System.out.println("tentando 2");
- pingSocket.send(new DatagramPacket(b, b.length, group, PINGPORT));
- pingSocket.setSoTimeout(1000);
- while(!new String(dp.getData(), 0, dp.getLength()).equals("hello"))
- pingSocket.receive(dp);
- //System.out.println(new String(dp.getData(), 0, dp.getLength()));
- n = 3;
- } catch (SocketException e) {
- System.out.println("AQUI");
- e.printStackTrace();
- } catch (SocketTimeoutException e) {
- System.out.println("timeout1!");
- b = "1".getBytes();
- receivedBytes = new byte[100];
- try {
- DatagramPacket dp = new DatagramPacket(receivedBytes, receivedBytes.length);
- System.out.println("tentando 1");
- pingSocket.send(new DatagramPacket(b, b.length, group, PINGPORT));
- pingSocket.setSoTimeout(1000);
- while(!new String(dp.getData(), 0, dp.getLength()).equals("hello"))
- pingSocket.receive(dp);
- n = 2;
- }
- catch (SocketTimeoutException e1) {
- System.out.println("timeout1!");
- n = 1;
- }
- catch (SocketException e1) {
- e1.printStackTrace();
- }
- catch (IOException e1) {
- e1.printStackTrace();
- }
- } catch (IOException e) {
- System.out.println("AQUI2");
- e.printStackTrace();
- }
- }
- public static void main(String args[]) {
- MulticastServer server = new MulticastServer();
- getN();
- new ImHereThread("1");
- System.out.println(n);
- server.start();
- System.out.println("FIM DO MAIN");
- }
- public void run() {
- int count = 2;
- System.out.println(this.getName() + " running...");
- try {
- socket.joinGroup(group);
- while (true) {
- byte[] buffer = new byte[256];
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
- socket.receive(packet);
- String messageReceived = new String(packet.getData(), 0, packet.getLength());
- System.out.println("Received packet from " + packet.getAddress().getHostAddress() + ":" + packet.getPort() + " with message:" + messageReceived);
- ReadMessageThread t = new ReadMessageThread("" + count, messageReceived, c, s, packet.getAddress().getHostAddress());
- count++;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement