KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > netbios > win32 > NetBIOSSocket


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.netbios.win32;
18
19 import org.alfresco.filesys.netbios.NetBIOSName;
20
21 /**
22  * NetBIOS Socket Class
23  *
24  * <p>Contains the details of a Winsock NetBIOS socket that was opened using native code.
25  *
26  * @author GKSpencer
27  */

28 public class NetBIOSSocket
29 {
30     // Socket types
31

32     private static final int TypeNormal = 0;
33     private static final int TypeListener = 1;
34     private static final int TypeDatagram = 2;
35     
36     // Flag to indicate if the NetBIOS socket interface has been initialized
37

38     private static boolean _nbSocketInit;
39     
40     // NetBIOS LANA that the socket is associated with
41

42     private int m_lana;
43     
44     // Socket pointer (Windows SOCKET)
45

46     private int m_socket;
47     
48     // NetBIOS name, either listening name or callers name
49

50     private NetBIOSName m_nbName;
51     
52     // Socket type
53

54     private int m_socketType;
55     
56     /**
57      * Initialize the Winsock NetBIOS interface
58      */

59     public static final void initializeSockets()
60         throws WinsockNetBIOSException {
61         
62         // Check if the NetBIOS socket interface has been initialized
63

64         if ( _nbSocketInit == false)
65         {
66             // Initialize the NetBIOS socket interface
67

68             Win32NetBIOS.InitializeSockets();
69             
70             // Indicate that the NetBIOS socket interface is initialized
71

72             _nbSocketInit = true;
73         }
74     }
75     
76     /**
77      * Shutdown the Winsock NetBIOS interface
78      */

79     public static final void shutdownSockets()
80     {
81         // Check if the NetBIOS socket interface has been initialized
82

83         if ( _nbSocketInit == true)
84         {
85             // Indicate that the NetBIOS socket interface is not initialized
86

87             _nbSocketInit = false;
88             
89             // Initialize the NetBIOS socket interface
90

91             Win32NetBIOS.ShutdownSockets();
92         }
93     }
94     
95     /**
96      * Determine if the Winsock NetBIOS interface is initialized
97      *
98      * @return boolean
99      */

100     public static final boolean isInitialized()
101     {
102         return _nbSocketInit;
103     }
104     
105     /**
106      * Create a NetBIOS socket to listen for incoming sessions on the specified LANA
107      *
108      * @param lana int
109      * @param nbName NetBIOSName
110      * @return NetBIOSSocket
111      * @exception NetBIOSSocketException
112      * @exception WinsockNetBIOSException
113      */

114     public static final NetBIOSSocket createListenerSocket(int lana, NetBIOSName nbName)
115         throws WinsockNetBIOSException, NetBIOSSocketException
116     {
117         // Initialize the Winsock NetBIOS interface
118

119         initializeSockets();
120         
121         // Create a new NetBIOS socket
122

123         int sockPtr = Win32NetBIOS.CreateSocket(lana);
124         if ( sockPtr == 0)
125             throw new NetBIOSSocketException("Failed to create NetBIOS socket");
126         
127         // Bind the socket to a NetBIOS name
128

129         if ( Win32NetBIOS.BindSocket( sockPtr, nbName.getNetBIOSName()) != 0)
130             throw new NetBIOSSocketException("Failed to bind NetBIOS socket");
131         
132         // Return the NetBIOS socket
133

134         return new NetBIOSSocket(lana, sockPtr, nbName, TypeListener);
135     }
136     
137     /**
138      * Create a NetBIOS datagram socket to send out mailslot announcements on the specified LANA
139      *
140      * @param lana int
141      * @return NetBIOSSocket
142      * @exception NetBIOSSocketException
143      * @exception WinsockNetBIOSException
144      */

145     public static final NetBIOSSocket createDatagramSocket(int lana)
146         throws WinsockNetBIOSException, NetBIOSSocketException
147     {
148         // Initialize the Winsock NetBIOS interface
149

150         initializeSockets();
151         
152         // Create a new NetBIOS socket
153

154         int sockPtr = Win32NetBIOS.CreateDatagramSocket(lana);
155         if ( sockPtr == 0)
156             throw new NetBIOSSocketException("Failed to create NetBIOS datagram socket");
157         
158         // Return the NetBIOS socket
159

160         return new NetBIOSSocket(lana, sockPtr, null, TypeDatagram);
161     }
162     
163     /**
164      * Class constructor
165      *
166      * @param lana int
167      * @param sockPtr int
168      * @param nbName NetBIOSName
169      * @param sockerType int
170      */

171     private NetBIOSSocket(int lana, int sockPtr, NetBIOSName nbName, int socketType)
172     {
173         m_lana = lana;
174         m_nbName = nbName;
175         m_socket = sockPtr;
176         
177         m_socketType = socketType;
178     }
179     
180     /**
181      * Return the NetBIOS LANA the socket is associated with
182      *
183      * @return int
184      */

185     public final int getLana()
186     {
187         return m_lana;
188     }
189
190     /**
191      * Determine if this is a datagram socket
192      *
193      * @return boolean
194      */

195     public final boolean isDatagramSocket()
196     {
197         return m_socketType == TypeDatagram ? true : false;
198     }
199     
200     /**
201      * Determine if this is a listener type socket
202      *
203      * @return boolean
204      */

205     public final boolean isListener()
206     {
207         return m_socketType == TypeListener ? true : false;
208     }
209     
210     /**
211      * Determine if the socket is valid
212      *
213      * @return boolean
214      */

215     public final boolean hasSocket()
216     {
217         return m_socket != 0 ? true : false;
218     }
219     
220     /**
221      * Return the socket pointer
222      *
223      * @return int
224      */

225     public final int getSocket()
226     {
227         return m_socket;
228     }
229     
230     /**
231      * Return the NetBIOS name. For a listening socket this is the local name, for a session
232      * socket this is the remote callers name.
233      *
234      * @return NetBIOSName
235      */

236     public final NetBIOSName getName()
237     {
238         return m_nbName;
239     }
240     
241     /**
242      * Write data to the session socket
243      *
244      * @param buf byte[]
245      * @param off int
246      * @param len int
247      * @return int
248      * @exception WinsockNetBIOSException
249      */

250     public final int write(byte[] buf, int off, int len)
251         throws WinsockNetBIOSException
252     {
253         // Check if this is a datagram socket
254

255         if ( isDatagramSocket())
256             throw new WinsockNetBIOSException("Write not allowed for datagram socket");
257         
258         return Win32NetBIOS.SendSocket( getSocket(), buf, off, len);
259     }
260     
261     /**
262      * Read data from the session socket
263      *
264      * @param buf byte[]
265      * @param off int
266      * @param maxLen int
267      * @return int
268      * @exception WinsockNetBIOSException
269      */

270     public final int read(byte[] buf, int off, int maxLen)
271         throws WinsockNetBIOSException
272     {
273         // Check if this is a datagram socket
274

275         if ( isDatagramSocket())
276             throw new WinsockNetBIOSException("Read not allowed for datagram socket");
277         
278         return Win32NetBIOS.ReceiveSocket( getSocket(), buf, off, maxLen);
279     }
280     
281     /**
282      * Send a datagram to a group name
283      *
284      * @param toName NetBIOSName
285      * @param buf byte[]
286      * @param off int
287      * @param len int
288      * @return int
289      * @exception WinsockNetBIOSException
290      */

291     public final int sendDatagram(NetBIOSName toName, byte[] buf, int off, int len)
292         throws WinsockNetBIOSException
293     {
294         // Check if this is a datagram socket
295

296         if ( isDatagramSocket() == false)
297             throw new WinsockNetBIOSException("Not a datagram type socket");
298         
299         return Win32NetBIOS.SendSocketDatagram( getSocket(), toName.getNetBIOSName(), buf, off, len);
300     }
301     
302     /**
303      * Listen for an incoming session connection and create a session socket for the new session
304      *
305      * @return NetBIOSSocket
306      * @exception NetBIOSSocketException
307      * @exception winsockNetBIOSException
308      */

309     public final NetBIOSSocket listen()
310         throws WinsockNetBIOSException, NetBIOSSocketException
311     {
312         // Check if this socket is a listener socket, and the socket is valid
313

314         if ( isListener() == false)
315             throw new NetBIOSSocketException("Not a listener type socket");
316
317         if ( hasSocket() == false)
318             throw new NetBIOSSocketException("NetBIOS socket not valid");
319         
320         // Wait for an incoming session request
321

322         byte[] callerName = new byte[NetBIOSName.NameLength];
323         
324         int sessSockPtr = Win32NetBIOS.ListenSocket( getSocket(), callerName);
325         if ( sessSockPtr == 0)
326             throw new NetBIOSSocketException("NetBIOS socket listen failed");
327         
328         // Return the new NetBIOS socket session
329

330         return new NetBIOSSocket(getLana(), sessSockPtr, new NetBIOSName(callerName, 0), TypeNormal);
331     }
332     
333     /**
334      * Close the socket
335      */

336     public final void closeSocket()
337     {
338         // Close the native socket, if valid
339

340         if ( hasSocket())
341         {
342             Win32NetBIOS.CloseSocket( getSocket());
343             setSocket(0);
344         }
345     }
346     
347     /**
348      * Set the socket pointer
349      *
350      * @param sockPtr int
351      */

352     protected final void setSocket(int sockPtr)
353     {
354         m_socket = sockPtr;
355     }
356     
357     /**
358      * Return the NetBIOS socket details as a string
359      *
360      * @return String
361      */

362     public String JavaDoc toString()
363     {
364         StringBuilder JavaDoc str = new StringBuilder JavaDoc();
365         
366         str.append("[LANA:");
367         str.append(getLana());
368         str.append(",Name:");
369         if ( getName() != null)
370             str.append(getName());
371         else
372             str.append("<None>");
373         
374         str.append(",Socket:");
375         if ( hasSocket())
376         {
377             str.append("0x");
378             str.append(Integer.toHexString(getSocket()));
379         }
380         else
381             str.append("<None>");
382
383         switch( m_socketType)
384         {
385         case TypeNormal:
386             str.append("Session");
387             break;
388         case TypeListener:
389             str.append("Listener");
390             break;
391         case TypeDatagram:
392             str.append("Datagram");
393             break;
394         }
395         
396         str.append("]");
397             
398         return str.toString();
399     }
400 }
401
Popular Tags