aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/sun/rmi/transport/tcp/ConnectionMultiplexer.java
blob: f756cd34f74fa4759df1be8e08389613b482bc4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright 1996-2003 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */
package sun.rmi.transport.tcp;

import java.io.*;
import java.util.*;
import java.rmi.server.LogStream;

import sun.rmi.runtime.Log;

/**
 * ConnectionMultiplexer manages the transparent multiplexing of
 * multiple virtual connections from one endpoint to another through
 * one given real connection to that endpoint.  The input and output
 * streams for the the underlying real connection must be supplied.
 * A callback object is also supplied to be informed of new virtual
 * connections opened by the remote endpoint.  After creation, the
 * run() method must be called in a thread created for demultiplexing
 * the connections.  The openConnection() method is called to
 * initiate a virtual connection from this endpoint.
 *
 * @author Peter Jones
 */
final class ConnectionMultiplexer {

    /** "multiplex" log level */
    static int logLevel = LogStream.parseLevel(getLogLevel());

    private static String getLogLevel() {
        return (String) java.security.AccessController.doPrivileged(
            new sun.security.action.GetPropertyAction("sun.rmi.transport.tcp.multiplex.logLevel"));
    }

    /* multiplex system log */
    static final Log multiplexLog =
        Log.getLog("sun.rmi.transport.tcp.multiplex",
                   "multiplex", ConnectionMultiplexer.logLevel);

    /** multiplexing protocol operation codes */
    private final static int OPEN     = 0xE1;
    private final static int CLOSE    = 0xE2;
    private final static int CLOSEACK = 0xE3;
    private final static int REQUEST  = 0xE4;
    private final static int TRANSMIT = 0xE5;

    /** object to notify for new connections from remote endpoint */
    private TCPChannel channel;

    /** input stream for underlying single connection */
    private InputStream in;

    /** output stream for underlying single connection */
    private OutputStream out;

    /** true if underlying connection originated from this endpoint
        (used for generating unique connection IDs) */
    private boolean orig;

    /** layered stream for reading formatted data from underlying connection */
    private DataInputStream dataIn;

    /** layered stream for writing formatted data to underlying connection */
    private DataOutputStream dataOut;

    /** table holding currently open connection IDs and related info */
    private Hashtable connectionTable = new Hashtable(7);

    /** number of currently open connections */
    private int numConnections = 0;

    /** maximum allowed open connections */
    private final static int maxConnections = 256;

    /** ID of last connection opened */
    private int lastID = 0x1001;

    /** true if this mechanism is still alive */
    private boolean alive = true;

    /**
     * Create a new ConnectionMultiplexer using the given underlying
     * input/output stream pair.  The run method must be called
     * (possibly on a new thread) to handle the demultiplexing.
     * @param channel object to notify when new connection is received
     * @param in input stream of underlying connection
     * @param out output stream of underlying connection
     * @param orig true if this endpoint intiated the underlying
     *        connection (needs to be set differently at both ends)
     */
    public ConnectionMultiplexer(
        TCPChannel    channel,
        InputStream   in,
        OutputStream  out,
        boolean       orig)
    {
        this.channel = channel;
        this.in      = in;
        this.out     = out;
        this.orig    = orig;

        dataIn = new DataInputStream(in);
        dataOut = new DataOutputStream(out);
    }

