001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }