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