jdk/src/share/classes/sun/rmi/transport/tcp/ConnectionMultiplexer.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 12040 558b0e0d5910
child 19211 32a04c562026
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12040
diff changeset
     2
 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
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 */
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
    88
    private Hashtable<Integer, MultiplexConnectionInfo> connectionTable = new Hashtable<>(7);
2
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
            MultiplexConnectionInfo info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                // read next op code from remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                op = dataIn.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                switch (op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // remote endpoint initiating new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                case OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        multiplexLog.log(Log.VERBOSE, "operation  OPEN " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   150
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    if (info != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                            "OPEN: Connection ID already exists");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    info = new MultiplexConnectionInfo(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    info.in = new MultiplexInputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    info.out = new MultiplexOutputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    synchronized (connectionTable) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   158
                        connectionTable.put(id, info);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        ++ numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    sun.rmi.transport.Connection conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    conn = new TCPConnection(channel, info.in, info.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    channel.acceptMultiplexConnection(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                // remote endpoint closing connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                case CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        multiplexLog.log(Log.VERBOSE, "operation  CLOSE " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   174
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                            "CLOSE: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    if (!info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        sendCloseAck(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    synchronized (connectionTable) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   183
                        connectionTable.remove(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        -- numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                // remote endpoint acknowledging close of connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                case CLOSEACK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            "operation  CLOSEACK " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   197
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            "CLOSEACK: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    if (!info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            "CLOSEACK: Connection not closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    synchronized (connectionTable) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   207
                        connectionTable.remove(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        -- numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                // remote endpoint declaring additional bytes receivable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                case REQUEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    id = dataIn.readUnsignedShort();
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   215
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                            "REQUEST: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    length = dataIn.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            "operation  REQUEST " + id + ": " + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    info.out.request(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                // remote endpoint transmitting data packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                case TRANSMIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    id = dataIn.readUnsignedShort();
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   232
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        throw new IOException("SEND: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    length = dataIn.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            "operation  TRANSMIT " + id + ": " + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    info.in.receive(length, dataIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    throw new IOException("Invalid operation: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                          Integer.toHexString(op));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Initiate a new multiplexed connection through the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public synchronized TCPConnection openConnection() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // generate ID that should not be already used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // If all possible 32768 IDs are used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // this method will block searching for a new ID forever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            lastID = (++ lastID) & 0x7FFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            id = lastID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // The orig flag (copied to the high bit of the ID) is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // to have two distinct ranges to choose IDs from for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // two endpoints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (orig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                id |= 0x8000;
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   274
        } while (connectionTable.get(id) != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        // create multiplexing streams and bookkeeping information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        MultiplexConnectionInfo info = new MultiplexConnectionInfo(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        info.in = new MultiplexInputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        info.out = new MultiplexOutputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // add to connection table if multiplexer has not died
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (!alive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                throw new IOException("Multiplexer connection dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (numConnections >= maxConnections)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                throw new IOException("Cannot exceed " + maxConnections +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    " simultaneous multiplexed connections");
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   288
            connectionTable.put(id, info);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            ++ numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        // inform remote endpoint of new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                dataOut.writeByte(OPEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                dataOut.writeShort(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return new TCPConnection(channel, info.in, info.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Shut down all connections and clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public void shutDown()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // inform all associated streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // return if multiplexer already officially dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (!alive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            alive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   321
            Enumeration<MultiplexConnectionInfo> enum_ =
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   322
                    connectionTable.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            while (enum_.hasMoreElements()) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   324
                MultiplexConnectionInfo info = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            connectionTable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            numConnections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // close underlying connection, if possible (and not already done)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Send request for more data on connection to remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param len number of more bytes that can be received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    void sendRequest(MultiplexConnectionInfo info, int len) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    dataOut.writeByte(REQUEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    dataOut.writeInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Send packet of requested data on connection to remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param buf array containg bytes to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param off offset of first array index of packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @param len number of bytes in packet to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    void sendTransmit(MultiplexConnectionInfo info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                      byte buf[], int off, int len) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    dataOut.writeByte(TRANSMIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    dataOut.writeInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    dataOut.write(buf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Inform remote endpoint that connection has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    void sendClose(MultiplexConnectionInfo info) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    dataOut.writeByte(CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    info.closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Acknowledge remote endpoint's closing of connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    void sendCloseAck(MultiplexConnectionInfo info) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    dataOut.writeByte(CLOSEACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    info.closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Shut down connection upon finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    protected void finalize() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
}