KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > win32 > Win32NetBIOSLanaMonitor


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.server.win32;
18
19 import java.util.BitSet JavaDoc;
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 /**
31  * Win32 NetBIOS LANA Monitor Class
32  * <p>
33  * Monitors the available NetBIOS LANAs to check for new network interfaces coming online. A session
34  * socket handler will be created for new LANAs as they appear.
35  */

36 public class Win32NetBIOSLanaMonitor extends Thread JavaDoc
37 {
38     // Constants
39
//
40
// Initial LANA listener array size
41

42     private static final int LanaListenerArraySize = 16;
43     
44     // Debug logging
45

46     private static final Log logger = LogFactory.getLog("org.alfresco.smb.protocol");
47
48     // Global LANA monitor
49

50     private static Win32NetBIOSLanaMonitor _lanaMonitor;
51
52     // Available LANA list and current status
53

54     private BitSet JavaDoc m_lanas;
55     private BitSet JavaDoc m_lanaSts;
56
57     // LANA status listeners
58

59     private LanaListener[] m_listeners;
60     
61     // SMB/CIFS server to add new session handlers to
62

63     private SMBServer m_server;
64
65     // Wakeup interval
66

67     private long m_wakeup;
68
69     // Shutdown request flag
70

71     private boolean m_shutdown;
72
73     // Debug output enable
74

75     private boolean m_debug;
76
77     /**
78      * Class constructor
79      *
80      * @param server SMBServer
81      * @param lanas int[]
82      * @param wakeup long
83      * @param debug boolean
84      */

85     Win32NetBIOSLanaMonitor(SMBServer server, int[] lanas, long wakeup, boolean debug)
86     {
87
88         // Set the SMB server and wakeup interval
89

90         m_server = server;
91         m_wakeup = wakeup;
92
93         m_debug = debug;
94
95         // Set the current LANAs in the available LANAs list
96

97         m_lanas = new BitSet JavaDoc();
98         m_lanaSts = new BitSet JavaDoc();
99         
100         if (lanas != null)
101         {
102
103             // Set the currently available LANAs
104

105             for (int i = 0; i < lanas.length; i++)
106                 m_lanas.set(lanas[i]);
107         }
108
109         // Initialize the online LANA status list
110

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         // Set the global LANA monitor, if not already set
120

121         if (_lanaMonitor == null)
122             _lanaMonitor = this;
123
124         // Start the LANA monitor thread
125

126         setDaemon(true);
127         start();
128     }
129
130     /**
131      * Return the global LANA monitor
132      *
133      * @return Win32NetBIOSLanaMonitor
134      */

135     public static Win32NetBIOSLanaMonitor getLanaMonitor()
136     {
137         return _lanaMonitor;
138     }
139
140     /**
141      * Add a LANA listener
142      *
143      * @param lana int
144      * @param listener LanaListener
145      */

146     public synchronized final void addLanaListener(int lana, LanaListener l)
147     {
148         // Range check the LANA id
149

150         if ( lana < 0 || lana > 255)
151             return;
152         
153         // Check if the listener array has been allocated
154

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             // Extend the LANA listener array
166

167             LanaListener[] newArray = new LanaListener[(lana + 3) & 0x00FC];
168            
169             // Copy the existing array to the extended array
170

171             System.arraycopy(m_listeners, 0, newArray, 0, m_listeners.length);
172             m_listeners = newArray;
173         }
174         
175         // Add the LANA listener
176

177         m_listeners[lana] = l;
178
179         // DEBUG
180

181         if (logger.isDebugEnabled() && hasDebug())
182             logger.debug("[SMB] Win32 NetBIOS register listener for LANA " + lana);
183     }
184
185     /**
186      * Remove a LANA listener
187      *
188      * @param lana int
189      */

190     public synchronized final void removeLanaListener(int lana)
191     {
192         // Validate the LANA id
193

194         if ( m_listeners == null || lana < 0 || lana >= m_listeners.length)
195             return;
196         
197         m_listeners[lana] = null;
198     }
199     
200     /**
201      * Thread method
202      */

203     public void run()
204     {
205         // Clear the shutdown flag
206

207         m_shutdown = false;
208
209         // If Winsock NetBIOS is not enabled then initialize the sockets interface
210

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                 // DEBUG
222

223                 if (logger.isDebugEnabled() && hasDebug())
224                     logger.debug("[SMB] Win32 NetBIOS initialization error", ex);
225                 
226                 // Shutdown the LANA monitor thread
227

228                 m_shutdown = true;
229             }
230         }
231         
232         // Loop until shutdown
233

