11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.net;
27
28 import java.io.FileDescriptor;
29 import java.io.IOException;
30 import java.io.InterruptedIOException;
31
32 /**
33 * Abstract datagram and multicast socket implementation base class.
34 * @author Pavani Diwanji
35 * @since JDK1.1
36 */
37
38 public abstract class DatagramSocketImpl implements SocketOptions {
39
40 /**
41 * The local port number.
42 */
43 protected int localPort;
44
45 /**
46 * The file descriptor object.
47 */
48 protected FileDescriptor fd;
49
50 /**
51 * Creates a datagram socket.
52 * @exception SocketException if there is an error in the
53 * underlying protocol, such as a TCP error.
54 */
55 protected abstract void create() throws SocketException;
56
57 /**
58 * Binds a datagram socket to a local port and address.
59 * @param lport the local port
60 * @param laddr the local address
61 * @exception SocketException if there is an error in the
62 * underlying protocol, such as a TCP error.
63 */
64 protected abstract void bind(int lport, InetAddress laddr) throws SocketException;
65
66 /**
67 * Sends a datagram packet. The packet contains the data and the
68 * destination address to send the packet to.
69 * @param p the packet to be sent.
70 * @exception IOException if an I/O exception occurs while sending the
224 * Close the socket.
225 */
226 protected abstract void close();
227
228 /**
229 * Gets the local port.
230 * @return an {@code int} representing the local port value
231 */
232 protected int getLocalPort() {
233 return localPort;
234 }
235
236 /**
237 * Gets the datagram socket file descriptor.
238 * @return a {@code FileDescriptor} object representing the datagram socket
239 * file descriptor
240 */
241 protected FileDescriptor getFileDescriptor() {
242 return fd;
243 }
244 }
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.net;
27
28 import java.io.FileDescriptor;
29 import java.io.IOException;
30 import java.io.InterruptedIOException;
31 import java.util.Set;
32 import java.util.HashSet;
33
34 /**
35 * Abstract datagram and multicast socket implementation base class.
36 * @author Pavani Diwanji
37 * @since JDK1.1
38 */
39
40 public abstract class DatagramSocketImpl implements SocketOptions {
41
42 /**
43 * The local port number.
44 */
45 protected int localPort;
46
47 /**
48 * The file descriptor object.
49 */
50 protected FileDescriptor fd;
51
52 /**
53 * The DatagramSocket or MulticastSocket
54 * that owns this impl
55 */
56 DatagramSocket socket;
57
58 void setDatagramSocket(DatagramSocket socket) {
59 this.socket = socket;
60 }
61
62 DatagramSocket getDatagramSocket() {
63 return socket;
64 }
65
66 /**
67 * Creates a datagram socket.
68 * @exception SocketException if there is an error in the
69 * underlying protocol, such as a TCP error.
70 */
71 protected abstract void create() throws SocketException;
72
73 /**
74 * Binds a datagram socket to a local port and address.
75 * @param lport the local port
76 * @param laddr the local address
77 * @exception SocketException if there is an error in the
78 * underlying protocol, such as a TCP error.
79 */
80 protected abstract void bind(int lport, InetAddress laddr) throws SocketException;
81
82 /**
83 * Sends a datagram packet. The packet contains the data and the
84 * destination address to send the packet to.
85 * @param p the packet to be sent.
86 * @exception IOException if an I/O exception occurs while sending the
240 * Close the socket.
241 */
242 protected abstract void close();
243
244 /**
245 * Gets the local port.
246 * @return an {@code int} representing the local port value
247 */
248 protected int getLocalPort() {
249 return localPort;
250 }
251
252 /**
253 * Gets the datagram socket file descriptor.
254 * @return a {@code FileDescriptor} object representing the datagram socket
255 * file descriptor
256 */
257 protected FileDescriptor getFileDescriptor() {
258 return fd;
259 }
260
261 /**
262 * Called to set a socket option.
263 *
264 * @param name The socket option
265 *
266 * @param value The value of the socket option. A value of {@code null}
267 * may be valid for some options.
268 *
269 * @throws UnsupportedOperationException if the DatagramSocketImpl does not
270 * support the option
271 *
272 * @throws NullPointerException if name is {@code null}
273 *
274 * @since 1.9
275 */
276 protected <T> void setOption(SocketOption<T> name, T value)
277 throws IOException
278 {
279 if (name == StandardSocketOptions.SO_SNDBUF) {
280 setOption(SocketOptions.SO_SNDBUF, value);
281 } else if (name == StandardSocketOptions.SO_RCVBUF) {
282 setOption(SocketOptions.SO_RCVBUF, value);
283 } else if (name == StandardSocketOptions.SO_REUSEADDR) {
284 setOption(SocketOptions.SO_REUSEADDR, value);
285 } else if (name == StandardSocketOptions.IP_TOS) {
286 setOption(SocketOptions.IP_TOS, value);
287 } else if (name == StandardSocketOptions.IP_MULTICAST_IF &&
288 (getDatagramSocket() instanceof MulticastSocket)) {
289 setOption(SocketOptions.IP_MULTICAST_IF2, value);
290 } else if (name == StandardSocketOptions.IP_MULTICAST_TTL &&
291 (getDatagramSocket() instanceof MulticastSocket)) {
292 if (! (value instanceof Integer)) {
293 throw new IllegalArgumentException("not an integer");
294 }
295 setTimeToLive((Integer)value);
296 } else if (name == StandardSocketOptions.IP_MULTICAST_LOOP &&
297 (getDatagramSocket() instanceof MulticastSocket)) {
298 setOption(SocketOptions.IP_MULTICAST_LOOP, value);
299 } else {
300 throw new UnsupportedOperationException("unsupported option");
301 }
302 }
303
304 /**
305 * Called to get a socket option.
306 *
307 * @param name The socket option
308 *
309 * @throws UnsupportedOperationException if the DatagramSocketImpl does not
310 * support the option
311 *
312 * @throws NullPointerException if name is {@code null}
313 *
314 * @since 1.9
315 */
316 protected <T> T getOption(SocketOption<T> name)
317 throws IOException
318 {
319 if (name == StandardSocketOptions.SO_SNDBUF) {
320 return (T) getOption(SocketOptions.SO_SNDBUF);
321 } else if (name == StandardSocketOptions.SO_RCVBUF) {
322 return (T) getOption(SocketOptions.SO_RCVBUF);
323 } else if (name == StandardSocketOptions.SO_REUSEADDR) {
324 return (T) getOption(SocketOptions.SO_REUSEADDR);
325 } else if (name == StandardSocketOptions.IP_TOS) {
326 return (T) getOption(SocketOptions.IP_TOS);
327 } else if (name == StandardSocketOptions.IP_MULTICAST_IF &&
328 (getDatagramSocket() instanceof MulticastSocket)) {
329 return (T) getOption(SocketOptions.IP_MULTICAST_IF2);
330 } else if (name == StandardSocketOptions.IP_MULTICAST_TTL &&
331 (getDatagramSocket() instanceof MulticastSocket)) {
332 Integer ttl = getTimeToLive();
333 return (T)ttl;
334 } else if (name == StandardSocketOptions.IP_MULTICAST_LOOP &&
335 (getDatagramSocket() instanceof MulticastSocket)) {
336 return (T) getOption(SocketOptions.IP_MULTICAST_LOOP);
337 } else {
338 throw new UnsupportedOperationException("unsupported option");
339 }
340 }
341
342 private static final Set<SocketOption<?>> dgSocketOptions =
343 new HashSet<>();
344
345 private static final Set<SocketOption<?>> mcSocketOptions =
346 new HashSet<>();
347
348 static {
349 dgSocketOptions.add(StandardSocketOptions.SO_SNDBUF);
350 dgSocketOptions.add(StandardSocketOptions.SO_RCVBUF);
351 dgSocketOptions.add(StandardSocketOptions.SO_REUSEADDR);
352 dgSocketOptions.add(StandardSocketOptions.IP_TOS);
353
354 mcSocketOptions.add(StandardSocketOptions.SO_SNDBUF);
355 mcSocketOptions.add(StandardSocketOptions.SO_RCVBUF);
356 mcSocketOptions.add(StandardSocketOptions.SO_REUSEADDR);
357 mcSocketOptions.add(StandardSocketOptions.IP_TOS);
358 mcSocketOptions.add(StandardSocketOptions.IP_MULTICAST_IF);
359 mcSocketOptions.add(StandardSocketOptions.IP_MULTICAST_TTL);
360 mcSocketOptions.add(StandardSocketOptions.IP_MULTICAST_LOOP);
361 };
362
363 /**
364 * Returns a set of SocketOptions supported by this impl
365 * and by this impl's socket (DatagramSocket or MulticastSocket)
366 *
367 * @return a Set of SocketOptions
368 */
369 protected Set<SocketOption<?>> supportedOptions() {
370 if (getDatagramSocket() instanceof MulticastSocket) {
371 return mcSocketOptions;
372 } else {
373 return dgSocketOptions;
374 }
375 }
376 }
|