jdk/src/share/classes/sun/rmi/transport/tcp/ConnectionMultiplexer.java
author mchung
Fri, 07 Mar 2014 13:00:25 -0800
changeset 23333 b0af2c7c8c91
parent 21278 ef8a3a2a72f2
permissions -rw-r--r--
8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 14342
diff changeset
     2
 * Copyright (c) 1996, 2013, 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;
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 21278
diff changeset
    30
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * ConnectionMultiplexer manages the transparent multiplexing of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * multiple virtual connections from one endpoint to another through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * one given real connection to that endpoint.  The input and output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * streams for the the underlying real connection must be supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A callback object is also supplied to be informed of new virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * connections opened by the remote endpoint.  After creation, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * run() method must be called in a thread created for demultiplexing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the connections.  The openConnection() method is called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * initiate a virtual connection from this endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 14342
diff changeset
    47
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class ConnectionMultiplexer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /** "multiplex" log level */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static int logLevel = LogStream.parseLevel(getLogLevel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static String getLogLevel() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    54
        return java.security.AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 21278
diff changeset
    55
           (PrivilegedAction<String>) () -> System.getProperty("sun.rmi.transport.tcp.multiplex.logLevel"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /* multiplex system log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static final Log multiplexLog =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Log.getLog("sun.rmi.transport.tcp.multiplex",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                   "multiplex", ConnectionMultiplexer.logLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** multiplexing protocol operation codes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final static int OPEN     = 0xE1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final static int CLOSE    = 0xE2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final static int CLOSEACK = 0xE3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final static int REQUEST  = 0xE4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final static int TRANSMIT = 0xE5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /** object to notify for new connections from remote endpoint */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private TCPChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** input stream for underlying single connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** output stream for underlying single connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** true if underlying connection originated from this endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        (used for generating unique connection IDs) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private boolean orig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /** layered stream for reading formatted data from underlying connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private DataInputStream dataIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** layered stream for writing formatted data to underlying connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private DataOutputStream dataOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** 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
    90
    private Hashtable<Integer, MultiplexConnectionInfo> connectionTable = new Hashtable<>(7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /** number of currently open connections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private int numConnections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** maximum allowed open connections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private final static int maxConnections = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /** ID of last connection opened */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int lastID = 0x1001;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /** true if this mechanism is still alive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private boolean alive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Create a new ConnectionMultiplexer using the given underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * input/output stream pair.  The run method must be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * (possibly on a new thread) to handle the demultiplexing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param channel object to notify when new connection is received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param in input stream of underlying connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param out output stream of underlying connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param orig true if this endpoint intiated the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *        connection (needs to be set differently at both ends)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public ConnectionMultiplexer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        TCPChannel    channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        InputStream   in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        OutputStream  out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        boolean       orig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.in      = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.out     = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.orig    = orig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        dataIn = new DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        dataOut = new DataOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Process multiplexing protocol received from underlying connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void run() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int op, id, length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            MultiplexConnectionInfo info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // read next op code from remote endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                op = dataIn.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                switch (op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // remote endpoint initiating new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                case OPEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        multiplexLog.log(Log.VERBOSE, "operation  OPEN " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   152
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    if (info != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                            "OPEN: Connection ID already exists");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    info = new MultiplexConnectionInfo(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    info.in = new MultiplexInputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    info.out = new MultiplexOutputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    synchronized (connectionTable) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   160
                        connectionTable.put(id, info);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        ++ numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    sun.rmi.transport.Connection conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    conn = new TCPConnection(channel, info.in, info.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    channel.acceptMultiplexConnection(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                // remote endpoint closing connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                case CLOSE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        multiplexLog.log(Log.VERBOSE, "operation  CLOSE " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   176
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                            "CLOSE: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    if (!info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        sendCloseAck(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    synchronized (connectionTable) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   185
                        connectionTable.remove(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        -- numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                // remote endpoint acknowledging close of connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                case CLOSEACK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    id = dataIn.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                            "operation  CLOSEACK " + id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   199
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            "CLOSEACK: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    if (!info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            "CLOSEACK: Connection not closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    synchronized (connectionTable) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   209
                        connectionTable.remove(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        -- numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                // remote endpoint declaring additional bytes receivable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                case REQUEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    id = dataIn.readUnsignedShort();
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   217
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                            "REQUEST: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    length = dataIn.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            "operation  REQUEST " + id + ": " + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    info.out.request(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                // remote endpoint transmitting data packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                case TRANSMIT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    id = dataIn.readUnsignedShort();
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   234
                    info = connectionTable.get(id);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    if (info == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        throw new IOException("SEND: Invalid connection ID");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    length = dataIn.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        multiplexLog.log(Log.VERBOSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                            "operation  TRANSMIT " + id + ": " + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    info.in.receive(length, dataIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    throw new IOException("Invalid operation: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                          Integer.toHexString(op));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Initiate a new multiplexed connection through the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public synchronized TCPConnection openConnection() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // generate ID that should not be already used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // If all possible 32768 IDs are used,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        // this method will block searching for a new ID forever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        int id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            lastID = (++ lastID) & 0x7FFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            id = lastID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // The orig flag (copied to the high bit of the ID) is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // to have two distinct ranges to choose IDs from for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // two endpoints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (orig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                id |= 0x8000;
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   276
        } while (connectionTable.get(id) != null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // create multiplexing streams and bookkeeping information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        MultiplexConnectionInfo info = new MultiplexConnectionInfo(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        info.in = new MultiplexInputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        info.out = new MultiplexOutputStream(this, info, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // add to connection table if multiplexer has not died
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (!alive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                throw new IOException("Multiplexer connection dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (numConnections >= maxConnections)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                throw new IOException("Cannot exceed " + maxConnections +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    " simultaneous multiplexed connections");
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   290
            connectionTable.put(id, info);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            ++ numConnections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // inform remote endpoint of new connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                dataOut.writeByte(OPEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                dataOut.writeShort(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return new TCPConnection(channel, info.in, info.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Shut down all connections and clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public void shutDown()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // inform all associated streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        synchronized (connectionTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            // return if multiplexer already officially dead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (!alive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            alive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   323
            Enumeration<MultiplexConnectionInfo> enum_ =
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   324
                    connectionTable.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            while (enum_.hasMoreElements()) {
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   326
                MultiplexConnectionInfo info = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                info.in.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            connectionTable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            numConnections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // close underlying connection, if possible (and not already done)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Send request for more data on connection to remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param len number of more bytes that can be received
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    void sendRequest(MultiplexConnectionInfo info, int len) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    dataOut.writeByte(REQUEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    dataOut.writeInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    throw e;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Send packet of requested data on connection to remote endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param info connection information structure
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19211
diff changeset
   371
     * @param buf array containing bytes to send
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param off offset of first array index of packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @param len number of bytes in packet to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    void sendTransmit(MultiplexConnectionInfo info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                      byte buf[], int off, int len) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    dataOut.writeByte(TRANSMIT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    dataOut.writeInt(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    dataOut.write(buf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    throw e;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Inform remote endpoint that connection has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    void sendClose(MultiplexConnectionInfo info) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        info.out.disconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    dataOut.writeByte(CLOSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    info.closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    throw e;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Acknowledge remote endpoint's closing of connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param info connection information structure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    void sendCloseAck(MultiplexConnectionInfo info) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        synchronized (dataOut) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (alive && !info.closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    dataOut.writeByte(CLOSEACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    dataOut.writeShort(info.id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    dataOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    info.closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    multiplexLog.log(Log.BRIEF, "exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    throw e;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Shut down connection upon finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    protected void finalize() throws Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        shutDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}