001: package winstone.nio;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.io.OutputStream;
006: import java.net.InetAddress;
007: import java.net.InetSocketAddress;
008: import java.net.ServerSocket;
009: import java.net.Socket;
010: import java.net.SocketTimeoutException;
011: import java.nio.ByteBuffer;
012: import java.nio.channels.SelectionKey;
013: import java.nio.channels.Selector;
014: import java.nio.channels.ServerSocketChannel;
015: import java.nio.channels.SocketChannel;
016: import java.util.Iterator;
017: import java.util.Set;
018:
019: /**
020: * @author <a href="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
021: * @version $Id: NioSocketServer.java,v 1.1 2006/08/27 14:22:32 rickknowles Exp $
022: */
023: public class NioSocketServer implements Runnable {
024:
025: private final static int LISTEN_PORT = 6475;
026:
027: private Thread thread;
028: private Selector selector;
029:
030: private ServerSocket serverSocket;
031:
032: public NioSocketServer(boolean useNIO) throws IOException {
033: if (useNIO) {
034: ServerSocketChannel ssc = ServerSocketChannel.open();
035: ssc.configureBlocking(false);
036: ServerSocket ss = ssc.socket();
037: ss.bind(new InetSocketAddress(LISTEN_PORT));
038:
039: this .selector = Selector.open();
040: ssc.register(this .selector, SelectionKey.OP_ACCEPT);
041: } else {
042: this .serverSocket = new ServerSocket(LISTEN_PORT);
043: this .serverSocket.setSoTimeout(500);
044: }
045:
046: this .thread = new Thread(this );
047: this .thread.setDaemon(true);
048: this .thread.start();
049: }
050:
051: public void run() {
052: boolean interrupted = false;
053: while (!interrupted) {
054: try {
055: if (this .selector != null) {
056: nioLoop();
057: } else {
058: jioLoop();
059: }
060: interrupted = Thread.interrupted();
061: } catch (IOException err) {
062: err.printStackTrace();
063: interrupted = true;
064: }
065: }
066: this .thread = null;
067: }
068:
069: private void nioLoop() throws IOException {
070: this .selector.select(500);
071: Set selectedKeys = this .selector.selectedKeys();
072: Iterator i = selectedKeys.iterator();
073: while (i.hasNext()) {
074: SelectionKey key = (SelectionKey) i.next();
075: if (key.isAcceptable()) {
076: ServerSocketChannel ssc = (ServerSocketChannel) key
077: .channel();
078: SocketChannel sc = ssc.accept();
079: sc.configureBlocking(false);
080: sc.register(this .selector, SelectionKey.OP_READ);
081: } else if (key.isReadable()) {
082: SocketChannel sc = (SocketChannel) key.channel();
083: ByteBuffer buffer = ByteBuffer.allocate(10);
084: buffer.clear();
085: sc.read(buffer);
086: buffer.flip();
087: sc.write(buffer);
088: sc.close();
089: }
090: i.remove();
091: }
092: }
093:
094: private void jioLoop() throws IOException {
095: Socket socket = null;
096: try {
097: socket = this .serverSocket.accept();
098: } catch (SocketTimeoutException err) {
099: }
100: if (socket != null) {
101: InputStream in = socket.getInputStream();
102: int pos = 0;
103: int read = 0;
104: byte buffer[] = new byte[10];
105: while ((pos < buffer.length)
106: && ((read = in.read(buffer, pos, buffer.length
107: - pos)) != -1)) {
108: pos += read;
109: }
110: OutputStream out = socket.getOutputStream();
111: out.write(buffer, 0, pos);
112: in.close();
113: out.close();
114: socket.close();
115: }
116: }
117:
118: public void stop() {
119: this .thread.interrupt();
120: }
121:
122: public static void main(String argv[]) throws Exception {
123:
124: String iterArg = argv.length > 1 ? argv[1] : "1000";
125: int ITERATION_COUNT = Integer.parseInt(iterArg);
126: boolean useNIO = argv.length > 0 && argv[0].equals("nio");
127:
128: InetAddress LOCATION = InetAddress.getLocalHost();
129: System.out.println("Address: " + LOCATION);
130:
131: NioSocketServer server = new NioSocketServer(useNIO);
132: Thread.sleep(1000);
133:
134: long startTime = System.currentTimeMillis();
135:
136: byte TEST_ARRAY[] = "1234567890".getBytes();
137: for (int n = 0; n < ITERATION_COUNT; n++) {
138: byte buffer[] = new byte[TEST_ARRAY.length];
139: Socket socket = new Socket(LOCATION, LISTEN_PORT);
140: socket.setSoTimeout(50);
141: OutputStream out = socket.getOutputStream();
142: out.write(TEST_ARRAY);
143:
144: InputStream in = socket.getInputStream();
145: int read = 0;
146: int pos = 0;
147: while ((pos < buffer.length)
148: && ((read = in.read(buffer, pos, buffer.length
149: - pos)) != -1)) {
150: pos += read;
151: }
152: in.close();
153: out.close();
154: socket.close();
155: // if (!Arrays.equals(TEST_ARRAY, buffer)) {
156: // throw new RuntimeException("in and out arrays are not equal");
157: // }
158: if (n % 500 == 0) {
159: System.out.println("Completed " + n + " iterations in "
160: + (System.currentTimeMillis() - startTime)
161: + "ms");
162: }
163: }
164: System.out.println("Completed " + ITERATION_COUNT
165: + " iterations in "
166: + (System.currentTimeMillis() - startTime) + "ms");
167: server.stop();
168: }
169: }
|