Search in sources :

Example 36 with DatagramPacket

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

Example 37 with DatagramPacket

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);
        }
    }
}
Also used : SocketException(java.net.SocketException) DatagramPacket(java.net.DatagramPacket) KernelException(com.jme3.network.kernel.KernelException) SocketException(java.net.SocketException) KernelException(com.jme3.network.kernel.KernelException) IOException(java.io.IOException)

Example 38 with DatagramPacket

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())));
}
Also used : Exchange(org.switchyard.Exchange) MulticastSocket(java.net.MulticastSocket) DatagramPacket(java.net.DatagramPacket) MockHandler(org.switchyard.test.MockHandler) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 39 with DatagramPacket

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

Example 40 with DatagramPacket

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

Aggregations

DatagramPacket (java.net.DatagramPacket)334 DatagramSocket (java.net.DatagramSocket)138 IOException (java.io.IOException)137 InetAddress (java.net.InetAddress)77 SocketException (java.net.SocketException)48 Test (org.junit.Test)37 InetSocketAddress (java.net.InetSocketAddress)34 SocketTimeoutException (java.net.SocketTimeoutException)30 UnknownHostException (java.net.UnknownHostException)26 MulticastSocket (java.net.MulticastSocket)19 InterruptedIOException (java.io.InterruptedIOException)17 SocketAddress (java.net.SocketAddress)16 ByteBuffer (java.nio.ByteBuffer)11 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)11 SyslogConnection (org.opennms.netmgt.syslogd.api.SyslogConnection)11 ArrayList (java.util.ArrayList)10 PortUnreachableException (java.net.PortUnreachableException)9 SyslogMessageLogDTO (org.opennms.netmgt.syslogd.api.SyslogMessageLogDTO)8 BindException (java.net.BindException)7 Transactional (org.springframework.transaction.annotation.Transactional)7