use of java.net.DatagramPacket in project hudson-2.x by hudson.
the class UDPBroadcastThread method run.
@Override
public void run() {
try {
mcs.joinGroup(MULTICAST);
ready.signal();
while (true) {
byte[] buf = new byte[2048];
DatagramPacket p = new DatagramPacket(buf, buf.length);
mcs.receive(p);
SocketAddress sender = p.getSocketAddress();
// prepare a response
TcpSlaveAgentListener tal = hudson.getTcpSlaveAgentListener();
StringBuilder rsp = new StringBuilder("<hudson>");
tag(rsp, "version", Hudson.VERSION);
tag(rsp, "url", hudson.getRootUrl());
tag(rsp, "slave-port", tal == null ? null : tal.getPort());
for (UDPBroadcastFragment f : UDPBroadcastFragment.all()) f.buildFragment(rsp, sender);
rsp.append("</hudson>");
byte[] response = rsp.toString().getBytes("UTF-8");
mcs.send(new DatagramPacket(response, response.length, sender));
}
} catch (ClosedByInterruptException e) {
// shut down
} catch (BindException e) {
// if we failed to listen to UDP, just silently abandon it, as a stack trace
// makes people unnecessarily concerned, for a feature that currently does no good.
LOGGER.log(Level.WARNING, "Failed to listen to UDP port " + PORT, e);
} catch (IOException e) {
// forcibly closed
if (shutdown)
return;
LOGGER.log(Level.WARNING, "UDP handling problem", e);
}
}
use of java.net.DatagramPacket in project jmonkeyengine by jMonkeyEngine.
the class UdpEndpoint method send.
public void send(ByteBuffer data) {
if (!isConnected()) {
throw new KernelException("Endpoint is not connected:" + this);
}
try {
DatagramPacket p = new DatagramPacket(data.array(), data.position(), data.remaining(), address);
// Just queue it up for the kernel threads to write
// out
kernel.enqueueWrite(this, p);
//socket.send(p);
} catch (Exception e) {
if (e instanceof SocketException) {
throw new KernelException("Error sending datagram to:" + address, e);
} else if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
}
}
use of java.net.DatagramPacket in project quickstarts by jboss-switchyard.
the class CamelNettyBindingTest method sendTextMessageThroughUdp.
@Test
public void sendTextMessageThroughUdp() throws Exception {
// replace existing implementation for testing purposes
_testKit.removeService("DefaultGreetingService");
final MockHandler greetingService = _testKit.registerInOnlyService("DefaultGreetingService");
MulticastSocket clientSocket = new MulticastSocket();
InetAddress group = InetAddress.getByName("localhost");
byte[] datagramBody = PAYLOAD.getBytes(Charset.defaultCharset());
DatagramPacket packet = new DatagramPacket(datagramBody, 0, datagramBody.length, group, 3940);
clientSocket.send(packet);
// sleep a bit to receive message on camel side
clientSocket.close();
greetingService.waitForOKMessage();
final LinkedBlockingQueue<Exchange> recievedMessages = greetingService.getMessages();
assertThat(recievedMessages, is(notNullValue()));
final Exchange recievedExchange = recievedMessages.iterator().next();
String content = recievedExchange.getMessage().getContent(String.class);
// the receive content is trimmed because extra bytes appended to frame by receiver
assertThat(PAYLOAD, is(equalTo(content.trim())));
}
use of java.net.DatagramPacket in project jmxtrans by jmxtrans.
the class UdpLoggingServer method processRequests.
private void processRequests(DatagramSocket socket) throws IOException {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String messageReceived = new String(packet.getData(), 0, packet.getLength(), charset);
log.debug("Message received: {}", messageReceived);
receivedMessages.add(messageReceived);
}
use of java.net.DatagramPacket in project Openfire by igniterealtime.
the class Channel method run.
/**
* Thread override method
*/
@Override
public void run() {
try {
while (enabled) {
// Block until a datagram appears:
packet = new DatagramPacket(buf, buf.length);
dataSocket.receive(packet);
if (handle(packet)) {
boolean resend = true;
for (DatagramListener dl : listeners) {
boolean send = dl.datagramReceived(packet);
if (resend && !send) {
resend = false;
}
}
if (resend) {
relayPacket(packet);
}
}
}
} catch (UnknownHostException uhe) {
if (enabled) {
Log.error("Unknown Host", uhe);
}
} catch (SocketException se) {
if (enabled) {
Log.error("Socket closed", se);
}
} catch (IOException ioe) {
if (enabled) {
Log.error("Communication error", ioe);
}
}
}
Aggregations