corba/src/share/classes/com/sun/corba/se/impl/transport/SocketOrChannelConnectionImpl.java
author tbell
Mon, 20 Apr 2009 00:12:19 -0700
changeset 2664 a0a22a8f16bd
parent 4 02bb8761fcce
child 3291 805a72a26925
permissions -rw-r--r--
6372405: Server thread hangs when fragments don't complete because of connection abort 5104239: Java: thread deadlock 6191561: JCK15: api/org_omg/PortableInterceptor/ClientRequestInfo/index.html#RIMethods sometime hang 6486322: org.omg.CORBA.ORB.init() thread safety issue 6420980: Security issue with the com.sun.corba.se.impl.orbutil.ORBUtility class 6465377: NullPointerException for RMI ORB in 1.5.0_08 6553303: Corba application fails w/ org.omg.CORBA.COMM_FAILURE: vmcid: SUN minor code: 203 completed: No 6438259: Wrong repository ID generated by IDLJ Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
2664
a0a22a8f16bd 6372405: Server thread hangs when fragments don't complete because of connection abort
tbell
parents: 4
diff changeset
     2
 * Copyright 2001-2007 Sun Microsystems, Inc.  All Rights Reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
02bb8761fcce Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
02bb8761fcce Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
02bb8761fcce Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
02bb8761fcce Initial load
duke
parents:
diff changeset
    23
 * have any questions.
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
package com.sun.corba.se.impl.transport;
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
import java.net.InetSocketAddress;
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import java.net.Socket;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.SelectableChannel;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.SelectionKey;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.SocketChannel;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import java.util.Collections;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import java.util.Hashtable;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import java.util.HashMap;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import java.util.Map;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
import org.omg.CORBA.COMM_FAILURE;
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
import org.omg.CORBA.CompletionStatus;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
import org.omg.CORBA.DATA_CONVERSION;
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
import org.omg.CORBA.INTERNAL;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
import org.omg.CORBA.MARSHAL;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import org.omg.CORBA.OBJECT_NOT_EXIST;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
import org.omg.CORBA.SystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
import com.sun.org.omg.SendingContext.CodeBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
import com.sun.corba.se.pept.broker.Broker;
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
import com.sun.corba.se.pept.encoding.InputObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
import com.sun.corba.se.pept.encoding.OutputObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
import com.sun.corba.se.pept.protocol.MessageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
import com.sun.corba.se.pept.transport.Acceptor;
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
import com.sun.corba.se.pept.transport.Connection;
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
import com.sun.corba.se.pept.transport.ConnectionCache;
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
import com.sun.corba.se.pept.transport.ContactInfo;
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
import com.sun.corba.se.pept.transport.EventHandler;
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
import com.sun.corba.se.pept.transport.InboundConnectionCache;
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
import com.sun.corba.se.pept.transport.OutboundConnectionCache;
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
import com.sun.corba.se.pept.transport.ResponseWaitingRoom;
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
import com.sun.corba.se.pept.transport.Selector;
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
import com.sun.corba.se.spi.ior.IOR;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
import com.sun.corba.se.spi.logging.CORBALogDomains;
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
import com.sun.corba.se.spi.orb.ORB ;
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
import com.sun.corba.se.spi.orbutil.threadpool.Work;
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
import com.sun.corba.se.spi.protocol.CorbaMessageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
import com.sun.corba.se.spi.transport.CorbaContactInfo;
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
import com.sun.corba.se.spi.transport.CorbaConnection;
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
import com.sun.corba.se.spi.transport.CorbaResponseWaitingRoom;
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
import com.sun.corba.se.spi.transport.ReadTimeouts;
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
import com.sun.corba.se.impl.encoding.CachedCodeBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
import com.sun.corba.se.impl.encoding.CDRInputStream_1_0;
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
import com.sun.corba.se.impl.encoding.CDROutputObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
import com.sun.corba.se.impl.encoding.CDROutputStream_1_0;
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
import com.sun.corba.se.impl.encoding.CodeSetComponentInfo;
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry;
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
import com.sun.corba.se.impl.logging.ORBUtilSystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
import com.sun.corba.se.impl.orbutil.ORBConstants;
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
import com.sun.corba.se.impl.orbutil.ORBUtility;
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
import com.sun.corba.se.impl.transport.CorbaResponseWaitingRoomImpl;
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
 * @author Harold Carr
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
public class SocketOrChannelConnectionImpl
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
    extends
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
        EventHandlerBase
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
    implements
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
        CorbaConnection,
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
        Work
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
{
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
    public static boolean dprintWriteLocks = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
    // New transport.
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
    protected long enqueueTime;
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
    protected SocketChannel socketChannel;
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
    public SocketChannel getSocketChannel()
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
        return socketChannel;
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
    // REVISIT:
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
    // protected for test: genericRPCMSGFramework.IIOPConnection constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
    protected CorbaContactInfo contactInfo;
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
    protected Acceptor acceptor;
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
    protected ConnectionCache connectionCache;
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
    // From iiop.Connection.java
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
    protected Socket socket;    // The socket used for this connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
    protected long timeStamp = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
    protected boolean isServer = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
    // Start at some value other than zero since this is a magic
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
    // value in some protocols.
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
    protected int requestId = 5;
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
    protected CorbaResponseWaitingRoom responseWaitingRoom;
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
    protected int state;
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
    protected java.lang.Object stateEvent = new java.lang.Object();
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
    protected java.lang.Object writeEvent = new java.lang.Object();
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
    protected boolean writeLocked;
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
    protected int serverRequestCount = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
    // Server request map: used on the server side of Connection
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
    // Maps request ID to IIOPInputStream.
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
    Map serverRequestMap = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
    // This is a flag associated per connection telling us if the
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
    // initial set of sending contexts were sent to the receiver
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
    // already...
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
    protected boolean postInitialContexts = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
    // Remote reference to CodeBase server (supplies
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
    // FullValueDescription, among other things)
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
    protected IOR codeBaseServerIOR;
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
    // CodeBase cache for this connection.  This will cache remote operations,
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
    // handle connecting, and ensure we don't do any remote operations until
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
    // necessary.
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
    protected CachedCodeBase cachedCodeBase = new CachedCodeBase(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
    protected ORBUtilSystemException wrapper ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
    // transport read timeout values
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
    protected ReadTimeouts readTimeouts;
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
    protected boolean shouldReadGiopHeaderOnly;
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
    // A message mediator used when shouldReadGiopHeaderOnly is
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
    // true to maintain request message state across execution in a
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
    // SelectorThread and WorkerThread.
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
    protected CorbaMessageMediator partialMessageMediator = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
    // Used in genericRPCMSGFramework test.
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
    protected SocketOrChannelConnectionImpl(ORB orb)
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
        this.orb = orb;
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
        wrapper = ORBUtilSystemException.get( orb,
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
            CORBALogDomains.RPC_TRANSPORT ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
        setWork(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
        responseWaitingRoom = new CorbaResponseWaitingRoomImpl(orb, this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
        setReadTimeouts(orb.getORBData().getTransportTCPReadTimeouts());
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
    // Both client and servers.
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
    protected SocketOrChannelConnectionImpl(ORB orb,
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
                                            boolean useSelectThreadToWait,
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
                                            boolean useWorkerThread)
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
        this(orb) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
        setUseSelectThreadToWait(useSelectThreadToWait);
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
        setUseWorkerThreadForEvent(useWorkerThread);
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
    // Client constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
    public SocketOrChannelConnectionImpl(ORB orb,
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
                                         CorbaContactInfo contactInfo,
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
                                         boolean useSelectThreadToWait,
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
                                         boolean useWorkerThread,
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
                                         String socketType,
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
                                         String hostname,
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
                                         int port)
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
        this(orb, useSelectThreadToWait, useWorkerThread);
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
        this.contactInfo = contactInfo;
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
            socket = orb.getORBData().getSocketFactory()
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
                .createSocket(socketType,
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
                              new InetSocketAddress(hostname, port));
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
            socketChannel = socket.getChannel();
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
            if (socketChannel != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
                boolean isBlocking = !useSelectThreadToWait;
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
                socketChannel.configureBlocking(isBlocking);
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
                // IMPORTANT: non-channel-backed sockets must use
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
                // dedicated reader threads.
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
                setUseSelectThreadToWait(false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
                dprint(".initialize: connection created: " + socket);
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
        } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
            throw wrapper.connectFailure(t, socketType, hostname,
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
                                         Integer.toString(port));
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
        state = OPENING;
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
    // Client-side convenience.
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
    public SocketOrChannelConnectionImpl(ORB orb,
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
                                         CorbaContactInfo contactInfo,
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
                                         String socketType,
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
                                         String hostname,
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
                                         int port)
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
        this(orb, contactInfo,
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
             orb.getORBData().connectionSocketUseSelectThreadToWait(),
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
             orb.getORBData().connectionSocketUseWorkerThreadForEvent(),
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
             socketType, hostname, port);
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
    // Server-side constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
    public SocketOrChannelConnectionImpl(ORB orb,
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
                                         Acceptor acceptor,
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
                                         Socket socket,
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
                                         boolean useSelectThreadToWait,
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
                                         boolean useWorkerThread)
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
        this(orb, useSelectThreadToWait, useWorkerThread);
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
        this.socket = socket;
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
        socketChannel = socket.getChannel();
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
        if (socketChannel != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
            // REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
                boolean isBlocking = !useSelectThreadToWait;
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
                socketChannel.configureBlocking(isBlocking);
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
            } catch (IOException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
                RuntimeException rte = new RuntimeException();
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
                rte.initCause(e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
                throw rte;
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
        this.acceptor = acceptor;
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
        serverRequestMap = Collections.synchronizedMap(new HashMap());
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
        isServer = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
        state = ESTABLISHED;
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
    // Server-side convenience
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
    public SocketOrChannelConnectionImpl(ORB orb,
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
                                         Acceptor acceptor,
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
                                         Socket socket)
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
        this(orb, acceptor, socket,
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
             (socket.getChannel() == null
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
              ? false
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
              : orb.getORBData().connectionSocketUseSelectThreadToWait()),
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
             (socket.getChannel() == null
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
              ? false
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
              : orb.getORBData().connectionSocketUseWorkerThreadForEvent()));
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
    ////////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
    // framework.transport.Connection
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
    public boolean shouldRegisterReadEvent()
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
        return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
    public boolean shouldRegisterServerReadEvent()
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
        return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
    public boolean read()
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
                dprint(".read->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
            CorbaMessageMediator messageMediator = readBits();
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
            if (messageMediator != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
                // Null can happen when client closes stream
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
                // causing purgecalls.
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
                return dispatch(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
            return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
                dprint(".read<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
    protected CorbaMessageMediator readBits()
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
                dprint(".readBits->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
            MessageMediator messageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
            // REVISIT - use common factory base class.
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
            if (contactInfo != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
                messageMediator =
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
                    contactInfo.createMessageMediator(orb, this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
            } else if (acceptor != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
                messageMediator = acceptor.createMessageMediator(orb, this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
                throw
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
                    new RuntimeException("SocketOrChannelConnectionImpl.readBits");
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
            return (CorbaMessageMediator) messageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
        } catch (ThreadDeath td) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
                dprint(".readBits: " + this + ": ThreadDeath: " + td, td);
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
                purgeCalls(wrapper.connectionAbort(td), false, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
            } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
                    dprint(".readBits: " + this + ": purgeCalls: Throwable: " + t, t);
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
            throw td;
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
        } catch (Throwable ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
                dprint(".readBits: " + this + ": Throwable: " + ex, ex);
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
                if (ex instanceof INTERNAL) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
                    sendMessageError(GIOPVersion.DEFAULT_VERSION);
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
            } catch (IOException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
                    dprint(".readBits: " + this +
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
                           ": sendMessageError: IOException: " + e, e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
            // REVISIT - make sure reader thread is killed.
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
            orb.getTransportManager().getSelector(0).unregisterForEvent(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
            // Notify anyone waiting.
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
            purgeCalls(wrapper.connectionAbort(ex), true, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
            // REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
            //keepRunning = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
            // REVISIT - if this is called after purgeCalls then
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
            // the state of the socket is ABORT so the writeLock
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
            // in close throws an exception.  It is ignored but
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
            // causes IBM (screen scraping) tests to fail.
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
            //close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
                dprint(".readBits<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   384
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   385
        return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   386
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   387
02bb8761fcce Initial load
duke
parents:
diff changeset
   388
    protected CorbaMessageMediator finishReadingBits(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
   389
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   390
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   391
02bb8761fcce Initial load
duke
parents:
diff changeset
   392
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   393
                dprint(".finishReadingBits->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   394
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   395
02bb8761fcce Initial load
duke
parents:
diff changeset
   396
            // REVISIT - use common factory base class.
02bb8761fcce Initial load
duke
parents:
diff changeset
   397
            if (contactInfo != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   398
                messageMediator =
02bb8761fcce Initial load
duke
parents:
diff changeset
   399
                    contactInfo.finishCreatingMessageMediator(orb, this, messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
   400
            } else if (acceptor != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   401
                messageMediator =
02bb8761fcce Initial load
duke
parents:
diff changeset
   402
                    acceptor.finishCreatingMessageMediator(orb, this, messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
   403
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   404
                throw
02bb8761fcce Initial load
duke
parents:
diff changeset
   405
                    new RuntimeException("SocketOrChannelConnectionImpl.finishReadingBits");
02bb8761fcce Initial load
duke
parents:
diff changeset
   406
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   407
            return (CorbaMessageMediator) messageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
   408
02bb8761fcce Initial load
duke
parents:
diff changeset
   409
        } catch (ThreadDeath td) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   410
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   411
                dprint(".finishReadingBits: " + this + ": ThreadDeath: " + td, td);
02bb8761fcce Initial load
duke
parents:
diff changeset
   412
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   413
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   414
                purgeCalls(wrapper.connectionAbort(td), false, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   415
            } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   416
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   417
                    dprint(".finishReadingBits: " + this + ": purgeCalls: Throwable: " + t, t);
02bb8761fcce Initial load
duke
parents:
diff changeset
   418
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   419
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   420
            throw td;
02bb8761fcce Initial load
duke
parents:
diff changeset
   421
        } catch (Throwable ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   422
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   423
                dprint(".finishReadingBits: " + this + ": Throwable: " + ex, ex);
02bb8761fcce Initial load
duke
parents:
diff changeset
   424
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   425
02bb8761fcce Initial load
duke
parents:
diff changeset
   426
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   427
                if (ex instanceof INTERNAL) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   428
                    sendMessageError(GIOPVersion.DEFAULT_VERSION);
02bb8761fcce Initial load
duke
parents:
diff changeset
   429
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   430
            } catch (IOException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   431
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   432
                    dprint(".finishReadingBits: " + this +
02bb8761fcce Initial load
duke
parents:
diff changeset
   433
                           ": sendMessageError: IOException: " + e, e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   434
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   435
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   436
            // REVISIT - make sure reader thread is killed.
02bb8761fcce Initial load
duke
parents:
diff changeset
   437
            orb.getTransportManager().getSelector(0).unregisterForEvent(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   438
            // Notify anyone waiting.
02bb8761fcce Initial load
duke
parents:
diff changeset
   439
            purgeCalls(wrapper.connectionAbort(ex), true, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   440
            // REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   441
            //keepRunning = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   442
            // REVISIT - if this is called after purgeCalls then
02bb8761fcce Initial load
duke
parents:
diff changeset
   443
            // the state of the socket is ABORT so the writeLock
02bb8761fcce Initial load
duke
parents:
diff changeset
   444
            // in close throws an exception.  It is ignored but
02bb8761fcce Initial load
duke
parents:
diff changeset
   445
            // causes IBM (screen scraping) tests to fail.
02bb8761fcce Initial load
duke
parents:
diff changeset
   446
            //close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   447
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   448
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   449
                dprint(".finishReadingBits<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   450
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   451
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   452
        return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   453
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   454
02bb8761fcce Initial load
duke
parents:
diff changeset
   455
    protected boolean dispatch(CorbaMessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
   456
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   457
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   458
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   459
                dprint(".dispatch->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   460
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   461
02bb8761fcce Initial load
duke
parents:
diff changeset
   462
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
   463
            // NOTE:
02bb8761fcce Initial load
duke
parents:
diff changeset
   464
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
   465
            // This call is the transition from the tranport block
02bb8761fcce Initial load
duke
parents:
diff changeset
   466
            // to the protocol block.
02bb8761fcce Initial load
duke
parents:
diff changeset
   467
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
   468
02bb8761fcce Initial load
duke
parents:
diff changeset
   469
            boolean result =
02bb8761fcce Initial load
duke
parents:
diff changeset
   470
                messageMediator.getProtocolHandler()
02bb8761fcce Initial load
duke
parents:
diff changeset
   471
                .handleRequest(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
   472
02bb8761fcce Initial load
duke
parents:
diff changeset
   473
            return result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   474
02bb8761fcce Initial load
duke
parents:
diff changeset
   475
        } catch (ThreadDeath td) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   476
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   477
                dprint(".dispatch: ThreadDeath", td );
02bb8761fcce Initial load
duke
parents:
diff changeset
   478
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   479
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   480
                purgeCalls(wrapper.connectionAbort(td), false, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   481
            } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   482
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   483
                    dprint(".dispatch: purgeCalls: Throwable", t);
02bb8761fcce Initial load
duke
parents:
diff changeset
   484
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   485
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   486
            throw td;
02bb8761fcce Initial load
duke
parents:
diff changeset
   487
        } catch (Throwable ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   488
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   489
                dprint(".dispatch: Throwable", ex ) ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   490
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   491
02bb8761fcce Initial load
duke
parents:
diff changeset
   492
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   493
                if (ex instanceof INTERNAL) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   494
                    sendMessageError(GIOPVersion.DEFAULT_VERSION);
02bb8761fcce Initial load
duke
parents:
diff changeset
   495
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   496
            } catch (IOException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   497
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   498
                    dprint(".dispatch: sendMessageError: IOException", e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   499
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   500
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   501
            purgeCalls(wrapper.connectionAbort(ex), false, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   502
            // REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   503
            //keepRunning = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   504
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   505
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   506
                dprint(".dispatch<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   507
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   508
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   509
02bb8761fcce Initial load
duke
parents:
diff changeset
   510
        return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   511
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   512
02bb8761fcce Initial load
duke
parents:
diff changeset
   513
    public boolean shouldUseDirectByteBuffers()
02bb8761fcce Initial load
duke
parents:
diff changeset
   514
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   515
        return getSocketChannel() != null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   516
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   517
02bb8761fcce Initial load
duke
parents:
diff changeset
   518
    public ByteBuffer read(int size, int offset, int length, long max_wait_time)
02bb8761fcce Initial load
duke
parents:
diff changeset
   519
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
   520
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   521
        if (shouldUseDirectByteBuffers()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   522
02bb8761fcce Initial load
duke
parents:
diff changeset
   523
            ByteBuffer byteBuffer =
02bb8761fcce Initial load
duke
parents:
diff changeset
   524
                orb.getByteBufferPool().getByteBuffer(size);
02bb8761fcce Initial load
duke
parents:
diff changeset
   525
02bb8761fcce Initial load
duke
parents:
diff changeset
   526
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   527
                // print address of ByteBuffer gotten from pool
02bb8761fcce Initial load
duke
parents:
diff changeset
   528
                int bbAddress = System.identityHashCode(byteBuffer);
02bb8761fcce Initial load
duke
parents:
diff changeset
   529
                StringBuffer sb = new StringBuffer(80);
02bb8761fcce Initial load
duke
parents:
diff changeset
   530
                sb.append(".read: got ByteBuffer id (");
02bb8761fcce Initial load
duke
parents:
diff changeset
   531
                sb.append(bbAddress).append(") from ByteBufferPool.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   532
                String msgStr = sb.toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
   533
                dprint(msgStr);
02bb8761fcce Initial load
duke
parents:
diff changeset
   534
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   535
02bb8761fcce Initial load
duke
parents:
diff changeset
   536
            byteBuffer.position(offset);
02bb8761fcce Initial load
duke
parents:
diff changeset
   537
            byteBuffer.limit(size);
02bb8761fcce Initial load
duke
parents:
diff changeset
   538
02bb8761fcce Initial load
duke
parents:
diff changeset
   539
            readFully(byteBuffer, length, max_wait_time);
02bb8761fcce Initial load
duke
parents:
diff changeset
   540
02bb8761fcce Initial load
duke
parents:
diff changeset
   541
            return byteBuffer;
02bb8761fcce Initial load
duke
parents:
diff changeset
   542
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   543
02bb8761fcce Initial load
duke
parents:
diff changeset
   544
        byte[] buf = new byte[size];
02bb8761fcce Initial load
duke
parents:
diff changeset
   545
        readFully(getSocket().getInputStream(), buf,
02bb8761fcce Initial load
duke
parents:
diff changeset
   546
                  offset, length, max_wait_time);
02bb8761fcce Initial load
duke
parents:
diff changeset
   547
        ByteBuffer byteBuffer = ByteBuffer.wrap(buf);
02bb8761fcce Initial load
duke
parents:
diff changeset
   548
        byteBuffer.limit(size);
02bb8761fcce Initial load
duke
parents:
diff changeset
   549
        return byteBuffer;
02bb8761fcce Initial load
duke
parents:
diff changeset
   550
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   551
02bb8761fcce Initial load
duke
parents:
diff changeset
   552
    public ByteBuffer read(ByteBuffer byteBuffer, int offset,
02bb8761fcce Initial load
duke
parents:
diff changeset
   553
                           int length, long max_wait_time)
02bb8761fcce Initial load
duke
parents:
diff changeset
   554
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
   555
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   556
        int size = offset + length;
02bb8761fcce Initial load
duke
parents:
diff changeset
   557
        if (shouldUseDirectByteBuffers()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   558
02bb8761fcce Initial load
duke
parents:
diff changeset
   559
            if (! byteBuffer.isDirect()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   560
                throw wrapper.unexpectedNonDirectByteBufferWithChannelSocket();
02bb8761fcce Initial load
duke
parents:
diff changeset
   561
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   562
            if (size > byteBuffer.capacity()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   563
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   564
                    // print address of ByteBuffer being released
02bb8761fcce Initial load
duke
parents:
diff changeset
   565
                    int bbAddress = System.identityHashCode(byteBuffer);
02bb8761fcce Initial load
duke
parents:
diff changeset
   566
                    StringBuffer bbsb = new StringBuffer(80);
02bb8761fcce Initial load
duke
parents:
diff changeset
   567
                    bbsb.append(".read: releasing ByteBuffer id (")
02bb8761fcce Initial load
duke
parents:
diff changeset
   568
                        .append(bbAddress).append(") to ByteBufferPool.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   569
                    String bbmsg = bbsb.toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
   570
                    dprint(bbmsg);
02bb8761fcce Initial load
duke
parents:
diff changeset
   571
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   572
                orb.getByteBufferPool().releaseByteBuffer(byteBuffer);
02bb8761fcce Initial load
duke
parents:
diff changeset
   573
                byteBuffer = orb.getByteBufferPool().getByteBuffer(size);
02bb8761fcce Initial load
duke
parents:
diff changeset
   574
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   575
            byteBuffer.position(offset);
02bb8761fcce Initial load
duke
parents:
diff changeset
   576
            byteBuffer.limit(size);
02bb8761fcce Initial load
duke
parents:
diff changeset
   577
            readFully(byteBuffer, length, max_wait_time);
02bb8761fcce Initial load
duke
parents:
diff changeset
   578
            byteBuffer.position(0);
02bb8761fcce Initial load
duke
parents:
diff changeset
   579
            byteBuffer.limit(size);
02bb8761fcce Initial load
duke
parents:
diff changeset
   580
            return byteBuffer;
02bb8761fcce Initial load
duke
parents:
diff changeset
   581
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   582
        if (byteBuffer.isDirect()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   583
            throw wrapper.unexpectedDirectByteBufferWithNonChannelSocket();
02bb8761fcce Initial load
duke
parents:
diff changeset
   584
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   585
        byte[] buf = new byte[size];
02bb8761fcce Initial load
duke
parents:
diff changeset
   586
        readFully(getSocket().getInputStream(), buf,
02bb8761fcce Initial load
duke
parents:
diff changeset
   587
                  offset, length, max_wait_time);
02bb8761fcce Initial load
duke
parents:
diff changeset
   588
        return ByteBuffer.wrap(buf);
02bb8761fcce Initial load
duke
parents:
diff changeset
   589
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   590
02bb8761fcce Initial load
duke
parents:
diff changeset
   591
    public void readFully(ByteBuffer byteBuffer, int size, long max_wait_time)
02bb8761fcce Initial load
duke
parents:
diff changeset
   592
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
   593
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   594
        int n = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   595
        int bytecount = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   596
        long time_to_wait = readTimeouts.get_initial_time_to_wait();
02bb8761fcce Initial load
duke
parents:
diff changeset
   597
        long total_time_in_wait = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   598
02bb8761fcce Initial load
duke
parents:
diff changeset
   599
        // The reading of data incorporates a strategy to detect a
02bb8761fcce Initial load
duke
parents:
diff changeset
   600
        // rogue client. The strategy is implemented as follows. As
02bb8761fcce Initial load
duke
parents:
diff changeset
   601
        // long as data is being read, at least 1 byte or more, we
02bb8761fcce Initial load
duke
parents:
diff changeset
   602
        // assume we have a well behaved client. If no data is read,
02bb8761fcce Initial load
duke
parents:
diff changeset
   603
        // then we sleep for a time to wait, re-calculate a new time to
02bb8761fcce Initial load
duke
parents:
diff changeset
   604
        // wait which is lengthier than the previous time spent waiting.
02bb8761fcce Initial load
duke
parents:
diff changeset
   605
        // Then, if the total time spent waiting does not exceed a
02bb8761fcce Initial load
duke
parents:
diff changeset
   606
        // maximum time we are willing to wait, we attempt another
02bb8761fcce Initial load
duke
parents:
diff changeset
   607
        // read. If the maximum amount of time we are willing to
02bb8761fcce Initial load
duke
parents:
diff changeset
   608
        // spend waiting for more data is exceeded, we throw an
02bb8761fcce Initial load
duke
parents:
diff changeset
   609
        // IOException.
02bb8761fcce Initial load
duke
parents:
diff changeset
   610
02bb8761fcce Initial load
duke
parents:
diff changeset
   611
        // NOTE: Reading of GIOP headers are treated with a smaller
02bb8761fcce Initial load
duke
parents:
diff changeset
   612
        //       maximum time to wait threshold. Based on extensive
02bb8761fcce Initial load
duke
parents:
diff changeset
   613
        //       performance testing, all GIOP headers are being
02bb8761fcce Initial load
duke
parents:
diff changeset
   614
        //       read in 1 read access.
02bb8761fcce Initial load
duke
parents:
diff changeset
   615
02bb8761fcce Initial load
duke
parents:
diff changeset
   616
        do {
02bb8761fcce Initial load
duke
parents:
diff changeset
   617
            bytecount = getSocketChannel().read(byteBuffer);
02bb8761fcce Initial load
duke
parents:
diff changeset
   618
02bb8761fcce Initial load
duke
parents:
diff changeset
   619
            if (bytecount < 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   620
                throw new IOException("End-of-stream");
02bb8761fcce Initial load
duke
parents:
diff changeset
   621
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   622
            else if (bytecount == 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   623
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   624
                    Thread.sleep(time_to_wait);
02bb8761fcce Initial load
duke
parents:
diff changeset
   625
                    total_time_in_wait += time_to_wait;
02bb8761fcce Initial load
duke
parents:
diff changeset
   626
                    time_to_wait =
02bb8761fcce Initial load
duke
parents:
diff changeset
   627
                        (long)(time_to_wait*readTimeouts.get_backoff_factor());
02bb8761fcce Initial load
duke
parents:
diff changeset
   628
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   629
                catch (InterruptedException ie) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   630
                    // ignore exception
02bb8761fcce Initial load
duke
parents:
diff changeset
   631
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   632
                        dprint("readFully(): unexpected exception "
02bb8761fcce Initial load
duke
parents:
diff changeset
   633
                                + ie.toString());
02bb8761fcce Initial load
duke
parents:
diff changeset
   634
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   635
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   636
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   637
            else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   638
                n += bytecount;
02bb8761fcce Initial load
duke
parents:
diff changeset
   639
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   640
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   641
        while (n < size && total_time_in_wait < max_wait_time);
02bb8761fcce Initial load
duke
parents:
diff changeset
   642
02bb8761fcce Initial load
duke
parents:
diff changeset
   643
        if (n < size && total_time_in_wait >= max_wait_time)
02bb8761fcce Initial load
duke
parents:
diff changeset
   644
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   645
            // failed to read entire message
02bb8761fcce Initial load
duke
parents:
diff changeset
   646
            throw wrapper.transportReadTimeoutExceeded(new Integer(size),
02bb8761fcce Initial load
duke
parents:
diff changeset
   647
                                      new Integer(n), new Long(max_wait_time),
02bb8761fcce Initial load
duke
parents:
diff changeset
   648
                                      new Long(total_time_in_wait));
02bb8761fcce Initial load
duke
parents:
diff changeset
   649
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   650
02bb8761fcce Initial load
duke
parents:
diff changeset
   651
        getConnectionCache().stampTime(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   652
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   653
02bb8761fcce Initial load
duke
parents:
diff changeset
   654
    // To support non-channel connections.
02bb8761fcce Initial load
duke
parents:
diff changeset
   655
    public void readFully(java.io.InputStream is, byte[] buf,
02bb8761fcce Initial load
duke
parents:
diff changeset
   656
                          int offset, int size, long max_wait_time)
02bb8761fcce Initial load
duke
parents:
diff changeset
   657
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
   658
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   659
        int n = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   660
        int bytecount = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   661
        long time_to_wait = readTimeouts.get_initial_time_to_wait();
02bb8761fcce Initial load
duke
parents:
diff changeset
   662
        long total_time_in_wait = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   663
02bb8761fcce Initial load
duke
parents:
diff changeset
   664
        // The reading of data incorporates a strategy to detect a
02bb8761fcce Initial load
duke
parents:
diff changeset
   665
        // rogue client. The strategy is implemented as follows. As
02bb8761fcce Initial load
duke
parents:
diff changeset
   666
        // long as data is being read, at least 1 byte or more, we
02bb8761fcce Initial load
duke
parents:
diff changeset
   667
        // assume we have a well behaved client. If no data is read,
02bb8761fcce Initial load
duke
parents:
diff changeset
   668
        // then we sleep for a time to wait, re-calculate a new time to
02bb8761fcce Initial load
duke
parents:
diff changeset
   669
        // wait which is lengthier than the previous time spent waiting.
02bb8761fcce Initial load
duke
parents:
diff changeset
   670
        // Then, if the total time spent waiting does not exceed a
02bb8761fcce Initial load
duke
parents:
diff changeset
   671
        // maximum time we are willing to wait, we attempt another
02bb8761fcce Initial load
duke
parents:
diff changeset
   672
        // read. If the maximum amount of time we are willing to
02bb8761fcce Initial load
duke
parents:
diff changeset
   673
        // spend waiting for more data is exceeded, we throw an
02bb8761fcce Initial load
duke
parents:
diff changeset
   674
        // IOException.
02bb8761fcce Initial load
duke
parents:
diff changeset
   675
02bb8761fcce Initial load
duke
parents:
diff changeset
   676
        // NOTE: Reading of GIOP headers are treated with a smaller
02bb8761fcce Initial load
duke
parents:
diff changeset
   677
        //       maximum time to wait threshold. Based on extensive
02bb8761fcce Initial load
duke
parents:
diff changeset
   678
        //       performance testing, all GIOP headers are being
02bb8761fcce Initial load
duke
parents:
diff changeset
   679
        //       read in 1 read access.
02bb8761fcce Initial load
duke
parents:
diff changeset
   680
02bb8761fcce Initial load
duke
parents:
diff changeset
   681
        do {
02bb8761fcce Initial load
duke
parents:
diff changeset
   682
            bytecount = is.read(buf, offset + n, size - n);
02bb8761fcce Initial load
duke
parents:
diff changeset
   683
            if (bytecount < 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   684
                throw new IOException("End-of-stream");
02bb8761fcce Initial load
duke
parents:
diff changeset
   685
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   686
            else if (bytecount == 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   687
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   688
                    Thread.sleep(time_to_wait);
02bb8761fcce Initial load
duke
parents:
diff changeset
   689
                    total_time_in_wait += time_to_wait;
02bb8761fcce Initial load
duke
parents:
diff changeset
   690
                    time_to_wait =
02bb8761fcce Initial load
duke
parents:
diff changeset
   691
                        (long)(time_to_wait*readTimeouts.get_backoff_factor());
02bb8761fcce Initial load
duke
parents:
diff changeset
   692
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   693
                catch (InterruptedException ie) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   694
                    // ignore exception
02bb8761fcce Initial load
duke
parents:
diff changeset
   695
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   696
                        dprint("readFully(): unexpected exception "
02bb8761fcce Initial load
duke
parents:
diff changeset
   697
                                + ie.toString());
02bb8761fcce Initial load
duke
parents:
diff changeset
   698
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   699
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   700
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   701
            else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   702
                n += bytecount;
02bb8761fcce Initial load
duke
parents:
diff changeset
   703
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   704
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   705
        while (n < size && total_time_in_wait < max_wait_time);
02bb8761fcce Initial load
duke
parents:
diff changeset
   706
02bb8761fcce Initial load
duke
parents:
diff changeset
   707
        if (n < size && total_time_in_wait >= max_wait_time)
02bb8761fcce Initial load
duke
parents:
diff changeset
   708
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   709
            // failed to read entire message
02bb8761fcce Initial load
duke
parents:
diff changeset
   710
            throw wrapper.transportReadTimeoutExceeded(new Integer(size),
02bb8761fcce Initial load
duke
parents:
diff changeset
   711
                                      new Integer(n), new Long(max_wait_time),
02bb8761fcce Initial load
duke
parents:
diff changeset
   712
                                      new Long(total_time_in_wait));
02bb8761fcce Initial load
duke
parents:
diff changeset
   713
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   714
02bb8761fcce Initial load
duke
parents:
diff changeset
   715
        getConnectionCache().stampTime(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   716
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   717
02bb8761fcce Initial load
duke
parents:
diff changeset
   718
    public void write(ByteBuffer byteBuffer)
02bb8761fcce Initial load
duke
parents:
diff changeset
   719
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
   720
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   721
        if (shouldUseDirectByteBuffers()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   722
            /* NOTE: cannot perform this test.  If one ask for a
02bb8761fcce Initial load
duke
parents:
diff changeset
   723
               ByteBuffer from the pool which is bigger than the size
02bb8761fcce Initial load
duke
parents:
diff changeset
   724
               of ByteBuffers managed by the pool, then the pool will
02bb8761fcce Initial load
duke
parents:
diff changeset
   725
               return a HeapByteBuffer.
02bb8761fcce Initial load
duke
parents:
diff changeset
   726
            if (byteBuffer.hasArray()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   727
                throw wrapper.unexpectedNonDirectByteBufferWithChannelSocket();
02bb8761fcce Initial load
duke
parents:
diff changeset
   728
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   729
            */
02bb8761fcce Initial load
duke
parents:
diff changeset
   730
            // IMPORTANT: For non-blocking SocketChannels, there's no guarantee
02bb8761fcce Initial load
duke
parents:
diff changeset
   731
            //            all bytes are written on first write attempt.
02bb8761fcce Initial load
duke
parents:
diff changeset
   732
            do {
02bb8761fcce Initial load
duke
parents:
diff changeset
   733
                getSocketChannel().write(byteBuffer);
02bb8761fcce Initial load
duke
parents:
diff changeset
   734
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   735
            while (byteBuffer.hasRemaining());
02bb8761fcce Initial load
duke
parents:
diff changeset
   736
02bb8761fcce Initial load
duke
parents:
diff changeset
   737
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   738
            if (! byteBuffer.hasArray()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   739
                throw wrapper.unexpectedDirectByteBufferWithNonChannelSocket();
02bb8761fcce Initial load
duke
parents:
diff changeset
   740
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   741
            byte[] tmpBuf = byteBuffer.array();
02bb8761fcce Initial load
duke
parents:
diff changeset
   742
            getSocket().getOutputStream().write(tmpBuf, 0, byteBuffer.limit());
02bb8761fcce Initial load
duke
parents:
diff changeset
   743
            getSocket().getOutputStream().flush();
02bb8761fcce Initial load
duke
parents:
diff changeset
   744
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   745
02bb8761fcce Initial load
duke
parents:
diff changeset
   746
        // TimeStamp connection to indicate it has been used
02bb8761fcce Initial load
duke
parents:
diff changeset
   747
        // Note granularity of connection usage is assumed for
02bb8761fcce Initial load
duke
parents:
diff changeset
   748
        // now to be that of a IIOP packet.
02bb8761fcce Initial load
duke
parents:
diff changeset
   749
        getConnectionCache().stampTime(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   750
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   751
02bb8761fcce Initial load
duke
parents:
diff changeset
   752
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   753
     * Note:it is possible for this to be called more than once
02bb8761fcce Initial load
duke
parents:
diff changeset
   754
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   755
    public synchronized void close()
02bb8761fcce Initial load
duke
parents:
diff changeset
   756
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   757
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   758
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   759
                dprint(".close->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   760
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   761
            writeLock();
02bb8761fcce Initial load
duke
parents:
diff changeset
   762
02bb8761fcce Initial load
duke
parents:
diff changeset
   763
            // REVISIT It will be good to have a read lock on the reader thread
02bb8761fcce Initial load
duke
parents:
diff changeset
   764
            // before we proceed further, to avoid the reader thread (server side)
02bb8761fcce Initial load
duke
parents:
diff changeset
   765
            // from processing requests. This avoids the risk that a new request
02bb8761fcce Initial load
duke
parents:
diff changeset
   766
            // will be accepted by ReaderThread while the ListenerThread is
02bb8761fcce Initial load
duke
parents:
diff changeset
   767
            // attempting to close this connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
   768
02bb8761fcce Initial load
duke
parents:
diff changeset
   769
            if (isBusy()) { // we are busy!
02bb8761fcce Initial load
duke
parents:
diff changeset
   770
                writeUnlock();
02bb8761fcce Initial load
duke
parents:
diff changeset
   771
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   772
                    dprint(".close: isBusy so no close: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   773
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   774
                return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   775
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   776
02bb8761fcce Initial load
duke
parents:
diff changeset
   777
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   778
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   779
                    sendCloseConnection(GIOPVersion.V1_0);
02bb8761fcce Initial load
duke
parents:
diff changeset
   780
                } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   781
                    wrapper.exceptionWhenSendingCloseConnection(t);
02bb8761fcce Initial load
duke
parents:
diff changeset
   782
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   783
02bb8761fcce Initial load
duke
parents:
diff changeset
   784
                synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
   785
                    state = CLOSE_SENT;
02bb8761fcce Initial load
duke
parents:
diff changeset
   786
                    stateEvent.notifyAll();
02bb8761fcce Initial load
duke
parents:
diff changeset
   787
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   788
02bb8761fcce Initial load
duke
parents:
diff changeset
   789
                // stop the reader without causing it to do purgeCalls
02bb8761fcce Initial load
duke
parents:
diff changeset
   790
                //Exception ex = new Exception();
02bb8761fcce Initial load
duke
parents:
diff changeset
   791
                //reader.stop(ex); // REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   792
02bb8761fcce Initial load
duke
parents:
diff changeset
   793
                // NOTE: !!!!!!
02bb8761fcce Initial load
duke
parents:
diff changeset
   794
                // This does writeUnlock().
02bb8761fcce Initial load
duke
parents:
diff changeset
   795
                purgeCalls(wrapper.connectionRebind(), false, true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   796
02bb8761fcce Initial load
duke
parents:
diff changeset
   797
            } catch (Exception ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   798
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   799
                    dprint(".close: exception: " + this, ex);
02bb8761fcce Initial load
duke
parents:
diff changeset
   800
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   801
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   802
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   803
                Selector selector = orb.getTransportManager().getSelector(0);
02bb8761fcce Initial load
duke
parents:
diff changeset
   804
                selector.unregisterForEvent(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   805
                if (socketChannel != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   806
                    socketChannel.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   807
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   808
                socket.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   809
            } catch (IOException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   810
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   811
                    dprint(".close: " + this, e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   812
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   813
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   814
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   815
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   816
                dprint(".close<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   817
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   818
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   819
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   820
02bb8761fcce Initial load
duke
parents:
diff changeset
   821
    public Acceptor getAcceptor()
02bb8761fcce Initial load
duke
parents:
diff changeset
   822
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   823
        return acceptor;
02bb8761fcce Initial load
duke
parents:
diff changeset
   824
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   825
02bb8761fcce Initial load
duke
parents:
diff changeset
   826
    public ContactInfo getContactInfo()
02bb8761fcce Initial load
duke
parents:
diff changeset
   827
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   828
        return contactInfo;
02bb8761fcce Initial load
duke
parents:
diff changeset
   829
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   830
02bb8761fcce Initial load
duke
parents:
diff changeset
   831
    public EventHandler getEventHandler()
02bb8761fcce Initial load
duke
parents:
diff changeset
   832
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   833
        return this;
02bb8761fcce Initial load
duke
parents:
diff changeset
   834
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   835
02bb8761fcce Initial load
duke
parents:
diff changeset
   836
    public OutputObject createOutputObject(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
   837
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   838
        // REVISIT - remove this method from Connection and all it subclasses.
02bb8761fcce Initial load
duke
parents:
diff changeset
   839
        throw new RuntimeException("*****SocketOrChannelConnectionImpl.createOutputObject - should not be called.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   840
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   841
02bb8761fcce Initial load
duke
parents:
diff changeset
   842
    // This is used by the GIOPOutputObject in order to
02bb8761fcce Initial load
duke
parents:
diff changeset
   843
    // throw the correct error when handling code sets.
02bb8761fcce Initial load
duke
parents:
diff changeset
   844
    // Can we determine if we are on the server side by
02bb8761fcce Initial load
duke
parents:
diff changeset
   845
    // other means?  XREVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   846
    public boolean isServer()
02bb8761fcce Initial load
duke
parents:
diff changeset
   847
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   848
        return isServer;
02bb8761fcce Initial load
duke
parents:
diff changeset
   849
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   850
02bb8761fcce Initial load
duke
parents:
diff changeset
   851
    public boolean isBusy()
02bb8761fcce Initial load
duke
parents:
diff changeset
   852
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   853
        if (serverRequestCount > 0 ||
02bb8761fcce Initial load
duke
parents:
diff changeset
   854
            getResponseWaitingRoom().numberRegistered() > 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   855
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   856
            return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   857
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   858
            return false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   859
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   860
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   861
02bb8761fcce Initial load
duke
parents:
diff changeset
   862
    public long getTimeStamp()
02bb8761fcce Initial load
duke
parents:
diff changeset
   863
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   864
        return timeStamp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   865
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   866
02bb8761fcce Initial load
duke
parents:
diff changeset
   867
    public void setTimeStamp(long time)
02bb8761fcce Initial load
duke
parents:
diff changeset
   868
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   869
        timeStamp = time;
02bb8761fcce Initial load
duke
parents:
diff changeset
   870
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   871
02bb8761fcce Initial load
duke
parents:
diff changeset
   872
    public void setState(String stateString)
02bb8761fcce Initial load
duke
parents:
diff changeset
   873
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   874
        synchronized (stateEvent) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   875
            if (stateString.equals("ESTABLISHED")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   876
                state =  ESTABLISHED;
02bb8761fcce Initial load
duke
parents:
diff changeset
   877
                stateEvent.notifyAll();
02bb8761fcce Initial load
duke
parents:
diff changeset
   878
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   879
                // REVISIT: ASSERT
02bb8761fcce Initial load
duke
parents:
diff changeset
   880
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   881
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   882
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   883
02bb8761fcce Initial load
duke
parents:
diff changeset
   884
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   885
     * Sets the writeLock for this connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
   886
     * If the writeLock is already set by someone else, block till the
02bb8761fcce Initial load
duke
parents:
diff changeset
   887
     * writeLock is released and can set by us.
02bb8761fcce Initial load
duke
parents:
diff changeset
   888
     * IMPORTANT: this connection's lock must be acquired before
02bb8761fcce Initial load
duke
parents:
diff changeset
   889
     * setting the writeLock and must be unlocked after setting the writeLock.
02bb8761fcce Initial load
duke
parents:
diff changeset
   890
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   891
    public void writeLock()
02bb8761fcce Initial load
duke
parents:
diff changeset
   892
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   893
      try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   894
        if (dprintWriteLocks && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   895
            dprint(".writeLock->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   896
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   897
        // Keep looping till we can set the writeLock.
02bb8761fcce Initial load
duke
parents:
diff changeset
   898
        while ( true ) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   899
            int localState = state;
02bb8761fcce Initial load
duke
parents:
diff changeset
   900
            switch ( localState ) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   901
02bb8761fcce Initial load
duke
parents:
diff changeset
   902
            case OPENING:
02bb8761fcce Initial load
duke
parents:
diff changeset
   903
                synchronized (stateEvent) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   904
                    if (state != OPENING) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   905
                        // somebody has changed 'state' so be careful
02bb8761fcce Initial load
duke
parents:
diff changeset
   906
                        break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   907
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   908
                    try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   909
                        stateEvent.wait();
02bb8761fcce Initial load
duke
parents:
diff changeset
   910
                    } catch (InterruptedException ie) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   911
                        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   912
                            dprint(".writeLock: OPENING InterruptedException: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   913
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   914
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   915
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   916
                // Loop back
02bb8761fcce Initial load
duke
parents:
diff changeset
   917
                break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   918
02bb8761fcce Initial load
duke
parents:
diff changeset
   919
            case ESTABLISHED:
02bb8761fcce Initial load
duke
parents:
diff changeset
   920
                synchronized (writeEvent) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   921
                    if (!writeLocked) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   922
                        writeLocked = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   923
                        return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   924
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   925
02bb8761fcce Initial load
duke
parents:
diff changeset
   926
                    try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   927
                        // do not stay here too long if state != ESTABLISHED
02bb8761fcce Initial load
duke
parents:
diff changeset
   928
                        // Bug 4752117
02bb8761fcce Initial load
duke
parents:
diff changeset
   929
                        while (state == ESTABLISHED && writeLocked) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   930
                            writeEvent.wait(100);
02bb8761fcce Initial load
duke
parents:
diff changeset
   931
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   932
                    } catch (InterruptedException ie) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   933
                        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   934
                            dprint(".writeLock: ESTABLISHED InterruptedException: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   935
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   936
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   937
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   938
                // Loop back
02bb8761fcce Initial load
duke
parents:
diff changeset
   939
                break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   940
02bb8761fcce Initial load
duke
parents:
diff changeset
   941
                //
02bb8761fcce Initial load
duke
parents:
diff changeset
   942
                // XXX
02bb8761fcce Initial load
duke
parents:
diff changeset
   943
                // Need to distinguish between client and server roles
02bb8761fcce Initial load
duke
parents:
diff changeset
   944
                // here probably.
02bb8761fcce Initial load
duke
parents:
diff changeset
   945
                //
02bb8761fcce Initial load
duke
parents:
diff changeset
   946
            case ABORT:
02bb8761fcce Initial load
duke
parents:
diff changeset
   947
                synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
   948
                    if (state != ABORT) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   949
                        break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   950
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   951
                    throw wrapper.writeErrorSend() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   952
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   953
02bb8761fcce Initial load
duke
parents:
diff changeset
   954
            case CLOSE_RECVD:
02bb8761fcce Initial load
duke
parents:
diff changeset
   955
                // the connection has been closed or closing
02bb8761fcce Initial load
duke
parents:
diff changeset
   956
                // ==> throw rebind exception
02bb8761fcce Initial load
duke
parents:
diff changeset
   957
                synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
   958
                    if (state != CLOSE_RECVD) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   959
                        break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   960
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   961
                    throw wrapper.connectionCloseRebind() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   962
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   963
02bb8761fcce Initial load
duke
parents:
diff changeset
   964
            default:
02bb8761fcce Initial load
duke
parents:
diff changeset
   965
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   966
                    dprint(".writeLock: default: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   967
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   968
                // REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   969
                throw new RuntimeException(".writeLock: bad state");
02bb8761fcce Initial load
duke
parents:
diff changeset
   970
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   971
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   972
      } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   973
        if (dprintWriteLocks && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   974
            dprint(".writeLock<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   975
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   976
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
   977
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   978
02bb8761fcce Initial load
duke
parents:
diff changeset
   979
    public void writeUnlock()
02bb8761fcce Initial load
duke
parents:
diff changeset
   980
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   981
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   982
            if (dprintWriteLocks && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   983
                dprint(".writeUnlock->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   984
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   985
            synchronized (writeEvent) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   986
                writeLocked = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   987
                writeEvent.notify(); // wake up one guy waiting to write
02bb8761fcce Initial load
duke
parents:
diff changeset
   988
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   989
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   990
            if (dprintWriteLocks && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   991
                dprint(".writeUnlock<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   992
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   993
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   994
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   995
02bb8761fcce Initial load
duke
parents:
diff changeset
   996
    // Assumes the caller handles writeLock and writeUnlock
02bb8761fcce Initial load
duke
parents:
diff changeset
   997
    public void sendWithoutLock(OutputObject outputObject)
02bb8761fcce Initial load
duke
parents:
diff changeset
   998
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   999
        // Don't we need to check for CloseConnection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1000
        // here?  REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
  1001
02bb8761fcce Initial load
duke
parents:
diff changeset
  1002
        // XREVISIT - Shouldn't the MessageMediator
02bb8761fcce Initial load
duke
parents:
diff changeset
  1003
        // be the one to handle writing the data here?
02bb8761fcce Initial load
duke
parents:
diff changeset
  1004
02bb8761fcce Initial load
duke
parents:
diff changeset
  1005
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1006
02bb8761fcce Initial load
duke
parents:
diff changeset
  1007
            // Write the fragment/message
02bb8761fcce Initial load
duke
parents:
diff changeset
  1008
02bb8761fcce Initial load
duke
parents:
diff changeset
  1009
            CDROutputObject cdrOutputObject = (CDROutputObject) outputObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1010
            cdrOutputObject.writeTo(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1011
            // REVISIT - no flush?
02bb8761fcce Initial load
duke
parents:
diff changeset
  1012
            //socket.getOutputStream().flush();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1013
02bb8761fcce Initial load
duke
parents:
diff changeset
  1014
        } catch (IOException e1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1015
02bb8761fcce Initial load
duke
parents:
diff changeset
  1016
            /*
02bb8761fcce Initial load
duke
parents:
diff changeset
  1017
             * ADDED(Ram J) 10/13/2000 In the event of an IOException, try
02bb8761fcce Initial load
duke
parents:
diff changeset
  1018
             * sending a CancelRequest for regular requests / locate requests
02bb8761fcce Initial load
duke
parents:
diff changeset
  1019
             */
02bb8761fcce Initial load
duke
parents:
diff changeset
  1020
02bb8761fcce Initial load
duke
parents:
diff changeset
  1021
            // Since IIOPOutputStream's msgheader is set only once, and not
02bb8761fcce Initial load
duke
parents:
diff changeset
  1022
            // altered during sending multiple fragments, the original
02bb8761fcce Initial load
duke
parents:
diff changeset
  1023
            // msgheader will always have the requestId.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1024
            // REVISIT This could be optimized to send a CancelRequest only
02bb8761fcce Initial load
duke
parents:
diff changeset
  1025
            // if any fragments had been sent already.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1026
02bb8761fcce Initial load
duke
parents:
diff changeset
  1027
            /* REVISIT: MOVE TO SUBCONTRACT
02bb8761fcce Initial load
duke
parents:
diff changeset
  1028
            Message msg = os.getMessage();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1029
            if (msg.getType() == Message.GIOPRequest ||
02bb8761fcce Initial load
duke
parents:
diff changeset
  1030
                    msg.getType() == Message.GIOPLocateRequest) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1031
                GIOPVersion requestVersion = msg.getGIOPVersion();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1032
                int requestId = MessageBase.getRequestId(msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1033
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1034
                    sendCancelRequest(requestVersion, requestId);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1035
                } catch (IOException e2) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1036
                    // most likely an abortive connection closure.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1037
                    // ignore, since nothing more can be done.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1038
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1039
02bb8761fcce Initial load
duke
parents:
diff changeset
  1040
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1041
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1042
            */
02bb8761fcce Initial load
duke
parents:
diff changeset
  1043
02bb8761fcce Initial load
duke
parents:
diff changeset
  1044
            // REVISIT When a send failure happens, purgeCalls() need to be
02bb8761fcce Initial load
duke
parents:
diff changeset
  1045
            // called to ensure that the connection is properly removed from
02bb8761fcce Initial load
duke
parents:
diff changeset
  1046
            // further usage (ie., cancelling pending requests with COMM_FAILURE
02bb8761fcce Initial load
duke
parents:
diff changeset
  1047
            // with an appropriate minor_code CompletionStatus.MAY_BE).
02bb8761fcce Initial load
duke
parents:
diff changeset
  1048
02bb8761fcce Initial load
duke
parents:
diff changeset
  1049
            // Relying on the IIOPOutputStream (as noted below) is not
02bb8761fcce Initial load
duke
parents:
diff changeset
  1050
            // sufficient as it handles COMM_FAILURE only for the final
02bb8761fcce Initial load
duke
parents:
diff changeset
  1051
            // fragment (during invoke processing). Note that COMM_FAILURE could
02bb8761fcce Initial load
duke
parents:
diff changeset
  1052
            // happen while sending the initial fragments.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1053
            // Also the IIOPOutputStream does not properly close the connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1054
            // It simply removes the connection from the table. An orderly
02bb8761fcce Initial load
duke
parents:
diff changeset
  1055
            // closure is needed (ie., cancel pending requests on the connection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1056
            // COMM_FAILURE as well.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1057
02bb8761fcce Initial load
duke
parents:
diff changeset
  1058
            // IIOPOutputStream will cleanup the connection info when it
02bb8761fcce Initial load
duke
parents:
diff changeset
  1059
            // sees this exception.
2664
a0a22a8f16bd 6372405: Server thread hangs when fragments don't complete because of connection abort
tbell
parents: 4
diff changeset
  1060
            SystemException exc = wrapper.writeErrorSend(e1);
a0a22a8f16bd 6372405: Server thread hangs when fragments don't complete because of connection abort
tbell
parents: 4
diff changeset
  1061
            purgeCalls(exc, false, true);
a0a22a8f16bd 6372405: Server thread hangs when fragments don't complete because of connection abort
tbell
parents: 4
diff changeset
  1062
            throw exc;
4
02bb8761fcce Initial load
duke
parents:
diff changeset
  1063
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1064
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1065
02bb8761fcce Initial load
duke
parents:
diff changeset
  1066
    public void registerWaiter(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1067
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1068
        responseWaitingRoom.registerWaiter(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1069
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1070
02bb8761fcce Initial load
duke
parents:
diff changeset
  1071
    public void unregisterWaiter(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1072
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1073
        responseWaitingRoom.unregisterWaiter(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1074
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1075
02bb8761fcce Initial load
duke
parents:
diff changeset
  1076
    public InputObject waitForResponse(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1077
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1078
        return responseWaitingRoom.waitForResponse(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1079
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1080
02bb8761fcce Initial load
duke
parents:
diff changeset
  1081
    public void setConnectionCache(ConnectionCache connectionCache)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1082
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1083
        this.connectionCache = connectionCache;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1084
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1085
02bb8761fcce Initial load
duke
parents:
diff changeset
  1086
    public ConnectionCache getConnectionCache()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1087
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1088
        return connectionCache;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1089
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1090
02bb8761fcce Initial load
duke
parents:
diff changeset
  1091
    ////////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
  1092
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1093
    // EventHandler methods
02bb8761fcce Initial load
duke
parents:
diff changeset
  1094
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1095
02bb8761fcce Initial load
duke
parents:
diff changeset
  1096
    public void setUseSelectThreadToWait(boolean x)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1097
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1098
        useSelectThreadToWait = x;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1099
        // REVISIT - Reading of a GIOP header only is information
02bb8761fcce Initial load
duke
parents:
diff changeset
  1100
        //           that should be passed into the constructor
02bb8761fcce Initial load
duke
parents:
diff changeset
  1101
        //           from the SocketOrChannelConnection factory.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1102
        setReadGiopHeaderOnly(shouldUseSelectThreadToWait());
02bb8761fcce Initial load
duke
parents:
diff changeset
  1103
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1104
02bb8761fcce Initial load
duke
parents:
diff changeset
  1105
    public void handleEvent()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1106
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1107
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1108
            dprint(".handleEvent->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1109
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1110
        getSelectionKey().interestOps(getSelectionKey().interestOps() &
02bb8761fcce Initial load
duke
parents:
diff changeset
  1111
                                      (~ getInterestOps()));
02bb8761fcce Initial load
duke
parents:
diff changeset
  1112
02bb8761fcce Initial load
duke
parents:
diff changeset
  1113
        if (shouldUseWorkerThreadForEvent()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1114
            Throwable throwable = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1115
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1116
                int poolToUse = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1117
                if (shouldReadGiopHeaderOnly()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1118
                    partialMessageMediator = readBits();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1119
                    poolToUse =
02bb8761fcce Initial load
duke
parents:
diff changeset
  1120
                        partialMessageMediator.getThreadPoolToUse();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1121
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1122
02bb8761fcce Initial load
duke
parents:
diff changeset
  1123
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1124
                    dprint(".handleEvent: addWork to pool: " + poolToUse);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1125
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1126
                orb.getThreadPoolManager().getThreadPool(poolToUse)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1127
                    .getWorkQueue(0).addWork(getWork());
02bb8761fcce Initial load
duke
parents:
diff changeset
  1128
            } catch (NoSuchThreadPoolException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1129
                throwable = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1130
            } catch (NoSuchWorkQueueException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1131
                throwable = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1132
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1133
            // REVISIT: need to close connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1134
            if (throwable != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1135
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1136
                    dprint(".handleEvent: " + throwable);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1137
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1138
                INTERNAL i = new INTERNAL("NoSuchThreadPoolException");
02bb8761fcce Initial load
duke
parents:
diff changeset
  1139
                i.initCause(throwable);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1140
                throw i;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1141
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1142
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1143
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1144
                dprint(".handleEvent: doWork");
02bb8761fcce Initial load
duke
parents:
diff changeset
  1145
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1146
            getWork().doWork();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1147
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1148
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1149
            dprint(".handleEvent<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1150
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1151
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1152
02bb8761fcce Initial load
duke
parents:
diff changeset
  1153
    public SelectableChannel getChannel()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1154
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1155
        return socketChannel;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1156
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1157
02bb8761fcce Initial load
duke
parents:
diff changeset
  1158
    public int getInterestOps()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1159
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1160
        return SelectionKey.OP_READ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1161
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1162
02bb8761fcce Initial load
duke
parents:
diff changeset
  1163
    //    public Acceptor getAcceptor() - already defined above.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1164
02bb8761fcce Initial load
duke
parents:
diff changeset
  1165
    public Connection getConnection()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1166
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1167
        return this;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1168
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1169
02bb8761fcce Initial load
duke
parents:
diff changeset
  1170
    ////////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
  1171
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1172
    // Work methods.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1173
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1174
02bb8761fcce Initial load
duke
parents:
diff changeset
  1175
    public String getName()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1176
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1177
        return this.toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1178
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1179
02bb8761fcce Initial load
duke
parents:
diff changeset
  1180
    public void doWork()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1181
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1182
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1183
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1184
                dprint(".doWork->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1185
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1186
02bb8761fcce Initial load
duke
parents:
diff changeset
  1187
            // IMPORTANT: Sanity checks on SelectionKeys such as
02bb8761fcce Initial load
duke
parents:
diff changeset
  1188
            //            SelectorKey.isValid() should not be done
02bb8761fcce Initial load
duke
parents:
diff changeset
  1189
            //            here.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1190
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1191
02bb8761fcce Initial load
duke
parents:
diff changeset
  1192
            if (!shouldReadGiopHeaderOnly()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1193
                read();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1194
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1195
            else {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1196
                // get the partialMessageMediator
02bb8761fcce Initial load
duke
parents:
diff changeset
  1197
                // created by SelectorThread
02bb8761fcce Initial load
duke
parents:
diff changeset
  1198
                CorbaMessageMediator messageMediator =
02bb8761fcce Initial load
duke
parents:
diff changeset
  1199
                                         this.getPartialMessageMediator();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1200
02bb8761fcce Initial load
duke
parents:
diff changeset
  1201
                // read remaining info needed in a MessageMediator
02bb8761fcce Initial load
duke
parents:
diff changeset
  1202
                messageMediator = finishReadingBits(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1203
02bb8761fcce Initial load
duke
parents:
diff changeset
  1204
                if (messageMediator != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1205
                    // Null can happen when client closes stream
02bb8761fcce Initial load
duke
parents:
diff changeset
  1206
                    // causing purgecalls.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1207
                    dispatch(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1208
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1209
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1210
        } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1211
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1212
                dprint(".doWork: ignoring Throwable: "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1213
                       + t
02bb8761fcce Initial load
duke
parents:
diff changeset
  1214
                       + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1215
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1216
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1217
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1218
                dprint(".doWork<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1219
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1220
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1221
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1222
02bb8761fcce Initial load
duke
parents:
diff changeset
  1223
    public void setEnqueueTime(long timeInMillis)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1224
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1225
        enqueueTime = timeInMillis;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1226
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1227
02bb8761fcce Initial load
duke
parents:
diff changeset
  1228
    public long getEnqueueTime()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1229
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1230
        return enqueueTime;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1231
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1232
02bb8761fcce Initial load
duke
parents:
diff changeset
  1233
    ////////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
  1234
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1235
    // spi.transport.CorbaConnection.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1236
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1237
02bb8761fcce Initial load
duke
parents:
diff changeset
  1238
    // IMPORTANT: Reader Threads must NOT read Giop header only.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1239
    public boolean shouldReadGiopHeaderOnly() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1240
        return shouldReadGiopHeaderOnly;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1241
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1242
02bb8761fcce Initial load
duke
parents:
diff changeset
  1243
    protected void setReadGiopHeaderOnly(boolean shouldReadHeaderOnly) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1244
        shouldReadGiopHeaderOnly = shouldReadHeaderOnly;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1245
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1246
02bb8761fcce Initial load
duke
parents:
diff changeset
  1247
    public ResponseWaitingRoom getResponseWaitingRoom()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1248
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1249
        return responseWaitingRoom;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1250
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1251
02bb8761fcce Initial load
duke
parents:
diff changeset
  1252
    // REVISIT - inteface defines isServer but already defined in
02bb8761fcce Initial load
duke
parents:
diff changeset
  1253
    // higher interface.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1254
02bb8761fcce Initial load
duke
parents:
diff changeset
  1255
    public void serverRequestMapPut(int requestId,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1256
                                    CorbaMessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1257
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1258
        serverRequestMap.put(new Integer(requestId), messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1259
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1260
02bb8761fcce Initial load
duke
parents:
diff changeset
  1261
    public CorbaMessageMediator serverRequestMapGet(int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1262
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1263
        return (CorbaMessageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1264
            serverRequestMap.get(new Integer(requestId));
02bb8761fcce Initial load
duke
parents:
diff changeset
  1265
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1266
02bb8761fcce Initial load
duke
parents:
diff changeset
  1267
    public void serverRequestMapRemove(int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1268
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1269
        serverRequestMap.remove(new Integer(requestId));
02bb8761fcce Initial load
duke
parents:
diff changeset
  1270
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1271
02bb8761fcce Initial load
duke
parents:
diff changeset
  1272
02bb8761fcce Initial load
duke
parents:
diff changeset
  1273
    // REVISIT: this is also defined in:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1274
    // com.sun.corba.se.spi.legacy.connection.Connection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1275
    public java.net.Socket getSocket()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1276
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1277
        return socket;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1278
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1279
02bb8761fcce Initial load
duke
parents:
diff changeset
  1280
    /** It is possible for a Close Connection to have been
02bb8761fcce Initial load
duke
parents:
diff changeset
  1281
     ** sent here, but we will not check for this. A "lazy"
02bb8761fcce Initial load
duke
parents:
diff changeset
  1282
     ** Exception will be thrown in the Worker thread after the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1283
     ** incoming request has been processed even though the connection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1284
     ** is closed before the request is processed. This is o.k because
02bb8761fcce Initial load
duke
parents:
diff changeset
  1285
     ** it is a boundary condition. To prevent it we would have to add
02bb8761fcce Initial load
duke
parents:
diff changeset
  1286
     ** more locks which would reduce performance in the normal case.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1287
     **/
02bb8761fcce Initial load
duke
parents:
diff changeset
  1288
    public synchronized void serverRequestProcessingBegins()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1289
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1290
        serverRequestCount++;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1291
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1292
02bb8761fcce Initial load
duke
parents:
diff changeset
  1293
    public synchronized void serverRequestProcessingEnds()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1294
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1295
        serverRequestCount--;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1296
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1297
02bb8761fcce Initial load
duke
parents:
diff changeset
  1298
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1299
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1300
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1301
02bb8761fcce Initial load
duke
parents:
diff changeset
  1302
    public synchronized int getNextRequestId()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1303
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1304
        return requestId++;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1305
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1306
02bb8761fcce Initial load
duke
parents:
diff changeset
  1307
    // Negotiated code sets for char and wchar data
02bb8761fcce Initial load
duke
parents:
diff changeset
  1308
    protected CodeSetComponentInfo.CodeSetContext codeSetContext = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1309
02bb8761fcce Initial load
duke
parents:
diff changeset
  1310
    public ORB getBroker()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1311
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1312
        return orb;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1313
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1314
02bb8761fcce Initial load
duke
parents:
diff changeset
  1315
    public CodeSetComponentInfo.CodeSetContext getCodeSetContext() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1316
        // Needs to be synchronized for the following case when the client
02bb8761fcce Initial load
duke
parents:
diff changeset
  1317
        // doesn't send the code set context twice, and we have two threads
02bb8761fcce Initial load
duke
parents:
diff changeset
  1318
        // in ServerRequestDispatcher processCodeSetContext.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1319
        //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1320
        // Thread A checks to see if there is a context, there is none, so
02bb8761fcce Initial load
duke
parents:
diff changeset
  1321
        //     it calls setCodeSetContext, getting the synch lock.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1322
        // Thread B checks to see if there is a context.  If we didn't synch,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1323
        //     it might decide to outlaw wchar/wstring.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1324
        if (codeSetContext == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1325
            synchronized(this) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1326
                return codeSetContext;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1327
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1328
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1329
02bb8761fcce Initial load
duke
parents:
diff changeset
  1330
        return codeSetContext;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1331
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1332
02bb8761fcce Initial load
duke
parents:
diff changeset
  1333
    public synchronized void setCodeSetContext(CodeSetComponentInfo.CodeSetContext csc) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1334
        // Double check whether or not we need to do this
02bb8761fcce Initial load
duke
parents:
diff changeset
  1335
        if (codeSetContext == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1336
02bb8761fcce Initial load
duke
parents:
diff changeset
  1337
            if (OSFCodeSetRegistry.lookupEntry(csc.getCharCodeSet()) == null ||
02bb8761fcce Initial load
duke
parents:
diff changeset
  1338
                OSFCodeSetRegistry.lookupEntry(csc.getWCharCodeSet()) == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1339
                // If the client says it's negotiated a code set that
02bb8761fcce Initial load
duke
parents:
diff changeset
  1340
                // isn't a fallback and we never said we support, then
02bb8761fcce Initial load
duke
parents:
diff changeset
  1341
                // it has a bug.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1342
                throw wrapper.badCodesetsFromClient() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1343
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1344
02bb8761fcce Initial load
duke
parents:
diff changeset
  1345
            codeSetContext = csc;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1346
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1347
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1348
02bb8761fcce Initial load
duke
parents:
diff changeset
  1349
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1350
    // from iiop.IIOPConnection.java
02bb8761fcce Initial load
duke
parents:
diff changeset
  1351
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1352
02bb8761fcce Initial load
duke
parents:
diff changeset
  1353
    // Map request ID to an InputObject.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1354
    // This is so the client thread can start unmarshaling
02bb8761fcce Initial load
duke
parents:
diff changeset
  1355
    // the reply and remove it from the out_calls map while the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1356
    // ReaderThread can still obtain the input stream to give
02bb8761fcce Initial load
duke
parents:
diff changeset
  1357
    // new fragments.  Only the ReaderThread touches the clientReplyMap,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1358
    // so it doesn't incur synchronization overhead.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1359
02bb8761fcce Initial load
duke
parents:
diff changeset
  1360
    public MessageMediator clientRequestMapGet(int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1361
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1362
        return responseWaitingRoom.getMessageMediator(requestId);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1363
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1364
02bb8761fcce Initial load
duke
parents:
diff changeset
  1365
    protected MessageMediator clientReply_1_1;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1366
02bb8761fcce Initial load
duke
parents:
diff changeset
  1367
    public void clientReply_1_1_Put(MessageMediator x)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1368
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1369
        clientReply_1_1 = x;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1370
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1371
02bb8761fcce Initial load
duke
parents:
diff changeset
  1372
    public MessageMediator clientReply_1_1_Get()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1373
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1374
        return  clientReply_1_1;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1375
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1376
02bb8761fcce Initial load
duke
parents:
diff changeset
  1377
    public void clientReply_1_1_Remove()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1378
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1379
        clientReply_1_1 = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1380
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1381
02bb8761fcce Initial load
duke
parents:
diff changeset
  1382
    protected MessageMediator serverRequest_1_1;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1383
02bb8761fcce Initial load
duke
parents:
diff changeset
  1384
    public void serverRequest_1_1_Put(MessageMediator x)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1385
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1386
        serverRequest_1_1 = x;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1387
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1388
02bb8761fcce Initial load
duke
parents:
diff changeset
  1389
    public MessageMediator serverRequest_1_1_Get()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1390
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1391
        return  serverRequest_1_1;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1392
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1393
02bb8761fcce Initial load
duke
parents:
diff changeset
  1394
    public void serverRequest_1_1_Remove()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1395
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1396
        serverRequest_1_1 = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1397
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1398
02bb8761fcce Initial load
duke
parents:
diff changeset
  1399
    protected String getStateString( int state )
02bb8761fcce Initial load
duke
parents:
diff changeset
  1400
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1401
        synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1402
            switch (state) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1403
            case OPENING : return "OPENING" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1404
            case ESTABLISHED : return "ESTABLISHED" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1405
            case CLOSE_SENT : return "CLOSE_SENT" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1406
            case CLOSE_RECVD : return "CLOSE_RECVD" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1407
            case ABORT : return "ABORT" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1408
            default : return "???" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1409
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1410
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1411
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1412
02bb8761fcce Initial load
duke
parents:
diff changeset
  1413
    public synchronized boolean isPostInitialContexts() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1414
        return postInitialContexts;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1415
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1416
02bb8761fcce Initial load
duke
parents:
diff changeset
  1417
    // Can never be unset...
02bb8761fcce Initial load
duke
parents:
diff changeset
  1418
    public synchronized void setPostInitialContexts(){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1419
        postInitialContexts = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1420
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1421
02bb8761fcce Initial load
duke
parents:
diff changeset
  1422
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
  1423
     * Wake up the outstanding requests on the connection, and hand them
02bb8761fcce Initial load
duke
parents:
diff changeset
  1424
     * COMM_FAILURE exception with a given minor code.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1425
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
  1426
     * Also, delete connection from connection table and
02bb8761fcce Initial load
duke
parents:
diff changeset
  1427
     * stop the reader thread.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1428
02bb8761fcce Initial load
duke
parents:
diff changeset
  1429
     * Note that this should only ever be called by the Reader thread for
02bb8761fcce Initial load
duke
parents:
diff changeset
  1430
     * this connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1431
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
  1432
     * @param minor_code The minor code for the COMM_FAILURE major code.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1433
     * @param die Kill the reader thread (this thread) before exiting.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1434
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
  1435
    public void purgeCalls(SystemException systemException,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1436
                           boolean die, boolean lockHeld)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1437
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1438
        int minor_code = systemException.minor;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1439
02bb8761fcce Initial load
duke
parents:
diff changeset
  1440
        try{
02bb8761fcce Initial load
duke
parents:
diff changeset
  1441
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1442
                dprint(".purgeCalls->: "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1443
                       + minor_code + "/" + die + "/" + lockHeld
02bb8761fcce Initial load
duke
parents:
diff changeset
  1444
                       + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1445
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1446
02bb8761fcce Initial load
duke
parents:
diff changeset
  1447
            // If this invocation is a result of ThreadDeath caused
02bb8761fcce Initial load
duke
parents:
diff changeset
  1448
            // by a previous execution of this routine, just exit.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1449
02bb8761fcce Initial load
duke
parents:
diff changeset
  1450
            synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1451
                if ((state == ABORT) || (state == CLOSE_RECVD)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1452
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1453
                        dprint(".purgeCalls: exiting since state is: "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1454
                               + getStateString(state)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1455
                               + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1456
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1457
                    return;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1458
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1459
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1460
02bb8761fcce Initial load
duke
parents:
diff changeset
  1461
            // Grab the writeLock (freeze the calls)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1462
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1463
                if (!lockHeld) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1464
                    writeLock();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1465
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1466
            } catch (SystemException ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1467
                if (orb.transportDebugFlag)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1468
                    dprint(".purgeCalls: SystemException" + ex
02bb8761fcce Initial load
duke
parents:
diff changeset
  1469
                           + "; continuing " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1470
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1471
02bb8761fcce Initial load
duke
parents:
diff changeset
  1472
            // Mark the state of the connection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1473
            // and determine the request status
02bb8761fcce Initial load
duke
parents:
diff changeset
  1474
            org.omg.CORBA.CompletionStatus completion_status;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1475
            synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1476
                if (minor_code == ORBUtilSystemException.CONNECTION_REBIND) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1477
                    state = CLOSE_RECVD;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1478
                    systemException.completed = CompletionStatus.COMPLETED_NO;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1479
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1480
                    state = ABORT;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1481
                    systemException.completed = CompletionStatus.COMPLETED_MAYBE;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1482
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1483
                stateEvent.notifyAll();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1484
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1485
02bb8761fcce Initial load
duke
parents:
diff changeset
  1486
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1487
                socket.getInputStream().close();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1488
                socket.getOutputStream().close();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1489
                socket.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1490
            } catch (Exception ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1491
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1492
                    dprint(".purgeCalls: Exception closing socket: " + ex
02bb8761fcce Initial load
duke
parents:
diff changeset
  1493
                           + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1494
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1495
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1496
02bb8761fcce Initial load
duke
parents:
diff changeset
  1497
            // Signal all threads with outstanding requests on this
02bb8761fcce Initial load
duke
parents:
diff changeset
  1498
            // connection and give them the SystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1499
02bb8761fcce Initial load
duke
parents:
diff changeset
  1500
            responseWaitingRoom.signalExceptionToAllWaiters(systemException);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1501
02bb8761fcce Initial load
duke
parents:
diff changeset
  1502
            if (contactInfo != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1503
                ((OutboundConnectionCache)getConnectionCache()).remove(contactInfo);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1504
            } else if (acceptor != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1505
                ((InboundConnectionCache)getConnectionCache()).remove(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1506
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1507
02bb8761fcce Initial load
duke
parents:
diff changeset
  1508
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1509
            // REVISIT: Stop the reader thread
02bb8761fcce Initial load
duke
parents:
diff changeset
  1510
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1511
02bb8761fcce Initial load
duke
parents:
diff changeset
  1512
            // Signal all the waiters of the writeLock.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1513
            // There are 4 types of writeLock waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1514
            // 1. Send waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1515
            // 2. SendReply waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1516
            // 3. cleanUp waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1517
            // 4. purge_call waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1518
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1519
02bb8761fcce Initial load
duke
parents:
diff changeset
  1520
            writeUnlock();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1521
02bb8761fcce Initial load
duke
parents:
diff changeset
  1522
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1523
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1524
                dprint(".purgeCalls<-: "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1525
                       + minor_code + "/" + die + "/" + lockHeld
02bb8761fcce Initial load
duke
parents:
diff changeset
  1526
                       + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1527
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1528
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1529
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1530
02bb8761fcce Initial load
duke
parents:
diff changeset
  1531
    /*************************************************************************
02bb8761fcce Initial load
duke
parents:
diff changeset
  1532
    * The following methods are for dealing with Connection cleaning for
02bb8761fcce Initial load
duke
parents:
diff changeset
  1533
    * better scalability of servers in high network load conditions.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1534
    **************************************************************************/
02bb8761fcce Initial load
duke
parents:
diff changeset
  1535
02bb8761fcce Initial load
duke
parents:
diff changeset
  1536
    public void sendCloseConnection(GIOPVersion giopVersion)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1537
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1538
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1539
        Message msg = MessageBase.createCloseConnection(giopVersion);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1540
        sendHelper(giopVersion, msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1541
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1542
02bb8761fcce Initial load
duke
parents:
diff changeset
  1543
    public void sendMessageError(GIOPVersion giopVersion)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1544
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1545
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1546
        Message msg = MessageBase.createMessageError(giopVersion);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1547
        sendHelper(giopVersion, msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1548
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1549
02bb8761fcce Initial load
duke
parents:
diff changeset
  1550
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
  1551
     * Send a CancelRequest message. This does not lock the connection, so the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1552
     * caller needs to ensure this method is called appropriately.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1553
     * @exception IOException - could be due to abortive connection closure.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1554
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
  1555
    public void sendCancelRequest(GIOPVersion giopVersion, int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1556
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1557
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1558
02bb8761fcce Initial load
duke
parents:
diff changeset
  1559
        Message msg = MessageBase.createCancelRequest(giopVersion, requestId);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1560
        sendHelper(giopVersion, msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1561
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1562
02bb8761fcce Initial load
duke
parents:
diff changeset
  1563
    protected void sendHelper(GIOPVersion giopVersion, Message msg)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1564
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1565
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1566
        // REVISIT: See comments in CDROutputObject constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1567
        CDROutputObject outputObject =
02bb8761fcce Initial load
duke
parents:
diff changeset
  1568
            new CDROutputObject((ORB)orb, null, giopVersion, this, msg,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1569
                                ORBConstants.STREAM_FORMAT_VERSION_1);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1570
        msg.write(outputObject);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1571
02bb8761fcce Initial load
duke
parents:
diff changeset
  1572
        outputObject.writeTo(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1573
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1574
02bb8761fcce Initial load
duke
parents:
diff changeset
  1575
    public void sendCancelRequestWithLock(GIOPVersion giopVersion,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1576
                                          int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1577
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1578
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1579
        writeLock();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1580
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1581
            sendCancelRequest(giopVersion, requestId);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1582
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1583
            writeUnlock();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1584
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1585
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1586
02bb8761fcce Initial load
duke
parents:
diff changeset
  1587
    // Begin Code Base methods ---------------------------------------
02bb8761fcce Initial load
duke
parents:
diff changeset
  1588
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1589
    // Set this connection's code base IOR.  The IOR comes from the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1590
    // SendingContext.  This is an optional service context, but all
02bb8761fcce Initial load
duke
parents:
diff changeset
  1591
    // JavaSoft ORBs send it.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1592
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1593
    // The set and get methods don't need to be synchronized since the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1594
    // first possible get would occur during reading a valuetype, and
02bb8761fcce Initial load
duke
parents:
diff changeset
  1595
    // that would be after the set.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1596
02bb8761fcce Initial load
duke
parents:
diff changeset
  1597
    // Sets this connection's code base IOR.  This is done after
02bb8761fcce Initial load
duke
parents:
diff changeset
  1598
    // getting the IOR out of the SendingContext service context.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1599
    // Our ORBs always send this, but it's optional in CORBA.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1600
02bb8761fcce Initial load
duke
parents:
diff changeset
  1601
    public final void setCodeBaseIOR(IOR ior) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1602
        codeBaseServerIOR = ior;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1603
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1604
02bb8761fcce Initial load
duke
parents:
diff changeset
  1605
    public final IOR getCodeBaseIOR() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1606
        return codeBaseServerIOR;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1607
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1608
02bb8761fcce Initial load
duke
parents:
diff changeset
  1609
    // Get a CodeBase stub to use in unmarshaling.  The CachedCodeBase
02bb8761fcce Initial load
duke
parents:
diff changeset
  1610
    // won't connect to the remote codebase unless it's necessary.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1611
    public final CodeBase getCodeBase() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1612
        return cachedCodeBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1613
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1614
02bb8761fcce Initial load
duke
parents:
diff changeset
  1615
    // End Code Base methods -----------------------------------------
02bb8761fcce Initial load
duke
parents:
diff changeset
  1616
02bb8761fcce Initial load
duke
parents:
diff changeset
  1617
    // set transport read thresholds
02bb8761fcce Initial load
duke
parents:
diff changeset
  1618
    protected void setReadTimeouts(ReadTimeouts readTimeouts) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1619
        this.readTimeouts = readTimeouts;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1620
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1621
02bb8761fcce Initial load
duke
parents:
diff changeset
  1622
    protected void setPartialMessageMediator(CorbaMessageMediator messageMediator) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1623
        partialMessageMediator = messageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1624
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1625
02bb8761fcce Initial load
duke
parents:
diff changeset
  1626
    protected CorbaMessageMediator getPartialMessageMediator() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1627
        return partialMessageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1628
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1629
02bb8761fcce Initial load
duke
parents:
diff changeset
  1630
    public String toString()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1631
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1632
        synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1633
            return
02bb8761fcce Initial load
duke
parents:
diff changeset
  1634
                "SocketOrChannelConnectionImpl[" + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1635
                + (socketChannel == null ?
02bb8761fcce Initial load
duke
parents:
diff changeset
  1636
                   socket.toString() : socketChannel.toString()) + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1637
                + getStateString( state ) + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1638
                + shouldUseSelectThreadToWait() + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1639
                + shouldUseWorkerThreadForEvent() + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1640
                + shouldReadGiopHeaderOnly()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1641
                + "]" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1642
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1643
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1644
02bb8761fcce Initial load
duke
parents:
diff changeset
  1645
    // Must be public - used in encoding.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1646
    public void dprint(String msg)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1647
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1648
        ORBUtility.dprint("SocketOrChannelConnectionImpl", msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1649
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1650
02bb8761fcce Initial load
duke
parents:
diff changeset
  1651
    protected void dprint(String msg, Throwable t)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1652
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1653
        dprint(msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1654
        t.printStackTrace(System.out);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1655
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1656
}
02bb8761fcce Initial load
duke
parents:
diff changeset
  1657
02bb8761fcce Initial load
duke
parents:
diff changeset
  1658
// End of file.