annotate rep/SessionManager.java @ 224:6b0dd92b8e45

add optimizer to Editor
author kent
date Sun, 31 Aug 2008 13:28:34 +0900
parents 18d6a7140fa3
children cbd67817e9cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
1 package rep;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
2
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
3 import java.io.IOException;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
4 import java.net.InetSocketAddress;
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
5 import java.nio.channels.SelectableChannel;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
6 import java.nio.channels.SelectionKey;
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
7 import java.util.Iterator;
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
8 import java.util.LinkedList;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
9 import java.util.List;
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
10 import java.util.concurrent.BlockingQueue;
192
6fb97c7a0b42 *** empty log message ***
pin
parents: 191
diff changeset
11 import java.util.concurrent.LinkedBlockingQueue;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
12
198
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
13 import javax.swing.SwingUtilities;
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
14
123
5b1a0574b406 *** empty log message ***
pin
parents: 122
diff changeset
15 import rep.channel.REPServerSocketChannel;
133
8dce4348966c *** empty log message ***
kono
parents: 124
diff changeset
16 import rep.channel.REPSocketChannel;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
17 import rep.handler.PacketSet;
146
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
18 import rep.handler.REPHandler;
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
19 import rep.handler.REPHandlerImpl;
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
20 import rep.handler.REPHandlerInMerge;
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
21 import rep.channel.REPSelector;
56
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
22 import rep.xml.SessionXMLDecoder;
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
23 import rep.xml.SessionXMLEncoder;
198
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
24 import rep.channel.REPSelectionKey;
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
25 /*
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
26 //+-------+--------+--------+-------+--------+---------+------+
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
27 //| cmd | session| editor | seqid | lineno | textsiz | text |
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
28 //| | id | id | | | | |
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
29 //+-------+--------+--------+-------+--------+---------+------+
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
30 //o-------header section (network order)-------------o
198
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
31 int cmd; // command
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
32 int sid; // session ID : uniqu to editing file
123
5b1a0574b406 *** empty log message ***
pin
parents: 122
diff changeset
33 int eid; // editor ID : owner editor ID = 1。Session に対して unique
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
34 int seqno; // Sequence number : sequence number はエディタごとに管理
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
35 int lineno; // line number
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
36 int textsize; // textsize : bytesize
198
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
37 byte[] text;
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
38 */
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
39
222
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
40 public class SessionManager implements SessionManagerEventListener{
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
41
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
42 private LinkedList<Session> sessionList;
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
43 private SessionManagerGUI gui;
198
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
44 private REPSelector<REPCommand> selector;
7
5067a6a93d01 *** empty log message ***
pin
parents: 6
diff changeset
45 private SessionManagerList smList;
17
806e34a6fecd *** empty log message ***
pin
parents: 16
diff changeset
46 private String myHost;
21
2d4bab638a71 *** empty log message ***
pin
parents: 19
diff changeset
47 private boolean isMaster = true;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
48 private List<Editor> editorList;
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
49 private String maxHost;
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
50 private List<PacketSet> waitingCommandInMerge;
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
51 private BlockingQueue<SessionManagerEvent> waitingQueue;
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
52 private static int temp_port;
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
53 private static int send_port;
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
54 static final int DEFAULT_PORT = 8766;
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
55
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
56 public SessionManager(int port) {
191
dbf59f82273e *** empty log message ***
pin
parents: 186
diff changeset
57
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
58 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
59
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
60 public void openSelector() throws IOException{
191
dbf59f82273e *** empty log message ***
pin
parents: 186
diff changeset
61 selector = REPSelector.create();
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
62 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
63
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
64 public void init(int port) throws InterruptedException, IOException {
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
65
186
d22384c0026c *** empty log message ***
kent
parents: 185
diff changeset
66 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open(new REPCommandPacker());
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
67 ssc.configureBlocking(false); //reuse address 必須
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
68 ssc.socket().setReuseAddress(true);
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
69 //getAllByNameで取れた全てのアドレスに対してbindする
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
70 ssc.socket().bind(new InetSocketAddress(port));
192
6fb97c7a0b42 *** empty log message ***
pin
parents: 191
diff changeset
71 ssc.register(selector, SelectionKey.OP_ACCEPT, new REPHandlerImpl(-1, this));
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
72
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
73 sessionList = new LinkedList<Session>();
7
5067a6a93d01 *** empty log message ***
pin
parents: 6
diff changeset
74 smList = new SessionManagerList();
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
75 editorList = new LinkedList<Editor>();
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
76 waitingCommandInMerge = new LinkedList<PacketSet>();
192
6fb97c7a0b42 *** empty log message ***
pin
parents: 191
diff changeset
77 waitingQueue = new LinkedBlockingQueue<SessionManagerEvent>();
215
7649238aaf66 *** empty log message ***
pin
parents: 213
diff changeset
78
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
79 }
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
80
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
81 private void mainLoop() throws IOException {
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
82 while(true){
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
83 if(checkSend()){
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
84 if(selector.selectNow() > 0){
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
85 select();
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
86 }
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
87 continue;
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
88 }
222
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
89 int i = selector.select();
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
90 System.out.println(i);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
91 select();
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
92 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
93 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
94
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
95 private boolean checkSend() {
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
96 for(Iterator<PacketSet> it = waitingCommandInMerge.iterator(); it.hasNext();){
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
97 PacketSet p = it.next();
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
98 if(p.getEditor().isMerging()) {
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
99 continue;
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
100 }else{
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
101 manage(p.channel, p.command);
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
102 it.remove();
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
103 return true;
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
104 }
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
105 }
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
106 return false;
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
107 }
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
108
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
109 private void select() throws IOException {
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
110 SessionManagerEvent e;
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
111 while((e = waitingQueue.poll())!=null){
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
112 e.exec();
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
113 }
198
ff3fcdcccc85 *** empty log message ***
pin
parents: 192
diff changeset
114 for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()){
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
115 if(key.isAcceptable()){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
116 /*** serverChannelはenableになったSelectionKeyのchannel ***/
199
456ba58cd042 *** empty log message ***
pin
parents: 198
diff changeset
117 REPSocketChannel<REPCommand> channel = key.accept(new REPCommandPacker());
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
118 registerChannel (selector, channel, SelectionKey.OP_READ);
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
119 channel = null;
123
5b1a0574b406 *** empty log message ***
pin
parents: 122
diff changeset
120
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
121 }else if(key.isReadable()){
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
122 REPHandler handler = (REPHandler)(key.attachment());
146
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
123 handler.handle(key);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
124
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
125 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
126 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
127 }
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
128
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
129 private void registerChannel(REPSelector selector, SelectableChannel channel, int ops) throws IOException {
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
130 if(channel == null) {
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
131 return;
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
132 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
133 channel.configureBlocking(false);
170
30cf7747d134 *** empty log message ***
pin
parents: 169
diff changeset
134 REPHandler handler = new REPHandlerImpl(-1, this);
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
135 channel.register(selector, ops, handler);
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
136 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
137
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
138 public void manage(REPSocketChannel<REPCommand> channel, REPCommand receivedCommand) {
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
139 if(receivedCommand == null) return;
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
140 //Session session;
141
6f15a8880ed8 *** empty log message ***
pin
parents: 140
diff changeset
141 REPCommand sendCommand = new REPCommand(receivedCommand);
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
142 REPSocketChannel<REPCommand> send = channel;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
143
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
144 switch(receivedCommand.cmd){
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
145
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
146 case REP.SMCMD_JOIN:
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
147 {
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
148 //どのSessionにも属さないエディタをリストに追加
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
149 //エディタとchannelは1対1
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
150 //エディタが新しくputする場合は新しくソケットを作る
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
151 Editor editor = new Editor(editorList.size(), channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
152 editor.setHost(myHost);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
153 editorList.add(editor);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
154
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
155 guiUpdate();
199
456ba58cd042 *** empty log message ***
pin
parents: 198
diff changeset
156
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
157 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
158
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
159 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
160
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
161 case REP.SMCMD_JOIN_ACK:
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
162 assert (false);
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
163 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
164
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
165 case REP.SMCMD_PUT:
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
166 {
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
167 //エディタのリストに追加
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
168 Editor editor = new Editor(editorList.size(), channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
169 editorList.add(editor);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
170
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
171 //Sessionを生成
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
172 int sid = sessionList.size();
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
173 editor = new Editor(0, channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
174 editor.setHost(myHost);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
175 Session session = new Session(sid, editor);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
176 session.hasOwner(true);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
177 sessionList.add(new Session(sid, editor));
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
178
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
179 guiUpdate();
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
180
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
181 //エディタにAckを送信
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
182 sendCommand.setCMD(REP.SMCMD_PUT_ACK);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
183 sendCommand.setEID(editor.getEID());
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
184 sendCommand.setSID(session.getSID());
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
185 editor.send(sendCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
186
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
187 //他のSessionManagerへSessionの追加を報告
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
188 //親に送って、親から子へ
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
189 SessionXMLEncoder sessionEncoder = new SessionXMLEncoder(session);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
190 REPCommand command = new REPCommand();
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
191 command.setSID(session.getSID());
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
192 command.setString(sessionEncoder.sessionListToXML());
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
193 command.setCMD(REP.SMCMD_UPDATE);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
194 smList.sendExcept(channel, command);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
195
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
196 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
197
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
198 break;
133
8dce4348966c *** empty log message ***
kono
parents: 124
diff changeset
199
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
200 case REP.SMCMD_SELECT:
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
201 {
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
202 //他のSessionManagerをエディタとしてSessionに追加
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
203 Editor editor = new Editor(channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
204 Session session = getSession(receivedCommand.sid);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
205 session.addEditor(editor);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
206
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
207 if(session.hasOwner()){
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
208 //このSessionManagerがオーナーを持っている場合、Sessionにエディタを追加し、エディタへAckを返す
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
209 sendCommand.setCMD(REP.SMCMD_SELECT_ACK);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
210 sendCommand.setEID(editor.getEID());
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
211 editor.send(sendCommand);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
212 }else{
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
213 //オーナーを持ってない場合は、オーナーを持っているSessionManagerへSELECTコマンドを中継する
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
214 Editor owner = session.getOwner();
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
215 owner.send(receivedCommand);
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
216 }
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
217 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
218
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
219 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
220
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
221 case REP.SMCMD_SELECT_ACK:
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
222 {
85
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
223 String hostport = receivedCommand.string;
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
224 Editor editor = getEditor(hostport);
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
225
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
226 if(editor != null) {
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
227 //host, port を見て、このコマンドが自分が送信したSelectコマンドのAckかどうかを判断する
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
228 REPCommand command = new REPCommand();
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
229 command.setCMD(REP.SMCMD_JOIN_ACK);
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
230 command.setSID(receivedCommand.sid);
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
231 command.setEID(receivedCommand.eid);
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
232 editor.send(command);
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
233
85
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
234 }else{
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
235 //自分が送信したコマンドでなければ、次のSessionManagerへ中継する
85
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
236 smList.sendExcept(channel, receivedCommand);
d5cd4d103eea *** empty log message ***
pin
parents: 84
diff changeset
237 }
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
238 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
239
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
240 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
241
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
242 case REP.SMCMD_SM_JOIN:
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
243
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
244 {
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
245 //SessionManagerのリストへ追加
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
246 smList.add(channel);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
247
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
248 //XMLからSessionListオブジェクトを生成する。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
249 SessionXMLDecoder decoder = new SessionXMLDecoder();
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
250 SessionList receivedSessionList = decoder.decode(receivedCommand.string);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
251
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
252 //myHost を設定。
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
253 //立ち上げ時にやるとlocalhostしか取れない
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
254 if(myHost == null) setMyHostName(getLocalHostName(channel));
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
255
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
256 //maxHost を設定。
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
257 if(setMaxHost(channel, receivedSessionList.getMaxHost())){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
258 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
259 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
260 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
261 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
262 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
263
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
264 //SessionListからXMLを生成。
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
265 //joinしてきたSessionManagerに対してACKを送信。
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
266 SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionList);
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
267 sendCommand = new REPCommand();
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
268 sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
269 sendCommand.setString(sessionlistEncoder.sessionListToXML());
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
270 send.write(sendCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
271
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
272 //その他の SessionManager に対して SMCMD_UPDATEを 送信。
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
273 sendCommand = new REPCommand();
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
274 sendCommand.setCMD(REP.SMCMD_UPDATE);
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
275 sendCommand.setString(receivedCommand.string);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
276 smList.sendExcept(channel, sendCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
277
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
278 }
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
279 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
280
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
281 case REP.SMCMD_SM_JOIN_ACK:
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
282
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
283 //XMLからSessionListオブジェクトを生成。
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
284 SessionXMLDecoder decoder2 = new SessionXMLDecoder();
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
285 SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
286
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
287 //maxHostを決定。
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
288 if(setMaxHost(channel, receivedSessionList2.getMaxHost())){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
289 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
290 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
291 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
292 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
293 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
294
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
295 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
296
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
297 case REP.SMCMD_UPDATE:
200
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
298 {
99
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
299 SessionXMLDecoder decoder3 = new SessionXMLDecoder();
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
300 SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
301
200
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
302 //UPDATEコマンドにより送られてきたSessionの情報を追加する
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
303 LinkedList<Session> list = receivedSessionList3.getList();
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
304 for(Session session : list){
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
305 session.getEditorList().get(0).setChannel(channel);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
306 sessionList.add(session);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
307 }
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
308
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
309 //他のSessionManagerへ中継する
99
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
310 smList.sendExcept(channel, receivedCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
311
200
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
312 //リストのコピーをGUIに渡す
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
313 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
314 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
315 //GUIに反映
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
316 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
317 SwingUtilities.invokeLater(doRun);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
318 }
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
319 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
320
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
321 case REP.SMCMD_UPDATE_ACK:
200
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
322 {
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
323 if(receivedCommand.sid > sessionList.size()){
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
324 Editor editor = new Editor(channel);
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
325 editor.setName(receivedCommand.string);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
326
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
327 Session session = new Session(editor);
73
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
328 session.addEditor(editor);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
329
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
330 sessionList.add(session);
200
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
331
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
332 //リストのコピーをGUIに渡す
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
333 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
334 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
335 //GUIに反映
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
336 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
337 SwingUtilities.invokeLater(doRun);
73
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
338 }
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
339 smList.sendToSlave(receivedCommand);
200
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
340 }
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
341 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
342
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
343 case REP.SMCMD_CH_MASTER:
200
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
344 {
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
345 //maxHost を設定。
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
346 if(setMaxHost(channel, receivedCommand.string)){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
347 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
348 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
349 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
350 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
351 }
200
2f0a0448de6b *** empty log message ***
pin
parents: 199
diff changeset
352 }
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
353 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
354
213
4d9b32666ed2 *** empty log message ***
pin
parents: 212
diff changeset
355 case REP.REPCMD_DELETE:
4d9b32666ed2 *** empty log message ***
pin
parents: 212
diff changeset
356 case REP.REPCMD_INSERT:
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
357 {
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
358 //sid から Session を取得
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
359 Session session = getSession(receivedCommand.sid);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
360 //マージの処理と次のエディタへコマンドを送信する処理
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
361 session.translate(channel, receivedCommand);
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
362
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
363
167
3dc194f5e28f *** empty log message ***
pin
parents: 164
diff changeset
364 Editor editor = session.getEditor(channel);
3dc194f5e28f *** empty log message ***
pin
parents: 164
diff changeset
365 Editor prevEditor = session.getPrevEditor(editor);
3dc194f5e28f *** empty log message ***
pin
parents: 164
diff changeset
366
201
0beb2bcfefe2 *** empty log message ***
pin
parents: 200
diff changeset
367 //マージ中のエディタはコマンドを受け取らない
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
368 if(editor.isMerging()){
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
369 //Handlerを切り替える
167
3dc194f5e28f *** empty log message ***
pin
parents: 164
diff changeset
370 setMergeState(prevEditor.getChannel(), session.getSID());
169
2974db051dd8 *** empty log message ***
pin
parents: 167
diff changeset
371 }else {
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
372 setNormalState(prevEditor.getChannel(), session.getSID());
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
373 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
374 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
375 break;
213
4d9b32666ed2 *** empty log message ***
pin
parents: 212
diff changeset
376
4d9b32666ed2 *** empty log message ***
pin
parents: 212
diff changeset
377 default:
4d9b32666ed2 *** empty log message ***
pin
parents: 212
diff changeset
378 assert(false);
4d9b32666ed2 *** empty log message ***
pin
parents: 212
diff changeset
379 break;
4d9b32666ed2 *** empty log message ***
pin
parents: 212
diff changeset
380
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
381 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
382 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
383
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
384 private void guiUpdate() {
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
385 //リストのコピーをGUIに渡す
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
386 LinkedList<Session> sList = new LinkedList<Session>(sessionList);
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
387 LinkedList<Editor> eList = new LinkedList<Editor>(editorList);
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
388 //GUIに反映
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
389 Runnable doRun = new DoGUIUpdate(sList, eList, gui);
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
390 SwingUtilities.invokeLater(doRun);
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
391 }
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
392
169
2974db051dd8 *** empty log message ***
pin
parents: 167
diff changeset
393 private void setNormalState(REPSocketChannel<REPCommand> channel, int sid) {
2974db051dd8 *** empty log message ***
pin
parents: 167
diff changeset
394 SelectionKey key = channel.keyFor(selector);
2974db051dd8 *** empty log message ***
pin
parents: 167
diff changeset
395 key.attach(new REPHandlerImpl(sid, this));
2974db051dd8 *** empty log message ***
pin
parents: 167
diff changeset
396 }
2974db051dd8 *** empty log message ***
pin
parents: 167
diff changeset
397
167
3dc194f5e28f *** empty log message ***
pin
parents: 164
diff changeset
398 private void setMergeState(REPSocketChannel<REPCommand> channel, int sid) {
3dc194f5e28f *** empty log message ***
pin
parents: 164
diff changeset
399 SelectionKey key = channel.keyFor(selector);
3dc194f5e28f *** empty log message ***
pin
parents: 164
diff changeset
400 key.attach(new REPHandlerInMerge(sid, this));
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
401 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
402
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
403 private Editor getEditor(String hostport) {
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
404 for(Editor editor : editorList){
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
405 if(editor.getHost() == hostport){
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
406 return editor;
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
407 }
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
408 }
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
409 return null;
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
410 }
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
411
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 222
diff changeset
412 public Editor getEditor(REPSocketChannel<REPCommand> channel){
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
413 for(Editor editor : editorList){
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
414 if(editor.getChannel() == channel){
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
415 return editor;
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
416 }
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
417 }
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
418 return null;
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
419 }
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
420
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
421 private Session getSession(int sid) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
422 for(Session session : sessionList){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
423 if(session.getSID() == sid) return session;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
424 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
425 return null;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
426 }
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
427
224
6b0dd92b8e45 add optimizer to Editor
kent
parents: 222
diff changeset
428 private boolean setMaxHost(REPSocketChannel<REPCommand> channel, String maxHost2) {
179
763aad2da6b3 *** empty log message ***
pin
parents: 178
diff changeset
429 if(maxHost.compareTo(maxHost2) > 0){
763aad2da6b3 *** empty log message ***
pin
parents: 178
diff changeset
430 return false;
763aad2da6b3 *** empty log message ***
pin
parents: 178
diff changeset
431 }else{
763aad2da6b3 *** empty log message ***
pin
parents: 178
diff changeset
432 maxHost = maxHost2;
763aad2da6b3 *** empty log message ***
pin
parents: 178
diff changeset
433 return true;
763aad2da6b3 *** empty log message ***
pin
parents: 178
diff changeset
434 }
139
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
435 }
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
436
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
437 private void setMyHostName(String localHostName) {
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
438 myHost = localHostName + temp_port;
81
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
439 if(maxHost == null) {
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
440 maxHost = myHost;
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
441 }
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
442 setHostToEditor(myHost);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
443 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
444
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
445 private void setHostToEditor(String myHost2) {
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
446 for(Editor editor : editorList){
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
447 editor.setHost(myHost2);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
448 }
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
449 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
450
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
451 public static void main(String[] args) throws InterruptedException, IOException {
191
dbf59f82273e *** empty log message ***
pin
parents: 186
diff changeset
452
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
453 int port = DEFAULT_PORT;
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
454 int port_s = DEFAULT_PORT;
113
ce84d70b6cc9 *** empty log message ***
pin
parents: 112
diff changeset
455 //System.setProperty("file.encoding", "UTF-8");
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
456 if(args.length > 0){
39
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
457 port = Integer.parseInt(args[0]);
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
458 port_s = Integer.parseInt(args[1]);
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
459 }
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
460 temp_port = port;
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
461 send_port = port_s;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
462 SessionManager sm = new SessionManager(port);
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
463 sm.openSelector();
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
464 sm.init(port);
191
dbf59f82273e *** empty log message ***
pin
parents: 186
diff changeset
465 sm.startGUI(sm);
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
466 sm.mainLoop();
191
dbf59f82273e *** empty log message ***
pin
parents: 186
diff changeset
467
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
468 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
469
191
dbf59f82273e *** empty log message ***
pin
parents: 186
diff changeset
470 private void startGUI(SessionManager sm) {
222
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
471 gui = new SessionManagerGUI();
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
472 Thread th = new Thread( gui );
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
473 th.start();
222
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
474 gui.addREPActionListener(this);
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
475 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
476
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
477 public void connectSession(String host) {
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
478 int port = DEFAULT_PORT;
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
479 port = send_port;
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
480 InetSocketAddress addr = new InetSocketAddress(host, port);
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
481 try {
186
d22384c0026c *** empty log message ***
kent
parents: 185
diff changeset
482 REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
483 sessionchannel.configureBlocking(true);
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
484 sessionchannel.connect(addr);
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
485 while(!sessionchannel.finishConnect()){
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
486 System.out.print("test afro");
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
487 }
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
488 System.out.println("");
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
489 registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
490
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
491 sm_join(sessionchannel);
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
492
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
493 }catch (IOException e) {
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
494 e.printStackTrace();
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
495 }
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
496 }
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
497
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
498 private void sm_join(REPSocketChannel<REPCommand> channel){
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
499
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
500 //SM_JOINコマンドを生成。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
501 REPCommand command = new REPCommand();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
502 command.setCMD(REP.SMCMD_SM_JOIN);
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
503
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
504 //hostnameをセット。
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
505 setMyHostName(getLocalHostName(channel));
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
506
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
507 //XMLを生成。送信コマンドにセット。
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
508 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionList);
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
509 String string = encoder.sessionListToXML();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
510 command.setString(string);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
511
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
512 //SM_JOINコマンドを送信。
186
d22384c0026c *** empty log message ***
kent
parents: 185
diff changeset
513 channel.write(command);
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
514 //SessionManagerのListに追加。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
515 smList.add(channel);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
516 }
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
517
139
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
518 private String getLocalHostName(REPSocketChannel channel) {
74
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
519 String host = null;
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
520 host = channel.socket().getLocalAddress().getHostName();
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
521 return host;
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
522 }
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
523
222
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
524 // public void connectionOccured(SessionManagerEvent event) {
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
525 // try {
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
526 // waitingQueue.put(event);
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
527 // } catch (InterruptedException e) {
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
528 // }
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
529 // selector.wakeup();
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
530 // }
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
531 //
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
532 // public void selectOccured(SessionManagerEvent event) {
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
533 // try {
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
534 // waitingQueue.put(event);
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
535 // } catch (InterruptedException e) {
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
536 // e.printStackTrace();
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
537 // }
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
538 // selector.wakeup();
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
539 // }
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
540
222
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
541 public void selectSession(SelectButtonEvent event) {
163
fb516794ee28 *** empty log message ***
pin
parents: 162
diff changeset
542 REPSocketChannel<REPCommand> channel = event.getEditorChannel();
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
543 int sid = event.getSID();
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
544 Session session = getSession(sid);
222
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
545 //selector.wakeup();
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
546 if(session.hasOwner()){
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
547 Editor editor = new Editor(channel);
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
548 session.addEditor(new Editor(channel));
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
549 REPCommand sendCommand = new REPCommand();
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
550 sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
551 sendCommand.setEID(editor.getEID());
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
552 sendCommand.setSID(sid);
186
d22384c0026c *** empty log message ***
kent
parents: 185
diff changeset
553 channel.write(sendCommand);
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
554 }else {
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
555 REPSocketChannel<REPCommand> editorChannel = event.getEditorChannel();
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
556 sid = event.getSID();
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
557 Editor editor = new Editor(editorChannel);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
558 editor.setHost(myHost);
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
559 session = getSession(sid);
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
560 session.addEditor(editor);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
561
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
562 Editor owner = session.getOwner();
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
563
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
564 REPCommand command = new REPCommand();
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
565 command.setCMD(REP.SMCMD_SELECT);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
566 command.setSID(sid);
178
a097b1d619a1 *** empty log message ***
pin
parents: 173
diff changeset
567 command.setString(editor.getHost());
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
568 owner.send(command);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
569 }
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
570 }
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
571
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
572 public void addWaitingCommand(PacketSet set) {
212
e8f716498caf *** empty log message ***
pin
parents: 201
diff changeset
573 waitingCommandInMerge.add(set);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
574 }
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
575
185
c2c47d7675a8 *** empty log message ***
pin
parents: 182
diff changeset
576 public List<Session> getSessionList() {
c2c47d7675a8 *** empty log message ***
pin
parents: 182
diff changeset
577 return sessionList;
c2c47d7675a8 *** empty log message ***
pin
parents: 182
diff changeset
578 }
c2c47d7675a8 *** empty log message ***
pin
parents: 182
diff changeset
579
c2c47d7675a8 *** empty log message ***
pin
parents: 182
diff changeset
580 public List<Editor> getEditorList() {
c2c47d7675a8 *** empty log message ***
pin
parents: 182
diff changeset
581 return editorList;
c2c47d7675a8 *** empty log message ***
pin
parents: 182
diff changeset
582 }
c2c47d7675a8 *** empty log message ***
pin
parents: 182
diff changeset
583
222
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
584 public void buttonPressed(SessionManagerEvent event) {
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
585 try {
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
586 waitingQueue.put(event);
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
587 } catch (InterruptedException e) {}
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
588 selector.wakeup();
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
589
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
590 }
18d6a7140fa3 *** empty log message ***
pin
parents: 215
diff changeset
591
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
592 }