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 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);
}
}
}
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 RtpSocket method flushSocket.
public static void flushSocket(DatagramSocket socket) {
/*
* Flush the socket
*/
int len = RtpPacket.getDataSize(RtpPacket.PCM_ENCODING, RtpPacket.MAX_SAMPLE_RATE, 2);
len += RtpPacket.HEADER_SIZE;
byte[] data = new byte[len];
DatagramPacket packet = new DatagramPacket(data, len);
int count = 0;
try {
socket.setSoTimeout(1);
while (true) {
try {
socket.receive(packet);
count++;
} catch (SocketTimeoutException e) {
break;
} catch (IOException e) {
Logger.println("Error flushing socket " + e.getMessage());
break;
}
}
} catch (SocketException e) {
Logger.println("Can't flush receiver socket!");
}
if (count > 0) {
if (Logger.logLevel >= Logger.LOG_MOREINFO) {
Logger.println("Packets flushed: " + count);
}
}
try {
socket.setSoTimeout(0);
} catch (SocketException e) {
Logger.println("Can't set socket timeout to 0!");
}
}
Aggregations