FileDocCategorySizeDatePackage
TimeServiceSelect.javaAPI DocExample1387Fri Apr 26 14:39:58 BST 2002com.ronsoft.books.nio.channels

TimeServiceSelect

public class TimeServiceSelect extends TimeService

Fields Summary
private Selector
selector
private long
giveup
Constructors Summary
public TimeServiceSelect(int port, long timeout)

		super (port);

		this.giveup = System.currentTimeMillis() + timeout;
		this.selector = selector.open();

		this.channel.configureBlocking (false);
		this.channel.register (this.selector, SelectionKey.OP_READ);
	
Methods Summary
public static TimeServicenewInstance(int listenPort)

System.out.println ("in newInstance");
		return (new TimeServiceSelect (listenPort, 5));
	
protected java.net.InetSocketAddressreceivePacket(java.nio.channels.DatagramChannel channel, java.nio.ByteBuffer buffer)

System.out.println ("In TimerServiceSelect receivePacket");
		buffer.clear();

		while (true) {
			InetSocketAddress sa;

			sa = (InetSocketAddress) channel.receive (buffer);

			if (sa != null) {
				return (sa);
			}

			long sleepTime = giveup - System.currentTimeMillis();

			if (sleepTime < 1) {
				return (null);	// time's up
			}

System.out.println ("Selecting for " + (sleepTime / 1000) + " seconds");
			selector.select (sleepTime);
		}