jdk/test/sun/management/jdp/JdpClient.java
changeset 15531 071efc9f31ad
child 21295 5c73446feb1f
equal deleted inserted replaced
15530:d918415aea3f 15531:071efc9f31ad
       
     1 /*
       
     2  * Copyright (c) 2012, 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 sun.management.jdp.JdpException;
       
    39 import sun.management.jdp.JdpJmxPacket;
       
    40 
       
    41 public class JdpClient {
       
    42 
       
    43     private static class PacketListener implements Runnable {
       
    44 
       
    45         private static final int BUFFER_LENGTH = 4096;
       
    46         private final DatagramChannel channel;
       
    47         private static int maxPacketCount = 1;
       
    48         private static int maxEmptyPacketCount = 10;
       
    49 
       
    50 
       
    51         PacketListener(DatagramChannel channel) {
       
    52             this.channel = channel;
       
    53         }
       
    54 
       
    55         @java.lang.Override
       
    56         public void run() {
       
    57             try {
       
    58                 Selector sel;
       
    59                 sel = Selector.open();
       
    60                 channel.configureBlocking(false);
       
    61                 channel.register(sel, SelectionKey.OP_READ);
       
    62                 ByteBuffer buf = ByteBuffer.allocate(1024);
       
    63 
       
    64                 int count = 1;
       
    65                 int emptyPacketsCount = 1;
       
    66 
       
    67                 try {
       
    68                     while (true) {
       
    69 
       
    70                         sel.selectedKeys().clear();
       
    71                         buf.rewind();
       
    72 
       
    73                         sel.select(10 * 1000);
       
    74                         channel.receive(buf);
       
    75 
       
    76                         if (buf.position() == 0 ){
       
    77                             if (JdpDoSomething.getVerbose()){
       
    78                                 System.err.println("Empty packet received");
       
    79                             }
       
    80                             if (++emptyPacketsCount > maxEmptyPacketCount){
       
    81                                 throw new RuntimeException("Test failed, maxEmptyPacketCount reached");
       
    82                             }
       
    83 
       
    84                             continue;
       
    85                         }
       
    86 
       
    87                         buf.flip();
       
    88                         byte[] dgramData = new byte[buf.remaining()];
       
    89                         buf.get(dgramData);
       
    90 
       
    91                         try {
       
    92                             JdpJmxPacket packet = new JdpJmxPacket(dgramData);
       
    93                             JdpDoSomething.printJdpPacket(packet);
       
    94                             if(++count > maxPacketCount){
       
    95                                    break;
       
    96                             }
       
    97                         } catch (JdpException e) {
       
    98                             e.printStackTrace();
       
    99                             throw new RuntimeException("Test failed");
       
   100                         }
       
   101 
       
   102                     }
       
   103 
       
   104                     System.out.println("OK: Test passed");
       
   105 
       
   106                 } finally {
       
   107                     sel.close();
       
   108                     channel.close();
       
   109                 }
       
   110             } catch (IOException e) {
       
   111                 e.printStackTrace();
       
   112                 throw new RuntimeException("Test failed");
       
   113             }
       
   114         }
       
   115     }
       
   116 
       
   117     public static void main(String[] args) {
       
   118         try {
       
   119             String discoveryPort = System.getProperty("com.sun.management.jdp.port");
       
   120             String discoveryAddress = System.getProperty("com.sun.management.jdp.address");
       
   121             if (discoveryAddress == null || discoveryPort == null) {
       
   122                 System.out.println("Test failed. address and port must be specified");
       
   123                 return;
       
   124             }
       
   125 
       
   126             int port = Integer.parseInt(discoveryPort);
       
   127             InetAddress address = InetAddress.getByName(discoveryAddress);
       
   128 
       
   129 
       
   130             ProtocolFamily family = (address instanceof Inet6Address)
       
   131                     ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
       
   132 
       
   133             DatagramChannel channel;
       
   134 
       
   135             channel = DatagramChannel.open(family);
       
   136             channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
       
   137             channel.bind(new InetSocketAddress(port));
       
   138 
       
   139             Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
       
   140             for (NetworkInterface interf : Collections.list(nets)) {
       
   141                 if (interf.supportsMulticast()) {
       
   142                     try {
       
   143                         channel.join(address, interf);
       
   144                     } catch (IOException e) {
       
   145                         // Skip not configured interfaces
       
   146                     }
       
   147                 }
       
   148             }
       
   149 
       
   150             PacketListener listener = new PacketListener(channel);
       
   151             new Thread(listener, "Jdp Client").start();
       
   152 
       
   153         } catch (RuntimeException e){
       
   154             System.out.println("Test failed.");
       
   155         } catch (Exception e) {
       
   156             e.printStackTrace();
       
   157             System.out.println("Test failed. unexpected error " + e);
       
   158         }
       
   159     }
       
   160 }