Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/management/jdp/JdpClient.java
38840 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.net.Inet6Address;25import java.net.InetAddress;26import java.net.InetSocketAddress;27import java.net.NetworkInterface;28import java.net.ProtocolFamily;29import java.net.StandardProtocolFamily;30import java.net.StandardSocketOptions;31import java.nio.ByteBuffer;32import java.nio.channels.DatagramChannel;33import java.nio.channels.SelectionKey;34import java.nio.channels.Selector;35import java.util.Collections;36import java.util.Enumeration;37import java.util.Map;3839import sun.management.jdp.JdpException;40import sun.management.jdp.JdpJmxPacket;41import sun.management.jdp.JdpPacketReader;4243public class JdpClient {4445private static class PacketListener implements Runnable {4647private static final int BUFFER_LENGTH = 4096;48private final DatagramChannel channel;49private static int maxPacketCount = 1;50private static int maxEmptyPacketCount = 10;5152private void get(Map<?, ?> map, String key)53throws JdpException {5455if (map.get(key) == null) {56throw new JdpException("Test failed, packet field " + key + " missed");57}58}5960private void checkFieldPresence(JdpJmxPacket p)61throws IOException, JdpException {6263byte[] b = p.getPacketData();6465JdpPacketReader reader = new JdpPacketReader(b);66Map<String, String> pMap = reader.getDiscoveryDataAsMap();6768get(pMap, JdpJmxPacket.UUID_KEY);69get(pMap, JdpJmxPacket.MAIN_CLASS_KEY);70get(pMap, JdpJmxPacket.JMX_SERVICE_URL_KEY);71// get(pMap, JdpJmxPacket.INSTANCE_NAME_KEY);72get(pMap, JdpJmxPacket.PROCESS_ID_KEY);73get(pMap, JdpJmxPacket.BROADCAST_INTERVAL_KEY);74get(pMap, JdpJmxPacket.RMI_HOSTNAME_KEY);75}767778PacketListener(DatagramChannel channel) {79this.channel = channel;80}8182@java.lang.Override83public void run() {84try {85Selector sel;86sel = Selector.open();87channel.configureBlocking(false);88channel.register(sel, SelectionKey.OP_READ);89ByteBuffer buf = ByteBuffer.allocate(1024);9091int count = 1;92int emptyPacketsCount = 1;9394try {95while (true) {9697// Use tcpdump -U -w - -s 1400 -c 2 -vv port 709598// to verify that correct packet being sent99sel.selectedKeys().clear();100buf.rewind();101102sel.select(10 * 1000);103channel.receive(buf);104105if (buf.position() == 0) {106if (JdpDoSomething.getVerbose()) {107System.err.println("Empty packet received");108}109if (++emptyPacketsCount > maxEmptyPacketCount) {110throw new RuntimeException("Test failed, maxEmptyPacketCount reached");111}112113continue;114}115116buf.flip();117byte[] dgramData = new byte[buf.remaining()];118buf.get(dgramData);119try {120JdpJmxPacket packet = new JdpJmxPacket(dgramData);121JdpDoSomething.printJdpPacket(packet);122checkFieldPresence(packet);123if (++count > maxPacketCount) {124break;125}126} catch (JdpException e) {127e.printStackTrace();128throw new RuntimeException("Test failed");129}130131}132133System.out.println("OK: Test passed");134135} finally {136sel.close();137channel.close();138}139} catch (IOException e) {140e.printStackTrace();141throw new RuntimeException("Test failed");142}143}144}145146public static void main(String[] args) {147try {148String discoveryPort = System.getProperty("com.sun.management.jdp.port");149String discoveryAddress = System.getProperty("com.sun.management.jdp.address");150if (discoveryAddress == null || discoveryPort == null) {151System.out.println("Test failed. address and port must be specified");152return;153}154155int port = Integer.parseInt(discoveryPort);156InetAddress address = InetAddress.getByName(discoveryAddress);157158159ProtocolFamily family = (address instanceof Inet6Address)160? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;161162DatagramChannel channel;163164channel = DatagramChannel.open(family);165channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);166channel.bind(new InetSocketAddress(port));167168Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();169for (NetworkInterface interf : Collections.list(nets)) {170if (interf.supportsMulticast()) {171try {172channel.join(address, interf);173} catch (IOException e) {174// Skip not configured interfaces175}176}177}178179PacketListener listener = new PacketListener(channel);180new Thread(listener, "Jdp Client").start();181182} catch (RuntimeException e) {183System.out.println("Test failed.");184} catch (Exception e) {185e.printStackTrace();186System.out.println("Test failed. unexpected error " + e);187}188}189}190191192