001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.transport;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    /**
021     * <p>
022     * A client that listens for multicast messages at a designated port. You may
023     * use this to for potential multicast issues when tuning distributed caches.
024     * </p>
025     *
026     * @author Michael C. Han
027     * @author Raymond Aug??
028     */
029    public class MulticastClientTool {
030    
031            public static void main(String[] args) {
032                    try {
033                            new MulticastClientTool(args);
034                    }
035                    catch (Exception e) {
036                            e.printStackTrace();
037    
038                            StringBuilder sb = new StringBuilder(3);
039    
040                            sb.append("Usage: java -classpath util-java.jar ");
041                            sb.append(MulticastClientTool.class.getName());
042                            sb.append("[-g] [-s] -h [multicastAddress] -p [port]");
043    
044                            System.err.println(sb.toString());
045    
046                            System.exit(1);
047                    }
048            }
049    
050            private MulticastClientTool(String[] args) throws Exception {
051                    Map<String, Object> argsMap = _getArgsMap(args);
052    
053                    Integer port = (Integer)argsMap.get("port");
054                    String host = (String)argsMap.get("host");
055                    Boolean gzipData = (Boolean)argsMap.get("gzip");
056                    Boolean shortData = (Boolean)argsMap.get("short");
057    
058                    DatagramHandler datagramHandler = new MulticastDatagramHandler(
059                            gzipData.booleanValue(), shortData.booleanValue());
060    
061                    MulticastTransport multicastTransport = new MulticastTransport(
062                            datagramHandler, host, port);
063    
064                    if (shortData.booleanValue()) {
065                            System.out.println("Truncating to 96 bytes.");
066                    }
067    
068                    System.out.println("Started up and waiting...");
069    
070                    multicastTransport.connect();
071    
072                    synchronized (multicastTransport) {
073                            multicastTransport.wait();
074                    }
075            }
076    
077            private Map<String, Object> _getArgsMap(String[] args) throws Exception {
078                    Map<String, Object> argsMap = new HashMap<String, Object>();
079    
080                    for (int i = 0; i < args.length; i++) {
081                            if (args[i].equals("-g")) {
082                                    argsMap.put("gzip", Boolean.TRUE);
083                            }
084                            else if (args[i].equals("-s")) {
085                                    argsMap.put("short", Boolean.TRUE);
086                            }
087                            else if (args[i].equals("-h")) {
088                                    argsMap.put("host", args[i + 1]);
089    
090                                    i++;
091                            }
092                            else if (args[i].equals("-p")) {
093                                    argsMap.put("port", new Integer(args[i + 1]));
094    
095                                    i++;
096                            }
097                    }
098    
099                    if (!argsMap.containsKey("gzip")) {
100                            argsMap.put("gzip", Boolean.FALSE);
101                    }
102    
103                    if (!argsMap.containsKey("short")) {
104                            argsMap.put("short", Boolean.FALSE);
105                    }
106    
107                    return argsMap;
108            }
109    
110    }