/* * Copyright (C) 2005 Alfresco, Inc. * * Licensed under the Mozilla Public License version 1.1 * with a permitted attribution clause. You may obtain a * copy of the License at * * http://www.alfresco.org/legal/license.txt * * 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. */ package org.alfresco.filesys.smb.mailslot; import java.io.IOException; import org.alfresco.filesys.netbios.NetBIOSName; import org.alfresco.filesys.netbios.win32.WinsockNetBIOSException; import org.alfresco.filesys.smb.ServerType; import org.alfresco.filesys.smb.TransactionNames; import org.alfresco.filesys.util.StringList; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** *
* The host announcer class periodically broadcasts a host announcement datagram to inform other
* Windows networking hosts of the local hosts existence and capabilities.
*/
public abstract class HostAnnouncer extends Thread
{
// Debug logging
protected static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol.mailslot");
// Shutdown announcement interval and message count
public static final int SHUTDOWN_WAIT = 2000; // 2 seconds
public static final int SHUTDOWN_COUNT = 3;
// Starting announcement interval, doubles until it reaches the configured interval
public static final long STARTING_INTERVAL = 5000; // 5 seconds
// Local host name(s) to announce
private StringList m_names;
// Domain to announce to
private String m_domain;
// Server comment string
private String m_comment;
// Announcement interval in minutes
private int m_interval;
// Server type flags, see org.alfresco.filesys.smb.SMBServerInfo
private int m_srvtype = ServerType.WorkStation + ServerType.Server;
// SMB mailslot packet
private SMBMailslotPacket m_smbPkt;
// Update count for the host announcement packet
private byte m_updateCount;
// Shutdown flag, host announcer should remove the announced name as it shuts down
private boolean m_shutdown = false;
// Debug output enable
private boolean m_debug;
/**
* HostAnnouncer constructor.
*/
public HostAnnouncer()
{
// Common constructor
commonConstructor();
}
/**
* Create a host announcer.
*
* @param name Host name to announce
* @param domain Domain name to announce to
* @param intval Announcement interval, in minutes
*/
public HostAnnouncer(String name, String domain, int intval)
{
// Common constructor
commonConstructor();
// Add the host to the list of names to announce
addHostName(name);
setDomain(domain);
setInterval(intval);
}
/**
* Common constructor code
*/
private final void commonConstructor()
{
// Allocate the host name list
m_names = new StringList();
}
/**
* Return the server comment string.
*
* @return java.lang.String
*/
public final String getComment()
{
return m_comment;
}
/**
* Return the domain name that the host announcement is directed to.
*
* @return java.lang.String
*/
public final String getDomain()
{
return m_domain;
}
/**
* Return the number of names being announced
*
* @return int
*/
public final int numberOfNames()
{
return m_names.numberOfStrings();
}
/**
* Return the specified host name being announced.
*
* @param idx int
* @return java.lang.String
*/
public final String getHostName(int idx)
{
if (idx < 0 || idx > m_names.numberOfStrings())
return null;
return m_names.getStringAt(idx);
}
/**
* Return the announcement interval, in minutes.
*
* @return int
*/
public final int getInterval()
{
return m_interval;
}
/**
* Return the server type flags.
*
* @return int
*/
public final int getServerType()
{
return m_srvtype;
}
/**
* Determine if debug output is enabled
*
* @return boolean
*/
public final boolean hasDebug()
{
return m_debug;
}
/**
* Enable/disable debug output
*
* @param dbg true or false
*/
public final void setDebug(boolean dbg)
{
m_debug = dbg;
}
/**
* Initialize the host announcement SMB.
*
* @param name String
*/
protected final void initHostAnnounceSMB(String name)
{
// Allocate the transact SMB
if (m_smbPkt == null)
m_smbPkt = new SMBMailslotPacket();
// Create the host announcement structure
byte[] data = new byte[256];
int pos = MailSlot.createHostAnnouncement(data, 0, name, m_comment, m_srvtype, m_interval, m_updateCount++);
// Create the mailslot SMB
m_smbPkt.initializeMailslotSMB(TransactionNames.MailslotBrowse, data, pos);
}
/**
* Start the host announcer thread.
*/
public void run()
{
// Initialize the host announcer
try
{
// Initialize the host announcer datagram socket
initialize();
}
catch (Exception ex)
{
// Debug
logger.error("HostAnnouncer initialization error", ex);
return;
}
// Clear the shutdown flag
m_shutdown = false;
// Send the host announcement datagram
long sleepTime = STARTING_INTERVAL;
long sleepNormal = getInterval() * 60 * 1000;
while (m_shutdown == false)
{
try
{
// Check if the network connection is valid
if (isNetworkEnabled())
{
// Loop through the host names to be announced
for (int i = 0; i < m_names.numberOfStrings(); i++)
{
// Create a host announcement transact SMB
String hostName = getHostName(i);
initHostAnnounceSMB(hostName);
// Send the host announce datagram
sendAnnouncement(hostName, m_smbPkt.getBuffer(), 0, m_smbPkt.getLength());
// DEBUG
if (logger.isDebugEnabled() && hasDebug())
logger.debug("HostAnnouncer: Announced host " + hostName);
}
}
else
{
// Reset the sleep interval to the starting interval as the network connection
// is not
// available
sleepTime = STARTING_INTERVAL;
}
// Sleep for a while
sleep(sleepTime);
// Update the sleep interval, if the network connection is enabled
if (isNetworkEnabled() && sleepTime < sleepNormal)
{
// Double the sleep interval until it exceeds the configured announcement
// interval.
// This is to send out more broadcasts when the server first starts.
sleepTime *= 2;
if (sleepTime > sleepNormal)
sleepTime = sleepNormal;
}
}
catch (WinsockNetBIOSException ex)
{
// Debug
if (m_shutdown == false)
logger.error("HostAnnouncer error", ex);
m_shutdown = true;
}
catch ( IOException ex)
{
// Debug
if (m_shutdown == false)
{
logger.error("HostAnnouncer error", ex);
logger.error(" Check