annotate rep/SessionManager.java @ 192:6fb97c7a0b42

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