KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > netbios > NetBIOSDatagramSocket


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;
18
19 import java.io.IOException JavaDoc;
20 import java.net.DatagramPacket JavaDoc;
21 import java.net.DatagramSocket JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.SocketException JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25
26 /**
27  * NetBIOS Datagram Socket Class
28  * <p>
29  * Singleton class that allows multiple users of the socket.
30  */

31 public class NetBIOSDatagramSocket
32 {
33     // Global NetBIOS datagram socket instance
34

35     private static NetBIOSDatagramSocket m_nbSocket;
36
37     // Default port and bind address
38

39     private static int m_defPort = RFCNetBIOSProtocol.DATAGRAM;
40     private static InetAddress JavaDoc m_defBindAddr;
41
42     // Datagram socket
43

44     private DatagramSocket JavaDoc m_socket;
45
46     // Broadcast address
47

48     private InetAddress JavaDoc m_broadcastAddr;
49
50     /**
51      * Class constructor
52      *
53      * @exception SocketException
54      * @exception UnknownHostException
55      */

56     private NetBIOSDatagramSocket() throws SocketException JavaDoc, UnknownHostException JavaDoc
57     {
58
59         // Create the datagram socket
60

61         if (m_defBindAddr == null)
62             m_socket = new DatagramSocket JavaDoc(m_defPort);
63         else
64             m_socket = new DatagramSocket JavaDoc(m_defPort, m_defBindAddr);
65
66         // Generate the broadcast mask
67

68         if (m_defBindAddr == null)
69             m_broadcastAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(null));
70         else
71             m_broadcastAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(m_defBindAddr
72                     .getHostAddress()));
73     }
74
75     /**
76      * Return the global NetBIOS datagram instance
77      *
78      * @return NetBIOSDatagramSocket
79      * @exception SocketException
80      * @exception UnknownHostException
81      */

82     public final static synchronized NetBIOSDatagramSocket getInstance() throws SocketException JavaDoc, UnknownHostException JavaDoc
83     {
84
85         // Check if the datagram socket has been created
86

87         if (m_nbSocket == null)
88             m_nbSocket = new NetBIOSDatagramSocket();
89
90         // Return the global NetBIOS datagram socket instance
91

92         return m_nbSocket;
93     }
94
95     /**
96      * Set the default port to use
97      *
98      * @param port int
99      */

100     public final static void setDefaultPort(int port)
101     {
102         m_defPort = port;
103     }
104
105     /**
106      * Set the address to bind the datagram socket to
107      *
108      * @param bindAddr InetAddress
109      */

110     public final static void setBindAddress(InetAddress JavaDoc bindAddr)
111     {
112         m_defBindAddr = bindAddr;
113     }
114
115     /**
116      * Receive a NetBIOS datagram
117      *
118      * @param dgram NetBIOSDatagram
119      * @return int
120      * @exception IOException
121      */

122     public final int receiveDatagram(NetBIOSDatagram dgram) throws IOException JavaDoc
123     {
124
125         // Create a datagram packet using the NetBIOS datagram buffer
126

127         DatagramPacket JavaDoc pkt = new DatagramPacket JavaDoc(dgram.getBuffer(), dgram.getBuffer().length);
128
129         // Receive a datagram
130

131         m_socket.receive(pkt);
132         return pkt.getLength();
133     }
134
135     /**
136      * Send a NetBIOS datagram
137      *
138      * @param dgram NetBIOSDatagram
139      * @param destAddr InetAddress
140      * @param destPort int
141      * @exception IOException
142      */

143     public final void sendDatagram(NetBIOSDatagram dgram, InetAddress JavaDoc destAddr, int destPort) throws IOException JavaDoc
144     {
145
146         // Create a datagram packet using the NetBIOS datagram buffer
147

148         DatagramPacket JavaDoc pkt = new DatagramPacket JavaDoc(dgram.getBuffer(), dgram.getLength(), destAddr, destPort);
149
150         // Send the NetBIOS datagram
151

152         m_socket.send(pkt);
153     }
154
155     /**
156      * Send a broadcast NetBIOS datagram
157      *
158      * @param dgram NetBIOSDatagram
159      * @exception IOException
160      */

161     public final void sendBroadcastDatagram(NetBIOSDatagram dgram) throws IOException JavaDoc
162     {
163
164         // Create a datagram packet using the NetBIOS datagram buffer
165

166         DatagramPacket JavaDoc pkt = new DatagramPacket JavaDoc(dgram.getBuffer(), dgram.getLength(), m_broadcastAddr, m_defPort);
167
168         // Send the NetBIOS datagram
169

170         m_socket.send(pkt);
171     }
172 }
173
Popular Tags