1 17 package org.alfresco.filesys.smb.mailslot; 18 19 import java.io.IOException ; 20 21 import org.alfresco.filesys.netbios.NetBIOSName; 22 import org.alfresco.filesys.netbios.win32.WinsockNetBIOSException; 23 import org.alfresco.filesys.smb.ServerType; 24 import org.alfresco.filesys.smb.TransactionNames; 25 import org.alfresco.filesys.util.StringList; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 34 public abstract class HostAnnouncer extends Thread 35 { 36 37 39 protected static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol.mailslot"); 40 41 43 public static final int SHUTDOWN_WAIT = 2000; public static final int SHUTDOWN_COUNT = 3; 45 46 48 public static final long STARTING_INTERVAL = 5000; 50 52 private StringList m_names; 53 54 56 private String m_domain; 57 58 60 private String m_comment; 61 62 64 private int m_interval; 65 66 68 private int m_srvtype = ServerType.WorkStation + ServerType.Server; 69 70 72 private SMBMailslotPacket m_smbPkt; 73 74 76 private byte m_updateCount; 77 78 80 private boolean m_shutdown = false; 81 82 84 private boolean m_debug; 85 86 89 public HostAnnouncer() 90 { 91 92 94 commonConstructor(); 95 } 96 97 104 public HostAnnouncer(String name, String domain, int intval) 105 { 106 107 109 commonConstructor(); 110 111 113 addHostName(name); 114 setDomain(domain); 115 setInterval(intval); 116 } 117 118 121 private final void commonConstructor() 122 { 123 124 126 m_names = new StringList(); 127 } 128 129 134 public final String getComment() 135 { 136 return m_comment; 137 } 138 139 144 public final String getDomain() 145 { 146 return m_domain; 147 } 148 149 154 public final int numberOfNames() 155 { 156 return m_names.numberOfStrings(); 157 } 158 159 165 public final String getHostName(int idx) 166 { 167 if (idx < 0 || idx > m_names.numberOfStrings()) 168 return null; 169 return m_names.getStringAt(idx); 170 } 171 172 177 public final int getInterval() 178 { 179 return m_interval; 180 } 181 182 187 public final int getServerType() 188 { 189 return m_srvtype; 190 } 191 192 197 public final boolean hasDebug() 198 { 199 return m_debug; 200 } 201 202 207 public final void setDebug(boolean dbg) 208 { 209 m_debug = dbg; 210 } 211 212 217 protected final void initHostAnnounceSMB(String name) 218 { 219 220 222 if (m_smbPkt == null) 223 m_smbPkt = new SMBMailslotPacket(); 224 225 227 byte[] data = new byte[256]; 228 int pos = MailSlot.createHostAnnouncement(data, 0, name, m_comment, m_srvtype, m_interval, m_updateCount++); 229 230 232 m_smbPkt.initializeMailslotSMB(TransactionNames.MailslotBrowse, data, pos); 233 } 234 235 238 public void run() 239 { 240 241 243 try 244 { 245 246 248 initialize(); 249 } 250 catch (Exception ex) 251 { 252 253 255 logger.error("HostAnnouncer initialization error", ex); 256 return; 257 } 258 259 261 m_shutdown = false; 262 263 265 long sleepTime = STARTING_INTERVAL; 266 long sleepNormal = getInterval() * 60 * 1000; 267 268 while (m_shutdown == false) 269 { 270 271 try 272 { 273 274 276 if (isNetworkEnabled()) 277 { 278 279 281 for (int i = 0; i < m_names.numberOfStrings(); i++) 282 { 283 284 286 String hostName = getHostName(i); 287 initHostAnnounceSMB(hostName); 288 289 291 sendAnnouncement(hostName, m_smbPkt.getBuffer(), 0, m_smbPkt.getLength()); 292 293 295 if (logger.isDebugEnabled() && hasDebug()) 296 logger.debug("HostAnnouncer: Announced host " + hostName); 297 } 298 } 299 else 300 { 301 302 306 sleepTime = STARTING_INTERVAL; 307 } 308 309 311 sleep(sleepTime); 312 313 315 if (isNetworkEnabled() && sleepTime < sleepNormal) 316 { 317 318 322 sleepTime *= 2; 323 if (sleepTime > sleepNormal) 324 sleepTime = sleepNormal; 325 } 326 } 327 catch (WinsockNetBIOSException ex) 328 { 329 331 if (m_shutdown == false) 332 logger.error("HostAnnouncer error", ex); 333 m_shutdown = true; 334 } 335 catch ( IOException ex) 336 { 337 339 if (m_shutdown == false) 340 { 341 logger.error("HostAnnouncer error", ex); 342 logger.error(" Check <broadcast> setting in file-servers.xml"); 343 } 344 m_shutdown = true; 345 } 346 catch (Exception ex) 347 { 348 350 if (m_shutdown == false) 351 logger.error("HostAnnouncer error", ex); 352 m_shutdown = true; 353 } 354 } 355 356 359 setInterval(0); 360 361 363 if ((m_srvtype & ServerType.Server) != 0) 364 m_srvtype -= ServerType.Server; 365 366 369 for (int j = 0; j < SHUTDOWN_COUNT; j++) 370 { 371 372 374 for (int i = 0; i < m_names.numberOfStrings(); i++) 375 { 376 377 379 String hostName = getHostName(i); 380 initHostAnnounceSMB(hostName); 381 382 384 try 385 { 386 387 389 sendAnnouncement(hostName, m_smbPkt.getBuffer(), 0, m_smbPkt.getLength()); 390 } 391 catch (Exception ex) 392 { 393 } 394 } 395 396 398 try 399 { 400 sleep(SHUTDOWN_WAIT); 401 } 402 catch (InterruptedException ex) 403 { 404 } 405 } 406 } 407 408 413 protected void initialize() throws Exception 414 { 415 } 416 417 422 public abstract boolean isNetworkEnabled(); 423 424 432 protected abstract void sendAnnouncement(String hostName, byte[] buf, int offset, int len) throws Exception ; 433 434 439 public final void setComment(String comment) 440 { 441 m_comment = comment; 442 if (m_comment != null && m_comment.length() > 80) 443 m_comment = m_comment.substring(0, 80); 444 } 445 446 451 public final void setDomain(String name) 452 { 453 m_domain = name.toUpperCase(); 454 } 455 456 461 public final void addHostName(String name) 462 { 463 m_names.addString(NetBIOSName.toUpperCaseName(name)); 464 } 465 466 471 public final void addHostNames(StringList names) 472 { 473 m_names.addStrings(names); 474 } 475 476 481 public final void setInterval(int intval) 482 { 483 m_interval = intval; 484 } 485 486 491 public final void setServerType(int typ) 492 { 493 m_srvtype = typ; 494 } 495 496 499 public final synchronized void shutdownAnnouncer() 500 { 501 502 504 m_shutdown = true; 505 interrupt(); 506 507 try 508 { 509 join(2000); 510 } 511 catch (InterruptedException ex) 512 { 513 } 514 } 515 } | Popular Tags |