MulticastDNSService.java 5.58 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: 1379 $
 * $Date: 2005-05-23 15:38:09 -0300 (Mon, 23 May 2005) $
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
19 20
 */

21
package org.jivesoftware.openfire.net;
22

23 24 25 26 27 28 29
import java.io.IOException;
import java.util.Map;
import java.util.TimerTask;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;

30 31 32 33
import org.jivesoftware.openfire.ServerPort;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.XMPPServerInfo;
import org.jivesoftware.openfire.container.BasicModule;
34 35 36 37 38 39
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener;
import org.jivesoftware.util.TaskEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
40 41

/**
42
 * Publishes Openfire information as a service using the Multicast DNS (marketed by Apple
43
 * as Rendezvous) protocol. This lets other nodes on the local network to discover
44
 * the name and port of Openfire.<p>
45
 *
46 47 48 49
 * 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>
50 51 52 53 54
 *
 * @author Matt Tucker
 */
public class MulticastDNSService extends BasicModule {

55 56
	private static final Logger Log = LoggerFactory.getLogger(MulticastDNSService.class);

57 58 59 60
    private JmDNS jmdns;

    public MulticastDNSService() {
        super("Multicast DNS Service");
61 62 63

        PropertyEventDispatcher.addListener(new PropertyEventListener() {

64
            public void propertySet(String property, Map params) {
65 66 67 68 69 70 71 72 73
                // Restart the service if component settings changes.
                if (property.equals("xmpp.component.socket.active") ||
                        property.equals(" xmpp.component.socket.port"))
                {
                    stop();
                    start();
                }
            }

74
            public void propertyDeleted(String property, Map params) {
75 76 77 78 79 80 81 82 83
                // Restart the service if component settings changes.
                if (property.equals("xmpp.component.socket.active") ||
                        property.equals(" xmpp.component.socket.port"))
                {
                    stop();
                    start();
                }
            }

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

87
            public void xmlPropertyDeleted(String property, Map params) {
88 89
            }
        });
90 91
    }

92 93
    @Override
	public void initialize(XMPPServer server) {
94 95 96
       
    }

97 98
    @Override
	public void start() throws IllegalStateException {
99 100 101 102
        // If the service isn't enabled, return.
        if (!JiveGlobals.getBooleanProperty("multicastDNS.enabled", false) ) {
            return;     
        }
103
        TimerTask startService = new TimerTask() {
104 105
            @Override
			public void run() {
106 107 108
                XMPPServerInfo info = XMPPServer.getInstance().getServerInfo();
                int clientPortNum = -1;
                int componentPortNum = -1;
109
                for (ServerPort port : info.getServerPorts()) {
110 111 112 113 114 115 116 117 118
                    if (port.isClientPort()) {
                        clientPortNum = port.getPort();
                    }
                    else if (port.isComponentPort()) {
                        componentPortNum = port.getPort();
                    }
                }
                try {
                    if (jmdns == null) {
119
                        jmdns = new JmDNS();
120
                    }
121
                    String serverName = XMPPServer.getInstance().getServerInfo().getXMPPDomain();
122 123

                    if (clientPortNum != -1) {
124
                        ServiceInfo clientService = new ServiceInfo("_xmpp-client._tcp.local.",
125 126
                                serverName + "._xmpp-client._tcp.local.", clientPortNum, "XMPP Server");
                        jmdns.registerService(clientService);
127
                    }
128
                    if (componentPortNum != -1) {
129
                        ServiceInfo componentService = new ServiceInfo("_xmpp-component._tcp.local.",
130 131
                                serverName +  "._xmpp-component._tcp.local.", componentPortNum, "XMPP Component Server");
                        jmdns.registerService(componentService);
132 133
                    }
                }
134
                 catch (IOException ioe) {
135
                    Log.error(ioe.getMessage(), ioe);
136 137 138
                }
            }
        };
139
        // Schedule the task to run in 5 seconds, to give Wildire time to start the ports. 
140
        TaskEngine.getInstance().schedule(startService, 5000);
141 142 143
    }


144 145
    @Override
	public void stop() {
146 147 148 149
        if (jmdns != null) {
            try {
                jmdns.close();
            }
150 151 152
            catch (Exception e) {
                // Ignore.
            }
153 154 155
        }
    }

156 157
    @Override
	public void destroy() {
158 159 160 161 162
        if (jmdns != null) {
            jmdns = null;
        }
    }
}