KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.alfresco.filesys.netbios.NetBIOSName;
20 import org.alfresco.filesys.netbios.win32.NetBIOS;
21 import org.alfresco.filesys.netbios.win32.NetBIOSSocket;
22 import org.alfresco.filesys.netbios.win32.Win32NetBIOS;
23 import org.alfresco.filesys.smb.server.win32.Win32NetBIOSSessionSocketHandler;
24
25 /**
26  * Winsock NetBIOS Host Announcer Class
27  *
28  * <p>
29  * The host announcer class periodically broadcasts a host announcement datagram to inform other
30  * Windows networking hosts of the local hosts existence and capabilities.
31  *
32  * <p>
33  * The Win32 NetBIOS host announcer sends out the announcements using datagrams sent via Winsock calls.
34  */

35 public class WinsockNetBIOSHostAnnouncer extends HostAnnouncer
36 {
37     // Associated session handler
38

39     private Win32NetBIOSSessionSocketHandler m_handler;
40
41     // Winsock NetBIOS datagram socket
42

43     private NetBIOSSocket m_dgramSocket;
44     
45     /**
46      * Create a host announcer.
47      *
48      * @param sessHandler Win32NetBIOSSessionSocketHandler
49      * @param domain Domain name to announce to
50      * @param intval Announcement interval, in minutes
51      */

52     public WinsockNetBIOSHostAnnouncer(Win32NetBIOSSessionSocketHandler handler, String JavaDoc domain, int intval)
53     {
54
55         // Save the handler
56

57         m_handler = handler;
58
59         // Add the host to the list of names to announce
60

61         addHostName(handler.getServerName());
62         setDomain(domain);
63         setInterval(intval);
64     }
65
66     /**
67      * Return the LANA
68      *
69      * @return int
70      */

71     public final int getLana()
72     {
73         return m_handler.getLANANumber();
74     }
75
76     /**
77      * Initialize the host announcer.
78      *
79      * @exception Exception
80      */

81     protected void initialize() throws Exception JavaDoc
82     {
83         // Set the thread name
84

85         setName("WinsockHostAnnouncer_L" + getLana());
86         
87         // Create the Winsock NetBIOS datagram socket
88

89         m_dgramSocket = NetBIOSSocket.createDatagramSocket(getLana());
90     }
91
92     /**
93      * Determine if the network connection used for the host announcement is valid
94      *
95      * @return boolean
96      */

97     public boolean isNetworkEnabled()
98     {
99         return m_handler.isLANAValid();
100     }
101
102     /**
103      * Send an announcement broadcast.
104      *
105      * @param hostName Host name being announced
106      * @param buf Buffer containing the host announcement mailslot message.
107      * @param offset Offset to the start of the host announcement message.
108      * @param len Host announcement message length.
109      */

110     protected void sendAnnouncement(String JavaDoc hostName, byte[] buf, int offset, int len) throws Exception JavaDoc
111     {
112
113         // Build the destination NetBIOS name using the domain/workgroup name
114

115         NetBIOSName destNbName = new NetBIOSName(getDomain(), NetBIOSName.MasterBrowser, false);
116
117         // Send the host announce datagram via the Win32 Netbios() API call
118

119         int sts = m_dgramSocket.sendDatagram(destNbName, buf, 0, len);
120         if ( sts != len)
121             logger.debug("WinsockNetBIOS host announce error");
122     }
123 }
Popular Tags