src/java.rmi/share/classes/sun/rmi/transport/tcp/TCPTransport.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 47032 jdk/src/java.rmi/share/classes/sun/rmi/transport/tcp/TCPTransport.java@98e444a1b204
child 59300 60bdcb9a7b94
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37892
55da13d60938 8155978: Remove HTTP proxy implementation and tests from RMI
rriggs
parents: 37667
diff changeset
     2
 * Copyright (c) 1996, 2016, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
13408
7b1c9c089326 7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents: 12040
diff changeset
    31
import java.lang.reflect.UndeclaredThrowableException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.ServerSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.rmi.RemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.server.ExportException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.rmi.server.LogStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.rmi.server.RMIFailureHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.rmi.server.RMISocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.rmi.server.RemoteCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.rmi.server.ServerNotActiveException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.rmi.server.UID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.AccessController;
28552
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
    52
import java.security.Permissions;
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    53
import java.security.PrivilegedAction;
28552
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
    54
import java.security.ProtectionDomain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.util.concurrent.ExecutorService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.util.concurrent.RejectedExecutionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.util.concurrent.SynchronousQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.util.concurrent.ThreadFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.util.concurrent.ThreadPoolExecutor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.util.concurrent.TimeUnit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.rmi.runtime.Log;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import sun.rmi.runtime.NewThreadAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.rmi.transport.Channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import sun.rmi.transport.Connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.rmi.transport.DGCAckHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import sun.rmi.transport.Endpoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import sun.rmi.transport.StreamRemoteCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import sun.rmi.transport.Target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import sun.rmi.transport.Transport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import sun.rmi.transport.TransportConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * TCPTransport is the socket-based implementation of the RMI Transport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * abstraction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @author Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
19211
32a04c562026 8022440: suppress deprecation warnings in sun.rmi
smarks
parents: 14342
diff changeset
    86
@SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
public class TCPTransport extends Transport {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /* tcp package log */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final Log tcpLog = Log.getLog("sun.rmi.transport.tcp", "tcp",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        LogStream.parseLevel(AccessController.doPrivileged(
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    92
            (PrivilegedAction<String>) () -> System.getProperty("sun.rmi.transport.tcp.logLevel"))));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /** maximum number of connection handler threads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static final int maxConnectionThreads =     // default no limit
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    96
        AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    97
            Integer.getInteger("sun.rmi.transport.tcp.maxConnectionThreads",
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
    98
                               Integer.MAX_VALUE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /** keep alive time for idle connection handler threads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final long threadKeepAliveTime =     // default 1 minute
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
   102
        AccessController.doPrivileged((PrivilegedAction<Long>) () ->
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
   103
            Long.getLong("sun.rmi.transport.tcp.threadKeepAliveTime", 60000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** thread pool for connection handlers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static final ExecutorService connectionThreadPool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        new ThreadPoolExecutor(0, maxConnectionThreads,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            threadKeepAliveTime, TimeUnit.MILLISECONDS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            new SynchronousQueue<Runnable>(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            new ThreadFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                public Thread newThread(Runnable runnable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    return AccessController.doPrivileged(new NewThreadAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        runnable, "TCP Connection(idle)", true, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /** total connections handled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static final AtomicInteger connectionCount = new AtomicInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /** client host for the current thread's connection */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final ThreadLocal<ConnectionHandler>
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   122
        threadConnectionHandler = new ThreadLocal<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
28552
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   124
    /** an AccessControlContext with no permissions */
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   125
    private static final AccessControlContext NOPERMS_ACC;
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   126
    static {
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   127
        Permissions perms = new Permissions();
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   128
        ProtectionDomain[] pd = { new ProtectionDomain(null, perms) };
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   129
        NOPERMS_ACC = new AccessControlContext(pd);
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   130
    }
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   131
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /** endpoints for this transport */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private final LinkedList<TCPEndpoint> epList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /** number of objects exported on this transport */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private int exportCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /** server socket for this transport */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private ServerSocket server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /** table mapping endpoints to channels */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private final Map<TCPEndpoint,Reference<TCPChannel>> channelTable =
12040
558b0e0d5910 7146763: Warnings cleanup in the sun.rmi and related packages
khazra
parents: 5506
diff changeset
   140
        new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static final RMISocketFactory defaultSocketFactory =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        RMISocketFactory.getDefaultSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /** number of milliseconds in accepted-connection timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Warning: this should be greater than 15 seconds (the client-side
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * timeout), and defaults to 2 hours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The maximum representable value is slightly more than 24 days
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * and 20 hours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private static final int connectionReadTimeout =    // default 2 hours
23333
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
   152
        AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
b0af2c7c8c91 8035808: Eliminate dependency to GetPropertyAction and other sun.security.action convenient classes
mchung
parents: 19211
diff changeset
   153
            Integer.getInteger("sun.rmi.transport.tcp.readTimeout", 2 * 3600 * 1000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Constructs a TCPTransport.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    TCPTransport(LinkedList<TCPEndpoint> epList)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // assert ((epList.size() != null) && (epList.size() >= 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.epList = epList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            tcpLog.log(Log.BRIEF, "Version = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                TransportConstants.Version + ", ep = " + getEndpoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Closes all cached connections in every channel subordinated to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * transport.  Currently, this only closes outgoing connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public void shedConnectionCaches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        List<TCPChannel> channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        synchronized (channelTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            channels = new ArrayList<TCPChannel>(channelTable.values().size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            for (Reference<TCPChannel> ref : channelTable.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                TCPChannel ch = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (ch != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    channels.add(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        for (TCPChannel channel : channels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            channel.shedCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns a <I>Channel</I> that generates connections to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * endpoint <I>ep</I>. A Channel is an object that creates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * manages connections of a particular type to some particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * address space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param ep the endpoint to which connections will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return the channel or null if the transport cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * generate connections to this endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public TCPChannel getChannel(Endpoint ep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        TCPChannel ch = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (ep instanceof TCPEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            synchronized (channelTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                Reference<TCPChannel> ref = channelTable.get(ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    ch = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                if (ch == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    TCPEndpoint tcpEndpoint = (TCPEndpoint) ep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    ch = new TCPChannel(this, tcpEndpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    channelTable.put(tcpEndpoint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                     new WeakReference<TCPChannel>(ch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Removes the <I>Channel</I> that generates connections to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * endpoint <I>ep</I>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void free(Endpoint ep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (ep instanceof TCPEndpoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            synchronized (channelTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                Reference<TCPChannel> ref = channelTable.remove(ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    TCPChannel channel = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    if (channel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        channel.shedCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Export the object so that it can accept incoming calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public void exportObject(Target target) throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * Ensure that a server socket is listening, and count this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * export while synchronized to prevent the server socket from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         * being closed due to concurrent unexports.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            listen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            exportCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * Try to add the Target to the exported object table; keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * counting this export (to keep server socket open) only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * that succeeds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            super.exportObject(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    decrementExportCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    protected synchronized void targetUnexported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        decrementExportCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Decrements the count of exported objects, closing the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * server socket if the count reaches zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void decrementExportCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        exportCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (exportCount == 0 && getEndpoint().getListenPort() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            ServerSocket ss = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Verify that the current access control context has permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * accept the connection being dispatched by the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    protected void checkAcceptPermission(AccessControlContext acc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        ConnectionHandler h = threadConnectionHandler.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (h == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throw new Error(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                "checkAcceptPermission not in ConnectionHandler thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        h.checkAcceptPermission(sm, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private TCPEndpoint getEndpoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        synchronized (epList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return epList.getLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Listen on transport's endpoint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private void listen() throws RemoteException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        TCPEndpoint ep = getEndpoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int port = ep.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (server == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    "(port " + port + ") create server socket");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                server = ep.newServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                 * Don't retry ServerSocket if creation fails since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                 * "port in use" will cause export to hang if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                 * RMIFailureHandler is not installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                Thread t = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    new NewThreadAction(new AcceptLoop(server),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                        "TCP Accept-" + port, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } catch (java.net.BindException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                throw new ExportException("Port already in use: " + port, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                throw new ExportException("Listen failed on port: " + port, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            // otherwise verify security access to existing server socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                sm.checkListen(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Worker for accepting connections from a server socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private class AcceptLoop implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        private final ServerSocket serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // state for throttling loop on exceptions (local to accept thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        private long lastExceptionTime = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        private int recentExceptionCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        AcceptLoop(ServerSocket serverSocket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            this.serverSocket = serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                executeAcceptLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                     * Only one accept loop is started per server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                     * socket, so after no more connections will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                     * accepted, ensure that the server socket is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                     * longer listening.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    serverSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * Accepts connections from the server socket and executes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * handlers for them in the thread pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        private void executeAcceptLoop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                tcpLog.log(Log.BRIEF, "listening on port " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                           getEndpoint().getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                Socket socket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    socket = serverSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                     * Find client host name (or "0.0.0.0" if unknown)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    InetAddress clientAddr = socket.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    String clientHost = (clientAddr != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                         ? clientAddr.getHostAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                         : "0.0.0.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                     * Execute connection handler in the thread pool,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                     * which uses non-system threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        connectionThreadPool.execute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                            new ConnectionHandler(socket, clientHost));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    } catch (RejectedExecutionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        closeSocket(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        tcpLog.log(Log.BRIEF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                   "rejected connection from " + clientHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                         * If the server socket has been closed, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                         * as because there are no more exported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                         * objects, then we expect accept to throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                         * exception, so just terminate normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        if (serverSocket.isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                            if (tcpLog.isLoggable(Level.WARNING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                tcpLog.log(Level.WARNING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                           "accept loop for " + serverSocket +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                           " throws", t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        } catch (Throwable tt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                         * Always close the accepted socket (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                         * if an exception occurs, but only after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                         * logging an unexpected exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        if (socket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                            closeSocket(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                     * In case we're running out of file descriptors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                     * release resources held in caches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    if (!(t instanceof SecurityException)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                            TCPEndpoint.shedConnectionCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        } catch (Throwable tt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                     * A NoClassDefFoundError can occur if no file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                     * descriptors are available, in which case this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                     * loop should not terminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    if (t instanceof Exception ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        t instanceof OutOfMemoryError ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        t instanceof NoClassDefFoundError)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        if (!continueAfterAcceptFailure(t)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        // continue loop
13408
7b1c9c089326 7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents: 12040
diff changeset
   472
                    } else if (t instanceof Error) {
7b1c9c089326 7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents: 12040
diff changeset
   473
                        throw (Error) t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    } else {
13408
7b1c9c089326 7187876: ClassCastException in TCPTransport.executeAcceptLoop
dmocek
parents: 12040
diff changeset
   475
                        throw new UndeclaredThrowableException(t);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         * Returns true if the accept loop should continue after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         * specified exception has been caught, or false if the accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         * loop should terminate (closing the server socket).  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         * there is an RMIFailureHandler, this method returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         * result of passing the specified exception to it; otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * this method always returns true, after sleeping to throttle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * the accept loop if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        private boolean continueAfterAcceptFailure(Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            RMIFailureHandler fh = RMISocketFactory.getFailureHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (fh != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                return fh.failure(t instanceof Exception ? (Exception) t :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                  new InvocationTargetException(t));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                throttleLoopOnException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * Throttles the accept loop after an exception has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * caught: if a burst of 10 exceptions in 5 seconds occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * then wait for 10 seconds to curb busy CPU usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        private void throttleLoopOnException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            if (lastExceptionTime == 0L || (now - lastExceptionTime) > 5000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                // last exception was long ago (or this is the first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                lastExceptionTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                recentExceptionCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                // exception burst window was started recently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                if (++recentExceptionCount >= 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                        Thread.sleep(10000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    } catch (InterruptedException ignore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /** close socket and eat exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    private static void closeSocket(Socket sock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            sock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            // eat exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * handleMessages decodes transport operations and handles messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * appropriately.  If an exception occurs during message handling,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    void handleMessages(Connection conn, boolean persistent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        int port = getEndpoint().getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            DataInputStream in = new DataInputStream(conn.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                int op = in.read();     // transport op
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (op == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        tcpLog.log(Log.BRIEF, "(port " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                            port + ") connection closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    tcpLog.log(Log.BRIEF, "(port " + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        ") op = " + op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                switch (op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                case TransportConstants.Call:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    // service incoming RMI call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    RemoteCall call = new StreamRemoteCall(conn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    if (serviceCall(call) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                case TransportConstants.Ping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    // send ack for ping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    DataOutputStream out =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        new DataOutputStream(conn.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    out.writeByte(TransportConstants.PingAck);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    conn.releaseOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                case TransportConstants.DGCAck:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    DGCAckHandler.received(UID.read(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    throw new IOException("unknown transport op " + op);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } while (persistent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            // exception during processing causes connection to close (below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                tcpLog.log(Log.BRIEF, "(port " + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    ") exception: ", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                conn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                // eat exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Returns the client host for the current thread's connection.  Throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * ServerNotActiveException if no connection is active for this thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public static String getClientHost() throws ServerNotActiveException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        ConnectionHandler h = threadConnectionHandler.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (h != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            return h.getClientHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            throw new ServerNotActiveException("not in a remote call");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Services messages on accepted connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    private class ConnectionHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        /** int value of "POST" in ASCII (Java's specified data formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         *  make this once-reviled tactic again socially acceptable) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        private static final int POST = 0x504f5354;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        /** most recently accept-authorized AccessControlContext */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        private AccessControlContext okContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        /** cache of accept-authorized AccessControlContexts */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        private Map<AccessControlContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    Reference<AccessControlContext>> authCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        /** security manager which authorized contexts in authCache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        private SecurityManager cacheSecurityManager = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        private Socket socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        private String remoteHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        ConnectionHandler(Socket socket, String remoteHost) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            this.socket = socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            this.remoteHost = remoteHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        String getClientHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return remoteHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * Verify that the given AccessControlContext has permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * accept this connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        void checkAcceptPermission(SecurityManager sm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                   AccessControlContext acc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
             * Note: no need to synchronize on cache-related fields, since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
             * method only gets called from the ConnectionHandler's thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            if (sm != cacheSecurityManager) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                okContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                authCache = new WeakHashMap<AccessControlContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                                            Reference<AccessControlContext>>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                cacheSecurityManager = sm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            if (acc.equals(okContext) || authCache.containsKey(acc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            InetAddress addr = socket.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            String host = (addr != null) ? addr.getHostAddress() : "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            sm.checkAccept(host, socket.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            authCache.put(acc, new SoftReference<AccessControlContext>(acc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            okContext = acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        public void run() {
28557
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   670
            Thread t = Thread.currentThread();
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   671
            String name = t.getName();
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   672
            try {
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   673
                t.setName("RMI TCP Connection(" +
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   674
                          connectionCount.incrementAndGet() +
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   675
                          ")-" + remoteHost);
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   676
                AccessController.doPrivileged((PrivilegedAction<Void>)() -> {
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   677
                    run0();
28552
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   678
                    return null;
608626229264 8055309: RMI needs better transportation considerations
smarks
parents: 25859
diff changeset
   679
                }, NOPERMS_ACC);
28557
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   680
            } finally {
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   681
                t.setName(name);
ce28716935ce 8062807: Exporting RMI objects fails when run under restrictive SecurityManager
smarks
parents: 28552
diff changeset
   682
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
47032
98e444a1b204 8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents: 41883
diff changeset
   685
        @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        private void run0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            TCPEndpoint endpoint = getEndpoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            int port = endpoint.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            threadConnectionHandler.set(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            // set socket to disable Nagle's algorithm (always send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            // immediately)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            // TBD: should this be left up to socket factory instead?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                socket.setTcpNoDelay(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                // if we fail to set this, ignore and proceed anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            // set socket to timeout after excessive idle time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                if (connectionReadTimeout > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    socket.setSoTimeout(connectionReadTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                // too bad, continue anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                InputStream sockIn = socket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                InputStream bufIn = sockIn.markSupported()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        ? sockIn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                        : new BufferedInputStream(sockIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
37892
55da13d60938 8155978: Remove HTTP proxy implementation and tests from RMI
rriggs
parents: 37667
diff changeset
   714
                // Read magic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                DataInputStream in = new DataInputStream(bufIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                int magic = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                // read and verify transport header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                short version = in.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                if (magic != TransportConstants.Magic ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    version != TransportConstants.Version) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    // protocol mismatch detected...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    // just close socket: this would recurse if we marshal an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    // exception to the client and the protocol at other end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    // doesn't match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    closeSocket(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                OutputStream sockOut = socket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                BufferedOutputStream bufOut =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    new BufferedOutputStream(sockOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                DataOutputStream out = new DataOutputStream(bufOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                int remotePort = socket.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                if (tcpLog.isLoggable(Log.BRIEF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    tcpLog.log(Log.BRIEF, "accepted socket from [" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                     remoteHost + ":" + remotePort + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                TCPEndpoint ep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                TCPChannel ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                TCPConnection conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                // send ack (or nack) for protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                byte protocol = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                switch (protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                case TransportConstants.SingleOpProtocol:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    // no ack for protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    // create dummy channel for receiving messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    ep = new TCPEndpoint(remoteHost, socket.getLocalPort(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                         endpoint.getClientSocketFactory(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                                         endpoint.getServerSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    ch = new TCPChannel(TCPTransport.this, ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    conn = new TCPConnection(ch, socket, bufIn, bufOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    // read input messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    handleMessages(conn, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                case TransportConstants.StreamProtocol:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    // send ack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    out.writeByte(TransportConstants.ProtocolAck);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    // suggest endpoint (in case client doesn't know host name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    if (tcpLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                        tcpLog.log(Log.VERBOSE, "(port " + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                            ") " + "suggesting " + remoteHost + ":" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                            remotePort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    out.writeUTF(remoteHost);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    out.writeInt(remotePort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    // read and discard (possibly bogus) endpoint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    // REMIND: would be faster to read 2 bytes then skip N+4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    String clientHost = in.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    int    clientPort = in.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    if (tcpLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        tcpLog.log(Log.VERBOSE, "(port " + port +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                            ") client using " + clientHost + ":" + clientPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    // create dummy channel for receiving messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    // (why not use clientHost and clientPort?)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    ep = new TCPEndpoint(remoteHost, socket.getLocalPort(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                                         endpoint.getClientSocketFactory(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                                         endpoint.getServerSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    ch = new TCPChannel(TCPTransport.this, ep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    conn = new TCPConnection(ch, socket, bufIn, bufOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    // read input messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    handleMessages(conn, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                case TransportConstants.MultiplexProtocol:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    if (tcpLog.isLoggable(Log.VERBOSE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                        tcpLog.log(Log.VERBOSE, "(port " + port +
47032
98e444a1b204 8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents: 41883
diff changeset
   802
                                ") rejecting multiplex protocol");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    }
47032
98e444a1b204 8087189: RMI server-side multiplex protocol support should be removed
rriggs
parents: 41883
diff changeset
   804
                    // Fall-through to reject use of MultiplexProtocol
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                    // protocol not understood, send nack and close socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    out.writeByte(TransportConstants.ProtocolNack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                // socket in unknown state: destroy socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                tcpLog.log(Log.BRIEF, "terminated with exception:", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                closeSocket(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
}