annotate rep/SessionManager.java @ 164:3841cc75b808

*** empty log message ***
author pin
date Thu, 28 Aug 2008 17:58:42 +0900
parents fb516794ee28
children 3dc194f5e28f
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;
11
167694db4796 *** empty log message ***
pin
parents: 10
diff changeset
5 import java.net.SocketAddress;
111
fe8306c6c8bf *** empty log message ***
pin
parents: 109
diff changeset
6 import java.nio.ByteBuffer;
fe8306c6c8bf *** empty log message ***
pin
parents: 109
diff changeset
7 import java.nio.CharBuffer;
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
8 import java.nio.channels.SelectableChannel;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
9 import java.nio.channels.SelectionKey;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
10 import java.nio.channels.Selector;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
11 import java.nio.channels.ServerSocketChannel;
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
12 import java.nio.channels.SocketChannel;
111
fe8306c6c8bf *** empty log message ***
pin
parents: 109
diff changeset
13 import java.nio.charset.CharacterCodingException;
fe8306c6c8bf *** empty log message ***
pin
parents: 109
diff changeset
14 import java.nio.charset.Charset;
fe8306c6c8bf *** empty log message ***
pin
parents: 109
diff changeset
15 import java.nio.charset.CharsetEncoder;
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
16 import java.util.LinkedList;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
17 import java.util.List;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
18 import java.util.Set;
15
78c211017d3d *** empty log message ***
pin
parents: 14
diff changeset
19 import java.util.StringTokenizer;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
20
123
5b1a0574b406 *** empty log message ***
pin
parents: 122
diff changeset
21 import rep.channel.REPServerSocketChannel;
133
8dce4348966c *** empty log message ***
kono
parents: 124
diff changeset
22 import rep.channel.REPSocketChannel;
146
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
23 import rep.channel.SelectionKeySimulator;
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
24 import rep.channel.SelectorSimulator;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
25 import rep.handler.PacketSet;
146
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
26 import rep.handler.REPHandler;
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
27 import rep.handler.REPHandlerImpl;
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
28 import rep.handler.REPHandlerInMerge;
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
29 import rep.channel.REPSelector;
56
6ead43b2475e *** empty log message ***
pin
parents: 53
diff changeset
30 import rep.xml.SessionXMLDecoder;
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
31 import rep.xml.SessionXMLEncoder;
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
32
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
33 //+-------+--------+--------+-------+--------+---------+------+
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
34 //| cmd | session| editor | seqid | lineno | textsiz | text |
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
35 //| | id | id | | | | |
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
36 //+-------+--------+--------+-------+--------+---------+------+
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
37 //o-------header section (network order)-------------o
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
38 /*int cmd; // command
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
39 int sid; // session ID : uniqu to editing file
123
5b1a0574b406 *** empty log message ***
pin
parents: 122
diff changeset
40 int eid; // editor ID : owner editor ID = 1。Session に対して unique
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
41 int seqno; // Sequence number : sequence number はエディタごとに管理
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
42 int lineno; // line number
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
43 int textsize; // textsize : bytesize
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
44 byte[] text;*/
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
45
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
46 public class SessionManager implements ConnectionListener, REPActionListener{
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
47
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
48
163
fb516794ee28 *** empty log message ***
pin
parents: 162
diff changeset
49 //private SessionList sessionlist;
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
50 private LinkedList<Session> sessionList;
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
51 private SessionManagerGUI gui;
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
52 private Selector selector;
7
5067a6a93d01 *** empty log message ***
pin
parents: 6
diff changeset
53 private SessionManagerList smList;
17
806e34a6fecd *** empty log message ***
pin
parents: 16
diff changeset
54 private String myHost;
21
2d4bab638a71 *** empty log message ***
pin
parents: 19
diff changeset
55 private boolean isMaster = true;
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
56 //private EditorList ownEditorList;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
57 private List<Editor> editorList;
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
58 private String maxHost;
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
59 private boolean isSimulation;
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
60 private List<PacketSet> packetSetList;
163
fb516794ee28 *** empty log message ***
pin
parents: 162
diff changeset
61 //private List<SessionManagerNode> managerList;
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
62 private static int temp_port;
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
63 private static int send_port;
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
64
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
65 static final int DEFAULT_PORT = 8766;
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
66
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
67 public SessionManager(int port) {
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
68 gui = new SessionManagerGUI();
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
69 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
70
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
71 public void openSelector() throws IOException{
123
5b1a0574b406 *** empty log message ***
pin
parents: 122
diff changeset
72 selector = REPSelector.open();
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
73 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
74
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
75 public void init(int port) throws InterruptedException, IOException {
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
76
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
77 REPServerSocketChannel<REPCommand> ssc = REPServerSocketChannel.<REPCommand>open();
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
78 ssc.configureBlocking(false); //reuse address 必須
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
79 ssc.socket().setReuseAddress(true);
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
80 ssc.socket().bind(new InetSocketAddress(port));
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
81 ssc.register(selector, SelectionKey.OP_ACCEPT);
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
82
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
83
163
fb516794ee28 *** empty log message ***
pin
parents: 162
diff changeset
84 //sessionlist = new SessionList();
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
85 sessionList = new LinkedList<Session>();
7
5067a6a93d01 *** empty log message ***
pin
parents: 6
diff changeset
86 smList = new SessionManagerList();
162
2bd3264abf55 *** empty log message ***
pin
parents: 160
diff changeset
87 //ownEditorList = new EditorList();
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
88 editorList = new LinkedList<Editor>();
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
89 packetSetList = new LinkedList<PacketSet>();
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
90
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
91 // main loop
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
92 mainLoop();
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
93 }
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
94
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
95 private void mainLoop() throws IOException {
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
96 while(true){
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
97 selector.select();
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
98 select();
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
99 }
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 private void select() throws IOException {
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
103 for(SelectionKey key : selector.selectedKeys()){
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
104 if(key.isAcceptable()){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
105 /*** serverChannelはenableになったSelectionKeyのchannel ***/
146
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
106 REPServerSocketChannel serverChannel = (REPServerSocketChannel)key.channel();
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
107 REPSocketChannel channel = serverChannel.accept(); //keyからchannelを取って、accept
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
108 registerChannel (selector, channel, SelectionKey.OP_READ);
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
109 channel = null;
123
5b1a0574b406 *** empty log message ***
pin
parents: 122
diff changeset
110
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
111 }else if(key.isReadable()){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
112
146
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
113 // REPSocketChannel<REPCommand> channel = (REPSocketChannel<REPCommand>)key.channel();
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
114 // REPPacketReceive receive = new REPPacketReceive(channel);
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
115 // receive.setkey(key);
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
116 // REPCommand receivedCommand = receive.unpackUConv();
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
117 // manage(channel, receivedCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
118
146
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
119 REPHandler handler = (REPHandler)key.attachment();
20beee6ca31a *** empty log message ***
pin
parents: 144
diff changeset
120 handler.handle(key);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
121
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
122 }else if(key.isConnectable()){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
123 System.out.println("Connectable");
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
124 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
125 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
126 }
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
127
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
128 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
129 if(channel == null) {
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
130 return;
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
131 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
132 channel.configureBlocking(false);
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
133 selector.wakeup();
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
134 REPHandler handler = new REPHandlerImpl(this);
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);
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
142 REPPacketSend send = new REPPacketSend(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にも属さないエディタをリストに追加
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
149 Editor editor = new Editor(editorList.size(), channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
150 editor.setHost(myHost);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
151 editorList.add(editor);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
152
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
153 //GUIに反映
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
154 gui.setComboEditor(editor.getEID(), channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
155 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
156
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
157
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
158 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
159
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
160 case REP.SMCMD_JOIN_ACK:
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
161
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
162 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
163
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
164 case REP.SMCMD_PUT:
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
165 {
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
166 //エディタのリストに追加
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
167 Editor editor = new Editor(editorList.size(), channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
168 editorList.add(editor);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
169
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
170 //Sessionを生成
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
171 int sid = sessionList.size();
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
172 editor = new Editor(0, channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
173 editor.setHost(myHost);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
174 Session session = new Session(sid, editor);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
175 session.hasOwner(true);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
176 sessionList.add(new Session(sid, editor));
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
177
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
178 //GUIに反映
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
179 gui.setComboSession(session.getSID(), session.getName());
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
180 gui.setComboEditor(editor.getEID(), editor.getChannel());
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
181
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
182 //エディタにAckを送信
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
183 sendCommand.setCMD(REP.SMCMD_PUT_ACK);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
184 sendCommand.setEID(editor.getEID());
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
185 sendCommand.setSID(session.getSID());
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
186 editor.send(sendCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
187
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
188 //他のSessionManagerへSessionの追加を報告
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 {
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
202 //エディタをSessionに追加
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 //SessionListへ追加し変換テーブルを生成する。
163
fb516794ee28 *** empty log message ***
pin
parents: 162
diff changeset
253 //sessionlist.update(channel, receivedSessionList);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
254
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
255 //myHost を設定。
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
256 if(myHost == null) setMyHostName(getLocalHostName(channel));
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
257
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
258 //maxHost を設定。
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
259 if(setMaxHost(channel, receivedSessionList.getMaxHost())){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
260 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
261 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
262 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
263 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
264 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
265
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
266 //SessionListからXMLを生成。
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
267 //joinしてきたSessionManagerに対してACKを送信。
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
268 SessionXMLEncoder sessionlistEncoder = new SessionXMLEncoder(sessionList);
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
269 sendCommand = new REPCommand();
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
270 sendCommand.setCMD(REP.SMCMD_SM_JOIN_ACK);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
271 sendCommand.setString(sessionlistEncoder.sessionListToXML());
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
272 send.send(sendCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
273
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
274 //その他の SessionManager に対して SMCMD_UPDATEを 送信。
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
275 sendCommand = new REPCommand();
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
276 sendCommand.setCMD(REP.SMCMD_UPDATE);
78
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
277 sendCommand.setString(receivedCommand.string);
8a4f92f31c58 *** empty log message ***
pin
parents: 77
diff changeset
278 smList.sendExcept(channel, sendCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
279
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
280 }
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
281 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
282
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
283 case REP.SMCMD_SM_JOIN_ACK:
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
284
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
285 //XMLからSessionListオブジェクトを生成。
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
286 SessionXMLDecoder decoder2 = new SessionXMLDecoder();
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
287 SessionList receivedSessionList2 = decoder2.decode(receivedCommand.string);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
288
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
289 //maxHostを決定。
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
290 if(setMaxHost(channel, receivedSessionList2.getMaxHost())){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
291 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
292 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
293 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
294 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
295 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
296
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
297 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
298
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
299 case REP.SMCMD_UPDATE:
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
300
99
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
301 SessionXMLDecoder decoder3 = new SessionXMLDecoder();
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
302 SessionList receivedSessionList3 = decoder3.decode(receivedCommand.string);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
303
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
304 //SessionListへ追加し変換テーブルを生成する。
163
fb516794ee28 *** empty log message ***
pin
parents: 162
diff changeset
305 //sessionlist.update(channel, receivedSessionList3);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
306
99
2f79cdc45a2e *** empty log message ***
pin
parents: 98
diff changeset
307 smList.sendExcept(channel, receivedCommand);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
308
100
25bf89525e70 *** empty log message ***
pin
parents: 99
diff changeset
309 for(Session session3 : receivedSessionList3.getList()){
25bf89525e70 *** empty log message ***
pin
parents: 99
diff changeset
310 gui.setComboSession(session3.getSID(), session3.getName());
25bf89525e70 *** empty log message ***
pin
parents: 99
diff changeset
311 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
312
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
313 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
314
9
2d4c2c19d2d0 *** empty log message ***
pin
parents: 8
diff changeset
315 case REP.SMCMD_UPDATE_ACK:
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
316 if(receivedCommand.sid > sessionList.size()){
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
317 Editor editor = new Editor(channel);
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
318 editor.setName(receivedCommand.string);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
319
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
320 Session session = new Session(editor);
73
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
321 session.addEditor(editor);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
322
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
323 sessionList.add(session);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
324
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
325 gui.setComboSession(session.getSID(), session.getName());
73
4d887f38284b *** empty log message ***
pin
parents: 72
diff changeset
326 }
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
327 smList.sendToSlave(receivedCommand);
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
328 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
329
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
330 // case REP.REPCMD_READ:
108
4e8ebfa2d529 *** empty log message ***
pin
parents: 107
diff changeset
331 // //sessionlist.sendCmd(channel, repCmd);
4e8ebfa2d529 *** empty log message ***
pin
parents: 107
diff changeset
332 // break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
333
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
334 case REP.SMCMD_CH_MASTER:
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
335 //maxHost を設定。
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
336 if(setMaxHost(channel, receivedCommand.string)){
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
337 sendCommand = new REPCommand();
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
338 sendCommand.setCMD(REP.SMCMD_CH_MASTER);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
339 sendCommand.setString(maxHost);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
340 smList.sendExcept(channel, sendCommand);
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
341 }
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
342 break;
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
343
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
344 default:
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
345 {
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
346 //sid から Session を取得
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
347 Session session = getSession(receivedCommand.sid);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
348 //マージの処理と次のエディタへコマンドを送信する処理
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
349 session.translate(channel, receivedCommand);
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
350
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
351 Editor editor = getEditor(channel);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
352 if(editor.isMerging()){
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
353 //Handlerを切り替える
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
354 setMergeState(channel, selector);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
355 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
356 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
357 break;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
358 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
359 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
360
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
361 private void setMergeState(REPSocketChannel<REPCommand> channel, Selector selector2) {
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
362 SelectionKey key = channel.keyFor(selector2);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
363 key.attach(new REPHandlerInMerge(this));
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
364 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
365
160
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
366 private Editor getEditor(String hostport) {
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
367 return null;
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
368 }
6a5974dd0368 *** empty log message ***
pin
parents: 158
diff changeset
369
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
370 private Editor getEditor(REPSocketChannel<REPCommand> channel) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
371 // TODO Auto-generated method stub
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
372 for(Editor editor : editorList){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
373 if(editor.getChannel() == channel){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
374 return editor;
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
375 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
376 }
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
377 return null;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
378 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
379
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
380 private Session getSession(int sid) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
381 for(Session session : sessionList){
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
382 if(session.getSID() == sid) return session;
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
383 }
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
384 return null;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
385 }
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
386
139
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
387 private boolean setMaxHost(REPSocketChannel channel, String maxHost2) {
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
388 // TODO Auto-generated method stub
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
389 return false;
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
390 }
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
391
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
392 private void setMyHostName(String localHostName) {
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
393 myHost = localHostName + temp_port;
81
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
394 if(maxHost == null) {
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
395 maxHost = myHost;
13819571691d *** empty log message ***
pin
parents: 80
diff changeset
396 }
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
397 setHostToEditor(myHost);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
398 }
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
399
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
400 private void setHostToEditor(String myHost2) {
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
401 for(Editor editor : editorList){
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
402 editor.setHost(myHost2);
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
403 }
76
97ca5f5447cd *** empty log message ***
pin
parents: 75
diff changeset
404 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
405
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
406 public static void main(String[] args) throws InterruptedException, IOException {
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
407 int port = DEFAULT_PORT;
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
408 int port_s = DEFAULT_PORT;
113
ce84d70b6cc9 *** empty log message ***
pin
parents: 112
diff changeset
409 //System.setProperty("file.encoding", "UTF-8");
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
410 if(args.length > 0){
39
c873ee0318f8 *** empty log message ***
pin
parents: 38
diff changeset
411 port = Integer.parseInt(args[0]);
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
412 port_s = Integer.parseInt(args[1]);
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
413 }
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
414 temp_port = port;
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
415 send_port = port_s;
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
416 SessionManager sm = new SessionManager(port);
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
417 sm.openSelector();
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
418 sm.openWindow();
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
419 sm.init(port);
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
420 sm.mainLoop();
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
421 }
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
422
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
423 private void openWindow() {
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
424 Thread th = new Thread( gui );
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
425 th.start();
75
a636b651dbce *** empty log message ***
pin
parents: 74
diff changeset
426 //System.out.println(sessionmanagerGUI.toString());
83
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
427 gui.addConnectionListener(this);
9381b4734a0b *** empty log message ***
pin
parents: 82
diff changeset
428 gui.addREPActionListener(this);
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
429 }
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
430
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
431 private void connectSession(String host) {
101
612cb62151ce *** empty log message ***
pin
parents: 100
diff changeset
432 int port = DEFAULT_PORT;
95
2462a5d11396 add hostname+port
chiaki
parents: 94
diff changeset
433 port = send_port;
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
434 InetSocketAddress addr = new InetSocketAddress(host, port);
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
435 try {
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
436 REPSocketChannel<REPCommand> sessionchannel = REPSocketChannel.<REPCommand>create();
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
437 sessionchannel.configureBlocking(true);
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
438 sessionchannel.connect(addr);
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
439 while(!sessionchannel.finishConnect()){
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
440 System.out.print("test afro");
6
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
441 }
bdb91f4cea26 It Can connect SessionManagers each other.
pin
parents: 5
diff changeset
442 System.out.println("");
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
443 registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
444
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
445 sm_join(sessionchannel);
45
10f1adf6d74d *** empty log message ***
pin
parents: 39
diff changeset
446
1
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
447 }catch (IOException e) {
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
448 e.printStackTrace();
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
449 }
3f5bf0255f5e *** empty log message ***
pin
parents: 0
diff changeset
450 }
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
451
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
452 private void sm_join(REPSocketChannel<REPCommand> channel){
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
453
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
454 //SM_JOINコマンドを生成。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
455 REPCommand command = new REPCommand();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
456 command.setCMD(REP.SMCMD_SM_JOIN);
79
d1877cfd131d *** empty log message ***
pin
parents: 78
diff changeset
457
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
458 //hostnameをセット。
82
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
459 setMyHostName(getLocalHostName(channel));
4bb583553a42 *** empty log message ***
pin
parents: 81
diff changeset
460
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
461 //XMLを生成。送信コマンドにセット。
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
462 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionList);
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
463 String string = encoder.sessionListToXML();
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
464 command.setString(string);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
465
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
466 //SM_JOINコマンドを送信。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
467 REPPacketSend send = new REPPacketSend(channel);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
468 send.send(command);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
469
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
470 //SessionManagerのListに追加。
77
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
471 smList.add(channel);
eb78312ea831 *** empty log message ***
pin
parents: 76
diff changeset
472 }
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
473
139
d6b94713cf45 *** empty log message ***
pin
parents: 133
diff changeset
474 private String getLocalHostName(REPSocketChannel channel) {
74
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
475 String host = null;
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
476 host = channel.socket().getLocalAddress().getHostName();
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
477 return host;
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
478 }
391d44c94799 *** empty log message ***
pin
parents: 73
diff changeset
479
2
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
480 public void connectionOccured(ConnectionEvent event) {
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
481 connectSession(event.getHost());
02fa9a68d9a2 *** empty log message ***
pin
parents: 1
diff changeset
482 }
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
483
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
484 public void ActionOccured(REPActionEvent event) {
104
91f053bdfb18 *** empty log message ***
pin
parents: 103
diff changeset
485
163
fb516794ee28 *** empty log message ***
pin
parents: 162
diff changeset
486 REPSocketChannel<REPCommand> channel = event.getEditorChannel();
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
487 int sid = event.getSID();
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
488 Session session = getSession(sid);
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
489 if(session.hasOwner()){
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
490 Editor editor = new Editor(channel);
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
491 session.addEditor(new Editor(channel));
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
492 REPCommand sendCommand = new REPCommand();
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
493 sendCommand.setCMD(REP.SMCMD_JOIN_ACK);
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
494 sendCommand.setEID(editor.getEID());
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
495 sendCommand.setSID(sid);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
496 REPPacketSend sender = new REPPacketSend(channel);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
497 sender.send(sendCommand);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
498 }else {
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
499 REPSocketChannel<REPCommand> editorChannel = event.getEditorChannel();
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
500 sid = event.getSID();
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
501 Editor editor = new Editor(editorChannel);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
502 editor.setHost(myHost);
164
3841cc75b808 *** empty log message ***
pin
parents: 163
diff changeset
503 session = getSession(sid);
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
504 session.addEditor(editor);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
505
158
5cc8cd48bded *** empty log message ***
pin
parents: 155
diff changeset
506 Editor owner = session.getOwner();
107
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
507
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
508 REPCommand command = new REPCommand();
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
509 command.setCMD(REP.SMCMD_SELECT);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
510 command.setSID(sid);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
511 command.setString(editor.getHost() + ":" + editor.getPort());
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
512 owner.send(command);
d32096538dc3 *** empty log message ***
pin
parents: 106
diff changeset
513 }
72
3cdf2d6d99d9 *** empty log message ***
pin
parents: 71
diff changeset
514
3cdf2d6d99d9 *** empty log message ***
pin
parents: 71
diff changeset
515
8
b774b87cc2c1 *** empty log message ***
pin
parents: 7
diff changeset
516 }
122
790c8dd42a7b *** empty log message ***
kono
parents: 117
diff changeset
517
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
518 public void addWaitingCommand(PacketSet set) {
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
519 // TODO Auto-generated method stub
155
bbd2801d8ce0 *** empty log message ***
pin
parents: 152
diff changeset
520 packetSetList.add(set);
144
0bf7f8d0f5f7 *** empty log message ***
pin
parents: 141
diff changeset
521 }
148
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
522
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
523 public void undo() {
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
524 // TODO Auto-generated method stub
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
525
6a5fe529b192 *** empty log message ***
pin
parents: 146
diff changeset
526 }
0
e41994ce73c7 *** empty log message ***
pin
parents:
diff changeset
527 }