Search in sources :

Example 21 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 22 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)

Example 23 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 24 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 25 with DatagramPacket

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

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