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                                    public void process(DatagramPacket packet) {
040                                            String s = new String(
041                                                    packet.getData(), 0, packet.getLength());
042    
043                                            System.out.println(s);
044                                    }
045    
046                                    public void errorReceived(Throwable t) {
047                                            t.printStackTrace();
048                                    }
049    
050                            };
051    
052                            MulticastTransport transport = new MulticastTransport(
053                                    handler, args[0], port);
054    
055                            transport.connect();
056    
057                            String msg =
058                                    InetAddress.getLocalHost().getHostName() + ":" + port +
059                                            " heartbeat " ;
060    
061                            int i = 0;
062    
063                            while (true) {
064                                    transport.sendMessage(msg + i);
065    
066                                    i++;
067    
068                                    Thread.sleep(interval);
069                            }
070                    }
071                    catch (Exception e) {
072                            e.printStackTrace();
073    
074                            System.err.println(
075                                    "Usage: java MulticastServerTool multicastAddress port " +
076                                            "interval");
077    
078                            System.exit(1);
079                    }
080            }
081    
082    }