Search in sources :

Example 96 with DatagramPacket

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;
}
Also used : DatagramPacket(java.net.DatagramPacket) JSONException(org.json.JSONException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 97 with DatagramPacket

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);
    }
}
Also used : DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException)

Example 98 with DatagramPacket

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);
        }
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Example 99 with DatagramPacket

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();
    }
}
Also used : FutureTask(java.util.concurrent.FutureTask) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) Test(org.junit.Test)

Example 100 with DatagramPacket

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);
}
Also used : DatagramPacket(java.net.DatagramPacket)

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