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