Dead
[official-gcc.git] / gomp-20050608-branch / libjava / gnu / java / net / PlainDatagramSocketImpl.java
blobf72d57331aee49efe72def86573d649a7ba4d199
1 /* PlainDatagramSocketImpl.java -- Default DatagramSocket implementation
2 Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.net;
41 import gnu.classpath.Configuration;
43 import java.io.IOException;
44 import java.net.DatagramPacket;
45 import java.net.DatagramSocketImpl;
46 import java.net.InetAddress;
47 import java.net.InetSocketAddress;
48 import java.net.NetworkInterface;
49 import java.net.SocketAddress;
50 import java.net.SocketException;
51 import java.net.SocketOptions;
53 /**
54 * Written using on-line Java Platform 1.2 API Specification, as well
55 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
56 * Status: Believed complete and correct.
59 /**
60 * This is the default socket implementation for datagram sockets.
61 * It makes native calls to C routines that implement BSD style
62 * SOCK_DGRAM sockets in the AF_INET family.
64 * @author Aaron M. Renn (arenn@urbanophile.com)
65 * @author Warren Levy (warrenl@cygnus.com)
67 public final class PlainDatagramSocketImpl extends DatagramSocketImpl
69 // Static initializer to load native library
70 static
72 if (Configuration.INIT_LOAD_LIBRARY)
74 System.loadLibrary("javanet");
78 // These fields are mirrored for use in native code to avoid cpp conflicts
79 // when the #defines in system header files are the same as the public fields.
80 static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
81 _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
82 _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
83 _Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST,
84 _Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE,
85 _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
86 _Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2,
87 _Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP,
88 _Jv_IP_TOS_ = SocketOptions.IP_TOS,
89 _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
90 _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
91 _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
92 _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
93 _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
95 /**
96 * This is the actual underlying file descriptor
98 int native_fd = -1;
101 * Lock object to serialize threads wanting to receive
103 private final Object RECEIVE_LOCK = new Object();
106 * Lock object to serialize threads wanting to send
108 private final Object SEND_LOCK = new Object();
110 // FIXME: Is this necessary? Could it help w/ DatagramSocket.getLocalAddress?
111 // InetAddress address;
113 // localAddress cache
114 InetAddress localAddress;
116 // 'timeout' is set/read by setOption/getOption.
117 int timeout = 0;
120 * Default do nothing constructor
122 public PlainDatagramSocketImpl()
126 protected void finalize() throws Throwable
128 synchronized (this)
130 if (native_fd != -1)
131 close();
133 super.finalize();
136 public int getNativeFD()
138 return native_fd;
142 * Binds this socket to a particular port and interface
144 * @param port The port to bind to
145 * @param addr The address to bind to
147 * @exception SocketException If an error occurs
149 protected native void bind(int port, InetAddress addr)
150 throws SocketException;
152 protected native void connect(InetAddress addr, int port)
153 throws SocketException;
155 protected native void disconnect();
158 * Creates a new datagram socket
160 * @exception SocketException If an error occurs
162 protected native void create() throws SocketException;
164 protected native int peek(InetAddress addr) throws IOException;
166 protected native int peekData(DatagramPacket packet) throws IOException;
169 * Sets the Time to Live value for the socket
171 * @param ttl The new TTL value
173 * @exception IOException If an error occurs
175 protected native void setTimeToLive(int ttl) throws IOException;
178 * Gets the Time to Live value for the socket
180 * @return The TTL value
182 * @exception IOException If an error occurs
184 protected native int getTimeToLive() throws IOException;
187 * Sends a packet of data to a remote host
189 * @param packet The packet to send
191 * @exception IOException If an error occurs
193 protected native void send(DatagramPacket packet) throws IOException;
196 * Receives a UDP packet from the network
198 * @param packet The packet to fill in with the data received
200 * @exception IOException IOException If an error occurs
202 protected native void receive(DatagramPacket packet) throws IOException;
205 * Sets the value of an option on the socket
207 * @param option_id The identifier of the option to set
208 * @param val The value of the option to set
210 * @exception SocketException If an error occurs
212 public native void setOption(int option_id, Object val)
213 throws SocketException;
216 * Retrieves the value of an option on the socket
218 * @param option_id The identifier of the option to retrieve
220 * @return The value of the option
222 * @exception SocketException If an error occurs
224 public native Object getOption(int option_id)
225 throws SocketException;
228 * Joins or leaves a broadcasting group on a given network interface.
229 * If the network interface is <code>null</code> the group is join/left on
230 * all locale network interfaces.
232 * @param inetAddr The broadcast address.
233 * @param netIf The network interface to join the group on.
234 * @param join True to join a broadcasting group, fals to leave it.
236 * @exception IOException If an error occurs.
238 private native void mcastGrp(InetAddress inetAddr, NetworkInterface netIf,
239 boolean join)
240 throws IOException;
243 * Closes the socket
245 protected native void close();
248 * Gets the Time to Live value for the socket
250 * @return The TTL value
252 * @exception IOException If an error occurs
254 * @deprecated 1.2
256 protected byte getTTL() throws IOException
258 return (byte) getTimeToLive();
262 * Sets the Time to Live value for the socket
264 * @param ttl The new TTL value
266 * @exception IOException If an error occurs
268 * @deprecated 1.2
270 protected void setTTL(byte ttl) throws IOException
272 setTimeToLive(((int) ttl) & 0xFF);
276 * Joins a multicast group
278 * @param addr The group to join
280 * @exception IOException If an error occurs
282 protected void join(InetAddress addr) throws IOException
284 mcastGrp(addr, null, true);
288 * Leaves a multicast group
290 * @param addr The group to leave
292 * @exception IOException If an error occurs
294 protected void leave(InetAddress addr) throws IOException
296 mcastGrp(addr, null, false);
299 protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
300 throws IOException
302 mcastGrp(((InetSocketAddress) mcastaddr).getAddress(), netIf, true);
305 protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
306 throws IOException
308 mcastGrp(((InetSocketAddress) mcastaddr).getAddress(), netIf, false);