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 java.net.DatagramPacket;
18  import java.net.InetAddress;
19  
20  /**
21   * <a href="MulticastServerTool.java.html"><b><i>View Source</i></b></a>
22   *
23   * <p>
24   * A server that will send out heart beat messages until you kill it. This
25   * enables you to try and debug multicast issues.
26   * </p>
27   *
28   * @author Michael C. Han
29   */
30  public class MulticastServerTool {
31  
32      public static void main(String[] args) {
33          try {
34              int port = Integer.parseInt(args[1]);
35              long interval = Long.parseLong(args[2]);
36  
37              DatagramHandler handler = new DatagramHandler() {
38  
39                  public void process(DatagramPacket packet) {
40                      String s = new String(
41                          packet.getData(), 0, packet.getLength());
42  
43                      System.out.println(s);
44                  }
45  
46                  public void errorReceived(Throwable t) {
47                      t.printStackTrace();
48                  }
49  
50              };
51  
52              MulticastTransport transport = new MulticastTransport(
53                  handler, args[0], port);
54  
55              transport.connect();
56  
57              String msg =
58                  InetAddress.getLocalHost().getHostName() + ":" + port +
59                      " heartbeat " ;
60  
61              int i = 0;
62  
63              while (true) {
64                  transport.sendMessage(msg + i);
65  
66                  i++;
67  
68                  Thread.sleep(interval);
69              }
70          }
71          catch (Exception e) {
72              e.printStackTrace();
73  
74              System.err.println(
75                  "Usage: java MulticastServerTool multicastAddress port " +
76                      "interval");
77  
78              System.exit(1);
79          }
80      }
81  
82  }