    /**
     * Process multiplexing protocol received from underlying connection.
     */
    public void run() throws IOException
    {
        try {
            int op, id, length;
            Integer idObj;
            MultiplexConnectionInfo info;

            while (true) {

                // read next op code from remote endpoint
                op = dataIn.readUnsignedByte();
                switch (op) {

                // remote endpoint initiating new connection
                case OPEN:
                    id = dataIn.readUnsignedShort();

                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
                        multiplexLog.log(Log.VERBOSE, "operation  OPEN " + id);
                    }

                    idObj = new Integer(id);
                    info =
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
                    if (info != null)
                        throw new IOException(
                            "OPEN: Connection ID already exists");
                    info = new MultiplexConnectionInfo(id);
                    info.in = new MultiplexInputStream(this, info, 2048);
                    info.out = new MultiplexOutputStream(this, info, 2048);
                    synchronized (connectionTable) {
                        connectionTable.put(idObj, info);
                        ++ numConnections;
                    }
                    sun.rmi.transport.Connection conn;
                    conn = new TCPConnection(channel, info.in, info.out);
                    channel.acceptMultiplexConnection(conn);
                    break;

                // remote endpoint closing connection
                case CLOSE:
                    id = dataIn.readUnsignedShort();

                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
                        multiplexLog.log(Log.VERBOSE, "operation  CLOSE " + id);
                    }

                    idObj = new Integer(id);
                    info =
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
                    if (info == null)
                        throw new IOException(
                            "CLOSE: Invalid connection ID");
                    info.in.disconnect();
                    info.out.disconnect();
                    if (!info.closed)
                        sendCloseAck(info);
                    synchronized (connectionTable) {
                        connectionTable.remove(idObj);
                        -- numConnections;
                    }
                    break;

                // remote endpoint acknowledging close of connection
                case CLOSEACK:
                    id = dataIn.readUnsignedShort();

                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
                        multiplexLog.log(Log.VERBOSE,
                            "operation  CLOSEACK " + id);
                    }

                    idObj = new Integer(id);
                    info =
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
                    if (info == null)
                        throw new IOException(
                            "CLOSEACK: Invalid connection ID");
                    if (!info.closed)
                        throw new IOException(
                            "CLOSEACK: Connection not closed");
                    info.in.disconnect();
                    info.out.disconnect();
                    synchronized (connectionTable) {
                        connectionTable.remove(idObj);
                        -- numConnections;
                    }
                    break;

                // remote endpoint declaring additional bytes receivable
                case REQUEST:
                    id = dataIn.readUnsignedShort();
                    idObj = new Integer(id);
                    info =
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
                    if (info == null)
                        throw new IOException(
                            "REQUEST: Invalid connection ID");
                    length = dataIn.readInt();

                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
                        multiplexLog.log(Log.VERBOSE,
                            "operation  REQUEST " + id + ": " + length);
                    }

                    info.out.request(length);
                    break;

                // remote endpoint transmitting data packet
                case TRANSMIT:
                    id = dataIn.readUnsignedShort();
                    idObj = new Integer(id);
                    info =
                        (MultiplexConnectionInfo) connectionTable.get(idObj);
                    if (info == null)
                        throw new IOException("SEND: Invalid connection ID");
                    length = dataIn.readInt();

                    if (multiplexLog.isLoggable(Log.VERBOSE)) {
                        multiplexLog.log(Log.VERBOSE,
                            "operation  TRANSMIT " + id + ": " + length);
                    }

                    info.in.receive(length, dataIn);
                    break;

