001    /**
002     * Copyright (c) 2000-2010 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 com.liferay.portal.kernel.util.StringBundler;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    /**
023     * <p>
024     * A client that listens for multicast messages at a designated port. You may
025     * use this to for potential multicast issues when tuning distributed caches.
026     * </p>
027     *
028     * @author Michael C. Han
029     * @author Raymond Augé
030     */
031    public class MulticastClientTool {
032    
033            public static void main(String[] args) {
034                    try {
035                            new MulticastClientTool(args);
036                    }
037                    catch (Exception e) {
038                            e.printStackTrace();
039    
040                            StringBundler sb = new StringBundler(4);
041    
042                            sb.append("Usage: java -classpath ");
043                            sb.append("commons-logging.jar:util-java.jar ");
044                            sb.append("com.liferay.util.transport.MulticastClientTool [-g] ");
045                            sb.append("[-s] -h [multicastAddress] -p [port]");
046    
047                            System.err.println(sb.toString());
048    
049                            System.exit(1);
050                    }
051            }
052    
053            private MulticastClientTool(String[] args) throws Exception {
054                    Map<String, Object> argsMap = _getArgsMap(args);
055    
056                    Integer port = (Integer)argsMap.get("port");
057                    String host = (String)argsMap.get("host");
058                    Boolean gzipData = (Boolean)argsMap.get("gzip");
059                    Boolean shortData = (Boolean)argsMap.get("short");
060    
061                    DatagramHandler handler = new MulticastDatagramHandler(
062                            gzipData.booleanValue(), shortData.booleanValue());
063    
064                    MulticastTransport transport = new MulticastTransport(
065                            handler, host, port);
066    
067                    if (shortData.booleanValue()) {
068                            System.out.println("Truncating to 96 bytes.");
069                    }
070    
071                    System.out.println("Started up and waiting...");
072    
073                    transport.connect();
074    
075                    synchronized (transport) {
076                            transport.wait();
077                    }
078            }
079    
080            private Map<String, Object> _getArgsMap(String[] args)
081                    throws Exception {
082    
083                    Map<String, Object> argsMap = new HashMap<String, Object>();
084    
085                    for (int i = 0; i < args.length; i++) {
086                            if (args[i].equals("-g")) {
087                                    argsMap.put("gzip", Boolean.TRUE);
088                            }
089                            else if (args[i].equals("-s")) {
090                                    argsMap.put("short", Boolean.TRUE);
091                            }
092                            else if (args[i].equals("-h")) {
093                                    argsMap.put("host", args[i + 1]);
094    
095                                    i++;
096                            }
097                            else if (args[i].equals("-p")) {
098                                    argsMap.put("port", new Integer(args[i + 1]));
099    
100                                    i++;
101                            }
102                    }
103    
104                    if (!argsMap.containsKey("gzip")) {
105                            argsMap.put("gzip", Boolean.FALSE);
106                    }
107    
108                    if (!argsMap.containsKey("short")) {
109                            argsMap.put("short", Boolean.FALSE);
110                    }
111    
112                    return argsMap;
113            }
114    
115    }