KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > net > MulticastDNSService


1 /**
2  * $RCSfile: MulticastDNSService.java,v $
3  * $Revision: 1.5 $
4  * $Date: 2005/05/23 18:38:09 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.net;
13
14 import org.jivesoftware.messenger.container.BasicModule;
15 import org.jivesoftware.messenger.XMPPServer;
16 import org.jivesoftware.messenger.XMPPServerInfo;
17 import org.jivesoftware.messenger.ServerPort;
18 import org.jivesoftware.util.Log;
19 import org.jivesoftware.admin.AdminConsole;
20
21 import javax.jmdns.JmDNS;
22 import javax.jmdns.ServiceInfo;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 /**
27  * Publishes Jive Messenger as a service using the Multicast DNS (marketed by Apple
28  * as Rendezvous) protocol. This lets other nodes on the local network to discover
29  * the name and port of Jive Messenger.<p>
30  *
31  * The multicast DNS entry is published with a type of "_xmpp-client._tcp.local.".
32  *
33  * @author Matt Tucker
34  */

35 public class MulticastDNSService extends BasicModule {
36
37     private JmDNS jmdns;
38     private ServiceInfo serviceInfo;
39
40     public MulticastDNSService() {
41         super("Multicast DNS Service");
42     }
43
44     public void initialize(XMPPServer server) {
45        
46     }
47
48     public void start() throws IllegalStateException JavaDoc {
49         if (jmdns != null) {
50             Runnable JavaDoc startService = new Runnable JavaDoc() {
51                 public void run() {
52                     XMPPServerInfo info = XMPPServer.getInstance().getServerInfo();
53                     Iterator JavaDoc ports = info.getServerPorts();
54                     int portNum = -1;
55                     while (ports.hasNext()) {
56                         ServerPort port = (ServerPort)ports.next();
57                         if (port.isClientPort() && !port.isSecure()) {
58                             portNum = port.getPort();
59                         }
60                     }
61                     try {
62                         if (jmdns == null) {
63                             jmdns = new JmDNS();
64                         }
65                         if (portNum != -1) {
66                             String JavaDoc serverName = AdminConsole.getAppName();
67                             serviceInfo = new ServiceInfo("_xmpp-client._tcp.local.",
68                                     serverName, portNum, 0, 0, "XMPP Server");
69                             jmdns.registerService(serviceInfo);
70                         }
71                     }
72                      catch (IOException JavaDoc ioe) {
73                         Log.error(ioe);
74                     }
75                 }
76             };
77             new Thread JavaDoc(startService).start();
78         }
79     }
80
81
82     public void stop() {
83         if (jmdns != null) {
84             try {
85                 jmdns.close();
86             }
87             catch (Exception JavaDoc e) { }
88         }
89     }
90
91     public void destroy() {
92         if (jmdns != null) {
93             jmdns = null;
94         }
95     }
96 }
Popular Tags