                default:
                    throw new IOException("Invalid operation: " +
                                          Integer.toHexString(op));
                }
            }
        } finally {
            shutDown();
        }
    }

    /**
     * Initiate a new multiplexed connection through the underlying
     * connection.
     */
    public synchronized TCPConnection openConnection() throws IOException
    {
        // generate ID that should not be already used
        // If all possible 32768 IDs are used,
        // this method will block searching for a new ID forever.
        int id;
        Integer idObj;
        do {
            lastID = (++ lastID) & 0x7FFF;
            id = lastID;

            // The orig flag (copied to the high bit of the ID) is used
            // to have two distinct ranges to choose IDs from for the
            // two endpoints.
            if (orig)
                id |= 0x8000;
            idObj = new Integer(id);
        } while (connectionTable.get(idObj) != null);

        // create multiplexing streams and bookkeeping information
        MultiplexConnectionInfo info = new MultiplexConnectionInfo(id);
        info.in = new MultiplexInputStream(this, info, 2048);
        info.out = new MultiplexOutputStream(this, info, 2048);

        // add to connection table if multiplexer has not died
        synchronized (connectionTable) {
            if (!alive)
                throw new IOException("Multiplexer connection dead");
            if (numConnections >= maxConnections)
                throw new IOException("Cannot exceed " + maxConnections +
                    " simultaneous multiplexed connections");
            connectionTable.put(idObj, info);
            ++ numConnections;
        }

        // inform remote endpoint of new connection
        synchronized (dataOut) {
            try {
                dataOut.writeByte(OPEN);
                dataOut.writeShort(id);
                dataOut.flush();
            } catch (IOException e) {
                multiplexLog.log(Log.BRIEF, "exception: ", e);

                shutDown();
                throw e;
            }
        }

        return new TCPConnection(channel, info.in, info.out);
    }

    /**
     * Shut down all connections and clean up.
     */
    public void shutDown()
    {
        // inform all associated streams
        synchronized (connectionTable) {
            // return if multiplexer already officially dead
            if (!alive)
                return;
            alive = false;

            Enumeration enum_ = connectionTable.elements();
            while (enum_.hasMoreElements()) {
                MultiplexConnectionInfo info =
                    (MultiplexConnectionInfo) enum_.nextElement();
                info.in.disconnect();
                info.out.disconnect();
            }
            connectionTable.clear();
            numConnections = 0;
        }

        // close underlying connection, if possible (and not already done)
        try {
            in.close();
        } catch (IOException e) {
        }
        try {
            out.close();
        } catch (IOException e) {
        }
    }

    /**
     * Send request for more data on connection to remote endpoint.
     * @param info connection information structure
     * @param len number of more bytes that can be received
     */
    void sendRequest(MultiplexConnectionInfo info, int len) throws IOException
    {
        synchronized (dataOut) {
            if (alive && !info.closed)
                try {
                    dataOut.writeByte(REQUEST);
                    dataOut.writeShort(info.id);
                    dataOut.writeInt(len);
                    dataOut.flush();
                } catch (IOException e) {
                    multiplexLog.log(Log.BRIEF, "exception: ", e);

                    shutDown();
                    throw e;
                }
        }
    }

    /**
     * Send packet of requested data on connection to remote endpoint.
     * @param info connection information structure
     * @param buf array containg bytes to send
     * @param off offset of first array index of packet
     * @param len number of bytes in packet to send
     */
    void sendTransmit(MultiplexConnectionInfo info,
                      byte buf[], int off, int len) throws IOException
    {
        synchronized (dataOut) {
            if (alive && !info.closed)
                try {
                    dataOut.writeByte(TRANSMIT);
                    dataOut.writeShort(info.id);
                    dataOut.writeInt(len);
                    dataOut.write(buf, off, len);
                    dataOut.flush();
                } catch (IOException e) {
                    multiplexLog.log(Log.BRIEF, "exception: ", e);

                    shutDown();
                    throw e;
                }
        }
    }

    /**
     * Inform remote endpoint that connection has been closed.
     * @param info connection information structure
     */
    void sendClose(MultiplexConnectionInfo info) throws IOException
    {
        info.out.disconnect();
        synchronized (dataOut) {
            if (alive && !info.closed)
                try {
                    dataOut.writeByte(CLOSE);
                    dataOut.writeShort(info.id);
                    dataOut.flush();
                    info.closed = true;
                } catch (IOException e) {
                    multiplexLog.log(Log.BRIEF, "exception: ", e);

                    shutDown();
                    throw e;
                }
        }
    }

    /**
     * Acknowledge remote endpoint's closing of connection.
     * @param info connection information structure
     */
    void sendCloseAck(MultiplexConnectionInfo info) throws IOException
    {
        synchronized (dataOut) {
            if (alive && !info.closed)
                try {
                    dataOut.writeByte(CLOSEACK);
                    dataOut.writeShort(info.id);
                    dataOut.flush();
                    info.closed = true;
                } catch (IOException e) {
                    multiplexLog.log(Log.BRIEF, "exception: ", e);

                    shutDown();
                    throw e;
                }
        }
    }

    /**
     * Shut down connection upon finalization.
     */
    protected void finalize() throws Throwable
    {
        super.finalize();
        shutDown();
    }
}