Examples of SelectionKey


Examples of java.nio.channels.SelectionKey

        try {
          waitPending();     // If a channel is being registered, wait.
          writeSelector.select(maxCallStartAge);
          Iterator<SelectionKey> iter = writeSelector.selectedKeys().iterator();
          while (iter.hasNext()) {
            SelectionKey key = iter.next();
            iter.remove();
            try {
              if (key.isValid() && key.isWritable()) {
                  doAsyncWrite(key);
              }
            } catch (IOException e) {
              LOG.info(getName() + ": doAsyncWrite threw exception " + e);
            }
          }
          long now = System.currentTimeMillis();
          if (now < lastPurgeTime + maxCallStartAge) {
            continue;
          }
          lastPurgeTime = now;
          //
          // If there were some calls that have not been sent out for a
          // long time, discard them.
          //
          LOG.debug("Checking for old call responses.");
          synchronized (writeSelector.keys()) {
            iter = writeSelector.keys().iterator();
            while (iter.hasNext()) {
              SelectionKey key = iter.next();
              try {
                doPurge(key, now);
              } catch (IOException e) {
                LOG.warn("Error in purging old calls " + e);
              }
View Full Code Here

Examples of java.nio.channels.SelectionKey

                    socket.getIOChannel().register(socket.getPoller().getSelector(), SelectionKey.OP_READ, key);
                } catch (Exception x) {
                    log.error("", x);
                }
            } else {
                final SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
                try {
                    boolean cancel = false;
                    if (key != null) {
                        final KeyAttachment att = (KeyAttachment) key.attachment();
                        if ( att!=null ) {
                            //handle callback flag
                            if ((interestOps & OP_CALLBACK) == OP_CALLBACK ) {
                                att.setCometNotify(true);
                            } else {
                                att.setCometNotify(false);
                            }
                            interestOps = (interestOps & (~OP_CALLBACK));//remove the callback flag
                            att.access();//to prevent timeout
                            //we are registering the key to start with, reset the fairness counter.
                            int ops = key.interestOps() | interestOps;
                            att.interestOps(ops);
                            if (att.getCometNotify()) key.interestOps(0);
                            else key.interestOps(ops);
                        } else {
                            cancel = true;
                        }
                    } else {
                        cancel = true;
View Full Code Here

Examples of java.nio.channels.SelectionKey

                    Iterator<SelectionKey> iterator =
                        keyCount > 0 ? selector.selectedKeys().iterator() : null;
                    // Walk through the collection of ready keys and dispatch
                    // any active event.
                    while (iterator != null && iterator.hasNext()) {
                        SelectionKey sk = iterator.next();
                        KeyAttachment attachment = (KeyAttachment)sk.attachment();
                        // Attachment may be null if another thread has called
                        // cancelledKey()
                        if (attachment == null) {
                            iterator.remove();
                        } else {
View Full Code Here

Examples of java.nio.channels.SelectionKey

            }
            //timeout
            Set<SelectionKey> keys = selector.keys();
            int keycount = 0;
            for (Iterator<SelectionKey> iter = keys.iterator(); iter.hasNext();) {
                SelectionKey key = iter.next();
                keycount++;
                try {
                    KeyAttachment ka = (KeyAttachment) key.attachment();
                    if ( ka == null ) {
                        cancelledKey(key, SocketStatus.ERROR); //we don't support any keys without attachments
                    } else if ( ka.getError() ) {
                        cancelledKey(key, SocketStatus.ERROR);//TODO this is not yet being used
                    } else if (ka.getCometNotify() ) {
                        ka.setCometNotify(false);
                        int ops = ka.interestOps() & ~OP_CALLBACK;
                        reg(key,ka,0);//avoid multiple calls, this gets re-registered after invocation
                        ka.interestOps(ops);
                        if (!processSocket(ka.getChannel(), SocketStatus.OPEN_READ, true)) processSocket(ka.getChannel(), SocketStatus.DISCONNECT, true);
                    } else if ((ka.interestOps()&SelectionKey.OP_READ) == SelectionKey.OP_READ ||
                              (ka.interestOps()&SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE) {
                        //only timeout sockets that we are waiting for a read from
                        long delta = now - ka.getLastAccess();
                        long timeout = ka.getTimeout();
                        boolean isTimedout = timeout > 0 && delta > timeout;
                        if ( close ) {
                            key.interestOps(0);
                            ka.interestOps(0); //avoid duplicate stop calls
                            processKey(key,ka);
                        } else if (isTimedout) {
                            key.interestOps(0);
                            ka.interestOps(0); //avoid duplicate timeout calls
                            cancelledKey(key, SocketStatus.TIMEOUT);
                        }
                    } else if (ka.isAsync() || ka.isComet()) {
                        if (close) {
                            key.interestOps(0);
                            ka.interestOps(0); //avoid duplicate stop calls
                            processKey(key,ka);
                        } else if (!ka.isAsync() || ka.getTimeout() > 0) {
                            // Async requests with a timeout of 0 or less never timeout
                            long delta = now - ka.getLastAccess();
View Full Code Here

Examples of java.nio.channels.SelectionKey

            this.status = status;
        }

        @Override
        public void run() {
            SelectionKey key = socket.getIOChannel().keyFor(
                    socket.getPoller().getSelector());
            KeyAttachment ka = null;

            if (key != null) {
                ka = (KeyAttachment)key.attachment();
            }

            // Upgraded connections need to allow multiple threads to access the
            // connection at the same time to enable blocking IO to be used when
            // NIO has been configured
View Full Code Here

Examples of java.nio.channels.SelectionKey

                }
                // get an iterator over the set of selected keys
                Iterator it = selector.selectedKeys().iterator();
                // look at each key in the selected set
                while (it.hasNext()) {
                    SelectionKey key = (SelectionKey) it.next();
                    // Is a new connection coming in?
                    if (key.isAcceptable()) {
                        ServerSocketChannel server =
                            (ServerSocketChannel) key.channel();
                        SocketChannel channel = server.accept();
                        registerChannel(selector,
                                        channel,
                                        SelectionKey.OP_READ,
                                        new ObjectReader(channel, selector,
                            callback));
                    }
                    // is there data to read on this channel?
                    if (key.isReadable()) {
                        readDataFromSocket(key);
                    } else {
                        key.interestOps(key.interestOps() & (~SelectionKey.OP_WRITE));
                    }

                    // remove key from selected set, it's been handled
                    it.remove();
                }
View Full Code Here

Examples of java.nio.channels.SelectionKey

   * @param cancellations
   */
  private void process_clear_interest_request(Interest interest) {

    SelectableChannel ch = interest.ch;
    SelectionKey key = ch.keyFor(selector);
    NIOHandler handler = interest.handler;

    if (key == null || !key.isValid()) {
     
      // TODO: maybe this should be considered an error?
      if (handler != null && interest.releaseNotify) {
        handler.released(ch);
      }
     
    } else {
     
      // get channel info
      NIOChannelInfo info = (NIOChannelInfo) key.attachment();
     
      if (info == null) {
        // error!
        throw new InternalError();
      }
     
      // tell info that we're note interested in this
      info.clear_interest(interest);
     
      // does that add up to, that we are not interested at all?
      if (info.updateInterestOpsFor(key) == 0) {
       
        // cancel the key
        key.cancel();
       
      }
    }
   
  }
View Full Code Here

Examples of java.nio.channels.SelectionKey

   * @param remove
   * @param active2
   */
  private void process_add_interest_request(Interest interest) {
    SelectableChannel ch = interest.ch;
    SelectionKey key = ch.keyFor(selector);
    NIOChannelInfo info = null;
   
    if (key != null && !key.isValid()) {
      try {
        selector.selectNow();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      key = null;
    }
    if (key == null || !key.isValid()) {
      try {
        key = ch.register(selector, interest.ops,
            info = new NIOChannelInfo(interest));
      } catch (ClosedChannelException e) {
        interest.handler.exception(ch, e);
      }
    } else {
      info = (NIOChannelInfo) key.attachment();
      info.add(interest);
      info.updateInterestOpsFor(key);
    }
   
  }
View Full Code Here

Examples of java.nio.channels.SelectionKey

    sb.append("; deliver=").append(deliver);

    sb.append("; select=").append(Integer.toBinaryString(event_mask));

    if (fd != null && fd.channel() != null) {
      SelectionKey sk = NIOSelector.interest(fd.channel());
      if (sk == null) {
        sb.append("; nointrest");
      } else if (!sk.isValid()) {

        sb.append("; cancelled");
      } else {

        try {
          sb.append("; ready="
              + Integer.toBinaryString(sk.readyOps()));
          sb.append("; interest="
              + Integer.toBinaryString(sk.interestOps()));
        } catch (CancelledKeyException e) {
          sb.append("; cancelled");
        }
      }
    }
View Full Code Here

Examples of java.nio.channels.SelectionKey

    @Override
    public void run() {
      LOG.info(getName() + ": starting");
      SERVER.set(Server.this);
      while (running) {
        SelectionKey key = null;
        try {
          selector.select();
          Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
          while (iter.hasNext()) {
            key = iter.next();
            iter.remove();
            try {
              if (key.isValid()) {
                if (key.isAcceptable())
                  doAccept(key);
                else if (key.isReadable())
                  doRead(key);
              }
            } catch (IOException e) {
            }
            key = null;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.