1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.transport;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   * <a href="MulticastClientTool.java.html"><b><i>View Source</i></b></a>
24   *
25   * <p>
26   * A client that listens for multicast messages at a designated port. You may
27   * use this to for potential multicast issues when tuning distributed caches.
28   * </p>
29   *
30   * @author Michael C. Han
31   * @author Raymond Augé
32   */
33  public class MulticastClientTool {
34  
35      public static void main(String[] args) {
36          try {
37              new MulticastClientTool(args);
38          }
39          catch (Exception e) {
40              e.printStackTrace();
41  
42              StringBundler sb = new StringBundler(4);
43  
44              sb.append("Usage: java -classpath ");
45              sb.append("commons-logging.jar:util-java.jar ");
46              sb.append("com.liferay.util.transport.MulticastClientTool [-g] ");
47              sb.append("[-s] -h [multicastAddress] -p [port]");
48  
49              System.err.println(sb.toString());
50  
51              System.exit(1);
52          }
53      }
54  
55      private MulticastClientTool(String[] args) throws Exception {
56          Map<String, Object> argsMap = _getArgsMap(args);
57  
58          Integer port = (Integer)argsMap.get("port");
59          String host = (String)argsMap.get("host");
60          Boolean gzipData = (Boolean)argsMap.get("gzip");
61          Boolean shortData = (Boolean)argsMap.get("short");
62  
63          DatagramHandler handler = new MulticastDatagramHandler(
64              gzipData.booleanValue(), shortData.booleanValue());
65  
66          MulticastTransport transport = new MulticastTransport(
67              handler, host, port);
68  
69          if (shortData.booleanValue()) {
70              System.out.println("Truncating to 96 bytes.");
71          }
72  
73          System.out.println("Started up and waiting...");
74  
75          transport.connect();
76  
77          synchronized (transport) {
78              transport.wait();
79          }
80      }
81  
82      private Map<String, Object> _getArgsMap(String[] args)
83          throws Exception {
84  
85          Map<String, Object> argsMap = new HashMap<String, Object>();
86  
87          for (int i = 0; i < args.length; i++) {
88              if (args[i].equals("-g")) {
89                  argsMap.put("gzip", Boolean.TRUE);
90              }
91              else if (args[i].equals("-s")) {
92                  argsMap.put("short", Boolean.TRUE);
93              }
94              else if (args[i].equals("-h")) {
95                  argsMap.put("host", args[i + 1]);
96  
97                  i++;
98              }
99              else if (args[i].equals("-p")) {
100                 argsMap.put("port", new Integer(args[i + 1]));
101 
102                 i++;
103             }
104         }
105 
106         if (!argsMap.containsKey("gzip")) {
107             argsMap.put("gzip", Boolean.FALSE);
108         }
109 
110         if (!argsMap.containsKey("short")) {
111             argsMap.put("short", Boolean.FALSE);
112         }
113 
114         return argsMap;
115     }
116 
117 }