jdk/src/share/classes/sun/rmi/transport/tcp/ConnectionMultiplexer.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.rmi.transport.tcp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.rmi.server.LogStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * ConnectionMultiplexer manages the transparent multiplexing of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * multiple virtual connections from one endpoint to another through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * one given real connection to that endpoint.  The input and output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * streams for the the underlying real connection must be supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A callback object is also supplied to be informed of new virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * connections opened by the remote endpoint.  After creation, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * run() method must be called in a thread created for demultiplexing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the connections.  The openConnection() method is called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * initiate a virtual connection from this endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
final class ConnectionMultiplexer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** "multiplex" log level */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static int logLevel = LogStream.parseLevel(getLogLevel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static String getLogLevel() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    52
        return java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            new sun.security.action.GetPropertyAction("sun.rmi.transport.tcp.multiplex.logLevel"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* multiplex system log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static final Log multiplexLog =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Log.getLog("sun.rmi.transport.tcp.multiplex",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                   "multiplex", ConnectionMultiplexer.logLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** multiplexing protocol operation codes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static int OPEN     = 0xE1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static int CLOSE    = 0xE2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final static int CLOSEACK = 0xE3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static int REQUEST  = 0xE4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static int TRANSMIT = 0xE5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /** object to notify for new connections from remote endpoint */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private TCPChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /** input stream for underlying single connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /** output stream for underlying single connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /** true if underlying connection originated from this endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        (used for generating unique connection IDs) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean orig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /** layered stream for reading formatted data from underlying connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private DataInputStream dataIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /** layered stream for writing formatted data to underlying connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private DataOutputStream dataOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /** table holding currently open connection IDs and related info */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private Hashtable connectionTable = new Hashtable(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /** number of currently open connections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int numConnections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** maximum allowed open connections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private final static int maxConnections = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /** ID of last connection opened */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private int lastID = 0x1001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /** true if this mechanism is still alive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private boolean alive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Create a new ConnectionMultiplexer using the given underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * input/output stream pair.  The run method must be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * (possibly on a new thread) to handle the demultiplexing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param channel object to notify when new connection is received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param in input stream of underlying connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param out output stream of underlying connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param orig true if this endpoint intiated the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *        connection (needs to be set differently at both ends)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public ConnectionMultiplexer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        TCPChannel    channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        InputStream   in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        OutputStream  out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        boolean       orig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.in      = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.out     = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.orig    = orig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        dataIn = new DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        dataOut = new DataOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Process multiplexing protocol received from underlying connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void run() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            int op, id, length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Integer idObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            MultiplexConnectionInfo info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                // read next op code from remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                op = dataIn.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                switch (op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                // remote endpoint initiating new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                case OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        multiplexLog.log(Log.VERBOSE, "operation  OPEN " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    idObj = new Integer(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    if (info != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            "OPEN: Connection ID already exists");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    info = new MultiplexConnectionInfo(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    info.in = new MultiplexInputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    info.out = new MultiplexOutputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        connectionTable.put(idObj, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        ++ numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    sun.rmi.transport.Connection conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    conn = new TCPConnection(channel, info.in, info.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    channel.acceptMultiplexConnection(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                // remote endpoint closing connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                case CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        multiplexLog.log(Log.VERBOSE, "operation  CLOSE " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    idObj = new Integer(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                            "CLOSE: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    if (!info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        sendCloseAck(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        connectionTable.remove(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        -- numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                // remote endpoint acknowledging close of connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                case CLOSEACK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                            "operation  CLOSEACK " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    idObj = new Integer(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                            "CLOSEACK: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    if (!info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            "CLOSEACK: Connection not closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        connectionTable.remove(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        -- numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                // remote endpoint declaring additional bytes receivable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                case REQUEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    idObj = new Integer(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            "REQUEST: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    length = dataIn.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                            "operation  REQUEST " + id + ": " + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    info.out.request(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                // remote endpoint transmitting data packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                case TRANSMIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    idObj = new Integer(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        throw new IOException("SEND: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    length = dataIn.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            "operation  TRANSMIT " + id + ": " + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    info.in.receive(length, dataIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    throw new IOException("Invalid operation: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                          Integer.toHexString(op));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Initiate a new multiplexed connection through the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public synchronized TCPConnection openConnection() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // generate ID that should not be already used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // If all possible 32768 IDs are used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // this method will block searching for a new ID forever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        Integer idObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            lastID = (++ lastID) & 0x7FFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            id = lastID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            // The orig flag (copied to the high bit of the ID) is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // to have two distinct ranges to choose IDs from for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // two endpoints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (orig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                id |= 0x8000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            idObj = new Integer(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } while (connectionTable.get(idObj) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // create multiplexing streams and bookkeeping information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        MultiplexConnectionInfo info = new MultiplexConnectionInfo(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        info.in = new MultiplexInputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        info.out = new MultiplexOutputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // add to connection table if multiplexer has not died
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (!alive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                throw new IOException("Multiplexer connection dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (numConnections >= maxConnections)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                throw new IOException("Cannot exceed " + maxConnections +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    " simultaneous multiplexed connections");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            connectionTable.put(idObj, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            ++ numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        // inform remote endpoint of new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                dataOut.writeByte(OPEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                dataOut.writeShort(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return new TCPConnection(channel, info.in, info.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Shut down all connections and clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public void shutDown()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // inform all associated streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // return if multiplexer already officially dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (!alive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            alive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            Enumeration enum_ = connectionTable.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                MultiplexConnectionInfo info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    (MultiplexConnectionInfo) enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            connectionTable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            numConnections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // close underlying connection, if possible (and not already done)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Send request for more data on connection to remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param len number of more bytes that can be received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    void sendRequest(MultiplexConnectionInfo info, int len) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    dataOut.writeByte(REQUEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    dataOut.writeInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Send packet of requested data on connection to remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param buf array containg bytes to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param off offset of first array index of packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param len number of bytes in packet to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    void sendTransmit(MultiplexConnectionInfo info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                      byte buf[], int off, int len) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    dataOut.writeByte(TRANSMIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    dataOut.writeInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    dataOut.write(buf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Inform remote endpoint that connection has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    void sendClose(MultiplexConnectionInfo info) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    dataOut.writeByte(CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    info.closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Acknowledge remote endpoint's closing of connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    void sendCloseAck(MultiplexConnectionInfo info) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    dataOut.writeByte(CLOSEACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    info.closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Shut down connection upon finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    protected void finalize() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
}