1 17 package org.alfresco.filesys.netbios.win32; 18 19 import org.alfresco.filesys.netbios.NetBIOSName; 20 21 28 public class NetBIOSSocket 29 { 30 32 private static final int TypeNormal = 0; 33 private static final int TypeListener = 1; 34 private static final int TypeDatagram = 2; 35 36 38 private static boolean _nbSocketInit; 39 40 42 private int m_lana; 43 44 46 private int m_socket; 47 48 50 private NetBIOSName m_nbName; 51 52 54 private int m_socketType; 55 56 59 public static final void initializeSockets() 60 throws WinsockNetBIOSException { 61 62 64 if ( _nbSocketInit == false) 65 { 66 68 Win32NetBIOS.InitializeSockets(); 69 70 72 _nbSocketInit = true; 73 } 74 } 75 76 79 public static final void shutdownSockets() 80 { 81 83 if ( _nbSocketInit == true) 84 { 85 87 _nbSocketInit = false; 88 89 91 Win32NetBIOS.ShutdownSockets(); 92 } 93 } 94 95 100 public static final boolean isInitialized() 101 { 102 return _nbSocketInit; 103 } 104 105 114 public static final NetBIOSSocket createListenerSocket(int lana, NetBIOSName nbName) 115 throws WinsockNetBIOSException, NetBIOSSocketException 116 { 117 119 initializeSockets(); 120 121 123 int sockPtr = Win32NetBIOS.CreateSocket(lana); 124 if ( sockPtr == 0) 125 throw new NetBIOSSocketException("Failed to create NetBIOS socket"); 126 127 129 if ( Win32NetBIOS.BindSocket( sockPtr, nbName.getNetBIOSName()) != 0) 130 throw new NetBIOSSocketException("Failed to bind NetBIOS socket"); 131 132 134 return new NetBIOSSocket(lana, sockPtr, nbName, TypeListener); 135 } 136 137 145 public static final NetBIOSSocket createDatagramSocket(int lana) 146 throws WinsockNetBIOSException, NetBIOSSocketException 147 { 148 150 initializeSockets(); 151 152 154 int sockPtr = Win32NetBIOS.CreateDatagramSocket(lana); 155 if ( sockPtr == 0) 156 throw new NetBIOSSocketException("Failed to create NetBIOS datagram socket"); 157 158 160 return new NetBIOSSocket(lana, sockPtr, null, TypeDatagram); 161 } 162 163 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 185 public final int getLana() 186 { 187 return m_lana; 188 } 189 190 195 public final boolean isDatagramSocket() 196 { 197 return m_socketType == TypeDatagram ? true : false; 198 } 199 200 205 public final boolean isListener() 206 { 207 return m_socketType == TypeListener ? true : false; 208 } 209 210 215 public final boolean hasSocket() 216 { 217 return m_socket != 0 ? true : false; 218 } 219 220 225 public final int getSocket() 226 { 227 return m_socket; 228 } 229 230 236 public final NetBIOSName getName() 237 { 238 return m_nbName; 239 } 240 241 250 public final int write(byte[] buf, int off, int len) 251 throws WinsockNetBIOSException 252 { 253 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 270 public final int read(byte[] buf, int off, int maxLen) 271 throws WinsockNetBIOSException 272 { 273 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 291 public final int sendDatagram(NetBIOSName toName, byte[] buf, int off, int len) 292 throws WinsockNetBIOSException 293 { 294 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 309 public final NetBIOSSocket listen() 310 throws WinsockNetBIOSException, NetBIOSSocketException 311 { 312 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 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 330 return new NetBIOSSocket(getLana(), sessSockPtr, new NetBIOSName(callerName, 0), TypeNormal); 331 } 332 333 336 public final void closeSocket() 337 { 338 340 if ( hasSocket()) 341 { 342 Win32NetBIOS.CloseSocket( getSocket()); 343 setSocket(0); 344 } 345 } 346 347 352 protected final void setSocket(int sockPtr) 353 { 354 m_socket = sockPtr; 355 } 356 357 362 public String toString() 363 { 364 StringBuilder str = new StringBuilder (); 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 |