001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.net;
019:
020: import java.io.FileDescriptor;
021: import java.io.IOException;
022:
023: import org.apache.harmony.luni.net.NetUtil;
024: import org.apache.harmony.luni.platform.Platform;
025:
026: /**
027: * The abstract superclass of datagram & multicast socket implementations.
028: */
029: public abstract class DatagramSocketImpl implements SocketOptions {
030:
031: protected FileDescriptor fd;
032:
033: protected int localPort;
034:
035: /**
036: * Constructs an unbound datagram socket implementation.
037: */
038: public DatagramSocketImpl() {
039: localPort = -1;
040: }
041:
042: /**
043: * Bind the datagram socket to the nominated localhost/port. Sockets must be
044: * bound prior to attempting to send or receive data.
045: *
046: * @param port
047: * the port on the localhost to bind
048: * @param addr
049: * the address on the multihomed localhost to bind
050: *
051: * @exception SocketException
052: * if an error occurred during bind, such as if the port was
053: * already bound
054: */
055: protected abstract void bind(int port, InetAddress addr)
056: throws SocketException;
057:
058: /**
059: * Close the socket.
060: */
061: protected abstract void close();
062:
063: /**
064: * This method allocates the socket descriptor in the underlying operating
065: * system.
066: */
067: protected abstract void create() throws SocketException;
068:
069: /**
070: * Answer the FileDescriptor, which will be invalid if the socket is closed
071: * or not bound.
072: *
073: * @return FileDescriptor the socket file descriptor
074: */
075: protected FileDescriptor getFileDescriptor() {
076: return fd;
077: }
078:
079: /**
080: * Answer the local address to which the socket is bound.
081: *
082: * @return InetAddress the local address to which the socket is bound.
083: */
084: InetAddress getLocalAddress() {
085: return Platform.getNetworkSystem().getSocketLocalAddress(fd,
086: NetUtil.preferIPv6Addresses());
087: }
088:
089: /**
090: * Answer the local port. If the socket was bound to any available port, as
091: * flagged by a <code>localPort</code> value of -1, query the IP stack.
092: *
093: * @return int the local port to which the socket is bound.
094: */
095: protected int getLocalPort() {
096: return localPort;
097: }
098:
099: /**
100: * Answer the nominated socket option.
101: *
102: * @param optID
103: * the socket option to retrieve
104: * @return Object the option value
105: * @exception SocketException
106: * thrown if an error occurs while accessing the option
107: */
108: public abstract Object getOption(int optID) throws SocketException;
109:
110: /**
111: * Answer the time-to-live (TTL) for multicast packets sent on this socket.
112: *
113: * @return java.net.InetAddress
114: * @throws IOException
115: * The exception description.
116: * @deprecated Replaced by getTimeToLive
117: * @see #getTimeToLive()
118: */
119: @Deprecated
120: protected abstract byte getTTL() throws IOException;
121:
122: /**
123: * Answer the time-to-live (TTL) for multicast packets sent on this socket.
124: *
125: * @return int
126: * @throws IOException
127: * The exception description.
128: */
129: protected abstract int getTimeToLive() throws IOException;
130:
131: /**
132: * Add this socket to the multicast group. A socket must join a group before
133: * data may be received. A socket may be a member of multiple groups but may
134: * join any group once.
135: *
136: * @param addr
137: * the multicast group to be joined
138: * @throws IOException
139: * may be thrown while joining a group
140: */
141: protected abstract void join(InetAddress addr) throws IOException;
142:
143: /**
144: * Add this socket to the multicast group. A socket must join a group before
145: * data may be received. A socket may be a member of multiple groups but may
146: * join any group once.
147: *
148: * @param addr
149: * the multicast group to be joined
150: * @param netInterface
151: * the network interface on which the addresses should be dropped
152: * @throws IOException
153: * may be thrown while joining a group
154: */
155: protected abstract void joinGroup(SocketAddress addr,
156: NetworkInterface netInterface) throws IOException;
157:
158: /**
159: * Remove the socket from the multicast group.
160: *
161: * @param addr
162: * the multicast group to be left
163: * @throws IOException
164: * May be thrown while leaving the group
165: */
166: protected abstract void leave(InetAddress addr) throws IOException;
167:
168: /**
169: * Remove the socket from the multicast group.
170: *
171: * @param addr
172: * the multicast group to be left
173: * @param netInterface
174: * the network interface on which the addresses should be dropped
175: * @throws IOException
176: * May be thrown while leaving the group
177: */
178: protected abstract void leaveGroup(SocketAddress addr,
179: NetworkInterface netInterface) throws IOException;
180:
181: /**
182: * Peek at the incoming packet to this socket and answer the sender's
183: * address into <code>sender</code>. The method will block until a packet
184: * is received or timeout expires and returns the sender's port.
185: *
186: * @exception IOException
187: * if a read error or timeout occurs
188: */
189: protected abstract int peek(InetAddress sender) throws IOException;
190:
191: /**
192: * Receive data into the supplied datagram packet. This call will block
193: * until either data is received or, if a timeout is set, the timeout
194: * expires. If the timeout expires, the InterruptedIOException is thrown.
195: *
196: * @exception IOException
197: * if a read error or timeout occurs
198: */
199: protected abstract void receive(DatagramPacket pack)
200: throws IOException;
201:
202: /**
203: * Sends the supplied datagram packet. The packet contains the destination
204: * host & port.
205: *
206: * @param pack
207: * DatagramPacket to send
208: *
209: * @exception IOException
210: * if a write error occurs
211: */
212: protected abstract void send(DatagramPacket pack)
213: throws IOException;
214:
215: /**
216: * Set the nominated socket option.
217: *
218: * @param optID
219: * the socket option to set
220: * @param val
221: * the option value
222: * @exception SocketException
223: * thrown if an error occurs while setting the option
224: */
225: public abstract void setOption(int optID, Object val)
226: throws SocketException;
227:
228: /**
229: * Set the time-to-live (TTL) for multicast packets sent on this socket.
230: *
231: * @param ttl
232: * the time-to-live, 0<ttl<= 255
233: * @throws IOException The exception thrown while setting the TTL
234: */
235: protected abstract void setTimeToLive(int ttl) throws IOException;
236:
237: /**
238: * Set the time-to-live (TTL) for multicast packets sent on this socket.
239: *
240: * @param ttl
241: * the time-to-live, 0<ttl<= 255
242: * @throws IOException The exception thrown while setting the TTL
243: * @deprecated Replaced by setTimeToLive
244: * @see #setTimeToLive(int)
245: */
246: @Deprecated
247: protected abstract void setTTL(byte ttl) throws IOException;
248:
249: /**
250: * Connect the socket to the specified remote address and port.
251: *
252: * @param inetAddr
253: * the remote address
254: * @param port
255: * the remote port
256: *
257: * @exception SocketException
258: * possibly thrown, if the datagram socket cannot be
259: * connected to the specified remote address and port
260: */
261: protected void connect(InetAddress inetAddr, int port)
262: throws SocketException {
263: // do nothing
264: }
265:
266: /**
267: * Disconnect the socket from the remote address and port.
268: */
269: protected void disconnect() {
270: // do nothing
271: }
272:
273: /**
274: * Receive data into the supplied datagram packet by peeking. The data is
275: * not removed and will be received by another peekData() or receive() call.
276: *
277: * This call will block until either data is received or, if a timeout is
278: * set, the timeout expires.
279: *
280: * @param pack
281: * the DatagramPacket used to store the data
282: *
283: * @return the port the packet was received from
284: *
285: * @exception IOException
286: * if an error occurs
287: */
288: protected abstract int peekData(DatagramPacket pack)
289: throws IOException;
290: }
|