comparison rep/SessionManager.java @ 123:5b1a0574b406 add-simulator

*** empty log message ***
author pin
date Wed, 27 Aug 2008 17:21:25 +0900
parents 790c8dd42a7b
children 97a321d91b79
comparison
equal deleted inserted replaced
122:790c8dd42a7b 123:5b1a0574b406
14 import java.nio.charset.Charset; 14 import java.nio.charset.Charset;
15 import java.nio.charset.CharsetEncoder; 15 import java.nio.charset.CharsetEncoder;
16 import java.util.LinkedList; 16 import java.util.LinkedList;
17 import java.util.StringTokenizer; 17 import java.util.StringTokenizer;
18 18
19 import rep.channel.REPServerSocketChannel;
20 import rep.simulator.REPSelector;
19 import rep.xml.SessionXMLDecoder; 21 import rep.xml.SessionXMLDecoder;
20 import rep.xml.SessionXMLEncoder; 22 import rep.xml.SessionXMLEncoder;
21 23
22 //+-------+--------+--------+-------+--------+---------+------+ 24 //+-------+--------+--------+-------+--------+---------+------+
23 //| cmd | session| editor | seqid | lineno | textsiz | text | 25 //| cmd | session| editor | seqid | lineno | textsiz | text |
24 //| | id | id | | | | | 26 //| | id | id | | | | |
25 //+-------+--------+--------+-------+--------+---------+------+ 27 //+-------+--------+--------+-------+--------+---------+------+
26 //o-------header section (network order)-------------o 28 //o-------header section (network order)-------------o
27 /*int cmd; // command 29 /*int cmd; // command
28 int sid; // session ID : uniqu to editing file 30 int sid; // session ID : uniqu to editing file
29 int eid; // editor ID : owner editor ID = 1。Session に対してユニーク 31 int eid; // editor ID : owner editor ID = 1。Session に対して unique
30 int seqno; // Sequence number : sequence number はエディタごとに管理 32 int seqno; // Sequence number : sequence number はエディタごとに管理
31 int lineno; // line number 33 int lineno; // line number
32 int textsize; // textsize : bytesize 34 int textsize; // textsize : bytesize
33 byte[] text;*/ 35 byte[] text;*/
34 36
55 public SessionManager(int port) { 57 public SessionManager(int port) {
56 gui = new SessionManagerGUI(); 58 gui = new SessionManagerGUI();
57 } 59 }
58 60
59 public void openSelector() throws IOException{ 61 public void openSelector() throws IOException{
60 selector = Selector.open(); 62 //selector = Selector.open();
63 selector = REPSelector.open();
61 } 64 }
62 65
63 public void sessionManagerNet(int port) throws InterruptedException, IOException { 66 public void sessionManagerNet(int port) throws InterruptedException, IOException {
64 67
65 ServerSocketChannel ssc = ServerSocketChannel.open(); 68 //ServerSocketChannel ssc = ServerSocketChannel.open();
69 ServerSocketChannel ssc = REPServerSocketChannel.open();
70
66 ssc.configureBlocking(false); //reuse address 必須 71 ssc.configureBlocking(false); //reuse address 必須
67 72
68 ssc.socket().setReuseAddress(true); 73 ssc.socket().setReuseAddress(true);
69 74
70 ssc.socket().bind(new InetSocketAddress(port)); 75 ssc.socket().bind(new InetSocketAddress(port));
79 selector.select(); 84 selector.select();
80 for(SelectionKey key : selector.selectedKeys()){ 85 for(SelectionKey key : selector.selectedKeys()){
81 if(key.isAcceptable()){ 86 if(key.isAcceptable()){
82 /*** serverChannelはenableになったSelectionKeyのchannel ***/ 87 /*** serverChannelはenableになったSelectionKeyのchannel ***/
83 ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel(); 88 ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
84 /*** EditorChannel を用いない記述 ***/
85 SocketChannel channel = serverChannel.accept(); //keyからchannelを取って、accept 89 SocketChannel channel = serverChannel.accept(); //keyからchannelを取って、accept
86 registerChannel (selector, channel, SelectionKey.OP_READ); 90 registerChannel (selector, channel, SelectionKey.OP_READ);
87 channel = null; 91 channel = null;
88 92
89 /*** EditorChannel を用いた記述 ****/
90 //EditorChannel echannel = (EditorChannel) ssc.accept();
91 //echannel.setIO();
92 //registerChannel(selector, echannel, SelectionKey.OP_READ);
93 //echannel = null;
94
95 /*** SelectableEditorChannel ***/
96 //SocketChannel channel = ssc.accept();
97 //SelectableEditorChannel echannel2 = new SelectableEditorChannel(channel);
98 //registerChannel(selector, echannel2, SelectionKey.OP_READ);
99 //channel = null;
100 //echannel2 = null;
101 93
102 }else if(key.isReadable()){ 94 }else if(key.isReadable()){
103 95
104 /*** EditorChannel を用いない記述 ***/
105 SocketChannel channel = (SocketChannel)key.channel(); 96 SocketChannel channel = (SocketChannel)key.channel();
106 REPPacketReceive receive = new REPPacketReceive(channel); //getPacket(), putPacket() にする。 97 REPPacketReceive receive = new REPPacketReceive(channel);
107 receive.setkey(key); 98 receive.setkey(key);
108 REPCommand receivedCommand = receive.unpackUConv(); 99 REPCommand receivedCommand = receive.unpackUConv();
109 //REPCommand receivedCommand = receive.unpack();
110 manager(channel, receivedCommand); 100 manager(channel, receivedCommand);
111 101
112 /*** EditorChannel を用いた記述 ****/
113 //EditorChannel echannel = (EditorChannel) key.channel();
114 //REPCommand command = echannel.getPacket();
115 //manager(echannel, command);
116 102
117 }else if(key.isConnectable()){ 103 }else if(key.isConnectable()){
118 System.out.println("Connectable"); 104 System.out.println("Connectable");
119 } 105 }
120 } 106 }
123 109
124 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException { 110 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
125 if(channel == null) { 111 if(channel == null) {
126 return; 112 return;
127 } 113 }
128 //System.out.println("registerChannel()");
129 channel.configureBlocking(false); 114 channel.configureBlocking(false);
130 selector.wakeup(); 115 selector.wakeup();
131 channel.register(selector, ops); 116 channel.register(selector, ops);
132 } 117 }
133 118
397 break; 382 break;
398 } 383 }
399 } 384 }
400 385
401 private int reverseCmd(int cmd) { 386 private int reverseCmd(int cmd) {
402 // TODO Auto-generated method stub
403 int kindOfCmd = 0; 387 int kindOfCmd = 0;
404 switch(cmd){ 388 switch(cmd){
405 case REP.REPCMD_INSERT: 389 case REP.REPCMD_INSERT:
406 kindOfCmd = REP.REPCMD_DELETE; 390 kindOfCmd = REP.REPCMD_DELETE;
407 break; 391 break;