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 java.net.DatagramPacket;
018    import java.net.InetAddress;
019    
020    /**
021     * <p>
022     * A server that will send out heart beat messages until you kill it. This
023     * enables you to try and debug multicast issues.
024     * </p>
025     *
026     * @author Michael C. Han
027     */
028    public class MulticastServerTool {
029    
030            public static void main(String[] args) {
031                    try {
032                            int port = Integer.parseInt(args[1]);
033                            long interval = Long.parseLong(args[2]);
034    
035                            DatagramHandler handler = new DatagramHandler() {
036    
037                                    public void process(DatagramPacket packet) {
038                                            String s = new String(
039                                                    packet.getData(), 0, packet.getLength());
040    
041                                            System.out.println(s);
042                                    }
043    
044                                    public void errorReceived(Throwable t) {
045                                            t.printStackTrace();
046                                    }
047    
048                            };
049    
050                            MulticastTransport transport = new MulticastTransport(
051                                    handler, args[0], port);
052    
053                            transport.connect();
054    
055                            String msg =
056                                    InetAddress.getLocalHost().getHostName() + ":" + port +
057                                            " heartbeat " ;
058    
059                            int i = 0;
060    
061                            while (true) {
062                                    transport.sendMessage(msg + i);
063    
064                                    i++;
065    
066                                    Thread.sleep(interval);
067                            }
068                    }
069                    catch (Exception e) {
070                            e.printStackTrace();
071    
072                            System.err.println(
073                                    "Usage: java MulticastServerTool multicastAddress port " +
074                                            "interval");
075    
076                            System.exit(1);
077                    }
078            }
079    
080    }