001: /*
002: * Copyright (c) 2001 by Matt Welsh and The Regents of the University of
003: * California. All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Matt Welsh <mdw@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.nbio;
026:
027: import java.io.*;
028: import java.net.*;
029:
030: class NonblockingSocketImpl {
031:
032: private NBIOFileDescriptor fd;
033: private InetAddress address;
034: private int port;
035: private int localport;
036:
037: private native void nbSocketCreate(boolean stream);
038:
039: private native void nbSocketConnect(InetAddress address, int port)
040: throws IOException;
041:
042: private native boolean nbSocketConnectDone() throws SocketException;
043:
044: private native void nbSocketBind(InetAddress address, int port)
045: throws IOException;
046:
047: private native void nbSocketListen(int count) throws IOException;
048:
049: private native int nbSocketAccept(NonblockingSocketImpl s,
050: boolean block) throws IOException;
051:
052: private native int nbSocketAvailable() throws IOException;
053:
054: private native void nbSocketClose() throws IOException;
055:
056: private native int nbSendTo(DatagramPacket p) throws IOException;
057:
058: private native int nbReceive(DatagramPacket p) throws IOException;
059:
060: private native void nbDisconnect() throws SocketException;
061:
062: // Multicast support
063: private native void nbJoinGroup(InetAddress address)
064: throws IOException;
065:
066: private native void nbLeaveGroup(InetAddress address)
067: throws IOException;
068:
069: private native void nbSetTimeToLive(int ttl) throws IOException;
070:
071: private native int nbGetTimeToLive() throws IOException;
072:
073: private native void nbSetInterface(InetAddress address)
074: throws IOException;
075:
076: private native void nbSeeLocalMessages(boolean state)
077: throws IOException;
078:
079: NonblockingSocketImpl() {
080: fd = new NBIOFileDescriptor();
081: }
082:
083: NonblockingSocketImpl(InetAddress address) {
084: this ();
085: this .address = address;
086: }
087:
088: protected void create(boolean stream) throws IOException {
089: nbSocketCreate(stream);
090: }
091:
092: protected void connect(String host, int port) throws IOException {
093: try {
094: InetAddress address = InetAddress.getByName(host);
095: this .address = address;
096: this .port = port;
097: try {
098: nbSocketConnect(address, port);
099: return;
100: } catch (IOException e) {
101: close();
102: throw e;
103: }
104: } catch (UnknownHostException e) {
105: close();
106: throw e;
107: }
108: }
109:
110: protected void connect(InetAddress address, int port)
111: throws IOException {
112: this .address = address;
113: this .port = port;
114: try {
115: nbSocketConnect(address, port);
116: } catch (IOException e) {
117: close();
118: throw e;
119: }
120: }
121:
122: protected boolean connectDone() throws SocketException {
123: return nbSocketConnectDone();
124: }
125:
126: protected void bind(InetAddress host, int port) throws IOException {
127: nbSocketBind(host, port);
128: }
129:
130: protected void listen(int backlog) throws IOException {
131: nbSocketListen(backlog);
132: }
133:
134: protected void accept(NonblockingSocketImpl s) throws IOException {
135: if (nbSocketAccept(s, true) < 0)
136: throw new IOException("Blocking accept() returned error");
137: }
138:
139: protected int nbAccept(NonblockingSocketImpl s) throws IOException {
140: return nbSocketAccept(s, false);
141: }
142:
143: protected InputStream getInputStream() throws IOException {
144: return new NonblockingSocketInputStream(this );
145: }
146:
147: protected OutputStream getOutputStream() throws IOException {
148: return new NonblockingSocketOutputStream(this );
149: }
150:
151: protected int available() throws IOException {
152: return nbSocketAvailable();
153: }
154:
155: protected void close() throws IOException {
156: if (fd != null) {
157: nbSocketClose();
158: fd = null;
159: }
160: }
161:
162: protected void finalize() throws IOException {
163: close();
164: }
165:
166: protected InetAddress getInetAddress() {
167: return address;
168: }
169:
170: protected int getPort() {
171: return port;
172: }
173:
174: protected int getLocalPort() {
175: return localport;
176: }
177:
178: public void setOption(int optID, Object value)
179: throws SocketException {
180: // XXX MDW Do nothing
181: }
182:
183: public Object getOption(int optID) throws SocketException {
184: // XXX MDW Need to implement
185: return new Boolean(false);
186: }
187:
188: protected int send(DatagramPacket p) throws IOException {
189: return nbSendTo(p);
190: }
191:
192: protected int receive(DatagramPacket p) throws IOException {
193: return nbReceive(p);
194: }
195:
196: protected void disconnect() throws IOException {
197: nbDisconnect();
198: }
199:
200: public String toString() {
201: return "NonblockingSocketImpl";
202: }
203:
204: protected NBIOFileDescriptor getFileDescriptor() {
205: return fd;
206: }
207:
208: // Multicast support
209: protected void joinGroup(InetAddress address) throws IOException {
210: nbJoinGroup(address);
211: }
212:
213: protected void leaveGroup(InetAddress address) throws IOException {
214: nbLeaveGroup(address);
215: }
216:
217: protected void setTimeToLive(int ttl) throws IOException {
218: nbSetTimeToLive(ttl);
219: }
220:
221: protected int getTimeToLive() throws IOException {
222: return nbGetTimeToLive();
223: }
224:
225: protected void setInterface(InetAddress addr) throws IOException {
226: // null -> INADDR_ANY
227: nbSetInterface(addr);
228: }
229:
230: protected void seeLocalMessages(boolean state) throws IOException {
231: nbSeeLocalMessages(state);
232: }
233:
234: }
|