MulticastDNSService.java 2.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * 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.
 */

12
package org.jivesoftware.messenger.net;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.XMPPServerInfo;
import org.jivesoftware.messenger.ServerPort;
import org.jivesoftware.util.Log;
import org.jivesoftware.admin.AdminConsole;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;
import java.io.IOException;
import java.util.Iterator;

/**
 * Publishes Jive Messenger as a service using the Multicast DNS (marketed by Apple
 * as Rendezvous) protocol. This lets other nodes on the local network to discover
 * the name and port of Jive Messenger.<p>
 *
Matt Tucker's avatar
Matt Tucker committed
31
 * The multicast DNS entry is published with a type of "_xmpp-client._tcp.local.".
32 33 34 35 36 37 38 39 40 41 42 43 44
 *
 * @author Matt Tucker
 */
public class MulticastDNSService extends BasicModule {

    private JmDNS jmdns;
    private ServiceInfo serviceInfo;

    public MulticastDNSService() {
        super("Multicast DNS Service");
    }

    public void initialize(XMPPServer server) {
45
       
46 47 48 49 50 51 52 53 54 55 56
    }

    public void start() throws IllegalStateException {
        if (jmdns != null) {
            Runnable startService = new Runnable() {
                public void run() {
                    XMPPServerInfo info = XMPPServer.getInstance().getServerInfo();
                    Iterator ports = info.getServerPorts();
                    int portNum = -1;
                    while (ports.hasNext()) {
                        ServerPort port = (ServerPort)ports.next();
57
                        if (port.isClientPort() && !port.isSecure()) {
58 59 60 61
                            portNum = port.getPort();
                        }
                    }
                    try {
62 63 64
                        if (jmdns == null) {
                            jmdns = new JmDNS();
                        }
65 66
                        if (portNum != -1) {
                            String serverName = AdminConsole.getAppName();
67
                            serviceInfo = new ServiceInfo("_xmpp-client._tcp.local.",
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                                    serverName, portNum, 0, 0, "XMPP Server");
                            jmdns.registerService(serviceInfo);
                        }
                    }
                     catch (IOException ioe) {
                        Log.error(ioe);
                    }
                }
            };
            new Thread(startService).start();
        }
    }


    public void stop() {
        if (jmdns != null) {
            try {
Matt Tucker's avatar
Matt Tucker committed
85
                jmdns.close();
86 87 88 89 90 91 92
            }
            catch (Exception e) { }
        }
    }

    public void destroy() {
        if (jmdns != null) {
Matt Tucker's avatar
Matt Tucker committed
93
            jmdns = null;
94 95 96
        }
    }
}