KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > mailslot > TcpipNetBIOSHostAnnouncer


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.mailslot;
18
19 import java.net.InetAddress JavaDoc;
20 import java.net.UnknownHostException JavaDoc;
21
22 import org.alfresco.filesys.netbios.NetBIOSDatagram;
23 import org.alfresco.filesys.netbios.NetBIOSDatagramSocket;
24 import org.alfresco.filesys.netbios.NetBIOSName;
25 import org.alfresco.filesys.netbios.NetworkSettings;
26 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol;
27
28 /**
29  * <p>
30  * TCP/IP NetBIOS host announcer implementation. Periodically broadcasts a host announcement
31  * datagram to inform other Windows networking hosts of the local hosts existence and capabilities.
32  */

33 public class TcpipNetBIOSHostAnnouncer extends HostAnnouncer
34 {
35
36     // Default port and announcement interval
37

38     public static final int PORT = RFCNetBIOSProtocol.DATAGRAM;
39     public static final int INTERVAL = 1; // minutes
40

41     // Local address to bind to, port to use
42

43     private InetAddress JavaDoc m_bindAddress;
44     private int m_port;
45
46     // Broadcast address and port
47

48     private InetAddress JavaDoc m_bcastAddr;
49     private int m_bcastPort = RFCNetBIOSProtocol.DATAGRAM;
50
51     // NetBIOS datagram
52

53     private NetBIOSDatagram m_nbdgram;
54
55     /**
56      * Default constructor.
57      */

58     public TcpipNetBIOSHostAnnouncer()
59     {
60
61         // Set the default port and interval
62

63         setPort(PORT);
64         setInterval(INTERVAL);
65     }
66
67     /**
68      * Create a host announcer.
69      *
70      * @param name Host name to announce
71      * @param domain Domain name to announce to
72      * @param intval Announcement interval, in minutes
73      * @param port Port to use
74      */

75     public TcpipNetBIOSHostAnnouncer(String JavaDoc name, String JavaDoc domain, int intval, int port)
76     {
77
78         // Add the host to the list of names to announce
79

80         addHostName(name);
81         setDomain(domain);
82         setInterval(intval);
83
84         // If port is zero then use the default port
85

86         if (port == 0)
87             setPort(PORT);
88         else
89             setPort(port);
90     }
91
92     /**
93      * Get the local address that the announcer should bind to.
94      *
95      * @return java.net.InetAddress
96      */

97     public final InetAddress JavaDoc getBindAddress()
98     {
99         return m_bindAddress;
100     }
101
102     /**
103      * Return the socket/port number that the announcer is using.
104      *
105      * @return int
106      */

107     public final int getPort()
108     {
109         return m_port;
110     }
111
112     /**
113      * Check if the announcer should bind to a particular local address, or all local addresses.
114      *
115      * @return boolean
116      */

117     public final boolean hasBindAddress()
118     {
119         return m_bindAddress != null ? true : false;
120     }
121
122     /**
123      * Set the broadcast address
124      *
125      * @param addr String
126      * @exception UnknownHostException
127      */

128     public final void setBroadcastAddress(String JavaDoc addr) throws UnknownHostException JavaDoc
129     {
130         m_bcastAddr = InetAddress.getByName(addr);
131     }
132
133     /**
134      * Set the broadcast address and port
135      *
136      * @param addr String
137      * @param int port
138      * @exception UnknownHostException
139      */

140     public final void setBroadcastAddress(String JavaDoc addr, int port) throws UnknownHostException JavaDoc
141     {
142         m_bcastAddr = InetAddress.getByName(addr);
143         m_bcastPort = port;
144     }
145
146     /**
147      * Initialize the host announcer.
148      *
149      * @exception Exception
150      */

151     protected void initialize() throws Exception JavaDoc
152     {
153
154         // Set this thread to be a daemon, set the thread name
155

156         if (hasBindAddress() == false)
157             setName("TCPHostAnnouncer");
158         else
159             setName("TCPHostAnnouncer_" + getBindAddress().getHostAddress());
160
161         // Check if at least one host name has been set, if not then use the local host name
162

163         if (numberOfNames() == 0)
164         {
165
166             // Get the local host name
167

168             addHostName(InetAddress.getLocalHost().getHostName());
169         }
170
171         // Allocate the NetBIOS datagram
172

173         m_nbdgram = new NetBIOSDatagram(512);
174
175         // If the broadcast address has not been set, generate a broadcast address
176

177         if (m_bcastAddr == null)
178             m_bcastAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(null));
179     }
180
181     /**
182      * Determine if the network connection used for the host announcement is valid
183      *
184      * @return boolean
185      */

186     public boolean isNetworkEnabled()
187     {
188         return true;
189     }
190
191     /**
192      * Send an announcement broadcast.
193      *
194      * @param hostName Host name being announced
195      * @param buf Buffer containing the host announcement mailslot message.
196      * @param offset Offset to the start of the host announcement message.
197      * @param len Host announcement message length.
198      */

199     protected void sendAnnouncement(String JavaDoc hostName, byte[] buf, int offset, int len) throws Exception JavaDoc
200     {
201
202         // Send the host announce datagram
203

204         m_nbdgram.SendDatagram(NetBIOSDatagram.DIRECT_GROUP, hostName, NetBIOSName.FileServer, getDomain(),
205                 NetBIOSName.MasterBrowser, buf, len, offset);
206     }
207
208     /**
209      * Set the local address to bind to.
210      *
211      * @param addr java.net.InetAddress
212      */

213     public final void setBindAddress(InetAddress JavaDoc addr)
214     {
215         m_bindAddress = addr;
216         NetBIOSDatagramSocket.setBindAddress(addr);
217     }
218
219     /**
220      * Set the socket/port number to use.
221      *
222      * @param port int
223      */

224     public final void setPort(int port)
225     {
226         m_port = port;
227         NetBIOSDatagramSocket.setDefaultPort(port);
228     }
229 }
Popular Tags