corba/src/share/classes/com/sun/corba/se/impl/transport/SocketOrChannelConnectionImpl.java
author skoppar
Thu, 07 Oct 2010 00:59:40 -0700
changeset 7579 cf90ea1653fb
parent 5555 b2b5ed3f0d0d
child 7672 aec650969dd5
permissions -rw-r--r--
6714797: InitialContext.close does not close NIO socket connections Reviewed-by: asaha
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 3291
diff changeset
     2
 * Copyright (c) 2001, 2009, Oracle and/or its affiliates. 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
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 3291
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 3291
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
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
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 3291
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 3291
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 3291
diff changeset
    23
 * questions.
4
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
            }
7579
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   814
            closeConnectionResources();
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   815
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   816
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   817
                dprint(".close<-: " + this);
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
7579
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   822
    public void closeConnectionResources() {
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   823
           if (orb.transportDebugFlag) {
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   824
               dprint(".closeConnectionResources->: " + this);
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   825
           }
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   826
           Selector selector = orb.getTransportManager().getSelector(0);
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   827
           selector.unregisterForEvent(this);
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   828
           try {
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   829
             if (socketChannel != null)
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   830
              socketChannel.close() ;
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   831
                if (socket != null && !socket.isClosed())
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   832
                socket.close() ;
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   833
           } catch (IOException e) {
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   834
             if (orb.transportDebugFlag) {
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   835
                 dprint( ".closeConnectionResources: " + this, e ) ;
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   836
             }
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   837
           }
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   838
           if (orb.transportDebugFlag) {
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   839
               dprint(".closeConnectionResources<-: " + this);
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   840
           }
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   841
     }
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   842
cf90ea1653fb 6714797: InitialContext.close does not close NIO socket connections
skoppar
parents: 5555
diff changeset
   843
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   844
    public Acceptor getAcceptor()
02bb8761fcce Initial load
duke
parents:
diff changeset
   845
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   846
        return acceptor;
02bb8761fcce Initial load
duke
parents:
diff changeset
   847
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   848
02bb8761fcce Initial load
duke
parents:
diff changeset
   849
    public ContactInfo getContactInfo()
02bb8761fcce Initial load
duke
parents:
diff changeset
   850
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   851
        return contactInfo;
02bb8761fcce Initial load
duke
parents:
diff changeset
   852
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   853
02bb8761fcce Initial load
duke
parents:
diff changeset
   854
    public EventHandler getEventHandler()
02bb8761fcce Initial load
duke
parents:
diff changeset
   855
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   856
        return this;
