/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* ConnectionMultiplexer manages the transparent multiplexing of
* multiple virtual connections from one endpoint to another through
* one given real connection to that endpoint. The input and output
* streams for the the underlying real connection must be supplied.
* A callback object is also supplied to be informed of new virtual
* connections opened by the remote endpoint. After creation, the
* run() method must be called in a thread created for demultiplexing
* the connections. The openConnection() method is called to
* initiate a virtual connection from this endpoint.
*
* @author Peter Jones
*/
final class ConnectionMultiplexer {
/** "multiplex" log level */
}
/* multiplex system log */
/** multiplexing protocol operation codes */
/** object to notify for new connections from remote endpoint */
/** input stream for underlying single connection */
/** output stream for underlying single connection */
/** true if underlying connection originated from this endpoint
(used for generating unique connection IDs) */
private boolean orig;
/** layered stream for reading formatted data from underlying connection */
/** layered stream for writing formatted data to underlying connection */
/** table holding currently open connection IDs and related info */
/** number of currently open connections */
/** maximum allowed open connections */
/** ID of last connection opened */
/** true if this mechanism is still alive */
private boolean alive = true;
/**
* Create a new ConnectionMultiplexer using the given underlying
* (possibly on a new thread) to handle the demultiplexing.
* @param channel object to notify when new connection is received
* @param in input stream of underlying connection
* @param out output stream of underlying connection
* @param orig true if this endpoint intiated the underlying
* connection (needs to be set differently at both ends)
*/
public ConnectionMultiplexer(
boolean orig)
{
}
/**
* Process multiplexing protocol received from underlying connection.
*/
{
try {
while (true) {
// read next op code from remote endpoint
switch (op) {
// remote endpoint initiating new connection
case OPEN:
}
throw new IOException(
"OPEN: Connection ID already exists");
synchronized (connectionTable) {
++ numConnections;
}
break;
// remote endpoint closing connection
case CLOSE:
}
throw new IOException(
"CLOSE: Invalid connection ID");
synchronized (connectionTable) {
-- numConnections;
}
break;
// remote endpoint acknowledging close of connection
case CLOSEACK:
"operation CLOSEACK " + id);
}
throw new IOException(
"CLOSEACK: Invalid connection ID");
throw new IOException(
"CLOSEACK: Connection not closed");
synchronized (connectionTable) {
-- numConnections;
}
break;
// remote endpoint declaring additional bytes receivable
case REQUEST:
throw new IOException(
"REQUEST: Invalid connection ID");
}
break;
// remote endpoint transmitting data packet
case TRANSMIT:
throw new IOException("SEND: Invalid connection ID");
}
break;
default:
throw new IOException("Invalid operation: " +
}
}
} finally {
shutDown();
}
}
/**
* Initiate a new multiplexed connection through the underlying
* connection.
*/
{
// generate ID that should not be already used
// If all possible 32768 IDs are used,
// this method will block searching for a new ID forever.
int id;
do {
// The orig flag (copied to the high bit of the ID) is used
// to have two distinct ranges to choose IDs from for the
// two endpoints.
if (orig)
id |= 0x8000;
// create multiplexing streams and bookkeeping information
// add to connection table if multiplexer has not died
synchronized (connectionTable) {
if (!alive)
throw new IOException("Multiplexer connection dead");
if (numConnections >= maxConnections)
" simultaneous multiplexed connections");
++ numConnections;
}
// inform remote endpoint of new connection
synchronized (dataOut) {
try {
} catch (IOException e) {
shutDown();
throw e;
}
}
}
/**
* Shut down all connections and clean up.
*/
public void shutDown()
{
// inform all associated streams
synchronized (connectionTable) {
// return if multiplexer already officially dead
if (!alive)
return;
alive = false;
while (enum_.hasMoreElements()) {
}
numConnections = 0;
}
// close underlying connection, if possible (and not already done)
try {
} catch (IOException e) {
}
try {
} catch (IOException e) {
}
}
/**
* Send request for more data on connection to remote endpoint.
* @param info connection information structure
* @param len number of more bytes that can be received
*/
{
synchronized (dataOut) {
try {
} catch (IOException e) {
shutDown();
throw e;
}
}
}
/**
* Send packet of requested data on connection to remote endpoint.
* @param info connection information structure
* @param buf array containg bytes to send
* @param off offset of first array index of packet
* @param len number of bytes in packet to send
*/
{
synchronized (dataOut) {
try {
} catch (IOException e) {
shutDown();
throw e;
}
}
}
/**
* Inform remote endpoint that connection has been closed.
* @param info connection information structure
*/
{
synchronized (dataOut) {
try {
} catch (IOException e) {
shutDown();
throw e;
}
}
}
/**
* Acknowledge remote endpoint's closing of connection.
* @param info connection information structure
*/
{
synchronized (dataOut) {
try {
} catch (IOException e) {
shutDown();
throw e;
}
}
}
/**
* Shut down connection upon finalization.
*/
{
super.finalize();
shutDown();
}
}