1 17 package org.alfresco.filesys.smb.server.win32; 18 19 import java.util.BitSet ; 20 21 import org.alfresco.filesys.netbios.win32.NetBIOSSocket; 22 import org.alfresco.filesys.netbios.win32.Win32NetBIOS; 23 import org.alfresco.filesys.netbios.win32.WinsockNetBIOSException; 24 import org.alfresco.filesys.server.config.ServerConfiguration; 25 import org.alfresco.filesys.smb.mailslot.Win32NetBIOSHostAnnouncer; 26 import org.alfresco.filesys.smb.server.SMBServer; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 36 public class Win32NetBIOSLanaMonitor extends Thread 37 { 38 42 private static final int LanaListenerArraySize = 16; 43 44 46 private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol"); 47 48 50 private static Win32NetBIOSLanaMonitor _lanaMonitor; 51 52 54 private BitSet m_lanas; 55 private BitSet m_lanaSts; 56 57 59 private LanaListener[] m_listeners; 60 61 63 private SMBServer m_server; 64 65 67 private long m_wakeup; 68 69 71 private boolean m_shutdown; 72 73 75 private boolean m_debug; 76 77 85 Win32NetBIOSLanaMonitor(SMBServer server, int[] lanas, long wakeup, boolean debug) 86 { 87 88 90 m_server = server; 91 m_wakeup = wakeup; 92 93 m_debug = debug; 94 95 97 m_lanas = new BitSet (); 98 m_lanaSts = new BitSet (); 99 100 if (lanas != null) 101 { 102 103 105 for (int i = 0; i < lanas.length; i++) 106 m_lanas.set(lanas[i]); 107 } 108 109 111 int[] curLanas = Win32NetBIOS.LanaEnumerate(); 112 113 if ( curLanas != null) 114 { 115 for ( int i = 0; i < curLanas.length; i++) 116 m_lanaSts.set(curLanas[i], true); 117 } 118 119 121 if (_lanaMonitor == null) 122 _lanaMonitor = this; 123 124 126 setDaemon(true); 127 start(); 128 } 129 130 135 public static Win32NetBIOSLanaMonitor getLanaMonitor() 136 { 137 return _lanaMonitor; 138 } 139 140 146 public synchronized final void addLanaListener(int lana, LanaListener l) 147 { 148 150 if ( lana < 0 || lana > 255) 151 return; 152 153 155 if ( m_listeners == null) 156 { 157 int len = LanaListenerArraySize; 158 if ( lana > len) 159 len = (lana + 3) & 0x00FC; 160 161 m_listeners = new LanaListener[len]; 162 } 163 else if ( lana > m_listeners.length) 164 { 165 167 LanaListener[] newArray = new LanaListener[(lana + 3) & 0x00FC]; 168 169 171 System.arraycopy(m_listeners, 0, newArray, 0, m_listeners.length); 172 m_listeners = newArray; 173 } 174 175 177 m_listeners[lana] = l; 178 179 181 if (logger.isDebugEnabled() && hasDebug()) 182 logger.debug("[SMB] Win32 NetBIOS register listener for LANA " + lana); 183 } 184 185 190 public synchronized final void removeLanaListener(int lana) 191 { 192 194 if ( m_listeners == null || lana < 0 || lana >= m_listeners.length) 195 return; 196 197 m_listeners[lana] = null; 198 } 199 200 203 public void run() 204 { 205 207 m_shutdown = false; 208 209 211 ServerConfiguration config = m_server.getConfiguration(); 212 213 if ( config.useWinsockNetBIOS() == false) 214 { 215 try 216 { 217 NetBIOSSocket.initializeSockets(); 218 } 219 catch (WinsockNetBIOSException ex) 220 { 221 223 if (logger.isDebugEnabled() && hasDebug()) 224 logger.debug("[SMB] Win32 NetBIOS initialization error", ex); 225 226 228 m_shutdown = true; 229 } 230 } 231 232 234 BitSet curLanas = new BitSet (); 235 236 while (m_shutdown == false) 237 { 238 239 241 Win32NetBIOS.waitForNetworkAddressChange(); 242 243 245 if ( m_shutdown == true) 246 continue; 247 248 250 curLanas.clear(); 251 252 254 int[] lanas = Win32NetBIOS.LanaEnumerate(); 255 if (lanas != null) 256 { 257 258 260 Win32NetBIOSSessionSocketHandler sessHandler = null; 261 262 for (int i = 0; i < lanas.length; i++) 263 { 264 265 267 int lana = lanas[i]; 268 curLanas.set(lana, true); 269 270 if (m_lanas.get(lana) == false) 271 { 272 273 275 if (logger.isDebugEnabled() && hasDebug()) 276 logger.debug("[SMB] Win32 NetBIOS found new LANA, " + lana); 277 278 280 sessHandler = new Win32NetBIOSSessionSocketHandler(m_server, lana, hasDebug()); 281 282 try 283 { 284 sessHandler.initialize(); 285 } 286 catch (Exception ex) 287 { 288 289 291 if (logger.isDebugEnabled() && hasDebug()) 292 logger.debug("[SMB] Win32 NetBIOS failed to create session handler for LANA " + lana, 293 ex); 294 295 297 sessHandler = null; 298 } 299 300 303 if (sessHandler != null) 304 { 305 306 308 m_server.addSessionHandler(sessHandler); 309 310 312 Thread nbThread = new Thread (sessHandler); 313 nbThread.setName("Win32NB_Handler_" + lana); 314 nbThread.start(); 315 316 318 if (logger.isDebugEnabled() && hasDebug()) 319 logger.debug("[SMB] Win32 NetBIOS created session handler on LANA " + lana); 320 321 323 if (config.hasWin32EnableAnnouncer()) 324 { 325 326 328 Win32NetBIOSHostAnnouncer hostAnnouncer = new Win32NetBIOSHostAnnouncer(sessHandler, 329 config.getDomainName(), config.getWin32HostAnnounceInterval()); 330 331 333 m_server.addHostAnnouncer(hostAnnouncer); 334 hostAnnouncer.start(); 335 336 338 if (logger.isDebugEnabled() && hasDebug()) 339 logger.debug("[SMB] Win32 NetBIOS host announcer enabled on LANA " + lana); 340 } 341 342 344 m_lanas.set(lana); 345 m_lanaSts.set(lana, true); 346 } 347 } 348 else 349 { 350 352 if ( m_lanaSts.get(lana) == false) 353 { 354 356 m_lanaSts.set(lana, true); 357 358 360 if ( m_listeners != null && lana < m_listeners.length && 361 m_listeners[lana] != null) 362 m_listeners[lana].lanaStatusChange(lana, true); 363 364 366 if (logger.isDebugEnabled() && hasDebug()) 367 logger.debug("[SMB] Win32 NetBIOS LANA online - " + lana); 368 } 369 } 370 } 371 372 374 for ( int i = 0; i < m_lanaSts.length(); i++) 375 { 376 if ( curLanas.get(i) == false && m_lanaSts.get(i) == true) 377 { 378 380 if (logger.isDebugEnabled() && hasDebug()) 381 logger.debug("[SMB] Win32 NetBIOS LANA offline - " + i); 382 383 385 m_lanaSts.set(i, false); 386 387 389 if ( m_listeners != null && m_listeners[i] != null) 390 { 391 393 m_listeners[i].lanaStatusChange(i, false); 394 } 395 } 396 } 397 } 398 } 399 } 400 401 406 public final boolean hasDebug() 407 { 408 return m_debug; 409 } 410 411 414 public final void shutdownRequest() 415 { 416 m_shutdown = true; 417 418 420 if ( m_server.getConfiguration().useWinsockNetBIOS()) 421 NetBIOSSocket.shutdownSockets(); 422 } 423 } 424 | Popular Tags |