summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/net/Broadcaster.java
blob: cfec0aeac2b8f46a8aa5d77c82e3352d403c38c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.orbekk.net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Broadcaster implements BroadcasterInterface {
    private Logger logger = LoggerFactory.getLogger(getClass());

    public List<InetAddress> getBroadcastAddresses() {
        List<InetAddress> broadcastAddresses = new LinkedList<InetAddress>();

        Enumeration<NetworkInterface> interfaces;
        try {
            interfaces = NetworkInterface.getNetworkInterfaces();
        } catch (SocketException e) {
            logger.warn("Network problem?", e.fillInStackTrace());
            return broadcastAddresses;
        }
        while (interfaces.hasMoreElements()) {
            NetworkInterface iface = interfaces.nextElement();
            try {
                if (iface.isLoopback()) {
                    logger.debug("Ignoring looback device " + iface.getName());
                    continue;
                }
                for (InterfaceAddress address : iface.getInterfaceAddresses()) {
                    InetAddress broadcast = address.getBroadcast();
                    if (broadcast != null) {
                        broadcastAddresses.add(broadcast);
                    }
                }
            } catch (SocketException e) {
                logger.info("Ignoring interface " + iface.getName(), e.fillInStackTrace());
            }
        }
        return broadcastAddresses;
    }

    @Override
    public boolean sendBroadcast(int port, byte[] data) {
        boolean successful = false;
        for (InetAddress broadcastAddress : getBroadcastAddresses()) {
            try {
                DatagramSocket socket = new DatagramSocket();
                socket.setBroadcast(true);
                DatagramPacket packet = new DatagramPacket(data, data.length, broadcastAddress, port);
                socket.send(packet);
                successful = true;
            } catch (SocketException e) {
                logger.warn("Failed to send broadcast to " + broadcastAddress +
                        ". ", e.fillInStackTrace());
            } catch (IOException e) {
                logger.warn("Error when sending broadcast to " +
                        broadcastAddress + ".", e.fillInStackTrace());
            }
        }
        return successful;
    }

    public static void main(String[] args) {
        int port = Integer.parseInt(args[0]);
        Broadcaster broadcaster = new Broadcaster();
        String message = "Broadcast from Java broadcaster.";
        broadcaster.sendBroadcast(port, message.getBytes());
    }
}