OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [java/] [net/] [PlainDatagramSocketImpl.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* PlainDatagramSocketImpl.java -- Default DatagramSocket implementation
2
   Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005  Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
 
39
package gnu.java.net;
40
 
41
import gnu.classpath.Configuration;
42
 
43
import java.io.IOException;
44
import java.net.DatagramPacket;
45
import java.net.DatagramSocketImpl;
46
import java.net.InetAddress;
47
import java.net.NetworkInterface;
48
import java.net.SocketAddress;
49
import java.net.SocketException;
50
 
51
/**
52
 * Written using on-line Java Platform 1.2 API Specification, as well
53
 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
54
 * Status:  Believed complete and correct.
55
 */
56
 
57
/**
58
 * This is the default socket implementation for datagram sockets.
59
 * It makes native calls to C routines that implement BSD style
60
 * SOCK_DGRAM sockets in the AF_INET family.
61
 *
62
 * @author Aaron M. Renn (arenn@urbanophile.com)
63
 * @author Warren Levy (warrenl@cygnus.com)
64
 */
65
public final class PlainDatagramSocketImpl extends DatagramSocketImpl
66
{
67
  // Static initializer to load native library
68
  static
69
  {
70
    if (Configuration.INIT_LOAD_LIBRARY)
71
      {
72
        System.loadLibrary("javanet");
73
      }
74
  }
75
 
76
  /**
77
   * Option id for the IP_TTL (time to live) value.
78
   */
79
  private static final int IP_TTL = 0x1E61; // 7777
80
 
81
  /**
82
   * This is the actual underlying file descriptor
83
   */
84
  int native_fd = -1;
85
 
86
  /**
87
   * Lock object to serialize threads wanting to receive
88
   */
89
  private final Object RECEIVE_LOCK = new Object();
90
 
91
  /**
92
   * Lock object to serialize threads wanting to send
93
   */
94
  private final Object SEND_LOCK = new Object();
95
 
96
  /**
97
   * Default do nothing constructor
98
   */
99
  public PlainDatagramSocketImpl()
100
  {
101
  }
102
 
103
  protected void finalize() throws Throwable
104
  {
105
    synchronized (this)
106
      {
107
        if (native_fd != -1)
108
          close();
109
      }
110
    super.finalize();
111
  }
112
 
113
  public int getNativeFD()
114
  {
115
    return native_fd;
116
  }
117
 
118
  /**
119
   * Binds this socket to a particular port and interface
120
   *
121
   * @param port The port to bind to
122
   * @param addr The address to bind to
123
   *
124
   * @exception SocketException If an error occurs
125
   */
126
  protected synchronized native void bind(int port, InetAddress addr)
127
    throws SocketException;
128
 
129
  /**
130
   * Creates a new datagram socket
131
   *
132
   * @exception SocketException If an error occurs
133
   */
134
  protected synchronized native void create() throws SocketException;
135
 
136
  /**
137
   * Sets the Time to Live value for the socket
138
   *
139
   * @param ttl The new TTL value
140
   *
141
   * @exception IOException If an error occurs
142
   */
143
  protected synchronized void setTimeToLive(int ttl) throws IOException
144
  {
145
    setOption(IP_TTL, new Integer(ttl));
146
  }
147
 
148
  /**
149
   * Gets the Time to Live value for the socket
150
   *
151
   * @return The TTL value
152
   *
153
   * @exception IOException If an error occurs
154
   */
155
  protected synchronized int getTimeToLive() throws IOException
156
  {
157
    Object obj = getOption(IP_TTL);
158
 
159
    if (! (obj instanceof Integer))
160
      throw new IOException("Internal Error");
161
 
162
    return ((Integer) obj).intValue();
163
  }
164
 
165
  /**
166
   * Sends a packet of data to a remote host
167
   *
168
   * @param addr The address to send to
169
   * @param port The port to send to
170
   * @param buf The buffer to send
171
   * @param offset The offset of the data in the buffer to send
172
   * @param len The length of the data to send
173
   *
174
   * @exception IOException If an error occurs
175
   */
176
  private native void sendto (InetAddress addr, int port,
177
                              byte[] buf, int offset, int len)
178
    throws IOException;
179
 
180
  /**
181
   * Sends a packet of data to a remote host
182
   *
183
   * @param packet The packet to send
184
   *
185
   * @exception IOException If an error occurs
186
   */
187
  protected void send(DatagramPacket packet) throws IOException
188
  {
189
    synchronized(SEND_LOCK)
190
      {
191
      sendto(packet.getAddress(), packet.getPort(), packet.getData(),
192
             packet.getOffset(), packet.getLength());
193
      }
194
 
195
  }
196
 
197
  /**
198
   * Receives a UDP packet from the network
199
   *
200
   * @param packet The packet to fill in with the data received
201
   *
202
   * @exception IOException IOException If an error occurs
203
   */
204
  protected void receive(DatagramPacket packet)
205
    throws IOException
206
  {
207
      synchronized(RECEIVE_LOCK)
208
        {
209
        receive0(packet);
210
        }
211
  }
212
 
213
  /**
214
   * Native call to receive a UDP packet from the network
215
   *
216
   * @param packet The packet to fill in with the data received
217
   *
218
   * @exception IOException IOException If an error occurs
219
   */
220
  private native void receive0(DatagramPacket packet) throws IOException;
221
 
222
  /**
223
   * Sets the value of an option on the socket
224
   *
225
   * @param option_id The identifier of the option to set
226
   * @param val The value of the option to set
227
   *
228
   * @exception SocketException If an error occurs
229
   */
230
  public synchronized native void setOption(int option_id, Object val)
231
    throws SocketException;
232
 
233
  /**
234
   * Retrieves the value of an option on the socket
235
   *
236
   * @param option_id The identifier of the option to retrieve
237
   *
238
   * @return The value of the option
239
   *
240
   * @exception SocketException If an error occurs
241
   */
242
  public synchronized native Object getOption(int option_id)
243
    throws SocketException;
244
 
245
  /**
246
   * Closes the socket
247
   */
248
  protected synchronized native void close();
249
 
250
  /**
251
   * Gets the Time to Live value for the socket
252
   *
253
   * @return The TTL value
254
   *
255
   * @exception IOException If an error occurs
256
   *
257
   * @deprecated 1.2
258
   */
259
  protected synchronized byte getTTL() throws IOException
260
  {
261
    return (byte) getTimeToLive();
262
  }
263
 
264
  /**
265
   * Sets the Time to Live value for the socket
266
   *
267
   * @param ttl The new TTL value
268
   *
269
   * @exception IOException If an error occurs
270
   *
271
   * @deprecated 1.2
272
   */
273
  protected synchronized void setTTL(byte ttl) throws IOException
274
  {
275
    setTimeToLive(((int) ttl) & 0xFF);
276
  }
277
 
278
  /**
279
   * Joins a multicast group
280
   *
281
   * @param addr The group to join
282
   *
283
   * @exception IOException If an error occurs
284
   */
285
  protected synchronized native void join(InetAddress addr) throws IOException;
286
 
287
  /**
288
   * Leaves a multicast group
289
   *
290
   * @param addr The group to leave
291
   *
292
   * @exception IOException If an error occurs
293
   */
294
  protected synchronized native void leave(InetAddress addr) throws IOException;
295
 
296
  /**
297
   * What does this method really do?
298
   */
299
  protected synchronized int peek(InetAddress addr) throws IOException
300
  {
301
    throw new IOException("Not Implemented Yet");
302
  }
303
 
304
  public int peekData(DatagramPacket packet)
305
  {
306
    throw new InternalError
307
      ("PlainDatagramSocketImpl::peekData is not implemented");
308
  }
309
 
310
  public void joinGroup(SocketAddress address, NetworkInterface netIf)
311
  {
312
    throw new InternalError
313
      ("PlainDatagramSocketImpl::joinGroup is not implemented");
314
  }
315
 
316
  public void leaveGroup(SocketAddress address, NetworkInterface netIf)
317
  {
318
    throw new InternalError
319
      ("PlainDatagramSocketImpl::leaveGroup is not implemented");
320
  }
321
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.