()
| 45 | } |
| 46 | |
| 47 | public void runMainLoop() throws Exception { |
| 48 | boolean keepRunning = true; |
| 49 | int port = 8988; |
| 50 | ServerSocketChannel serverChannel = ServerSocketChannel.open(); |
| 51 | try { |
| 52 | serverChannel.configureBlocking(false); |
| 53 | serverChannel.socket().bind(new InetSocketAddress("0.0.0.0", port)); |
| 54 | Selector selector = Selector.open(); |
| 55 | serverChannel.register(selector, SelectionKey.OP_ACCEPT, null); |
| 56 | while (keepRunning) { |
| 57 | System.out.println("Running main loop"); |
| 58 | selector.select(10000); |
| 59 | for (SelectionKey key : selector.selectedKeys()) { |
| 60 | if (key.isAcceptable()) { |
| 61 | System.out.println("Accepting new connection"); |
| 62 | SocketChannel c = ((ServerSocketChannel) key.channel()).accept(); |
| 63 | if (c != null) { |
| 64 | c.configureBlocking(false); |
| 65 | c.register(selector, SelectionKey.OP_READ, new Connection()); |
| 66 | } |
| 67 | } else { |
| 68 | SocketChannel c = (SocketChannel) key.channel(); |
| 69 | if (c.isOpen() && key.isReadable()) { |
| 70 | Connection connection = (Connection)key.attachment(); |
| 71 | connection.handleRead(c); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | selector.selectedKeys().clear(); |
| 76 | } |
| 77 | } finally { |
| 78 | serverChannel.close(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public static void main(String args[]) { |
| 83 | try { |
no test coverage detected