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() });
}
}
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
}
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations