use of java.net.DatagramPacket in project GNS by MobilityFirst.
the class ActiveDatagramChannel method receiveMessage.
@Override
public Message receiveMessage() throws IOException {
byte[] buf = new byte[maxPacketSize];
DatagramPacket packet = new DatagramPacket(buf, maxPacketSize);
socket.receive(packet);
ActiveMessage am = null;
try {
am = new ActiveMessage(packet.getData());
} catch (JSONException e) {
e.printStackTrace();
}
return am;
}
use of java.net.DatagramPacket in project GNS by MobilityFirst.
the class LookupWorker method sendResponse.
/**
* Returns a response to the sender.
*
* @param responseBytes
*/
private void sendResponse(byte[] responseBytes) {
DatagramPacket outgoingPacket = new DatagramPacket(responseBytes, responseBytes.length, incomingPacket.getAddress(), incomingPacket.getPort());
try {
socket.send(outgoingPacket);
NameResolution.getLogger().log(Level.FINE, "Response sent to {0} {1}", new Object[] { incomingPacket.getAddress().toString(), incomingPacket.getPort() });
} catch (IOException e) {
NameResolution.getLogger().log(Level.SEVERE, "Failed to send response{0}", e);
}
}
use of java.net.DatagramPacket in project GNS by MobilityFirst.
the class UdpDnsServer method run.
@Override
public void run() {
NameResolution.getLogger().log(Level.INFO, "Starting local DNS Server on port {0}{1}fallback DNS server at {2}", new Object[] { sock.getLocalPort(), gnsServerIP != null ? (" with GNS server at " + gnsServerIP + " and ") : " with ", dnsServerIP });
while (true) {
try {
final short udpLength = 512;
while (true) {
byte[] incomingData = new byte[udpLength];
DatagramPacket incomingPacket = new DatagramPacket(incomingData, incomingData.length);
// Read the incoming request
incomingPacket.setLength(incomingData.length);
try {
sock.receive(incomingPacket);
} catch (InterruptedIOException e) {
continue;
}
executor.execute(new LookupWorker(sock, incomingPacket, incomingData, gnsServer, dnsServer, dnsCache, handler));
}
} catch (IOException e) {
NameResolution.getLogger().log(Level.SEVERE, "Error in UDP Server (will sleep for 3 seconds and try again): {0}", e);
ThreadUtils.sleep(3000);
}
}
}
use of java.net.DatagramPacket in project opennms by OpenNMS.
the class NativeSocketTest method testServer.
@Test
public void testServer() throws Exception {
String[] cmds = new String[] { "echo", "echo2", "quit" };
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
for (final String cmd : cmds) {
final DatagramSocket sock = socket;
final FutureTask<DatagramPacket> task = new FutureTask<DatagramPacket>(new Callable<DatagramPacket>() {
@Override
public DatagramPacket call() throws Exception {
printf("Sending cmd: %s\n", cmd);
final byte[] data = cmd.getBytes(StandardCharsets.UTF_8);
final DatagramPacket p = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), sock.getLocalPort());
sock.send(p);
printf("Receiving...\n");
final DatagramPacket r = new DatagramPacket(new byte[128], 128);
sock.receive(r);
printf("Received\n");
return r;
}
});
m_executor.execute(task);
final DatagramPacket r = task.get(10, TimeUnit.SECONDS);
assertNotNull(r);
final String response = new String(r.getData(), r.getOffset(), r.getLength(), StandardCharsets.UTF_8);
printf("Received Response: %s from %s:%d\n", response, r.getAddress().getHostAddress(), r.getPort());
assertEquals(cmd, response);
}
} finally {
if (socket != null)
socket.close();
}
}
use of java.net.DatagramPacket in project opennms by OpenNMS.
the class SnmpPortal method send.
/**
* Transmits the passed buffer to the respective peer agent. If a failure
* occurs then an IOException is thrown.
*
* @param peer
* The SNMP peer destination
* @param buf
* The buffer to transmit.
* @param length
* The valid length of the buffer
*
* @exception java.lang.IOException
* For more details see java.net.DatagramSocket.
*
* @see java.net.DatagramSocket
*
*/
void send(SnmpPeer peer, byte[] buf, int length) throws java.io.IOException {
//
// create a new datagram packet
//
DatagramPacket pkt = new DatagramPacket(buf, length, peer.getPeer(), peer.getPort());
m_comm.send(pkt);
}
Aggregations