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