Search in sources :

Example 6 with DatagramPacket

use of java.net.DatagramPacket in project hadoop by apache.

the class TestStatsDMetrics method testPutMetrics2.

@Test(timeout = 3000)
public void testPutMetrics2() throws IOException {
    StatsDSink sink = new StatsDSink();
    List<MetricsTag> tags = new ArrayList<MetricsTag>();
    tags.add(new MetricsTag(MsInfo.Hostname, null));
    tags.add(new MetricsTag(MsInfo.Context, "jvm"));
    tags.add(new MetricsTag(MsInfo.ProcessName, "process"));
    Set<AbstractMetric> metrics = new HashSet<AbstractMetric>();
    metrics.add(makeMetric("foo1", 1, MetricType.COUNTER));
    metrics.add(makeMetric("foo2", 2, MetricType.GAUGE));
    MetricsRecord record = new MetricsRecordImpl(MsInfo.Context, (long) 10000, tags, metrics);
    try (DatagramSocket sock = new DatagramSocket()) {
        sock.setReceiveBufferSize(8192);
        final StatsDSink.StatsD mockStatsD = new StatsD(sock.getLocalAddress().getHostName(), sock.getLocalPort());
        Whitebox.setInternalState(sink, "statsd", mockStatsD);
        final DatagramPacket p = new DatagramPacket(new byte[8192], 8192);
        sink.putMetrics(record);
        sock.receive(p);
        String result = new String(p.getData(), 0, p.getLength(), Charset.forName("UTF-8"));
        assertTrue("Received data did not match data sent", result.equals("process.jvm.Context.foo1:1|c") || result.equals("process.jvm.Context.foo2:2|g"));
    } finally {
        sink.close();
    }
}
Also used : StatsDSink(org.apache.hadoop.metrics2.sink.StatsDSink) MetricsRecord(org.apache.hadoop.metrics2.MetricsRecord) ArrayList(java.util.ArrayList) AbstractMetric(org.apache.hadoop.metrics2.AbstractMetric) MetricsTag(org.apache.hadoop.metrics2.MetricsTag) StatsD(org.apache.hadoop.metrics2.sink.StatsDSink.StatsD) DatagramSocket(java.net.DatagramSocket) StatsD(org.apache.hadoop.metrics2.sink.StatsDSink.StatsD) DatagramPacket(java.net.DatagramPacket) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with DatagramPacket

use of java.net.DatagramPacket in project hadoop by apache.

the class SimpleUdpClient method run.

public void run() throws IOException {
    InetAddress IPAddress = InetAddress.getByName(host);
    byte[] sendData = request.getBytes();
    byte[] receiveData = new byte[65535];
    // Use the provided socket if there is one, else just make a new one.
    DatagramSocket socket = this.clientSocket == null ? new DatagramSocket() : this.clientSocket;
    try {
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
        socket.send(sendPacket);
        socket.setSoTimeout(udpTimeoutMillis);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        socket.receive(receivePacket);
        // Check reply status
        XDR xdr = new XDR(Arrays.copyOfRange(receiveData, 0, receivePacket.getLength()));
        RpcReply reply = RpcReply.read(xdr);
        if (reply.getState() != RpcReply.ReplyState.MSG_ACCEPTED) {
            throw new IOException("Request failed: " + reply.getState());
        }
    } finally {
        // caller of this UDP client to close that socket.
        if (this.clientSocket == null) {
            socket.close();
        }
    }
}
Also used : DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 8 with DatagramPacket

use of java.net.DatagramPacket in project hadoop by apache.

the class TestUdpServer method testRequest.

static void testRequest(XDR request, XDR request2) {
    try {
        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName("localhost");
        byte[] sendData = request.getBytes();
        byte[] receiveData = new byte[65535];
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, Nfs3Constant.SUN_RPCBIND);
        clientSocket.send(sendPacket);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        clientSocket.close();
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: localhost.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for " + "the connection to: localhost.");
        System.exit(1);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) IOException(java.io.IOException) InetAddress(java.net.InetAddress)

Example 9 with DatagramPacket

use of java.net.DatagramPacket in project hadoop by apache.

the class TestPortmap method testRegistration.

@Test(timeout = 1000)
public void testRegistration() throws IOException, InterruptedException {
    XDR req = new XDR();
    RpcCall.getInstance(++xid, RpcProgramPortmap.PROGRAM, RpcProgramPortmap.VERSION, RpcProgramPortmap.PMAPPROC_SET, new CredentialsNone(), new VerifierNone()).write(req);
    PortmapMapping sent = new PortmapMapping(90000, 1, PortmapMapping.TRANSPORT_TCP, 1234);
    sent.serialize(req);
    byte[] reqBuf = req.getBytes();
    DatagramSocket s = new DatagramSocket();
    DatagramPacket p = new DatagramPacket(reqBuf, reqBuf.length, pm.getUdpServerLoAddress());
    try {
        s.send(p);
    } finally {
        s.close();
    }
    // Give the server a chance to process the request
    Thread.sleep(100);
    boolean found = false;
    @SuppressWarnings("unchecked") Map<String, PortmapMapping> map = (Map<String, PortmapMapping>) Whitebox.getInternalState(pm.getHandler(), "map");
    for (PortmapMapping m : map.values()) {
        if (m.getPort() == sent.getPort() && PortmapMapping.key(m).equals(PortmapMapping.key(sent))) {
            found = true;
            break;
        }
    }
    Assert.assertTrue("Registration failed", found);
}
Also used : DatagramSocket(java.net.DatagramSocket) XDR(org.apache.hadoop.oncrpc.XDR) VerifierNone(org.apache.hadoop.oncrpc.security.VerifierNone) DatagramPacket(java.net.DatagramPacket) CredentialsNone(org.apache.hadoop.oncrpc.security.CredentialsNone) Map(java.util.Map) Test(org.junit.Test)

Example 10 with DatagramPacket

use of java.net.DatagramPacket in project tomcat by apache.

the class MultiCastSender method send.

@Override
public int send(String mess) throws Exception {
    if (s == null) {
        try {
            group = InetAddress.getByName(config.getGroup());
            if (config.getHost() != null) {
                InetAddress addr = InetAddress.getByName(config.getHost());
                InetSocketAddress addrs = new InetSocketAddress(addr, config.getMultiport());
                s = new MulticastSocket(addrs);
            } else
                s = new MulticastSocket(config.getMultiport());
            s.setTimeToLive(config.getTtl());
            s.joinGroup(group);
        } catch (Exception ex) {
            log.error("Unable to use multicast: " + ex);
            s = null;
            return -1;
        }
    }
    byte[] buf;
    buf = mess.getBytes(StandardCharsets.US_ASCII);
    DatagramPacket data = new DatagramPacket(buf, buf.length, group, config.getMultiport());
    try {
        s.send(data);
    } catch (Exception ex) {
        log.error("Unable to send collected load information: " + ex);
        s.close();
        s = null;
        return -1;
    }
    return 0;
}
Also used : MulticastSocket(java.net.MulticastSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) InetAddress(java.net.InetAddress)

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