@Override
	public void run() {
		if (logger.isInfoEnabled()) {
			logger.info(String.format("SelectorThread for Management=%s started.", this.management.getName()));
		}
		while (this.started) {
			try {
				FastList<ChangeRequest> pendingChanges = this.management.getPendingChanges();

				// Process any pending changes
				synchronized (pendingChanges) {
					Iterator<ChangeRequest> changes = pendingChanges.iterator();
					while (changes.hasNext()) {
						ChangeRequest change = changes.next();
						switch (change.getType()) {
						case ChangeRequest.CHANGEOPS:
							pendingChanges.remove(change);
							SelectionKey key = change.getSocketChannel().keyFor(this.selector);
							key.interestOps(change.getOps());
							break;
						case ChangeRequest.REGISTER:
							pendingChanges.remove(change);
							SelectionKey key1 = change.getSocketChannel().register(this.selector, change.getOps());
							key1.attach(change.getAssociation());
							break;
						case ChangeRequest.CONNECT:
							if (!change.getAssociation().isStarted()) {
								// if Association is stopped - remove pending connection requests  
								pendingChanges.remove(change);
							} else {
								if (change.getExecutionTime() <= System.currentTimeMillis()) {
									pendingChanges.remove(change);
									change.getAssociation().initiateConnection();
								}
							}
							break;
						case ChangeRequest.CLOSE:
							pendingChanges.remove(change);
							change.getAssociation().close();
						}
					}// end of while
				}

				// Wait for an event one of the registered channels
				this.selector.select(500);

				// System.out.println("Done selecting " +
				// this.selector.selectedKeys().size());

				// Iterate over the set of keys for which events are available
				Iterator<SelectionKey> selectedKeys = this.selector.selectedKeys().iterator();

				while (selectedKeys.hasNext()) {
					SelectionKey key = selectedKeys.next();
					selectedKeys.remove();

					if (!key.isValid()) {
						continue;
					}

					// Check what event is available and deal with it
					if (key.isConnectable()) {
						this.finishConnection(key);
					} else if (key.isAcceptable()) {
						this.accept(key);
					} else if (key.isReadable()) {
						this.read(key);
					} else if (key.isWritable()) {
						this.write(key);
					}
				}

			} catch (Exception e) {
				logger.error("Error while selecting the ready keys", e);
			}
		}

		try {
			this.selector.close();
		} catch (IOException e) {
			logger.error(String.format("Error while closing Selector for SCTP Management=%s", this.management.getName()));
		}

		if (logger.isInfoEnabled()) {
			logger.info(String.format("SelectorThread for Management=%s stopped.", this.management.getName()));
		}
	}
@ Override public void run ( ) { if ( logger . isInfoEnabled ( ) ) { logger . info ( String . format ( "SelectorThread for Management=%s started." , this . management . getName ( ) ) ) ; } while ( this . started ) { try { FastList < ChangeRequest > pendingChanges = this . management . getPendingChanges ( ) ; synchronized ( pendingChanges ) { Iterator < ChangeRequest > changes = pendingChanges . iterator ( ) ; while ( changes . hasNext ( ) ) { ChangeRequest change = changes . next ( ) ; switch ( change . getType ( ) ) { case ChangeRequest . CHANGEOPS : pendingChanges . remove ( change ) ; SelectionKey key = change . getSocketChannel ( ) . keyFor ( this . selector ) ; key . interestOps ( change . getOps ( ) ) ; break ; case ChangeRequest . REGISTER : pendingChanges . remove ( change ) ; SelectionKey key1 = change . getSocketChannel ( ) . register ( this . selector , change . getOps ( ) ) ; key1 . attach ( change . getAssociation ( ) ) ; break ; case ChangeRequest . CONNECT : if ( ! change . getAssociation ( ) . isStarted ( ) ) { pendingChanges . remove ( change ) ; } else { if ( change . getExecutionTime ( ) <= System . currentTimeMillis ( ) ) { pendingChanges . remove ( change ) ; change . getAssociation ( ) . initiateConnection ( ) ; } } break ; case ChangeRequest . CLOSE : pendingChanges . remove ( change ) ; change . getAssociation ( ) . close ( ) ; } } } this . selector . select ( 500 ) ; Iterator < SelectionKey > selectedKeys = this . selector . selectedKeys ( ) . iterator ( ) ; while ( selectedKeys . hasNext ( ) ) { SelectionKey key = selectedKeys . next ( ) ; selectedKeys . remove ( ) ; if ( ! key . isValid ( ) ) { continue ; } if ( key . isConnectable ( ) ) { this . finishConnection ( key ) ; } else if ( key . isAcceptable ( ) ) { this . accept ( key ) ; } else if ( key . isReadable ( ) ) { this . read ( key ) ; } else if ( key . isWritable ( ) ) { this . write ( key ) ; } } } catch ( Exception e ) { logger . error ( "Error while selecting the ready keys" , e ) ; } } try { this . selector . close ( ) ; } catch ( IOException e ) { logger . error ( String . format ( "Error while closing Selector for SCTP Management=%s" , this . management . getName ( ) ) ) ; } if ( logger . isInfoEnabled ( ) ) { logger . info ( String . format ( "SelectorThread for Management=%s stopped." , this . management . getName ( ) ) ) ; } }
/*
(non-Javadoc)

@see java.lang.Runnable#run()