1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.net.Inet6Address;
  26 import java.net.InetAddress;
  27 import java.net.InetSocketAddress;
  28 import java.net.NetworkInterface;
  29 import java.net.ProtocolFamily;
  30 import java.net.StandardProtocolFamily;
  31 import java.net.StandardSocketOptions;
  32 import java.nio.ByteBuffer;
  33 import java.nio.channels.DatagramChannel;
  34 import java.nio.channels.SelectionKey;
  35 import java.nio.channels.Selector;
  36 import java.util.Collections;
  37 import java.util.Enumeration;
  38 import java.util.Map;
  39 import sun.management.jdp.JdpException;
  40 import sun.management.jdp.JdpJmxPacket;
  41 import sun.management.jdp.JdpPacketReader;
  42 
  43 public class JdpClient {
  44 
  45     private static class PacketListener implements Runnable {
  46 
  47         private static final int BUFFER_LENGTH = 4096;
  48         private final DatagramChannel channel;
  49         private static int maxPacketCount = 1;
  50         private static int maxEmptyPacketCount = 10;
  51 
  52         private void get(Map<?,?> map, String key)
  53             throws JdpException {
  54 
  55             if (map.get(key) == null) {
  56                   throw new JdpException("Test failed, packet field " + key + " missed");
  57             }
  58         }
  59 
  60         private void checkFieldPresence(JdpJmxPacket p)
  61             throws IOException, JdpException {
  62 
  63             byte[] b = p.getPacketData();
  64 
  65             JdpPacketReader reader = new JdpPacketReader(b);
  66             Map<String,String> pMap = reader.getDiscoveryDataAsMap();
  67 
  68             get(pMap, JdpJmxPacket.UUID_KEY);
  69             get(pMap, JdpJmxPacket.MAIN_CLASS_KEY);
  70             get(pMap, JdpJmxPacket.JMX_SERVICE_URL_KEY);
  71             // get(pMap, JdpJmxPacket.INSTANCE_NAME_KEY);
  72             get(pMap, JdpJmxPacket.PROCESS_ID_KEY);
  73             get(pMap, JdpJmxPacket.BROADCAST_INTERVAL_KEY);
  74             get(pMap, JdpJmxPacket.RMI_HOSTNAME_KEY);
  75         }
  76 
  77 
  78         PacketListener(DatagramChannel channel) {
  79             this.channel = channel;
  80         }
  81 
  82         @java.lang.Override
  83         public void run() {
  84             try {
  85                 Selector sel;
  86                 sel = Selector.open();
  87                 channel.configureBlocking(false);
  88                 channel.register(sel, SelectionKey.OP_READ);
  89                 ByteBuffer buf = ByteBuffer.allocate(1024);
  90 
  91                 int count = 1;
  92                 int emptyPacketsCount = 1;
  93 
  94                 try {
  95                     while (true) {
  96 
  97                         // Use tcpdump -U -w - -s 1400 -c 2 -vv port 7095
  98                         // to verify that correct packet being sent
  99                         sel.selectedKeys().clear();
 100                         buf.rewind();
 101 
 102                         sel.select(10 * 1000);
 103                         channel.receive(buf);
 104 
 105                         if (buf.position() == 0 ){
 106                             if (JdpDoSomething.getVerbose()){
 107                                 System.err.println("Empty packet received");
 108                             }
 109                             if (++emptyPacketsCount > maxEmptyPacketCount){
 110                                 throw new RuntimeException("Test failed, maxEmptyPacketCount reached");
 111                             }
 112 
 113                             continue;
 114                         }
 115 
 116                         buf.flip();
 117                         byte[] dgramData = new byte[buf.remaining()];
 118                         buf.get(dgramData);
 119                         try {
 120                             JdpJmxPacket packet = new JdpJmxPacket(dgramData);
 121                             JdpDoSomething.printJdpPacket(packet);
 122                             checkFieldPresence(packet);
 123                             if(++count > maxPacketCount){
 124                                    break;
 125                             }
 126                         } catch (JdpException e) {
 127                             e.printStackTrace();
 128                             throw new RuntimeException("Test failed");
 129                         }
 130 
 131                     }
 132 
 133                     System.out.println("OK: Test passed");
 134 
 135                 } finally {
 136                     sel.close();
 137                     channel.close();
 138                 }
 139             } catch (IOException e) {
 140                 e.printStackTrace();
 141                 throw new RuntimeException("Test failed");
 142             }
 143         }
 144     }
 145 
 146     public static void main(String[] args) {
 147         try {
 148             String discoveryPort = System.getProperty("com.sun.management.jdp.port");
 149             String discoveryAddress = System.getProperty("com.sun.management.jdp.address");
 150             if (discoveryAddress == null || discoveryPort == null) {
 151                 System.out.println("Test failed. address and port must be specified");
 152                 return;
 153             }
 154 
 155             int port = Integer.parseInt(discoveryPort);
 156             InetAddress address = InetAddress.getByName(discoveryAddress);
 157 
 158 
 159             ProtocolFamily family = (address instanceof Inet6Address)
 160                     ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
 161 
 162             DatagramChannel channel;
 163 
 164             channel = DatagramChannel.open(family);
 165             channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
 166             channel.bind(new InetSocketAddress(port));
 167 
 168             Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
 169             for (NetworkInterface interf : Collections.list(nets)) {
 170                 if (interf.supportsMulticast()) {
 171                     try {
 172                         channel.join(address, interf);
 173                     } catch (IOException e) {
 174                         // Skip not configured interfaces
 175                     }
 176                 }
 177             }
 178 
 179             PacketListener listener = new PacketListener(channel);
 180             new Thread(listener, "Jdp Client").start();
 181 
 182         } catch (RuntimeException e){
 183             System.out.println("Test failed.");
 184         } catch (Exception e) {
 185             e.printStackTrace();
 186             System.out.println("Test failed. unexpected error " + e);
 187         }
 188     }
 189 }