1 36 37 import java.io.*; 38 import java.nio.channels.*; 39 import java.util.*; 40 41 51 class Dispatcher1 implements Dispatcher { 52 53 private Selector sel; 54 55 Dispatcher1() throws IOException { 56 sel = Selector.open(); 57 } 58 59 public void run() { 61 for (;;) { 62 try { 63 dispatch(); 64 } catch (IOException x) { 65 x.printStackTrace(); 66 } 67 } 68 } 69 70 private void dispatch() throws IOException { 71 sel.select(); 72 for (Iterator i = sel.selectedKeys().iterator(); i.hasNext(); ) { 73 SelectionKey sk = (SelectionKey)i.next(); 74 i.remove(); 75 Handler h = (Handler)sk.attachment(); 76 h.handle(sk); 77 } 78 } 79 80 public void register(SelectableChannel ch, int ops, Handler h) 81 throws IOException { 82 ch.register(sel, ops, h); 83 } 84 } 85 | Popular Tags |