01: // Copyright (c) 2005 Brian Wellington (bwelling@xbill.org)
02:
03: package org.xbill.DNS;
04:
05: import java.io.*;
06: import java.net.*;
07: import java.nio.channels.*;
08: import org.xbill.DNS.utils.hexdump;
09:
10: class Client {
11:
12: protected long endTime;
13: protected SelectionKey key;
14:
15: protected Client(SelectableChannel channel, long endTime)
16: throws IOException {
17: boolean done = false;
18: Selector selector = null;
19: this .endTime = endTime;
20: try {
21: selector = Selector.open();
22: channel.configureBlocking(false);
23: key = channel.register(selector, 0);
24: done = true;
25: } finally {
26: if (!done && selector != null)
27: selector.close();
28: if (!done)
29: channel.close();
30: }
31: }
32:
33: static protected void blockUntil(SelectionKey key, long endTime)
34: throws IOException {
35: long timeout = endTime - System.currentTimeMillis();
36: int nkeys = 0;
37: if (timeout > 0)
38: nkeys = key.selector().select(timeout);
39: else if (timeout == 0)
40: nkeys = key.selector().selectNow();
41: if (nkeys == 0)
42: throw new SocketTimeoutException();
43: }
44:
45: static protected void verboseLog(String prefix, byte[] data) {
46: if (Options.check("verbosemsg"))
47: System.err.println(hexdump.dump(prefix, data));
48: }
49:
50: void cleanup() throws IOException {
51: key.selector().close();
52: key.channel().close();
53: }
54:
55: }
|