diff options
author | eea1 <eea1@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1999-08-24 23:11:26 +0000 |
---|---|---|
committer | eea1 <eea1@ae88bc3d-4319-0410-8dbf-d08b4c9d3795> | 1999-08-24 23:11:26 +0000 |
commit | 9f0ea1f62b3fe5bcab38b8027c930e468564fadd (patch) | |
tree | c44f66d82e2b0e659cfba5ff615784f4fdc25e51 /java | |
parent | f4f016dd3db9a5265862df1bccf6c3d906f3d855 (diff) | |
download | ATCD-9f0ea1f62b3fe5bcab38b8027c930e468564fadd.tar.gz |
Updated source files for SOCK_SAP.
Diffstat (limited to 'java')
-rw-r--r-- | java/JACE/SOCK_SAP/INETAddr.java | 96 | ||||
-rw-r--r-- | java/JACE/SOCK_SAP/SOCKAcceptor.java | 113 | ||||
-rw-r--r-- | java/JACE/SOCK_SAP/SOCKConnector.java | 73 | ||||
-rw-r--r-- | java/JACE/SOCK_SAP/SOCKStream.java | 227 | ||||
-rw-r--r-- | java/JACE/SOCK_SAP/package.html | 8 |
5 files changed, 517 insertions, 0 deletions
diff --git a/java/JACE/SOCK_SAP/INETAddr.java b/java/JACE/SOCK_SAP/INETAddr.java new file mode 100644 index 00000000000..11b74a2b0ba --- /dev/null +++ b/java/JACE/SOCK_SAP/INETAddr.java @@ -0,0 +1,96 @@ +/************************************************* + * + * = PACKAGE + * JACE.SOCK_SAP + * + * = FILENAME + * INETAddr.java + * + *@author Chris Cleeland + * + *************************************************/ +package JACE.SOCK_SAP; + +import java.io.*; +import java.net.*; +import JACE.OS.*; + +/** + * Defines an endpoint of a connection, encapsulating host and port. + * This is only a part-way implementation of C++ ACE's ACE_INET_Addr. + * <P> + * Currently the class is very limited in its capabilities; it will + * be expanded in future revisions of ACE. + */ +public class INETAddr // extends Addr +{ + private InetAddress addr_; + private int port_ = 0; + /** + */ + public INETAddr () + { + // Do nothing constructor + } + + /** + * Create an INETAddr from a port/hostname + *@param port port number to connect with server at + *@param hostname hostname of the server + */ + public INETAddr (int port, String hostname) throws UnknownHostException + { + super(); + port_ = port; + addr_ = InetAddress.getByName(hostname); + // Should really use getAllByName(), + // but I don't think we do that in + // C++ ACE, even. + } + + /** + * Create an INETAddr from an address. + * @param address an address in the form "ip-number:port-number", <em>e.g.</em> <pre>tango.cs.wustl.edu:1234</pre> or <pre>128.252.166.57:1234</pre>; if no ':' is present address is assumed to be <b>INADDR_ANY</b> and address contains only the port number + * @throws UnknownHostException + */ + public INETAddr (String address) throws UnknownHostException + { + int colon = address.indexOf(':'); + if (colon != 0) + { + addr_ = InetAddress.getByName(address.substring(0, colon)); + address = address.substring(colon+1); + } + + port_ = Integer.parseInt(address); + } + + /** + * Return the name of the host. + */ + public String getHostName() + { + return addr_.getHostName(); + } + + /** + * Return the dotted Internet address. + */ + public String getHostAddr() + { + return addr_.toString(); + } + + /** + * Return the port number. + */ + public int getPortNumber() + { + return port_; + } + + public String toString() + { + return getHostAddr() + Integer.toString(port_); + } +} diff --git a/java/JACE/SOCK_SAP/SOCKAcceptor.java b/java/JACE/SOCK_SAP/SOCKAcceptor.java new file mode 100644 index 00000000000..dc9e12c8496 --- /dev/null +++ b/java/JACE/SOCK_SAP/SOCKAcceptor.java @@ -0,0 +1,113 @@ +/************************************************* + * + * = PACKAGE + * JACE.SOCK_SAP + * + * = FILENAME + * SOCKAcceptor.java + * + *@author Prashant Jain + * + *************************************************/ +package JACE.SOCK_SAP; + +import java.io.*; +import java.net.*; +import JACE.OS.*; + +/** + * Defines a factory that creates new SOCKStreams passively. + */ +public class SOCKAcceptor +{ + // = Initialization + + /** + * Create a SOCKAcceptor. Do nothing constructor. Allows user to + * call open() later and pass in the port number. + */ + public SOCKAcceptor () + { + } + + /** + * Create a SOCKAcceptor. + *@param port port number where the server will listen for connections + */ + public SOCKAcceptor (int port) throws IOException + { + this.open (port); + } + + /** + * Create socket to listen for connections on. + *@param port port number where the server will listen for connections + */ + public void open(int port) throws IOException + { + // Close old socket (if there is one) + this.close (); + + // Create a new server socket + this.listenSocket_ = new ServerSocket (port); + // ACE.DEBUG ("Server listening on port " + port); + } + + /** + * Close the socket and do any necessary cleanup. + */ + public void close () throws IOException + { + if (this.listenSocket_ != null) + { + this.listenSocket_.close (); + this.listenSocket_ = null; + } + } + + /** + * Accept a connection. The streams are set when the method returns. + *@param sockStream SOCK Stream to use for the connection + */ + public void accept (SOCKStream sockStream) throws SocketException, IOException + { + // Block in accept. Returns when a connection shows up and sets + // the streams + sockStream.socket (this.listenSocket_.accept ()); + ACE.DEBUG ("Accepted connection from " + + sockStream.socket ().getInetAddress ()); + } + + /** + * Get the underlying listen socket. + *@return the underlying listen socket + */ + public ServerSocket listenSocket () + { + return this.listenSocket_; + } + + /** + * Set the underlying listen socket. + *@param s the underlying listen socket + */ + public void listenSocket (ServerSocket s) + { + this.listenSocket_ = s; + } + + /** + * Clean up when the garbage collector gets run (if at all). Note + * that there is no guarantee that finalize () will get called. + *@exception Throwable (Probably IOException from the socket level) + */ + protected void finalize () throws Throwable + { + super.finalize (); + this.close (); + } + + // Socket on which listen for connections (by default initialized to + // null) + private ServerSocket listenSocket_; +} diff --git a/java/JACE/SOCK_SAP/SOCKConnector.java b/java/JACE/SOCK_SAP/SOCKConnector.java new file mode 100644 index 00000000000..cc3a558f77f --- /dev/null +++ b/java/JACE/SOCK_SAP/SOCKConnector.java @@ -0,0 +1,73 @@ +/************************************************* + * + * = PACKAGE + * JACE.SOCK_SAP + * + * = FILENAME + * SOCKConnector.java + * + *@author Prashant Jain + * + *************************************************/ +package JACE.SOCK_SAP; + +import java.io.*; +import java.net.*; +import JACE.OS.*; + +/** + * Defines an active connection factory for the socket wrappers. + */ +public class SOCKConnector +{ + // = Initialization + + /** + * Create a SOCKConnector. Do nothing constructor. Allows user to + * call connect() later. + */ + public SOCKConnector () + { + // Do nothing constructor + } + + /** + * Create a SOCKConnector and connect to the server. + *@param sockStream SOCK Stream to use for the connection + *@param hostname hostname of the server + *@param port port number to connect with server at + */ + public SOCKConnector (SOCKStream sockStream, + String hostname, + int port) throws SocketException, IOException + { + this.connect (sockStream, + hostname, + port); + } + + /** + * Connect to the server. + *@param sockStream SOCK Stream to use for the connection + *@param hostname hostname of the server + *@param port port number to connect with server at + */ + public void connect (SOCKStream sockStream, + String hostname, + int port) throws SocketException, IOException + { + sockStream.socket (new Socket (hostname, port)); + } + + /** + * Connect to the server. + *@param sockStream SOCK Stream to use for the connection + *@param addr INETAddr instance specifying host/port + */ + public void connect (SOCKStream sockStream, + INETAddr addr) throws SocketException, IOException + { + sockStream.socket (new Socket (addr.getHostName(), + addr.getPortNumber())); + } +} diff --git a/java/JACE/SOCK_SAP/SOCKStream.java b/java/JACE/SOCK_SAP/SOCKStream.java new file mode 100644 index 00000000000..05e2535e62a --- /dev/null +++ b/java/JACE/SOCK_SAP/SOCKStream.java @@ -0,0 +1,227 @@ +/************************************************* + * + * = PACKAGE + * JACE.SOCK_SAP + * + * = FILENAME + * SOCKStream.java + * + *@author Prashant Jain + * + *************************************************/ +package JACE.SOCK_SAP; + +import java.io.*; +import java.net.*; +import JACE.OS.*; + +/** + * This adds additional wrapper methods atop the java Socket class. + */ +public class SOCKStream +{ + /** + * Create a default SOCK Stream. Do nothing constructor. + */ + public SOCKStream () + { + } + + /** + * Create a SOCK Stream. + *@param s Socket to initialize SOCK Stream with. + */ + public SOCKStream (Socket s) throws IOException + { + this.socket (s); + } + + /** + * Set the socket and the underlying streams. + *@param s Socket associated with the SOCK Stream. + */ + public void socket (Socket s) throws IOException + { + this.socket_ = s; + // Note that if s is not a valid socket or is null, the + // following calls will throw exceptions + + this.iStream_ = + new DataInputStream(new BufferedInputStream(s.getInputStream())); + + this.oStream_ = + new DataOutputStream(new BufferedOutputStream(s.getOutputStream())); + } + + /* Get the underlying Socket. + *@return the underlying socket + */ + public Socket socket () + { + return this.socket_; + } + + /** + * Close the streams and the underlying socket. + */ + public void close () throws IOException + { + if (this.socket_ != null) + this.socket_.close (); + this.socket_ = null; + } + + // = The following send and recv methods are overloaded to provide a + // flexible interface + + /** + * Send a StringBuffer. Note that the method blocks. + *@param s the StringBuffer to send + *@return the length of the StringBuffer + */ + public int send (StringBuffer s) throws IOException + { + // Get the data out + String buf = s.toString (); + + //this.oStream_.println(buf); + this.oStream_.writeChars(buf.toString()); + this.oStream_.writeChar('\n'); + this.oStream_.flush (); + + return buf.length (); + } + + /** + * Send a String. Note that the method blocks. + *@param s the String to send + *@return the length of the String + */ + public int send (String s) throws IOException + { + this.oStream_.writeChars(s); + this.oStream_.writeChar('\n'); + + //this.oStream_.println(s); + this.oStream_.flush(); + + return s.length (); + } + + /** + * Send an array of bytes. Note that the method blocks. + *@param b array of bytes to send + *@param offset offset into the byte array to start sending from + *@param length number of bytes to send + *@return number of bytes sent + */ + public int sendN (byte[] b, int offset, int length) throws IOException + { + this.oStream_.write (b, offset, length); + this.oStream_.flush (); + return length; + } + + /** + * Receive data and append it to the StringBuffer that was passed + * in. Note that the method blocks. + *@param s the StringBuffer to append the result of the recv to + *@return the length of the String received + */ + public int recv (StringBuffer s) throws IOException + { + int len = 0; + char in = (char)this.iStream_.readByte(); + + while (in != '\n') { + s.append(in); + in = (char)this.iStream_.readByte(); + len++; + } + + return len; + } + + /** + * Receive an array of characters. This method blocks until either + * all the bytes are read, the end of the stream is detected, or + * an exception is thrown. + *@param b byte array to receive the data in + *@param offset the start offset of the data in the byte array. + *@param n number of bytes to receive + *@return n + */ + public int recvN (byte[] b, int offset, int n) throws IOException + { + this.iStream_.readFully (b, offset, n); + return n; + } + + /** + * Set the underlying input stream. + *@param iStream the input stream + */ + public void inputStream (InputStream iStream) + { + this.iStream_ = new DataInputStream(new BufferedInputStream(iStream)); + } + + /** + * Get the underlying input stream. + *@return the underlying input stream + */ + public InputStream inputStream () + { + return this.iStream_; + } + + /** + * Set the underlying output stream. + *@param iStream the output stream + */ + public void outputStream (OutputStream oStream) + { + this.oStream_ = new DataOutputStream(new BufferedOutputStream(oStream)); + } + + /** + * Get the underlying output stream. + *@return the underlying output stream + */ + public OutputStream outputStream () + { + return this.oStream_; + } + + /** + * Get the underlying stream wrapped in a buffered DataOutputStream + */ + public DataOutputStream dataOutputStream () + { + return this.oStream_; + } + + /** + * Get the underlying stream wrapped in a buffered DataInputStream + */ + public DataInputStream dataInputStream () + { + return this.iStream_; + } + + /** + * Cleanup when the SOCK Stream is garbage collected. + *@exception Throwable (Probably IOException from the socket level) + */ + protected void finalize () throws Throwable + { + super.finalize (); + this.close (); + } + + private Socket socket_; + + // = The input and output streams (by default null) + private DataInputStream iStream_; + private DataOutputStream oStream_; +} diff --git a/java/JACE/SOCK_SAP/package.html b/java/JACE/SOCK_SAP/package.html new file mode 100644 index 00000000000..fa4b9346aab --- /dev/null +++ b/java/JACE/SOCK_SAP/package.html @@ -0,0 +1,8 @@ +<!-- $Id$ --> +<HTML> +<BODY> +Wrappers for the sockets system. + +@see JACE.Connection.Acceptor +</BODY> +</HTML> |