Search in sources :

Example 46 with DatagramPacket

use of java.net.DatagramPacket in project openhab1-addons by openhab.

the class MilightBinding method sendMessage.

protected void sendMessage(String messageBytes, String bridgeId) {
    String bridgeIp = bridgeIpConfig.get(bridgeId);
    Integer bridgePort = bridgePortConfig.get(bridgeId);
    if (bridgePort == null) {
        bridgePort = DEFAULT_PORT;
    }
    try {
        byte[] buffer = getMessageBytes(messageBytes);
        InetAddress IPAddress = InetAddress.getByName(bridgeIp);
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, IPAddress, bridgePort);
        DatagramSocket datagramSocket = new DatagramSocket();
        datagramSocket.send(packet);
        datagramSocket.close();
        logger.debug("Sent packet '{}' to bridge '{}' ({}:{})", new Object[] { messageBytes, bridgeId, bridgeIp, bridgePort });
    } catch (Exception e) {
        logger.error("Failed to send Message to '{}': ", new Object[] { bridgeIp, e.getMessage() });
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 47 with DatagramPacket

use of java.net.DatagramPacket in project openhab1-addons by openhab.

the class HarmonyHubDiscovery method sendDiscoveryMessage.

/**
     * Send broadcast message over all active interfaces
     *
     * @param discoverString
     *            String to be used for the discovery
     */
private void sendDiscoveryMessage(String discoverString) {
    DatagramSocket bcSend = null;
    try {
        bcSend = new DatagramSocket();
        bcSend.setBroadcast(true);
        byte[] sendData = discoverString.getBytes();
        // Broadcast the message over all the network interfaces
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface networkInterface = interfaces.nextElement();
            if (networkInterface.isLoopback() || !networkInterface.isUp()) {
                continue;
            }
            for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
                InetAddress[] broadcast = new InetAddress[3];
                broadcast[0] = InetAddress.getByName("224.0.0.1");
                broadcast[1] = InetAddress.getByName("255.255.255.255");
                broadcast[2] = interfaceAddress.getBroadcast();
                broadcast[3] = InetAddress.getByName(optionalHost);
                for (InetAddress bc : broadcast) {
                    // Send the broadcast package!
                    if (bc != null && !bc.isLoopbackAddress()) {
                        try {
                            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc, DISCO_PORT);
                            bcSend.send(sendPacket);
                        } catch (IOException e) {
                            logger.error("IO error during HarmonyHub discovery: {}", e.getMessage());
                        } catch (Exception e) {
                            logger.error(e.getMessage(), e);
                        }
                        logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(), networkInterface.getDisplayName());
                    }
                }
            }
        }
    } catch (IOException e) {
        logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage());
    } finally {
        try {
            if (bcSend != null) {
                bcSend.close();
            }
        } catch (Exception e) {
        // Ignore
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) InterfaceAddress(java.net.InterfaceAddress) DatagramPacket(java.net.DatagramPacket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) InetAddress(java.net.InetAddress) IOException(java.io.IOException)

Example 48 with DatagramPacket

use of java.net.DatagramPacket in project openhab1-addons by openhab.

the class AnelUDPConnector method sendDatagram.

/**
     * Send data to the specified address via the specified UDP port.
     *
     * @param data
     *            The data to send.
     * @throws Exception
     *             If an exception occurred.
     */
void sendDatagram(byte[] data) throws Exception {
    if (data == null || data.length == 0) {
        throw new IllegalArgumentException("data must not be null or empty");
    }
    try {
        final InetAddress ipAddress = InetAddress.getByName(host);
        final DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, sendPort);
        final DatagramSocket socket = new DatagramSocket();
        socket.send(packet);
        socket.close();
    } catch (SocketException e) {
        throw new Exception(e);
    } catch (UnknownHostException e) {
        throw new Exception("Could not resolve host: " + host, e);
    } catch (IOException e) {
        throw new Exception(e);
    }
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 49 with DatagramPacket

use of java.net.DatagramPacket in project openhab1-addons by openhab.

the class AnelUDPConnector method receiveDatagram.

/**
     * This is a blocking call for receiving data from the specific UDP port.
     *
     * @return The received data.
     * @throws Exception
     *             If an exception occurred.
     */
byte[] receiveDatagram() throws Exception {
    try {
        if (socket == null) {
            socket = new DatagramSocket(receivePort);
        }
        // Create a packet
        final DatagramPacket packet = new DatagramPacket(new byte[MAX_PACKET_SIZE], MAX_PACKET_SIZE);
        // Receive a packet (blocking)
        socket.receive(packet);
        return Arrays.copyOfRange(packet.getData(), 0, packet.getLength() - 1);
    } catch (SocketException e) {
        if (e.getMessage() != null && e.getMessage().equals("socket closed")) {
            // happens during shutdown
            return null;
        }
        throw new Exception(e);
    } catch (IOException e) {
        throw new Exception(e);
    }
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 50 with DatagramPacket

use of java.net.DatagramPacket in project openhab1-addons by openhab.

the class NibeHeatPumpUDPConnector method receiveDatagram.

@Override
public byte[] receiveDatagram() throws NibeHeatPumpException {
    final int PACKETSIZE = 255;
    try {
        if (socket == null) {
            socket = new DatagramSocket(port);
        }
        // Create a packet
        DatagramPacket packet = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE);
        // Receive a packet (blocking)
        socket.receive(packet);
        return Arrays.copyOfRange(packet.getData(), 0, packet.getLength());
    } catch (SocketException e) {
        throw new NibeHeatPumpException(e);
    } catch (IOException e) {
        throw new NibeHeatPumpException(e);
    }
}
Also used : SocketException(java.net.SocketException) DatagramSocket(java.net.DatagramSocket) NibeHeatPumpException(org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Aggregations

DatagramPacket (java.net.DatagramPacket)302 IOException (java.io.IOException)126 DatagramSocket (java.net.DatagramSocket)121 InetAddress (java.net.InetAddress)69 SocketException (java.net.SocketException)43 Test (org.junit.Test)35 InetSocketAddress (java.net.InetSocketAddress)34 SocketTimeoutException (java.net.SocketTimeoutException)30 UnknownHostException (java.net.UnknownHostException)21 MulticastSocket (java.net.MulticastSocket)18 InterruptedIOException (java.io.InterruptedIOException)17 SocketAddress (java.net.SocketAddress)15 ByteBuffer (java.nio.ByteBuffer)11 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 SyslogConnection (org.opennms.netmgt.syslogd.api.SyslogConnection)11 PortUnreachableException (java.net.PortUnreachableException)9 ArrayList (java.util.ArrayList)9 SyslogMessageLogDTO (org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO)8 BindException (java.net.BindException)7 Transactional (org.springframework.transaction.annotation.Transactional)7