MulticastDNSService.java 4.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/**
 * $RCSfile$
 * $Revision: 1379 $
 * $Date: 2005-05-23 15:38:09 -0300 (Mon, 23 May 2005) $
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.wildfire.net;

import org.jivesoftware.util.Log;
15 16 17
import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener;
import org.jivesoftware.util.TaskEngine;
18 19 20 21
import org.jivesoftware.wildfire.ServerPort;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.XMPPServerInfo;
import org.jivesoftware.wildfire.container.BasicModule;
22 23 24 25

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import java.io.IOException;
26 27
import java.util.Map;
import java.util.TimerTask;
28 29

/**
30
 * Publishes Wildfire information as a service using the Multicast DNS (marketed by Apple
31 32 33
 * as Rendezvous) protocol. This lets other nodes on the local network to discover
 * the name and port of Wildfire.<p>
 *
34 35 36 37
 * The multicast DNS entries published:<ul>
 *  <li>Client connections: type of "_xmpp-client._tcp.local.".
 *  <li>Component connections: type of "_xmpp-component._tcp.local.".
 * </ul>
38 39 40 41 42 43 44 45 46
 *
 * @author Matt Tucker
 */
public class MulticastDNSService extends BasicModule {

    private JmDNS jmdns;

    public MulticastDNSService() {
        super("Multicast DNS Service");
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

        PropertyEventDispatcher.addListener(new PropertyEventListener() {

            public void propertySet(String property, Map params) {
                // Restart the service if component settings changes.
                if (property.equals("xmpp.component.socket.active") ||
                        property.equals(" xmpp.component.socket.port"))
                {
                    stop();
                    start();
                }
            }

            public void propertyDeleted(String property, Map params) {
                // Restart the service if component settings changes.
                if (property.equals("xmpp.component.socket.active") ||
                        property.equals(" xmpp.component.socket.port"))
                {
                    stop();
                    start();
                }
            }

            public void xmlPropertySet(String property, Map params) {
            }

            public void xmlPropertyDeleted(String property, Map params) {
            }
        });
76 77 78 79 80 81 82
    }

    public void initialize(XMPPServer server) {
       
    }

    public void start() throws IllegalStateException {
83 84 85 86 87
        TimerTask startService = new TimerTask() {
            public void run() {
                XMPPServerInfo info = XMPPServer.getInstance().getServerInfo();
                int clientPortNum = -1;
                int componentPortNum = -1;
88
                for (ServerPort port : info.getServerPorts()) {
89 90 91 92 93 94 95 96 97 98
                    if (port.isClientPort()) {
                        clientPortNum = port.getPort();
                    }
                    else if (port.isComponentPort()) {
                        componentPortNum = port.getPort();
                    }
                }
                try {
                    if (jmdns == null) {
                        jmdns = new JmDNS();
99
                    }
100 101 102 103 104 105
                    String serverName = XMPPServer.getInstance().getServerInfo().getName();

                    if (clientPortNum != -1) {
                        ServiceInfo clientService = new ServiceInfo("_xmpp-client._tcp.local.",
                                serverName + "._xmpp-client._tcp.local.", clientPortNum, "XMPP Server");
                        jmdns.registerService(clientService);
106
                    }
107 108 109 110
                    if (componentPortNum != -1) {
                        ServiceInfo componentService = new ServiceInfo("_xmpp-component._tcp.local.",
                                serverName +  "._xmpp-component._tcp.local.", componentPortNum, "XMPP Component Server");
                        jmdns.registerService(componentService);
111 112
                    }
                }
113 114 115 116 117 118 119
                 catch (IOException ioe) {
                    Log.error(ioe);
                }
            }
        };
        // Schedule the task to run in 5 seconds, to give Wildire time to start the ports. 
        TaskEngine.getInstance().schedule(startService, 5000);
120 121 122 123 124 125 126 127
    }


    public void stop() {
        if (jmdns != null) {
            try {
                jmdns.close();
            }
128 129 130
            catch (Exception e) {
                // Ignore.
            }
131 132 133 134 135 136 137 138 139
        }
    }

    public void destroy() {
        if (jmdns != null) {
            jmdns = null;
        }
    }
}