Пример #1
0
  public void registerInterestOps(EventHandler eventHandler) {
    if (orb.transportDebugFlag) {
      dprint(".registerInterestOps:-> " + eventHandler);
    }

    SelectionKey selectionKey = eventHandler.getSelectionKey();
    if (selectionKey.isValid()) {
      int ehOps = eventHandler.getInterestOps();
      SelectionKeyAndOp keyAndOp = new SelectionKeyAndOp(selectionKey, ehOps);
      synchronized (interestOpsList) {
        interestOpsList.add(keyAndOp);
      }
      // tell Selector Thread there's an update to a SelectorKey's Ops
      selector.wakeup();
    } else {
      wrapper.selectionKeyInvalid(eventHandler.toString());
      if (orb.transportDebugFlag) {
        dprint(".registerInterestOps: EventHandler SelectionKey not valid " + eventHandler);
      }
    }

    if (orb.transportDebugFlag) {
      dprint(".registerInterestOps:<- ");
    }
  }
Пример #2
0
  public void unregisterForEvent(EventHandler eventHandler) {
    if (orb.transportDebugFlag) {
      dprint(".unregisterForEvent: " + eventHandler);
    }

    if (isClosed()) {
      if (orb.transportDebugFlag) {
        dprint(".unregisterForEvent: closed: " + eventHandler);
      }
      return;
    }

    if (eventHandler.shouldUseSelectThreadToWait()) {
      SelectionKey selectionKey;
      synchronized (deferredRegistrations) {
        selectionKey = eventHandler.getSelectionKey();
      }
      if (selectionKey != null) {
        selectionKey.cancel();
      }
      selector.wakeup();
      return;
    }

    switch (eventHandler.getInterestOps()) {
      case SelectionKey.OP_ACCEPT:
        destroyListenerThread(eventHandler);
        break;
      case SelectionKey.OP_READ:
        destroyReaderThread(eventHandler);
        break;
      default:
        if (orb.transportDebugFlag) {
          dprint(".unregisterForEvent: default: " + eventHandler);
        }
        throw new RuntimeException("SelectorImpl.uregisterForEvent: unknown interest ops");
    }
  }
Пример #3
0
 private void handleDeferredRegistrations() {
   synchronized (deferredRegistrations) {
     int deferredListSize = deferredRegistrations.size();
     for (int i = 0; i < deferredListSize; i++) {
       EventHandler eventHandler = (EventHandler) deferredRegistrations.get(i);
       if (orb.transportDebugFlag) {
         dprint(".handleDeferredRegistrations: " + eventHandler);
       }
       SelectableChannel channel = eventHandler.getChannel();
       SelectionKey selectionKey = null;
       try {
         selectionKey =
             channel.register(selector, eventHandler.getInterestOps(), (Object) eventHandler);
       } catch (ClosedChannelException e) {
         if (orb.transportDebugFlag) {
           dprint(".handleDeferredRegistrations: " + e);
         }
       }
       eventHandler.setSelectionKey(selectionKey);
     }
     deferredRegistrations.clear();
   }
 }
Пример #4
0
  public void registerForEvent(EventHandler eventHandler) {
    if (orb.transportDebugFlag) {
      dprint(".registerForEvent: " + eventHandler);
    }

    if (isClosed()) {
      if (orb.transportDebugFlag) {
        dprint(".registerForEvent: closed: " + eventHandler);
      }
      return;
    }

    if (eventHandler.shouldUseSelectThreadToWait()) {
      synchronized (deferredRegistrations) {
        deferredRegistrations.add(eventHandler);
      }
      if (!selectorStarted) {
        startSelector();
      }
      selector.wakeup();
      return;
    }

    switch (eventHandler.getInterestOps()) {
      case SelectionKey.OP_ACCEPT:
        createListenerThread(eventHandler);
        break;
      case SelectionKey.OP_READ:
        createReaderThread(eventHandler);
        break;
      default:
        if (orb.transportDebugFlag) {
          dprint(".registerForEvent: default: " + eventHandler);
        }
        throw new RuntimeException("SelectorImpl.registerForEvent: unknown interest ops");
    }
  }
Пример #5
0
 private void createReaderThread(EventHandler eventHandler) {
   if (orb.transportDebugFlag) {
     dprint(".createReaderThread: " + eventHandler);
   }
   Connection connection = eventHandler.getConnection();
   ReaderThread readerThread = new ReaderThreadImpl(orb, connection, this);
   readerThreads.put(eventHandler, readerThread);
   Throwable throwable = null;
   try {
     orb.getThreadPoolManager().getThreadPool(0).getWorkQueue(0).addWork((Work) readerThread);
   } catch (NoSuchThreadPoolException e) {
     throwable = e;
   } catch (NoSuchWorkQueueException e) {
     throwable = e;
   }
   if (throwable != null) {
     RuntimeException rte = new RuntimeException(throwable.toString());
     rte.initCause(throwable);
     throw rte;
   }
 }
Пример #6
0
 private void createListenerThread(EventHandler eventHandler) {
   if (orb.transportDebugFlag) {
     dprint(".createListenerThread: " + eventHandler);
   }
   Acceptor acceptor = eventHandler.getAcceptor();
   ListenerThread listenerThread = new ListenerThreadImpl(orb, acceptor, this);
   listenerThreads.put(eventHandler, listenerThread);
   Throwable throwable = null;
   try {
     orb.getThreadPoolManager().getThreadPool(0).getWorkQueue(0).addWork((Work) listenerThread);
   } catch (NoSuchThreadPoolException e) {
     throwable = e;
   } catch (NoSuchWorkQueueException e) {
     throwable = e;
   }
   if (throwable != null) {
     RuntimeException rte = new RuntimeException(throwable.toString());
     rte.initCause(throwable);
     throw rte;
   }
 }
Пример #7
0
 public void run() {
   setName("SelectorThread");
   while (!closed) {
     try {
       int n = 0;
       if (timeout == 0 && orb.transportDebugFlag) {
         dprint(".run: Beginning of selection cycle");
       }
       handleDeferredRegistrations();
       enableInterestOps();
       try {
         n = selector.select(timeout);
       } catch (IOException e) {
         if (orb.transportDebugFlag) {
           dprint(".run: selector.select: " + e);
         }
       }
       if (closed) {
         selector.close();
         if (orb.transportDebugFlag) {
           dprint(".run: closed - .run return");
         }
         return;
       }
       /*
         if (timeout == 0 && orb.transportDebugFlag) {
         dprint(".run: selector.select() returned: " + n);
         }
         if (n == 0) {
         continue;
         }
       */
       Iterator iterator = selector.selectedKeys().iterator();
       if (orb.transportDebugFlag) {
         if (iterator.hasNext()) {
           dprint(".run: n = " + n);
         }
       }
       while (iterator.hasNext()) {
         SelectionKey selectionKey = (SelectionKey) iterator.next();
         iterator.remove();
         EventHandler eventHandler = (EventHandler) selectionKey.attachment();
         try {
           eventHandler.handleEvent();
         } catch (Throwable t) {
           if (orb.transportDebugFlag) {
             dprint(".run: eventHandler.handleEvent", t);
           }
         }
       }
       if (timeout == 0 && orb.transportDebugFlag) {
         dprint(".run: End of selection cycle");
       }
     } catch (Throwable t) {
       // IMPORTANT: ignore all errors so the select thread keeps running.
       // Otherwise a guaranteed hang.
       if (orb.transportDebugFlag) {
         dprint(".run: ignoring", t);
       }
     }
   }
 }