1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.transport;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * <a href="MulticastClientTool.java.html"><b><i>View Source</i></b></a>
33   *
34   * <p>
35   * A client that listens for multicast messages at a designated port. You may
36   * use this to for potential multicast issues when tuning distributed caches.
37   * </p>
38   *
39   * @author Michael C. Han
40   * @author Raymond Augé
41   *
42   */
43  public class MulticastClientTool {
44  
45      public static void main(String[] args) {
46          try {
47              new MulticastClientTool(args);
48          }
49          catch (Exception e) {
50              _log.error(e);
51  
52              StringBuilder sb = new StringBuilder();
53  
54              sb.append("Usage: java -classpath ");
55              sb.append("commons-logging.jar:util-java.jar ");
56              sb.append("com.liferay.util.transport.MulticastClientTool [-g] ");
57              sb.append("[-s] -h [multicastAddress] -p [port]");
58  
59              System.err.println(sb.toString());
60  
61              System.exit(1);
62          }
63      }
64  
65      private MulticastClientTool(String[] args) throws Exception {
66          Map<String, Object> argsMap = _getArgsMap(args);
67  
68          Integer port = (Integer)argsMap.get("port");
69          String host = (String)argsMap.get("host");
70          Boolean gzipData = (Boolean)argsMap.get("gzip");
71          Boolean shortData = (Boolean)argsMap.get("short");
72  
73          DatagramHandler handler = new MulticastDatagramHandler(
74              gzipData.booleanValue(), shortData.booleanValue());
75  
76          MulticastTransport transport = new MulticastTransport(
77              handler, host, port);
78  
79          if (_log.isInfoEnabled()) {
80              if (shortData.booleanValue()) {
81                  _log.info("Truncating to 96 bytes.");
82              }
83  
84              _log.info("Started up and waiting...");
85          }
86  
87          transport.connect();
88  
89          synchronized (transport) {
90              transport.wait();
91          }
92      }
93  
94      private Map<String, Object> _getArgsMap(String[] args)
95          throws Exception {
96  
97          Map<String, Object> argsMap = new HashMap<String, Object>();
98  
99          for (int i = 0; i < args.length; i++) {
100             if (args[i].equals("-g")) {
101                 argsMap.put("gzip", Boolean.TRUE);
102             }
103             else if (args[i].equals("-s")) {
104                 argsMap.put("short", Boolean.TRUE);
105             }
106             else if (args[i].equals("-h")) {
107                 argsMap.put("host", args[i + 1]);
108 
109                 i++;
110             }
111             else if (args[i].equals("-p")) {
112                 argsMap.put("port", new Integer(args[i + 1]));
113 
114                 i++;
115             }
116         }
117 
118         if (!argsMap.containsKey("gzip")) {
119             argsMap.put("gzip", Boolean.FALSE);
120         }
121 
122         if (!argsMap.containsKey("short")) {
123             argsMap.put("short", Boolean.FALSE);
124         }
125 
126         return argsMap;
127     }
128 
129     private static final Log _log = LogFactoryUtil.getLog(
130         MulticastClientTool.class);
131 
132 }