02bb8761fcce Initial load
duke
parents:
diff changeset
   857
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   858
02bb8761fcce Initial load
duke
parents:
diff changeset
   859
    public OutputObject createOutputObject(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
   860
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   861
        // REVISIT - remove this method from Connection and all it subclasses.
02bb8761fcce Initial load
duke
parents:
diff changeset
   862
        throw new RuntimeException("*****SocketOrChannelConnectionImpl.createOutputObject - should not be called.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   863
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   864
02bb8761fcce Initial load
duke
parents:
diff changeset
   865
    // This is used by the GIOPOutputObject in order to
02bb8761fcce Initial load
duke
parents:
diff changeset
   866
    // throw the correct error when handling code sets.
02bb8761fcce Initial load
duke
parents:
diff changeset
   867
    // Can we determine if we are on the server side by
02bb8761fcce Initial load
duke
parents:
diff changeset
   868
    // other means?  XREVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   869
    public boolean isServer()
02bb8761fcce Initial load
duke
parents:
diff changeset
   870
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   871
        return isServer;
02bb8761fcce Initial load
duke
parents:
diff changeset
   872
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   873
02bb8761fcce Initial load
duke
parents:
diff changeset
   874
    public boolean isBusy()
02bb8761fcce Initial load
duke
parents:
diff changeset
   875
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   876
        if (serverRequestCount > 0 ||
02bb8761fcce Initial load
duke
parents:
diff changeset
   877
            getResponseWaitingRoom().numberRegistered() > 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   878
        {
02bb8761fcce Initial load
duke
parents:
diff changeset
   879
            return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   880
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   881
            return false;
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
    public long getTimeStamp()
02bb8761fcce Initial load
duke
parents:
diff changeset
   886
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   887
        return timeStamp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   888
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   889
02bb8761fcce Initial load
duke
parents:
diff changeset
   890
    public void setTimeStamp(long time)
02bb8761fcce Initial load
duke
parents:
diff changeset
   891
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   892
        timeStamp = time;
02bb8761fcce Initial load
duke
parents:
diff changeset
   893
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   894
02bb8761fcce Initial load
duke
parents:
diff changeset
   895
    public void setState(String stateString)
02bb8761fcce Initial load
duke
parents:
diff changeset
   896
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   897
        synchronized (stateEvent) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   898
            if (stateString.equals("ESTABLISHED")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   899
                state =  ESTABLISHED;
02bb8761fcce Initial load
duke
parents:
diff changeset
   900
                stateEvent.notifyAll();
02bb8761fcce Initial load
duke
parents:
diff changeset
   901
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   902
                // REVISIT: ASSERT
02bb8761fcce Initial load
duke
parents:
diff changeset
   903
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   904
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   905
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   906
02bb8761fcce Initial load
duke
parents:
diff changeset
   907
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   908
     * Sets the writeLock for this connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
   909
     * If the writeLock is already set by someone else, block till the
02bb8761fcce Initial load
duke
parents:
diff changeset
   910
     * writeLock is released and can set by us.
02bb8761fcce Initial load
duke
parents:
diff changeset
   911
     * IMPORTANT: this connection's lock must be acquired before
02bb8761fcce Initial load
duke
parents:
diff changeset
   912
     * setting the writeLock and must be unlocked after setting the writeLock.
02bb8761fcce Initial load
duke
parents:
diff changeset
   913
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   914
    public void writeLock()
02bb8761fcce Initial load
duke
parents:
diff changeset
   915
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   916
      try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   917
        if (dprintWriteLocks && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   918
            dprint(".writeLock->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   919
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   920
        // Keep looping till we can set the writeLock.
02bb8761fcce Initial load
duke
parents:
diff changeset
   921
        while ( true ) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   922
            int localState = state;
02bb8761fcce Initial load
duke
parents:
diff changeset
   923
            switch ( localState ) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   924
02bb8761fcce Initial load
duke
parents:
diff changeset
   925
            case OPENING:
02bb8761fcce Initial load
duke
parents:
diff changeset
   926
                synchronized (stateEvent) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   927
                    if (state != OPENING) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   928
                        // somebody has changed 'state' so be careful
02bb8761fcce Initial load
duke
parents:
diff changeset
   929
                        break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   930
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   931
                    try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   932
                        stateEvent.wait();
02bb8761fcce Initial load
duke
parents:
diff changeset
   933
                    } catch (InterruptedException ie) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   934
                        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   935
                            dprint(".writeLock: OPENING InterruptedException: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   936
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   937
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   938
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   939
                // Loop back
02bb8761fcce Initial load
duke
parents:
diff changeset
   940
                break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   941
02bb8761fcce Initial load
duke
parents:
diff changeset
   942
            case ESTABLISHED:
02bb8761fcce Initial load
duke
parents:
diff changeset
   943
                synchronized (writeEvent) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   944
                    if (!writeLocked) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   945
                        writeLocked = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   946
                        return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   947
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   948
02bb8761fcce Initial load
duke
parents:
diff changeset
   949
                    try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   950
                        // do not stay here too long if state != ESTABLISHED
02bb8761fcce Initial load
duke
parents:
diff changeset
   951
                        // Bug 4752117
02bb8761fcce Initial load
duke
parents:
diff changeset
   952
                        while (state == ESTABLISHED && writeLocked) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   953
                            writeEvent.wait(100);
02bb8761fcce Initial load
duke
parents:
diff changeset
   954
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   955
                    } catch (InterruptedException ie) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   956
                        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   957
                            dprint(".writeLock: ESTABLISHED InterruptedException: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   958
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   959
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   960
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   961
                // Loop back
02bb8761fcce Initial load
duke
parents:
diff changeset
   962
                break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   963
02bb8761fcce Initial load
duke
parents:
diff changeset
   964
                //
02bb8761fcce Initial load
duke
parents:
diff changeset
   965
                // XXX
02bb8761fcce Initial load
duke
parents:
diff changeset
   966
                // Need to distinguish between client and server roles
02bb8761fcce Initial load
duke
parents:
diff changeset
   967
                // here probably.
02bb8761fcce Initial load
duke
parents:
diff changeset
   968
                //
02bb8761fcce Initial load
duke
parents:
diff changeset
   969
            case ABORT:
02bb8761fcce Initial load
duke
parents:
diff changeset
   970
                synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
   971
                    if (state != ABORT) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   972
                        break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   973
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   974
                    throw wrapper.writeErrorSend() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   975
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   976
02bb8761fcce Initial load
duke
parents:
diff changeset
   977
            case CLOSE_RECVD:
02bb8761fcce Initial load
duke
parents:
diff changeset
   978
                // the connection has been closed or closing
02bb8761fcce Initial load
duke
parents:
diff changeset
   979
                // ==> throw rebind exception
02bb8761fcce Initial load
duke
parents:
diff changeset
   980
                synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
   981
                    if (state != CLOSE_RECVD) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   982
                        break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   983
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   984
                    throw wrapper.connectionCloseRebind() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
   985
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   986
02bb8761fcce Initial load
duke
parents:
diff changeset
   987
            default:
02bb8761fcce Initial load
duke
parents:
diff changeset
   988
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   989
                    dprint(".writeLock: default: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   990
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   991
                // REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
   992
                throw new RuntimeException(".writeLock: bad state");
02bb8761fcce Initial load
duke
parents:
diff changeset
   993
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   994
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   995
      } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
   996
        if (dprintWriteLocks && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   997
            dprint(".writeLock<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   998
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   999
      }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1000
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1001
02bb8761fcce Initial load
duke
parents:
diff changeset
  1002
    public void writeUnlock()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1003
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1004
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1005
            if (dprintWriteLocks && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1006
                dprint(".writeUnlock->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1007
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1008
            synchronized (writeEvent) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1009
                writeLocked = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1010
                writeEvent.notify(); // wake up one guy waiting to write
02bb8761fcce Initial load
duke
parents:
diff changeset
  1011
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1012
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1013
            if (dprintWriteLocks && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1014
                dprint(".writeUnlock<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1015
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1016
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1017
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1018
02bb8761fcce Initial load
duke
parents:
diff changeset
  1019
    // Assumes the caller handles writeLock and writeUnlock
02bb8761fcce Initial load
duke
parents:
diff changeset
  1020
    public void sendWithoutLock(OutputObject outputObject)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1021
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1022
        // Don't we need to check for CloseConnection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1023
        // here?  REVISIT
02bb8761fcce Initial load
duke
parents:
diff changeset
  1024
02bb8761fcce Initial load
duke
parents:
diff changeset
  1025
        // XREVISIT - Shouldn't the MessageMediator
02bb8761fcce Initial load
duke
parents:
diff changeset
  1026
        // be the one to handle writing the data here?
02bb8761fcce Initial load
duke
parents:
diff changeset
  1027
02bb8761fcce Initial load
duke
parents:
diff changeset
  1028
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1029
02bb8761fcce Initial load
duke
parents:
diff changeset
  1030
            // Write the fragment/message
02bb8761fcce Initial load
duke
parents:
diff changeset
  1031
02bb8761fcce Initial load
duke
parents:
diff changeset
  1032
            CDROutputObject cdrOutputObject = (CDROutputObject) outputObject;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1033
            cdrOutputObject.writeTo(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1034
            // REVISIT - no flush?
02bb8761fcce Initial load
duke
parents:
diff changeset
  1035
            //socket.getOutputStream().flush();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1036
02bb8761fcce Initial load
duke
parents:
diff changeset
  1037
        } catch (IOException e1) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1038
02bb8761fcce Initial load
duke
parents:
diff changeset
  1039
            /*
02bb8761fcce Initial load
duke
parents:
diff changeset
  1040
             * ADDED(Ram J) 10/13/2000 In the event of an IOException, try
02bb8761fcce Initial load
duke
parents:
diff changeset
  1041
             * sending a CancelRequest for regular requests / locate requests
02bb8761fcce Initial load
duke
parents:
diff changeset
  1042
             */
02bb8761fcce Initial load
duke
parents:
diff changeset
  1043
02bb8761fcce Initial load
duke
parents:
diff changeset
  1044
            // Since IIOPOutputStream's msgheader is set only once, and not
02bb8761fcce Initial load
duke
parents:
diff changeset
  1045
            // altered during sending multiple fragments, the original
02bb8761fcce Initial load
duke
parents:
diff changeset
  1046
            // msgheader will always have the requestId.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1047
            // REVISIT This could be optimized to send a CancelRequest only
02bb8761fcce Initial load
duke
parents:
diff changeset
  1048
            // if any fragments had been sent already.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1049
02bb8761fcce Initial load
duke
parents:
diff changeset
  1050
            /* REVISIT: MOVE TO SUBCONTRACT
02bb8761fcce Initial load
duke
parents:
diff changeset
  1051
            Message msg = os.getMessage();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1052
            if (msg.getType() == Message.GIOPRequest ||
02bb8761fcce Initial load
duke
parents:
diff changeset
  1053
                    msg.getType() == Message.GIOPLocateRequest) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1054
                GIOPVersion requestVersion = msg.getGIOPVersion();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1055
                int requestId = MessageBase.getRequestId(msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1056
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1057
                    sendCancelRequest(requestVersion, requestId);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1058
                } catch (IOException e2) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1059
                    // most likely an abortive connection closure.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1060
                    // ignore, since nothing more can be done.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1061
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1062
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
02bb8761fcce Initial load
duke
parents:
diff changeset
  1067
            // REVISIT When a send failure happens, purgeCalls() need to be
02bb8761fcce Initial load
duke
parents:
diff changeset
  1068
            // called to ensure that the connection is properly removed from
02bb8761fcce Initial load
duke
parents:
diff changeset
  1069
            // further usage (ie., cancelling pending requests with COMM_FAILURE
02bb8761fcce Initial load
duke
parents:
diff changeset
  1070
            // with an appropriate minor_code CompletionStatus.MAY_BE).
02bb8761fcce Initial load
duke
parents:
diff changeset
  1071
02bb8761fcce Initial load
duke
parents:
diff changeset
  1072
            // Relying on the IIOPOutputStream (as noted below) is not
02bb8761fcce Initial load
duke
parents:
diff changeset
  1073
            // sufficient as it handles COMM_FAILURE only for the final
02bb8761fcce Initial load
duke
parents:
diff changeset
  1074
            // fragment (during invoke processing). Note that COMM_FAILURE could
02bb8761fcce Initial load
duke
parents:
diff changeset
  1075
            // happen while sending the initial fragments.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1076
            // Also the IIOPOutputStream does not properly close the connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1077
            // It simply removes the connection from the table. An orderly
02bb8761fcce Initial load
duke
parents:
diff changeset
  1078
            // closure is needed (ie., cancel pending requests on the connection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1079
            // COMM_FAILURE as well.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1080
02bb8761fcce Initial load
duke
parents:
diff changeset
  1081
            // IIOPOutputStream will cleanup the connection info when it
02bb8761fcce Initial load
duke
parents:
diff changeset
  1082
            // sees this exception.
2664
a0a22a8f16bd 6372405: Server thread hangs when fragments don't complete because of connection abort
tbell
parents: 4
diff changeset
  1083
            SystemException exc = wrapper.writeErrorSend(e1);
a0a22a8f16bd 6372405: Server thread hangs when fragments don't complete because of connection abort
tbell
parents: 4
diff changeset
  1084
            purgeCalls(exc, false, true);
a0a22a8f16bd 6372405: Server thread hangs when fragments don't complete because of connection abort
tbell
parents: 4
diff changeset
  1085
            throw exc;
4
02bb8761fcce Initial load
duke
parents:
diff changeset
  1086
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1087
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1088
02bb8761fcce Initial load
duke
parents:
diff changeset
  1089
    public void registerWaiter(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1090
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1091
        responseWaitingRoom.registerWaiter(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1092
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1093
02bb8761fcce Initial load
duke
parents:
diff changeset
  1094
    public void unregisterWaiter(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1095
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1096
        responseWaitingRoom.unregisterWaiter(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1097
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1098
02bb8761fcce Initial load
duke
parents:
diff changeset
  1099
    public InputObject waitForResponse(MessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1100
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1101
        return responseWaitingRoom.waitForResponse(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1102
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1103
02bb8761fcce Initial load
duke
parents:
diff changeset
  1104
    public void setConnectionCache(ConnectionCache connectionCache)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1105
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1106
        this.connectionCache = connectionCache;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1107
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1108
02bb8761fcce Initial load
duke
parents:
diff changeset
  1109
    public ConnectionCache getConnectionCache()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1110
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1111
        return connectionCache;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1112
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1113
02bb8761fcce Initial load
duke
parents:
diff changeset
  1114
    ////////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
  1115
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1116
    // EventHandler methods
02bb8761fcce Initial load
duke
parents:
diff changeset
  1117
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1118
02bb8761fcce Initial load
duke
parents:
diff changeset
  1119
    public void setUseSelectThreadToWait(boolean x)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1120
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1121
        useSelectThreadToWait = x;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1122
        // REVISIT - Reading of a GIOP header only is information
02bb8761fcce Initial load
duke
parents:
diff changeset
  1123
        //           that should be passed into the constructor
02bb8761fcce Initial load
duke
parents:
diff changeset
  1124
        //           from the SocketOrChannelConnection factory.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1125
        setReadGiopHeaderOnly(shouldUseSelectThreadToWait());
02bb8761fcce Initial load
duke
parents:
diff changeset
  1126
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1127
02bb8761fcce Initial load
duke
parents:
diff changeset
  1128
    public void handleEvent()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1129
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1130
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1131
            dprint(".handleEvent->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1132
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1133
        getSelectionKey().interestOps(getSelectionKey().interestOps() &
02bb8761fcce Initial load
duke
parents:
diff changeset
  1134
                                      (~ getInterestOps()));
02bb8761fcce Initial load
duke
parents:
diff changeset
  1135
02bb8761fcce Initial load
duke
parents:
diff changeset
  1136
        if (shouldUseWorkerThreadForEvent()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1137
            Throwable throwable = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1138
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1139
                int poolToUse = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1140
                if (shouldReadGiopHeaderOnly()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1141
                    partialMessageMediator = readBits();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1142
                    poolToUse =
02bb8761fcce Initial load
duke
parents:
diff changeset
  1143
                        partialMessageMediator.getThreadPoolToUse();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1144
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1145
02bb8761fcce Initial load
duke
parents:
diff changeset
  1146
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1147
                    dprint(".handleEvent: addWork to pool: " + poolToUse);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1148
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1149
                orb.getThreadPoolManager().getThreadPool(poolToUse)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1150
                    .getWorkQueue(0).addWork(getWork());
02bb8761fcce Initial load
duke
parents:
diff changeset
  1151
            } catch (NoSuchThreadPoolException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1152
                throwable = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1153
            } catch (NoSuchWorkQueueException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1154
                throwable = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1155
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1156
            // REVISIT: need to close connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1157
            if (throwable != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1158
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1159
                    dprint(".handleEvent: " + throwable);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1160
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1161
                INTERNAL i = new INTERNAL("NoSuchThreadPoolException");
02bb8761fcce Initial load
duke
parents:
diff changeset
  1162
                i.initCause(throwable);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1163
                throw i;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1164
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1165
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1166
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1167
                dprint(".handleEvent: doWork");
02bb8761fcce Initial load
duke
parents:
diff changeset
  1168
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1169
            getWork().doWork();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1170
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1171
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1172
            dprint(".handleEvent<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1173
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1174
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1175
02bb8761fcce Initial load
duke
parents:
diff changeset
  1176
    public SelectableChannel getChannel()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1177
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1178
        return socketChannel;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1179
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1180
02bb8761fcce Initial load
duke
parents:
diff changeset
  1181
    public int getInterestOps()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1182
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1183
        return SelectionKey.OP_READ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1184
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1185
02bb8761fcce Initial load
duke
parents:
diff changeset
  1186
    //    public Acceptor getAcceptor() - already defined above.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1187
02bb8761fcce Initial load
duke
parents:
diff changeset
  1188
    public Connection getConnection()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1189
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1190
        return this;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1191
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1192
02bb8761fcce Initial load
duke
parents:
diff changeset
  1193
    ////////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
  1194
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1195
    // Work methods.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1196
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1197
02bb8761fcce Initial load
duke
parents:
diff changeset
  1198
    public String getName()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1199
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1200
        return this.toString();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1201
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1202
02bb8761fcce Initial load
duke
parents:
diff changeset
  1203
    public void doWork()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1204
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1205
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1206
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1207
                dprint(".doWork->: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1208
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1209
02bb8761fcce Initial load
duke
parents:
diff changeset
  1210
            // IMPORTANT: Sanity checks on SelectionKeys such as
02bb8761fcce Initial load
duke
parents:
diff changeset
  1211
            //            SelectorKey.isValid() should not be done
02bb8761fcce Initial load
duke
parents:
diff changeset
  1212
            //            here.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1213
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1214
02bb8761fcce Initial load
duke
parents:
diff changeset
  1215
            if (!shouldReadGiopHeaderOnly()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1216
                read();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1217
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1218
            else {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1219
                // get the partialMessageMediator
02bb8761fcce Initial load
duke
parents:
diff changeset
  1220
                // created by SelectorThread
02bb8761fcce Initial load
duke
parents:
diff changeset
  1221
                CorbaMessageMediator messageMediator =
02bb8761fcce Initial load
duke
parents:
diff changeset
  1222
                                         this.getPartialMessageMediator();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1223
02bb8761fcce Initial load
duke
parents:
diff changeset
  1224
                // read remaining info needed in a MessageMediator
02bb8761fcce Initial load
duke
parents:
diff changeset
  1225
                messageMediator = finishReadingBits(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1226
02bb8761fcce Initial load
duke
parents:
diff changeset
  1227
                if (messageMediator != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1228
                    // Null can happen when client closes stream
02bb8761fcce Initial load
duke
parents:
diff changeset
  1229
                    // causing purgecalls.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1230
                    dispatch(messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1231
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1232
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1233
        } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1234
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1235
                dprint(".doWork: ignoring Throwable: "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1236
                       + t
02bb8761fcce Initial load
duke
parents:
diff changeset
  1237
                       + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1238
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1239
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1240
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1241
                dprint(".doWork<-: " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1242
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1243
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1244
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1245
02bb8761fcce Initial load
duke
parents:
diff changeset
  1246
    public void setEnqueueTime(long timeInMillis)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1247
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1248
        enqueueTime = timeInMillis;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1249
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1250
02bb8761fcce Initial load
duke
parents:
diff changeset
  1251
    public long getEnqueueTime()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1252
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1253
        return enqueueTime;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1254
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1255
02bb8761fcce Initial load
duke
parents:
diff changeset
  1256
    ////////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
  1257
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1258
    // spi.transport.CorbaConnection.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1259
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1260
02bb8761fcce Initial load
duke
parents:
diff changeset
  1261
    // IMPORTANT: Reader Threads must NOT read Giop header only.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1262
    public boolean shouldReadGiopHeaderOnly() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1263
        return shouldReadGiopHeaderOnly;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1264
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1265
02bb8761fcce Initial load
duke
parents:
diff changeset
  1266
    protected void setReadGiopHeaderOnly(boolean shouldReadHeaderOnly) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1267
        shouldReadGiopHeaderOnly = shouldReadHeaderOnly;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1268
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1269
02bb8761fcce Initial load
duke
parents:
diff changeset
  1270
    public ResponseWaitingRoom getResponseWaitingRoom()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1271
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1272
        return responseWaitingRoom;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1273
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1274
02bb8761fcce Initial load
duke
parents:
diff changeset
  1275
    // REVISIT - inteface defines isServer but already defined in
02bb8761fcce Initial load
duke
parents:
diff changeset
  1276
    // higher interface.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1277
02bb8761fcce Initial load
duke
parents:
diff changeset
  1278
    public void serverRequestMapPut(int requestId,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1279
                                    CorbaMessageMediator messageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1280
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1281
        serverRequestMap.put(new Integer(requestId), messageMediator);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1282
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1283
02bb8761fcce Initial load
duke
parents:
diff changeset
  1284
    public CorbaMessageMediator serverRequestMapGet(int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1285
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1286
        return (CorbaMessageMediator)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1287
            serverRequestMap.get(new Integer(requestId));
02bb8761fcce Initial load
duke
parents:
diff changeset
  1288
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1289
02bb8761fcce Initial load
duke
parents:
diff changeset
  1290
    public void serverRequestMapRemove(int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1291
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1292
        serverRequestMap.remove(new Integer(requestId));
02bb8761fcce Initial load
duke
parents:
diff changeset
  1293
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1294
02bb8761fcce Initial load
duke
parents:
diff changeset
  1295
02bb8761fcce Initial load
duke
parents:
diff changeset
  1296
    // REVISIT: this is also defined in:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1297
    // com.sun.corba.se.spi.legacy.connection.Connection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1298
    public java.net.Socket getSocket()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1299
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1300
        return socket;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1301
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1302
02bb8761fcce Initial load
duke
parents:
diff changeset
  1303
    /** It is possible for a Close Connection to have been
02bb8761fcce Initial load
duke
parents:
diff changeset
  1304
     ** sent here, but we will not check for this. A "lazy"
02bb8761fcce Initial load
duke
parents:
diff changeset
  1305
     ** Exception will be thrown in the Worker thread after the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1306
     ** incoming request has been processed even though the connection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1307
     ** is closed before the request is processed. This is o.k because
02bb8761fcce Initial load
duke
parents:
diff changeset
  1308
     ** it is a boundary condition. To prevent it we would have to add
02bb8761fcce Initial load
duke
parents:
diff changeset
  1309
     ** more locks which would reduce performance in the normal case.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1310
     **/
02bb8761fcce Initial load
duke
parents:
diff changeset
  1311
    public synchronized void serverRequestProcessingBegins()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1312
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1313
        serverRequestCount++;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1314
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1315
02bb8761fcce Initial load
duke
parents:
diff changeset
  1316
    public synchronized void serverRequestProcessingEnds()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1317
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1318
        serverRequestCount--;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1319
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1320
02bb8761fcce Initial load
duke
parents:
diff changeset
  1321
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1322
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1323
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1324
02bb8761fcce Initial load
duke
parents:
diff changeset
  1325
    public synchronized int getNextRequestId()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1326
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1327
        return requestId++;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1328
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1329
02bb8761fcce Initial load
duke
parents:
diff changeset
  1330
    // Negotiated code sets for char and wchar data
02bb8761fcce Initial load
duke
parents:
diff changeset
  1331
    protected CodeSetComponentInfo.CodeSetContext codeSetContext = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1332
02bb8761fcce Initial load
duke
parents:
diff changeset
  1333
    public ORB getBroker()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1334
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1335
        return orb;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1336
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1337
02bb8761fcce Initial load
duke
parents:
diff changeset
  1338
    public CodeSetComponentInfo.CodeSetContext getCodeSetContext() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1339
        // Needs to be synchronized for the following case when the client
02bb8761fcce Initial load
duke
parents:
diff changeset
  1340
        // doesn't send the code set context twice, and we have two threads
02bb8761fcce Initial load
duke
parents:
diff changeset
  1341
        // in ServerRequestDispatcher processCodeSetContext.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1342
        //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1343
        // Thread A checks to see if there is a context, there is none, so
02bb8761fcce Initial load
duke
parents:
diff changeset
  1344
        //     it calls setCodeSetContext, getting the synch lock.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1345
        // Thread B checks to see if there is a context.  If we didn't synch,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1346
        //     it might decide to outlaw wchar/wstring.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1347
        if (codeSetContext == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1348
            synchronized(this) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1349
                return codeSetContext;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1350
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1351
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1352
02bb8761fcce Initial load
duke
parents:
diff changeset
  1353
        return codeSetContext;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1354
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1355
02bb8761fcce Initial load
duke
parents:
diff changeset
  1356
    public synchronized void setCodeSetContext(CodeSetComponentInfo.CodeSetContext csc) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1357
        // Double check whether or not we need to do this
02bb8761fcce Initial load
duke
parents:
diff changeset
  1358
        if (codeSetContext == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1359
02bb8761fcce Initial load
duke
parents:
diff changeset
  1360
            if (OSFCodeSetRegistry.lookupEntry(csc.getCharCodeSet()) == null ||
02bb8761fcce Initial load
duke
parents:
diff changeset
  1361
                OSFCodeSetRegistry.lookupEntry(csc.getWCharCodeSet()) == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1362
                // If the client says it's negotiated a code set that
02bb8761fcce Initial load
duke
parents:
diff changeset
  1363
                // isn't a fallback and we never said we support, then
02bb8761fcce Initial load
duke
parents:
diff changeset
  1364
                // it has a bug.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1365
                throw wrapper.badCodesetsFromClient() ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1366
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1367
02bb8761fcce Initial load
duke
parents:
diff changeset
  1368
            codeSetContext = csc;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1369
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1370
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1371
02bb8761fcce Initial load
duke
parents:
diff changeset
  1372
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1373
    // from iiop.IIOPConnection.java
02bb8761fcce Initial load
duke
parents:
diff changeset
  1374
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1375
02bb8761fcce Initial load
duke
parents:
diff changeset
  1376
    // Map request ID to an InputObject.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1377
    // This is so the client thread can start unmarshaling
02bb8761fcce Initial load
duke
parents:
diff changeset
  1378
    // the reply and remove it from the out_calls map while the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1379
    // ReaderThread can still obtain the input stream to give
02bb8761fcce Initial load
duke
parents:
diff changeset
  1380
    // new fragments.  Only the ReaderThread touches the clientReplyMap,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1381
    // so it doesn't incur synchronization overhead.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1382
02bb8761fcce Initial load
duke
parents:
diff changeset
  1383
    public MessageMediator clientRequestMapGet(int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1384
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1385
        return responseWaitingRoom.getMessageMediator(requestId);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1386
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1387
02bb8761fcce Initial load
duke
parents:
diff changeset
  1388
    protected MessageMediator clientReply_1_1;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1389
02bb8761fcce Initial load
duke
parents:
diff changeset
  1390
    public void clientReply_1_1_Put(MessageMediator x)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1391
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1392
        clientReply_1_1 = x;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1393
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1394
02bb8761fcce Initial load
duke
parents:
diff changeset
  1395
    public MessageMediator clientReply_1_1_Get()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1396
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1397
        return  clientReply_1_1;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1398
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1399
02bb8761fcce Initial load
duke
parents:
diff changeset
  1400
    public void clientReply_1_1_Remove()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1401
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1402
        clientReply_1_1 = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1403
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1404
02bb8761fcce Initial load
duke
parents:
diff changeset
  1405
    protected MessageMediator serverRequest_1_1;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1406
02bb8761fcce Initial load
duke
parents:
diff changeset
  1407
    public void serverRequest_1_1_Put(MessageMediator x)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1408
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1409
        serverRequest_1_1 = x;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1410
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1411
02bb8761fcce Initial load
duke
parents:
diff changeset
  1412
    public MessageMediator serverRequest_1_1_Get()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1413
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1414
        return  serverRequest_1_1;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1415
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1416
02bb8761fcce Initial load
duke
parents:
diff changeset
  1417
    public void serverRequest_1_1_Remove()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1418
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1419
        serverRequest_1_1 = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1420
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1421
02bb8761fcce Initial load
duke
parents:
diff changeset
  1422
    protected String getStateString( int state )
02bb8761fcce Initial load
duke
parents:
diff changeset
  1423
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1424
        synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1425
            switch (state) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1426
            case OPENING : return "OPENING" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1427
            case ESTABLISHED : return "ESTABLISHED" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1428
            case CLOSE_SENT : return "CLOSE_SENT" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1429
            case CLOSE_RECVD : return "CLOSE_RECVD" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1430
            case ABORT : return "ABORT" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1431
            default : return "???" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1432
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1433
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1434
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1435
02bb8761fcce Initial load
duke
parents:
diff changeset
  1436
    public synchronized boolean isPostInitialContexts() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1437
        return postInitialContexts;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1438
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1439
02bb8761fcce Initial load
duke
parents:
diff changeset
  1440
    // Can never be unset...
02bb8761fcce Initial load
duke
parents:
diff changeset
  1441
    public synchronized void setPostInitialContexts(){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1442
        postInitialContexts = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1443
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1444
02bb8761fcce Initial load
duke
parents:
diff changeset
  1445
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
  1446
     * Wake up the outstanding requests on the connection, and hand them
02bb8761fcce Initial load
duke
parents:
diff changeset
  1447
     * COMM_FAILURE exception with a given minor code.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1448
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
  1449
     * Also, delete connection from connection table and
02bb8761fcce Initial load
duke
parents:
diff changeset
  1450
     * stop the reader thread.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1451
02bb8761fcce Initial load
duke
parents:
diff changeset
  1452
     * Note that this should only ever be called by the Reader thread for
02bb8761fcce Initial load
duke
parents:
diff changeset
  1453
     * this connection.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1454
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
  1455
     * @param minor_code The minor code for the COMM_FAILURE major code.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1456
     * @param die Kill the reader thread (this thread) before exiting.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1457
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
  1458
    public void purgeCalls(SystemException systemException,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1459
                           boolean die, boolean lockHeld)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1460
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1461
        int minor_code = systemException.minor;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1462
02bb8761fcce Initial load
duke
parents:
diff changeset
  1463
        try{
02bb8761fcce Initial load
duke
parents:
diff changeset
  1464
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1465
                dprint(".purgeCalls->: "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1466
                       + minor_code + "/" + die + "/" + lockHeld
02bb8761fcce Initial load
duke
parents:
diff changeset
  1467
                       + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1468
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1469
02bb8761fcce Initial load
duke
parents:
diff changeset
  1470
            // If this invocation is a result of ThreadDeath caused
02bb8761fcce Initial load
duke
parents:
diff changeset
  1471
            // by a previous execution of this routine, just exit.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1472
02bb8761fcce Initial load
duke
parents:
diff changeset
  1473
            synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1474
                if ((state == ABORT) || (state == CLOSE_RECVD)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1475
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1476
                        dprint(".purgeCalls: exiting since state is: "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1477
                               + getStateString(state)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1478
                               + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1479
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1480
                    return;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1481
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1482
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1483
02bb8761fcce Initial load
duke
parents:
diff changeset
  1484
            // Grab the writeLock (freeze the calls)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1485
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1486
                if (!lockHeld) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1487
                    writeLock();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1488
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1489
            } catch (SystemException ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1490
                if (orb.transportDebugFlag)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1491
                    dprint(".purgeCalls: SystemException" + ex
02bb8761fcce Initial load
duke
parents:
diff changeset
  1492
                           + "; continuing " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1493
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1494
02bb8761fcce Initial load
duke
parents:
diff changeset
  1495
            // Mark the state of the connection
02bb8761fcce Initial load
duke
parents:
diff changeset
  1496
            // and determine the request status
02bb8761fcce Initial load
duke
parents:
diff changeset
  1497
            org.omg.CORBA.CompletionStatus completion_status;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1498
            synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1499
                if (minor_code == ORBUtilSystemException.CONNECTION_REBIND) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1500
                    state = CLOSE_RECVD;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1501
                    systemException.completed = CompletionStatus.COMPLETED_NO;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1502
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1503
                    state = ABORT;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1504
                    systemException.completed = CompletionStatus.COMPLETED_MAYBE;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1505
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1506
                stateEvent.notifyAll();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1507
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1508
02bb8761fcce Initial load
duke
parents:
diff changeset
  1509
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1510
                socket.getInputStream().close();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1511
                socket.getOutputStream().close();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1512
                socket.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1513
            } catch (Exception ex) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1514
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1515
                    dprint(".purgeCalls: Exception closing socket: " + ex
02bb8761fcce Initial load
duke
parents:
diff changeset
  1516
                           + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1517
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1518
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1519
02bb8761fcce Initial load
duke
parents:
diff changeset
  1520
            // Signal all threads with outstanding requests on this
02bb8761fcce Initial load
duke
parents:
diff changeset
  1521
            // connection and give them the SystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1522
02bb8761fcce Initial load
duke
parents:
diff changeset
  1523
            responseWaitingRoom.signalExceptionToAllWaiters(systemException);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1524
02bb8761fcce Initial load
duke
parents:
diff changeset
  1525
            if (contactInfo != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1526
                ((OutboundConnectionCache)getConnectionCache()).remove(contactInfo);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1527
            } else if (acceptor != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1528
                ((InboundConnectionCache)getConnectionCache()).remove(this);
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
            // REVISIT: Stop the reader thread
02bb8761fcce Initial load
duke
parents:
diff changeset
  1533
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1534
02bb8761fcce Initial load
duke
parents:
diff changeset
  1535
            // Signal all the waiters of the writeLock.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1536
            // There are 4 types of writeLock waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1537
            // 1. Send waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1538
            // 2. SendReply waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1539
            // 3. cleanUp waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1540
            // 4. purge_call waiters:
02bb8761fcce Initial load
duke
parents:
diff changeset
  1541
            //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1542
02bb8761fcce Initial load
duke
parents:
diff changeset
  1543
            writeUnlock();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1544
02bb8761fcce Initial load
duke
parents:
diff changeset
  1545
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1546
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1547
                dprint(".purgeCalls<-: "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1548
                       + minor_code + "/" + die + "/" + lockHeld
02bb8761fcce Initial load
duke
parents:
diff changeset
  1549
                       + " " + this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1550
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1551
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1552
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1553
02bb8761fcce Initial load
duke
parents:
diff changeset
  1554
    /*************************************************************************
02bb8761fcce Initial load
duke
parents:
diff changeset
  1555
    * The following methods are for dealing with Connection cleaning for
02bb8761fcce Initial load
duke
parents:
diff changeset
  1556
    * better scalability of servers in high network load conditions.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1557
    **************************************************************************/
02bb8761fcce Initial load
duke
parents:
diff changeset
  1558
02bb8761fcce Initial load
duke
parents:
diff changeset
  1559
    public void sendCloseConnection(GIOPVersion giopVersion)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1560
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1561
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1562
        Message msg = MessageBase.createCloseConnection(giopVersion);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1563
        sendHelper(giopVersion, msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1564
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1565
02bb8761fcce Initial load
duke
parents:
diff changeset
  1566
    public void sendMessageError(GIOPVersion giopVersion)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1567
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1568
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1569
        Message msg = MessageBase.createMessageError(giopVersion);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1570
        sendHelper(giopVersion, msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1571
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1572
02bb8761fcce Initial load
duke
parents:
diff changeset
  1573
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
  1574
     * Send a CancelRequest message. This does not lock the connection, so the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1575
     * caller needs to ensure this method is called appropriately.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1576
     * @exception IOException - could be due to abortive connection closure.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1577
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
  1578
    public void sendCancelRequest(GIOPVersion giopVersion, int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1579
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1580
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1581
02bb8761fcce Initial load
duke
parents:
diff changeset
  1582
        Message msg = MessageBase.createCancelRequest(giopVersion, requestId);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1583
        sendHelper(giopVersion, msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1584
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1585
02bb8761fcce Initial load
duke
parents:
diff changeset
  1586
    protected void sendHelper(GIOPVersion giopVersion, Message msg)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1587
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1588
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1589
        // REVISIT: See comments in CDROutputObject constructor.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1590
        CDROutputObject outputObject =
02bb8761fcce Initial load
duke
parents:
diff changeset
  1591
            new CDROutputObject((ORB)orb, null, giopVersion, this, msg,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1592
                                ORBConstants.STREAM_FORMAT_VERSION_1);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1593
        msg.write(outputObject);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1594
02bb8761fcce Initial load
duke
parents:
diff changeset
  1595
        outputObject.writeTo(this);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1596
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1597
02bb8761fcce Initial load
duke
parents:
diff changeset
  1598
    public void sendCancelRequestWithLock(GIOPVersion giopVersion,
02bb8761fcce Initial load
duke
parents:
diff changeset
  1599
                                          int requestId)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1600
        throws IOException
02bb8761fcce Initial load
duke
parents:
diff changeset
  1601
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1602
        writeLock();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1603
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1604
            sendCancelRequest(giopVersion, requestId);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1605
        } finally {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1606
            writeUnlock();
02bb8761fcce Initial load
duke
parents:
diff changeset
  1607
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1608
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1609
02bb8761fcce Initial load
duke
parents:
diff changeset
  1610
    // Begin Code Base methods ---------------------------------------
02bb8761fcce Initial load
duke
parents:
diff changeset
  1611
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1612
    // Set this connection's code base IOR.  The IOR comes from the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1613
    // SendingContext.  This is an optional service context, but all
02bb8761fcce Initial load
duke
parents:
diff changeset
  1614
    // JavaSoft ORBs send it.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1615
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
  1616
    // The set and get methods don't need to be synchronized since the
02bb8761fcce Initial load
duke
parents:
diff changeset
  1617
    // first possible get would occur during reading a valuetype, and
02bb8761fcce Initial load
duke
parents:
diff changeset
  1618
    // that would be after the set.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1619
02bb8761fcce Initial load
duke
parents:
diff changeset
  1620
    // Sets this connection's code base IOR.  This is done after
02bb8761fcce Initial load
duke
parents:
diff changeset
  1621
    // getting the IOR out of the SendingContext service context.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1622
    // Our ORBs always send this, but it's optional in CORBA.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1623
02bb8761fcce Initial load
duke
parents:
diff changeset
  1624
    public final void setCodeBaseIOR(IOR ior) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1625
        codeBaseServerIOR = ior;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1626
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1627
02bb8761fcce Initial load
duke
parents:
diff changeset
  1628
    public final IOR getCodeBaseIOR() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1629
        return codeBaseServerIOR;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1630
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1631
02bb8761fcce Initial load
duke
parents:
diff changeset
  1632
    // Get a CodeBase stub to use in unmarshaling.  The CachedCodeBase
02bb8761fcce Initial load
duke
parents:
diff changeset
  1633
    // won't connect to the remote codebase unless it's necessary.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1634
    public final CodeBase getCodeBase() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1635
        return cachedCodeBase;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1636
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1637
02bb8761fcce Initial load
duke
parents:
diff changeset
  1638
    // End Code Base methods -----------------------------------------
02bb8761fcce Initial load
duke
parents:
diff changeset
  1639
02bb8761fcce Initial load
duke
parents:
diff changeset
  1640
    // set transport read thresholds
02bb8761fcce Initial load
duke
parents:
diff changeset
  1641
    protected void setReadTimeouts(ReadTimeouts readTimeouts) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1642
        this.readTimeouts = readTimeouts;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1643
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1644
02bb8761fcce Initial load
duke
parents:
diff changeset
  1645
    protected void setPartialMessageMediator(CorbaMessageMediator messageMediator) {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1646
        partialMessageMediator = messageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1647
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1648
02bb8761fcce Initial load
duke
parents:
diff changeset
  1649
    protected CorbaMessageMediator getPartialMessageMediator() {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1650
        return partialMessageMediator;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1651
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1652
02bb8761fcce Initial load
duke
parents:
diff changeset
  1653
    public String toString()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1654
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1655
        synchronized ( stateEvent ){
02bb8761fcce Initial load
duke
parents:
diff changeset
  1656
            return
02bb8761fcce Initial load
duke
parents:
diff changeset
  1657
                "SocketOrChannelConnectionImpl[" + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1658
                + (socketChannel == null ?
02bb8761fcce Initial load
duke
parents:
diff changeset
  1659
                   socket.toString() : socketChannel.toString()) + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1660
                + getStateString( state ) + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1661
                + shouldUseSelectThreadToWait() + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1662
                + shouldUseWorkerThreadForEvent() + " "
02bb8761fcce Initial load
duke
parents:
diff changeset
  1663
                + shouldReadGiopHeaderOnly()
02bb8761fcce Initial load
duke
parents:
diff changeset
  1664
                + "]" ;
02bb8761fcce Initial load
duke
parents:
diff changeset
  1665
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1666
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1667
02bb8761fcce Initial load
duke
parents:
diff changeset
  1668
    // Must be public - used in encoding.
02bb8761fcce Initial load
duke
parents:
diff changeset
  1669
    public void dprint(String msg)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1670
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1671
        ORBUtility.dprint("SocketOrChannelConnectionImpl", msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1672
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1673
02bb8761fcce Initial load
duke
parents:
diff changeset
  1674
    protected void dprint(String msg, Throwable t)
02bb8761fcce Initial load
duke
parents:
diff changeset
  1675
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
  1676
        dprint(msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1677
        t.printStackTrace(System.out);
02bb8761fcce Initial load
duke
parents:
diff changeset
  1678
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
  1679
}
02bb8761fcce Initial load
duke
parents:
diff changeset
  1680
02bb8761fcce Initial load
duke
parents:
diff changeset
  1681
// End of file.