KickJava   Java API By Example, From Geeks To Geeks.

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


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.Win32NetBIOS;
22 import org.alfresco.filesys.smb.server.win32.Win32NetBIOSSessionSocketHandler;
23
24 /**
25  * <p>
26  * The host announcer class periodically broadcasts a host announcement datagram to inform other
27  * Windows networking hosts of the local hosts existence and capabilities.
28  * <p>
29  * The Win32 NetBIOS host announcer sends out the announcements using datagrams sent via the Win32
30  * Netbios() Netapi32 call.
31  */

32 public class Win32NetBIOSHostAnnouncer extends HostAnnouncer
33 {
34
35     // Associated session handler
36

37     Win32NetBIOSSessionSocketHandler m_handler;
38
39     /**
40      * Create a host announcer.
41      *
42      * @param sessHandler Win32NetBIOSSessionSocketHandler
43      * @param domain Domain name to announce to
44      * @param intval Announcement interval, in minutes
45      */

46     public Win32NetBIOSHostAnnouncer(Win32NetBIOSSessionSocketHandler handler, String JavaDoc domain, int intval)
47     {
48
49         // Save the handler
50

51         m_handler = handler;
52
53         // Add the host to the list of names to announce
54

55         addHostName(handler.getServerName());
56         setDomain(domain);
57         setInterval(intval);
58     }
59
60     /**
61      * Return the LANA
62      *
63      * @return int
64      */

65     public final int getLana()
66     {
67         return m_handler.getLANANumber();
68     }
69
70     /**
71      * Return the host name NetBIOS number
72      *
73      * @return int
74      */

75     public final int getNameNumber()
76     {
77         return m_handler.getNameNumber();
78     }
79
80     /**
81      * Initialize the host announcer.
82      *
83      * @exception Exception
84      */

85     protected void initialize() throws Exception JavaDoc
86     {
87
88         // Set the thread name
89

90         setName("Win32HostAnnouncer_L" + getLana());
91     }
92
93     /**
94      * Determine if the network connection used for the host announcement is valid
95      *
96      * @return boolean
97      */

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

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

116         NetBIOSName destNbName = new NetBIOSName(getDomain(), NetBIOSName.MasterBrowser, false);
117         byte[] destName = destNbName.getNetBIOSName();
118
119         // Send the host announce datagram via the Win32 Netbios() API call
120

121         int sts = Win32NetBIOS.SendDatagram(getLana(), getNameNumber(), destName, buf, 0, len);
122         if ( sts != NetBIOS.NRC_GoodRet)
123             logger.debug("Win32NetBIOS host announce error " + NetBIOS.getErrorString( -sts));
124     }
125 }
Popular Tags