OSDN Git Service

* All files: Updated copyright to reflect Cygnus purchase.
[pf3gnuchains/gcc-fork.git] / libjava / java / net / PlainDatagramSocketImpl.java
1 // PlainDatagramSocketImpl.java - Implementation of DatagramSocketImpl.
2
3 /* Copyright (C) 1999  Red Hat, Inc.
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package java.net;
12 import java.io.IOException;
13
14 /**
15  * @author Warren Levy <warrenl@cygnus.com>
16  * @date May 3, 1999.
17  */
18
19 /**
20  * Written using on-line Java Platform 1.2 API Specification, as well
21  * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
22  * Status:  Believed complete and correct.
23  */
24
25 class PlainDatagramSocketImpl extends DatagramSocketImpl
26 {
27   // These fields are mirrored for use in native code to avoid cpp conflicts
28   // when the #defines in system header files are the same as the public fields.
29   static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
30                    _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
31                    _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
32                    _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
33                    _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
34                    _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
35                    _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
36                    _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF;
37
38   int fnum = -1;
39
40   // FIXME: Is this necessary?  Could it help w/ DatagramSocket.getLocalAddress?
41   // InetAddress address;
42   
43   // localAddress cache  
44   InetAddress localAddress;
45
46   // 'timeout' is set/read by setOption/getOption.
47   int timeout = 0;
48
49   // FIXME: Probably should have bind (and create?) calls from DatagramSocket
50   // constuctor.  If so, then same change should be made to the corresponding
51   // Socket (non-datagram) classes.  This allows the implementation more
52   // complete control over how the socket is set up and used (e.g. connect,
53   // setting options, etc.).
54   public PlainDatagramSocketImpl()
55   {
56   }
57
58   protected native void bind(int lport, InetAddress laddr)
59         throws SocketException;
60   protected native void create() throws SocketException;
61   protected native int peek(InetAddress i) throws IOException;
62   protected native void setTimeToLive(int ttl) throws IOException;
63   protected native int getTimeToLive() throws IOException;
64   protected native void send(DatagramPacket p) throws IOException;
65   protected native void receive(DatagramPacket p) throws IOException;
66   public native void setOption(int optID, Object value) throws SocketException;
67   public native Object getOption(int optID) throws SocketException;
68   private native void mcastGrp(InetAddress inetaddr, boolean join)
69         throws IOException;
70
71   protected void close()
72   {
73     // FIXME: The close method in each of the DatagramSocket* classes does
74     // not throw an IOException.  The issue is that FileDescriptor.close()
75     // in natFileDescriptorPosix.cc can throw one, so we have to catch
76     // it here.  It seems that FileDescriptor.close is properly throwing
77     // the IOException on errors since many of the java.io classes depend
78     // on that.  This probably requires a bit more research but for now,
79     // we'll catch the IOException here.
80     try
81       {
82         fd.close();
83       }
84     catch (IOException e)
85       {
86         System.err.println("PlainDatagramSocketImpl.close: Error closing - " +
87           e.getMessage());
88       }
89   }
90
91   // Deprecated in JDK 1.2.
92   protected byte getTTL() throws IOException
93   {
94     return (byte) getTimeToLive();
95   }
96
97   // Deprecated in JDK 1.2.
98   protected void setTTL(byte ttl) throws IOException
99   {
100     setTimeToLive(((int) ttl) & 0xFF);
101   }
102
103   protected void join(InetAddress inetaddr) throws IOException
104   {
105     mcastGrp(inetaddr, true);
106   }
107
108   protected void leave(InetAddress inetaddr) throws IOException
109   {
110     mcastGrp(inetaddr, false);
111   }
112 }