comparison rep/SessionManager.java @ 231:b837feb00132

*** empty log message ***
author pin
date Sun, 31 Aug 2008 18:50:08 +0900
parents f816e0cbe6fd
children dae90ded1bcd
comparison
equal deleted inserted replaced
230:43445986113b 231:b837feb00132
5 import java.nio.channels.SelectableChannel; 5 import java.nio.channels.SelectableChannel;
6 import java.nio.channels.SelectionKey; 6 import java.nio.channels.SelectionKey;
7 import java.util.Iterator; 7 import java.util.Iterator;
8 import java.util.LinkedList; 8 import java.util.LinkedList;
9 import java.util.List; 9 import java.util.List;
10 import java.util.Set;
10 import java.util.concurrent.BlockingQueue; 11 import java.util.concurrent.BlockingQueue;
11 import java.util.concurrent.LinkedBlockingQueue; 12 import java.util.concurrent.LinkedBlockingQueue;
12 13
13 import javax.swing.SwingUtilities; 14 import javax.swing.SwingUtilities;
14 15
56 public SessionManager(int port) { 57 public SessionManager(int port) {
57 58
58 } 59 }
59 60
60 public void openSelector() throws IOException{ 61 public void openSelector() throws IOException{
61 selector = REPSelector.create(); 62 selector = REPSelector.<REPCommand>create();
62 } 63 }
63 64
64 public void init(int port) throws InterruptedException, IOException { 65 public void init(int port) throws InterruptedException, IOException {
65 66
66 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker()); 67 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker());
81 Session defaultSession = new Session(sessionList.size(), "DefaultSession.txt", new Editor(0,null)); 82 Session defaultSession = new Session(sessionList.size(), "DefaultSession.txt", new Editor(0,null));
82 sessionList.add(defaultSession); 83 sessionList.add(defaultSession);
83 84
84 } 85 }
85 86
86 private void mainLoop() throws IOException { 87 public void mainLoop() throws IOException {
87 while(true){ 88 while(true){
88 if(checkSend()){ 89 if(checkSend()){
89 if(selector.selectNow() > 0){ 90 if(selector.selectNow() > 0){
90 select(); 91 select();
91 } 92 }
92 continue; 93 continue;
93 } 94 }
94 int i = selector.select(); 95 int i = selector.select();
95 System.out.println("SessionManager.mainLoop():select:"+i);
96 select(); 96 select();
97 } 97 }
98 } 98 }
99 99
100 private boolean checkSend() { 100 private boolean checkSend() {
114 private void select() throws IOException { 114 private void select() throws IOException {
115 SessionManagerEvent e; 115 SessionManagerEvent e;
116 while((e = waitingQueue.poll())!=null){ 116 while((e = waitingQueue.poll())!=null){
117 e.exec(); 117 e.exec();
118 } 118 }
119 for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()){ 119
120 Set<REPSelectionKey<REPCommand>> keys = selector.selectedKeys1();
121 for(REPSelectionKey<REPCommand> key : keys){
120 if(key.isAcceptable()){ 122 if(key.isAcceptable()){
121 /*** serverChannelはenableになったSelectionKeyのchannel ***/
122 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker()); 123 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
124 System.out.println("SessionManager.select() : channel = " + channel);
123 registerChannel (channel, SelectionKey.OP_READ); 125 registerChannel (channel, SelectionKey.OP_READ);
124 channel = null; 126 channel = null;
125 127
126 }else if(key.isReadable()){ 128 }else if(key.isReadable()){
127 REPHandler handler = (REPHandler)(key.attachment()); 129 REPHandler handler = (REPHandler)(key.attachment());
128
129 handler.handle(key); 130 handler.handle(key);
130 131
131 } 132 }
132 } 133 }
133 } 134 }
386 387
387 } 388 }
388 } 389 }
389 390
390 private void guiUpdate() { 391 private void guiUpdate() {
392 if(gui == null){
393 //System.out.println("SessionManager.guiUpdate() : gui = " + gui);
394 return;
395 }
391 //リストのコピーをGUIに渡す 396 //リストのコピーをGUIに渡す
392 LinkedList<Session> sList = new LinkedList<Session>(sessionList); 397 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
393 LinkedList<Editor> eList = new LinkedList<Editor>(editorList); 398 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
394 //GUIに反映 399 //GUIに反映
395 Runnable doRun = new DoGUIUpdate(sList, eList, gui); 400 Runnable doRun = new DoGUIUpdate(sList, eList, gui);