corba/src/share/classes/com/sun/corba/se/impl/transport/SelectorImpl.java
author coffeys
Wed, 27 Jun 2012 21:09:29 +0100
changeset 13171 1ac5e9a54a6e
parent 7581 26d1bb99615f
child 20904 8a9f0f04a42d
permissions -rw-r--r--
7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8 Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 7581
diff changeset
     2
 * Copyright (c) 2003, 2012, 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: 4
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: 4
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: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
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.nio.channels.ClosedChannelException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.SelectableChannel;
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.SelectionKey;
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.Selector;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import java.util.HashMap;
7581
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
    35
import java.util.Map;
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import java.util.Iterator;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import java.util.List;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import com.sun.corba.se.pept.broker.Broker;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import com.sun.corba.se.pept.transport.Acceptor;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
import com.sun.corba.se.pept.transport.Connection;
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
import com.sun.corba.se.pept.transport.EventHandler;
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
import com.sun.corba.se.pept.transport.ListenerThread;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
import com.sun.corba.se.pept.transport.ReaderThread;
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
import com.sun.corba.se.spi.logging.CORBALogDomains;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import com.sun.corba.se.spi.orb.ORB;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
import com.sun.corba.se.spi.orbutil.threadpool.Work;
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
import com.sun.corba.se.spi.orbutil.threadpool.NoSuchThreadPoolException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
import com.sun.corba.se.spi.orbutil.threadpool.NoSuchWorkQueueException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
import com.sun.corba.se.impl.logging.ORBUtilSystemException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
import com.sun.corba.se.impl.orbutil.ORBUtility;
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
 * @author Harold Carr
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
public class SelectorImpl
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
    extends
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
        Thread
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
    implements
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
        com.sun.corba.se.pept.transport.Selector
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
{
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
    private ORB orb;
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
    private Selector selector;
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
    private long timeout;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
    private List deferredRegistrations;
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
    private List interestOpsList;
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
    private HashMap listenerThreads;
7581
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
    70
    private Map readerThreads;
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
    private boolean selectorStarted;
13171
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 7581
diff changeset
    72
    private volatile boolean closed;
1ac5e9a54a6e 7162902: Umbrella port of a number of corba bug fixes from JDK 6 to jdk7u/8
coffeys
parents: 7581
diff changeset
    73
    private ORBUtilSystemException wrapper;
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
    public SelectorImpl(ORB orb)
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
        this.orb = orb;
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
        selector = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
        selectorStarted = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
        timeout = 60000;
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
        deferredRegistrations = new ArrayList();
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
        interestOpsList = new ArrayList();
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
        listenerThreads = new HashMap();
7581
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
    85
        readerThreads = java.util.Collections.synchronizedMap(new HashMap());
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
        closed = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
        wrapper = ORBUtilSystemException.get(orb,CORBALogDomains.RPC_TRANSPORT);
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
    public void setTimeout(long timeout)
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
        this.timeout = timeout;
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
    public long getTimeout()
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
        return timeout;
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
    public void registerInterestOps(EventHandler eventHandler)
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
            dprint(".registerInterestOps:-> " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
        SelectionKey selectionKey = eventHandler.getSelectionKey();
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
        if (selectionKey.isValid()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
            int ehOps = eventHandler.getInterestOps();
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
            SelectionKeyAndOp keyAndOp = new SelectionKeyAndOp(selectionKey, ehOps);
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
            synchronized(interestOpsList) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
                interestOpsList.add(keyAndOp);
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
            // tell Selector Thread there's an update to a SelectorKey's Ops
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
            selector.wakeup();
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
        else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
            wrapper.selectionKeyInvalid(eventHandler.toString());
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
                dprint(".registerInterestOps: EventHandler SelectionKey not valid " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
            dprint(".registerInterestOps:<- ");
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
    public void registerForEvent(EventHandler eventHandler)
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
            dprint(".registerForEvent: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
        if (isClosed()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
                dprint(".registerForEvent: closed: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
            return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
        if (eventHandler.shouldUseSelectThreadToWait()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
            synchronized (deferredRegistrations) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
                deferredRegistrations.add(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
            if (! selectorStarted) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
                startSelector();
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
            selector.wakeup();
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
            return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
        switch (eventHandler.getInterestOps()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
        case SelectionKey.OP_ACCEPT :
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
            createListenerThread(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
            break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
        case SelectionKey.OP_READ :
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
            createReaderThread(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
            break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
        default:
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
                dprint(".registerForEvent: default: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
            throw new RuntimeException(
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
                "SelectorImpl.registerForEvent: unknown interest ops");
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
    public void unregisterForEvent(EventHandler eventHandler)
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
            dprint(".unregisterForEvent: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
        if (isClosed()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
                dprint(".unregisterForEvent: closed: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
            return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
        if (eventHandler.shouldUseSelectThreadToWait()) {
7581
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
   182
            SelectionKey selectionKey ;
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
   183
            synchronized(deferredRegistrations) {
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
   184
                selectionKey = eventHandler.getSelectionKey();
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
   185
            }
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
   186
            if (selectionKey != null) {
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
   187
                selectionKey.cancel();
26d1bb99615f 6896157: unsynchronized hashmap in com.sun.corba.se.impl.transport.SelectorImpl.createReaderThread
skoppar
parents: 5555
diff changeset
   188
            }
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
            selector.wakeup();
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
            return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
        switch (eventHandler.getInterestOps()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
        case SelectionKey.OP_ACCEPT :
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
            destroyListenerThread(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
            break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
        case SelectionKey.OP_READ :
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
            destroyReaderThread(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
            break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
        default:
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
                dprint(".unregisterForEvent: default: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
            throw new RuntimeException(
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
                "SelectorImpl.uregisterForEvent: unknown interest ops");
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
    public void close()
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
            dprint(".close");
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
        if (isClosed()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
                dprint(".close: already closed");
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
            return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
        setClosed(true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
        Iterator i;
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
        // Kill listeners.
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
        i = listenerThreads.values().iterator();
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
        while (i.hasNext()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
            ListenerThread listenerThread = (ListenerThread) i.next();
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
            listenerThread.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
        // Kill readers.
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
        i = readerThreads.values().iterator();
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
        while (i.hasNext()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
            ReaderThread readerThread = (ReaderThread) i.next();
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
            readerThread.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
        // Selector
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
            if (selector != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
                // wakeup Selector thread to process close request
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
                selector.wakeup();
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
        } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
                dprint(".close: selector.close: " + t);
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
    ///////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
    // Thread methods.
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
    public void run()
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
        setName("SelectorThread");
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
        while (!closed) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
            try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
                int n = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
                if (timeout == 0 && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
                    dprint(".run: Beginning of selection cycle");
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
                handleDeferredRegistrations();
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
                enableInterestOps();
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
                    n = selector.select(timeout);
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
                } catch (IOException  e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
                        dprint(".run: selector.select: " + e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
                if (closed) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
                    selector.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
                        dprint(".run: closed - .run return");
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
                    return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
                /*
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
                  if (timeout == 0 && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
                  dprint(".run: selector.select() returned: " + n);
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
                  }
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
                  if (n == 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
                  continue;
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
                  }
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
                */
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
                Iterator iterator = selector.selectedKeys().iterator();
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
                    if (iterator.hasNext()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
                        dprint(".run: n = " + n);
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
                while (iterator.hasNext()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
                    SelectionKey selectionKey = (SelectionKey) iterator.next();
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
                    iterator.remove();
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
                    EventHandler eventHandler = (EventHandler)
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
                        selectionKey.attachment();
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
                    try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
                        eventHandler.handleEvent();
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
                    } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
                        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
                            dprint(".run: eventHandler.handleEvent", t);
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
                if (timeout == 0 && orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
                    dprint(".run: End of selection cycle");
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
            } catch (Throwable t) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
                // IMPORTANT: ignore all errors so the select thread keeps running.
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
                // Otherwise a guaranteed hang.
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
                    dprint(".run: ignoring", t);
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
    /////////////////////////////////////////////////////
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
    // Implementation.
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
    private synchronized boolean isClosed ()
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
        return closed;
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
    private synchronized void setClosed(boolean closed)
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
        this.closed = closed;
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
    private void startSelector()
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
            selector = Selector.open();
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
        } catch (IOException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
                dprint(".startSelector: Selector.open: IOException: " + e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
            // REVISIT - better handling/reporting
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
            RuntimeException rte =
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
                new RuntimeException(".startSelector: Selector.open exception");
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
            rte.initCause(e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
            throw rte;
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
        setDaemon(true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
        start();
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
        selectorStarted = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
            dprint(".startSelector: selector.start completed.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
    private void handleDeferredRegistrations()
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
        synchronized (deferredRegistrations) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
            int deferredListSize = deferredRegistrations.size();
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
            for (int i = 0; i < deferredListSize; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
                EventHandler eventHandler =
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
                    (EventHandler)deferredRegistrations.get(i);
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
                    dprint(".handleDeferredRegistrations: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
                SelectableChannel channel = eventHandler.getChannel();
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
                SelectionKey selectionKey = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
                    selectionKey =
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
                        channel.register(selector,
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
                                         eventHandler.getInterestOps(),
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
                                         (Object)eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
                } catch (ClosedChannelException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
                    if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
                        dprint(".handleDeferredRegistrations: " + e);
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   384
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   385
                eventHandler.setSelectionKey(selectionKey);
02bb8761fcce Initial load
duke
parents:
diff changeset
   386
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   387
            deferredRegistrations.clear();
02bb8761fcce Initial load
duke
parents:
diff changeset
   388
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   389
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   390
02bb8761fcce Initial load
duke
parents:
diff changeset
   391
    private void enableInterestOps()
02bb8761fcce Initial load
duke
parents:
diff changeset
   392
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   393
        synchronized (interestOpsList) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   394
            int listSize = interestOpsList.size();
02bb8761fcce Initial load
duke
parents:
diff changeset
   395
            if (listSize > 0) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   396
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   397
                    dprint(".enableInterestOps:->");
02bb8761fcce Initial load
duke
parents:
diff changeset
   398
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   399
                SelectionKey selectionKey = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   400
                SelectionKeyAndOp keyAndOp = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   401
                int keyOp, selectionKeyOps = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   402
                for (int i = 0; i < listSize; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   403
                    keyAndOp = (SelectionKeyAndOp)interestOpsList.get(i);
02bb8761fcce Initial load
duke
parents:
diff changeset
   404
                    selectionKey = keyAndOp.selectionKey;
02bb8761fcce Initial load
duke
parents:
diff changeset
   405
02bb8761fcce Initial load
duke
parents:
diff changeset
   406
                    // Need to check if the SelectionKey is valid because a
02bb8761fcce Initial load
duke
parents:
diff changeset
   407
                    // connection's SelectionKey could be put on the list to
02bb8761fcce Initial load
duke
parents:
diff changeset
   408
                    // have its OP enabled and before it's enabled be reclaimed.
02bb8761fcce Initial load
duke
parents:
diff changeset
   409
                    // Otherwise, the enabling of the OP will throw an exception
02bb8761fcce Initial load
duke
parents:
diff changeset
   410
                    // here and exit this method an potentially not enable all
02bb8761fcce Initial load
duke
parents:
diff changeset
   411
                    // registered ops.
02bb8761fcce Initial load
duke
parents:
diff changeset
   412
                    //
02bb8761fcce Initial load
duke
parents:
diff changeset
   413
                    // So, we ignore SelectionKeys that are invalid. They will get
02bb8761fcce Initial load
duke
parents:
diff changeset
   414
                    // cleaned up on the next Selector.select() call.
02bb8761fcce Initial load
duke
parents:
diff changeset
   415
02bb8761fcce Initial load
duke
parents:
diff changeset
   416
                    if (selectionKey.isValid()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   417
                        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   418
                            dprint(".enableInterestOps: " + keyAndOp);
02bb8761fcce Initial load
duke
parents:
diff changeset
   419
                        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   420
                        keyOp = keyAndOp.keyOp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   421
                        selectionKeyOps = selectionKey.interestOps();
02bb8761fcce Initial load
duke
parents:
diff changeset
   422
                        selectionKey.interestOps(selectionKeyOps | keyOp);
02bb8761fcce Initial load
duke
parents:
diff changeset
   423
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   424
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   425
                interestOpsList.clear();
02bb8761fcce Initial load
duke
parents:
diff changeset
   426
                if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   427
                    dprint(".enableInterestOps:<-");
02bb8761fcce Initial load
duke
parents:
diff changeset
   428
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   429
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   430
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   431
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   432
02bb8761fcce Initial load
duke
parents:
diff changeset
   433
    private void createListenerThread(EventHandler eventHandler)
02bb8761fcce Initial load
duke
parents:
diff changeset
   434
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   435
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   436
            dprint(".createListenerThread: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   437
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   438
        Acceptor acceptor = eventHandler.getAcceptor();
02bb8761fcce Initial load
duke
parents:
diff changeset
   439
        ListenerThread listenerThread =
02bb8761fcce Initial load
duke
parents:
diff changeset
   440
            new ListenerThreadImpl(orb, acceptor, this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   441
        listenerThreads.put(eventHandler, listenerThread);
02bb8761fcce Initial load
duke
parents:
diff changeset
   442
        Throwable throwable = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   443
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   444
            orb.getThreadPoolManager().getThreadPool(0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   445
                .getWorkQueue(0).addWork((Work)listenerThread);
02bb8761fcce Initial load
duke
parents:
diff changeset
   446
        } catch (NoSuchThreadPoolException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   447
            throwable = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
   448
        } catch (NoSuchWorkQueueException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   449
            throwable = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
   450
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   451
        if (throwable != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   452
            RuntimeException rte = new RuntimeException(throwable.toString());
02bb8761fcce Initial load
duke
parents:
diff changeset
   453
            rte.initCause(throwable);
02bb8761fcce Initial load
duke
parents:
diff changeset
   454
            throw rte;
02bb8761fcce Initial load
duke
parents:
diff changeset
   455
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   456
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   457
02bb8761fcce Initial load
duke
parents:
diff changeset
   458
    private void destroyListenerThread(EventHandler eventHandler)
02bb8761fcce Initial load
duke
parents:
diff changeset
   459
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   460
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   461
            dprint(".destroyListenerThread: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   462
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   463
        ListenerThread listenerThread = (ListenerThread)
02bb8761fcce Initial load
duke
parents:
diff changeset
   464
            listenerThreads.get(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   465
        if (listenerThread == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   466
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   467
                dprint(".destroyListenerThread: cannot find ListenerThread - ignoring.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   468
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   469
            return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   470
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   471
        listenerThreads.remove(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   472
        listenerThread.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   473
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   474
02bb8761fcce Initial load
duke
parents:
diff changeset
   475
    private void createReaderThread(EventHandler eventHandler)
02bb8761fcce Initial load
duke
parents:
diff changeset
   476
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   477
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   478
            dprint(".createReaderThread: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   479
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   480
        Connection connection = eventHandler.getConnection();
02bb8761fcce Initial load
duke
parents:
diff changeset
   481
        ReaderThread readerThread =
02bb8761fcce Initial load
duke
parents:
diff changeset
   482
            new ReaderThreadImpl(orb, connection, this);
02bb8761fcce Initial load
duke
parents:
diff changeset
   483
        readerThreads.put(eventHandler, readerThread);
02bb8761fcce Initial load
duke
parents:
diff changeset
   484
        Throwable throwable = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   485
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   486
            orb.getThreadPoolManager().getThreadPool(0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   487
                .getWorkQueue(0).addWork((Work)readerThread);
02bb8761fcce Initial load
duke
parents:
diff changeset
   488
        } catch (NoSuchThreadPoolException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   489
            throwable = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
   490
        } catch (NoSuchWorkQueueException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   491
            throwable = e;
02bb8761fcce Initial load
duke
parents:
diff changeset
   492
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   493
        if (throwable != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   494
            RuntimeException rte = new RuntimeException(throwable.toString());
02bb8761fcce Initial load
duke
parents:
diff changeset
   495
            rte.initCause(throwable);
02bb8761fcce Initial load
duke
parents:
diff changeset
   496
            throw rte;
02bb8761fcce Initial load
duke
parents:
diff changeset
   497
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   498
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   499
02bb8761fcce Initial load
duke
parents:
diff changeset
   500
    private void destroyReaderThread(EventHandler eventHandler)
02bb8761fcce Initial load
duke
parents:
diff changeset
   501
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   502
        if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   503
            dprint(".destroyReaderThread: " + eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   504
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   505
        ReaderThread readerThread = (ReaderThread)
02bb8761fcce Initial load
duke
parents:
diff changeset
   506
            readerThreads.get(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   507
        if (readerThread == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   508
            if (orb.transportDebugFlag) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   509
                dprint(".destroyReaderThread: cannot find ReaderThread - ignoring.");
02bb8761fcce Initial load
duke
parents:
diff changeset
   510
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   511
            return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   512
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   513
        readerThreads.remove(eventHandler);
02bb8761fcce Initial load
duke
parents:
diff changeset
   514
        readerThread.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   515
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   516
02bb8761fcce Initial load
duke
parents:
diff changeset
   517
    private void dprint(String msg)
02bb8761fcce Initial load
duke
parents:
diff changeset
   518
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   519
        ORBUtility.dprint("SelectorImpl", msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
   520
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   521
02bb8761fcce Initial load
duke
parents:
diff changeset
   522
    protected void dprint(String msg, Throwable t)
02bb8761fcce Initial load
duke
parents:
diff changeset
   523
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   524
        dprint(msg);
02bb8761fcce Initial load
duke
parents:
diff changeset
   525
        t.printStackTrace(System.out);
02bb8761fcce Initial load
duke
parents:
diff changeset
   526
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   527
02bb8761fcce Initial load
duke
parents:
diff changeset
   528
    // Private class to contain a SelectionKey and a SelectionKey op.
02bb8761fcce Initial load
duke
parents:
diff changeset
   529
    // Used only by SelectorImpl to register and enable SelectionKey
02bb8761fcce Initial load
duke
parents:
diff changeset
   530
    // Op.
02bb8761fcce Initial load
duke
parents:
diff changeset
   531
    // REVISIT - Could do away with this class and use the EventHanlder
02bb8761fcce Initial load
duke
parents:
diff changeset
   532
    //           directly.
02bb8761fcce Initial load
duke
parents:
diff changeset
   533
    private class SelectionKeyAndOp
02bb8761fcce Initial load
duke
parents:
diff changeset
   534
    {
02bb8761fcce Initial load
duke
parents:
diff changeset
   535
        // A SelectionKey.[OP_READ|OP_WRITE|OP_ACCEPT|OP_CONNECT]
02bb8761fcce Initial load
duke
parents:
diff changeset
   536
        public int keyOp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   537
        public SelectionKey selectionKey;
02bb8761fcce Initial load
duke
parents:
diff changeset
   538
02bb8761fcce Initial load
duke
parents:
diff changeset
   539
        // constructor
02bb8761fcce Initial load
duke
parents:
diff changeset
   540
        public SelectionKeyAndOp(SelectionKey selectionKey, int keyOp) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   541
            this.selectionKey = selectionKey;
02bb8761fcce Initial load
duke
parents:
diff changeset
   542
            this.keyOp = keyOp;
02bb8761fcce Initial load
duke
parents:
diff changeset
   543
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   544
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   545
02bb8761fcce Initial load
duke
parents:
diff changeset
   546
// End of file.
02bb8761fcce Initial load
duke
parents:
diff changeset
   547
}