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