234         BitSet JavaDoc curLanas = new BitSet JavaDoc();
235         
236         while (m_shutdown == false)
237         {
238
239             // Wait for a network address change event
240

241             Win32NetBIOS.waitForNetworkAddressChange();
242             
243             // Check if the monitor has been closed
244

245             if ( m_shutdown == true)
246                 continue;
247
248             // Clear the current active LANA bit set
249

250             curLanas.clear();
251             
252             // Get the available LANA list
253

254             int[] lanas = Win32NetBIOS.LanaEnumerate();
255             if (lanas != null)
256             {
257
258                 // Check if there are any new LANAs available
259

260                 Win32NetBIOSSessionSocketHandler sessHandler = null;
261
262                 for (int i = 0; i < lanas.length; i++)
263                 {
264
265                     // Get the current LANA id, check if it's a known LANA
266

267                     int lana = lanas[i];
268                     curLanas.set(lana, true);
269                     
270                     if (m_lanas.get(lana) == false)
271                     {
272
273                         // DEBUG
274

275                         if (logger.isDebugEnabled() && hasDebug())
276                             logger.debug("[SMB] Win32 NetBIOS found new LANA, " + lana);
277
278                         // Create a single Win32 NetBIOS session handler using the specified LANA
279

280                         sessHandler = new Win32NetBIOSSessionSocketHandler(m_server, lana, hasDebug());
281
282                         try
283                         {
284                             sessHandler.initialize();
285                         }
286                         catch (Exception JavaDoc ex)
287                         {
288
289                             // DEBUG
290

291                             if (logger.isDebugEnabled() && hasDebug())
292                                 logger.debug("[SMB] Win32 NetBIOS failed to create session handler for LANA " + lana,
293                                         ex);
294
295                             // Clear the session handler
296

297                             sessHandler = null;
298                         }
299
300                         // If the session handler was initialized successfully add it to the
301
// SMB/CIFS server
302

303                         if (sessHandler != null)
304                         {
305
306                             // Add the session handler to the SMB/CIFS server
307

308                             m_server.addSessionHandler(sessHandler);
309
310                             // Run the NetBIOS session handler in a seperate thread
311

312                             Thread JavaDoc nbThread = new Thread JavaDoc(sessHandler);
313                             nbThread.setName("Win32NB_Handler_" + lana);
314                             nbThread.start();
315
316                             // DEBUG
317

318                             if (logger.isDebugEnabled() && hasDebug())
319                                 logger.debug("[SMB] Win32 NetBIOS created session handler on LANA " + lana);
320
321                             // Check if a host announcer should be enabled
322

323                             if (config.hasWin32EnableAnnouncer())
324                             {
325
326                                 // Create a host announcer
327

328                                 Win32NetBIOSHostAnnouncer hostAnnouncer = new Win32NetBIOSHostAnnouncer(sessHandler,
329                                         config.getDomainName(), config.getWin32HostAnnounceInterval());
330
331                                 // Add the host announcer to the SMB/CIFS server list
332

333                                 m_server.addHostAnnouncer(hostAnnouncer);
334                                 hostAnnouncer.start();
335
336                                 // DEBUG
337

338                                 if (logger.isDebugEnabled() && hasDebug())
339                                     logger.debug("[SMB] Win32 NetBIOS host announcer enabled on LANA " + lana);
340                             }
341
342                             // Set the LANA in the available LANA list, and set the current status to online
343

344                             m_lanas.set(lana);
345                             m_lanaSts.set(lana, true);
346                         }
347                     }
348                     else
349                     {
350                         // Check if the LANA has just come back online
351

352                         if ( m_lanaSts.get(lana) == false)
353                         {
354                             // Change the LANA status to indicate the LANA is back online
355

356                             m_lanaSts.set(lana, true);
357                             
358                             // Inform the listener that the LANA is back online
359

360                             if ( m_listeners != null && lana < m_listeners.length &&
361                                     m_listeners[lana] != null)
362                                 m_listeners[lana].lanaStatusChange(lana, true);
363                             
364                             // DEBUG
365

366                             if (logger.isDebugEnabled() && hasDebug())
367                                 logger.debug("[SMB] Win32 NetBIOS LANA online - " + lana);
368                         }
369                     }
370                 }
371                 
372                 // Check if there are any LANAs that have gone offline
373

374                 for ( int i = 0; i < m_lanaSts.length(); i++)
375                 {
376                     if ( curLanas.get(i) == false && m_lanaSts.get(i) == true)
377                     {
378                         // DEBUG
379

380                         if (logger.isDebugEnabled() && hasDebug())
381                             logger.debug("[SMB] Win32 NetBIOS LANA offline - " + i);
382
383                         // Change the LANA status
384

385                         m_lanaSts.set(i, false);
386                         
387                         // Check if there is an associated listener for the LANA
388

389                         if ( m_listeners != null && m_listeners[i] != null)
390                         {
391                             // Notify the LANA listener that the LANA is now offline
392

393                             m_listeners[i].lanaStatusChange(i, false);
394                         }
395                     }
396                 }
397             }
398         }
399     }
400
401     /**
402      * Determine if debug output is enabled
403      *
404      * @return boolean
405      */

406     public final boolean hasDebug()
407     {
408         return m_debug;
409     }
410
411     /**
412      * Request the LANA monitor thread to shutdown
413      */

414     public final void shutdownRequest()
415     {
416         m_shutdown = true;
417         
418         // If Winsock NetBIOS is being used shutdown the Winsock interface
419

420         if ( m_server.getConfiguration().useWinsockNetBIOS())
421             NetBIOSSocket.shutdownSockets();
422     }
423 }
424
Popular Tags