1 /*
   2  * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.corba.se.impl.transport;
  27 
  28 import java.io.IOException;
  29 
  30 import com.sun.corba.se.pept.transport.Acceptor;
  31 import com.sun.corba.se.pept.transport.ListenerThread;
  32 import com.sun.corba.se.pept.transport.Selector;
  33 
  34 import com.sun.corba.se.spi.orb.ORB;
  35 import com.sun.corba.se.spi.orbutil.threadpool.Work;
  36 
  37 import com.sun.corba.se.impl.orbutil.ORBUtility;
  38 
  39 
  40 public class ListenerThreadImpl
  41     implements
  42         ListenerThread,
  43         Work
  44 {
  45     private ORB orb;
  46     private Acceptor acceptor;
  47     private Selector selector;
  48     private boolean keepRunning;
  49     private long enqueueTime;
  50 
  51     public ListenerThreadImpl(ORB orb, Acceptor acceptor, Selector selector)
  52     {
  53         this.orb = orb;
  54         this.acceptor = acceptor;
  55         this.selector = selector;
  56         keepRunning = true;
  57     }
  58 
  59     ////////////////////////////////////////////////////
  60     //
  61     // ListenerThread methods.
  62     //
  63 
  64     public Acceptor getAcceptor()
  65     {
  66         return acceptor;
  67     }
  68 
  69     public void close()
  70     {
  71         if (orb.transportDebugFlag) {
  72             dprint(".close: " + acceptor);
  73         }
  74 
  75         keepRunning = false;
  76     }
  77 
  78     ////////////////////////////////////////////////////
  79     //
  80     // Work methods.
  81     //
  82 
  83     // REVISIT - this needs alot more from previous ListenerThread
  84 
  85     public void doWork()
  86     {
  87         try {
  88             if (orb.transportDebugFlag) {
  89                 dprint(".doWork: Start ListenerThread: " + acceptor);
  90             }
  91             while (keepRunning) {
  92                 try {
  93                     if (orb.transportDebugFlag) {
  94                         dprint(".doWork: BEFORE ACCEPT CYCLE: " + acceptor);
  95                     }
  96 
  97                     acceptor.accept();
  98 
  99                     if (orb.transportDebugFlag) {
 100                         dprint(".doWork: AFTER ACCEPT CYCLE: " + acceptor);
 101                     }
 102                 } catch (Throwable t) {
 103                     if (orb.transportDebugFlag) {
 104                         dprint(".doWork: Exception in accept: " + acceptor,t);
 105                     }
 106                     orb.getTransportManager().getSelector(0)
 107                         .unregisterForEvent(getAcceptor().getEventHandler());
 108                     getAcceptor().close();
 109                 }
 110             }
 111         } finally {
 112             if (orb.transportDebugFlag) {
 113                 dprint(".doWork: Terminated ListenerThread: " + acceptor);
 114             }
 115         }
 116     }
 117 
 118     public void setEnqueueTime(long timeInMillis)
 119     {
 120         enqueueTime = timeInMillis;
 121     }
 122 
 123     public long getEnqueueTime()
 124     {
 125         return enqueueTime;
 126     }
 127 
 128     public String getName() { return "ListenerThread"; }
 129 
 130     ////////////////////////////////////////////////////
 131     //
 132     // Implementation.
 133     //
 134 
 135     private void dprint(String msg)
 136     {
 137         ORBUtility.dprint("ListenerThreadImpl", msg);
 138     }
 139 
 140     private void dprint(String msg, Throwable t)
 141     {
 142         dprint(msg);
 143         t.printStackTrace(System.out);
 144     }
 145 }
 146 
 147 // End of file.