0
|
1 package rep;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.InetSocketAddress;
|
11
|
5 import java.net.SocketAddress;
|
2
|
6 import java.nio.channels.SelectableChannel;
|
0
|
7 import java.nio.channels.SelectionKey;
|
|
8 import java.nio.channels.Selector;
|
|
9 import java.nio.channels.ServerSocketChannel;
|
|
10 import java.nio.channels.SocketChannel;
|
15
|
11 import java.util.StringTokenizer;
|
0
|
12
|
56
|
13 import rep.xml.SessionXMLDecoder;
|
45
|
14 import rep.xml.SessionXMLEncoder;
|
|
15
|
1
|
16 //+-------+--------+--------+-------+--------+---------+------+
|
|
17 //| cmd | session| editor | seqid | lineno | textsiz | text |
|
|
18 //| | id | id | | | | |
|
|
19 //+-------+--------+--------+-------+--------+---------+------+
|
|
20 //o-------header section (network order)-------------o
|
|
21 /*int cmd; // command
|
|
22 int sid; // session ID
|
|
23 int eid; // editor ID
|
|
24 int seqno; // Sequence number
|
|
25 int lineno; // line number
|
|
26 int textsize; // textsize
|
|
27 byte[] text;*/
|
|
28
|
8
|
29 public class SessionManager implements ConnectionListener, REPActionListener{
|
0
|
30
|
|
31
|
|
32 private SessionList sessionlist;
|
5
|
33 //SocketChannel sessionchannel;
|
2
|
34 private SessionManagerGUI sessionmanagerGUI;
|
|
35 private Selector selector;
|
7
|
36 private SessionManagerList smList;
|
17
|
37 private String myHost;
|
21
|
38 private boolean isMaster = true;
|
23
|
39 private EditorList editorList;
|
6
|
40 //private SocketChannel sessionchannel;
|
7
|
41 //private boolean co;
|
2
|
42 public SessionManager(int port) {
|
|
43 sessionmanagerGUI = new SessionManagerGUI();
|
|
44 }
|
|
45
|
|
46 public void openSelector() throws IOException{
|
|
47 selector = Selector.open();
|
|
48 }
|
0
|
49
|
|
50 public void sessionManagerNet(int port) throws InterruptedException, IOException {
|
|
51 /**
|
|
52 * @param args
|
|
53 * @throws IOException
|
|
54 * @throws InterruptedException
|
|
55 * @throws IOException
|
|
56 * @throws InterruptedException
|
|
57 */
|
2
|
58 System.out.println("sessionManagerNet()");
|
|
59
|
0
|
60 ServerSocketChannel ssc = ServerSocketChannel.open();
|
38
|
61 ssc.configureBlocking(false);
|
0
|
62 ssc.socket().bind(new InetSocketAddress(port));
|
|
63 ssc.register(selector, SelectionKey.OP_ACCEPT);
|
6
|
64
|
|
65
|
0
|
66 sessionlist = new SessionList();
|
7
|
67 smList = new SessionManagerList();
|
23
|
68 editorList = new EditorList();
|
0
|
69
|
|
70 while(true){
|
|
71 selector.select();
|
|
72 for(SelectionKey key : selector.selectedKeys()){
|
|
73 if(key.isAcceptable()){
|
28
|
74 /*** serverChannelはenableになったSelectionKeyのchannel ***/
|
|
75 ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel();
|
9
|
76 /*** EditorChannel を用いない記述 ***/
|
28
|
77 SocketChannel channel = serverChannel.accept(); //keyからchannelを取って、accept
|
2
|
78 registerChannel (selector, channel, SelectionKey.OP_READ);
|
0
|
79 channel = null;
|
9
|
80
|
|
81 /*** EditorChannel を用いた記述 ****/
|
|
82 //EditorChannel echannel = (EditorChannel) ssc.accept();
|
|
83 //echannel.setIO();
|
|
84 //registerChannel(selector, echannel, SelectionKey.OP_READ);
|
|
85 //echannel = null;
|
|
86
|
|
87 /*** SelectableEditorChannel ***/
|
|
88 //SocketChannel channel = ssc.accept();
|
|
89 //SelectableEditorChannel echannel2 = new SelectableEditorChannel(channel);
|
|
90 //registerChannel(selector, echannel2, SelectionKey.OP_READ);
|
|
91 //channel = null;
|
|
92 //echannel2 = null;
|
|
93
|
6
|
94 }else if(key.isReadable()){
|
9
|
95
|
|
96 /*** EditorChannel を用いない記述 ***/
|
0
|
97 SocketChannel channel = (SocketChannel)key.channel();
|
9
|
98 REPPacketReceive repRec = new REPPacketReceive(channel); //getPacket(), putPacket() にする。
|
28
|
99 repRec.setkey(key);
|
53
|
100 //REPCommand repCom = repRec.unpackUConv();
|
0
|
101 REPCommand repCom = repRec.unpack();
|
|
102 manager(channel, repCom);
|
9
|
103
|
|
104 /*** EditorChannel を用いた記述 ****/
|
|
105 //EditorChannel echannel = (EditorChannel) key.channel();
|
|
106 //REPCommand command = echannel.getPacket();
|
|
107 //manager(echannel, command);
|
|
108
|
6
|
109 }else if(key.isConnectable()){
|
|
110 System.out.println("Connectable");
|
21
|
111 }
|
0
|
112 }
|
|
113 }
|
|
114 }
|
1
|
115
|
2
|
116 private synchronized void registerChannel(Selector selector, SelectableChannel channel, int ops) throws IOException {
|
|
117 if(channel == null) {
|
|
118 return;
|
|
119 }
|
9
|
120 //System.out.println("registerChannel()");
|
2
|
121 channel.configureBlocking(false);
|
6
|
122 selector.wakeup();
|
2
|
123 channel.register(selector, ops);
|
|
124 }
|
|
125
|
0
|
126 private void manager(SocketChannel channel, REPCommand repCmd) {
|
|
127 if(repCmd == null) return;
|
|
128 switch(repCmd.cmd){
|
38
|
129
|
0
|
130 case REP.SMCMD_JOIN:
|
21
|
131 if(isMaster){
|
23
|
132 int eid = editorList.addEditor(channel, repCmd);
|
|
133 repCmd.setEID(eid);
|
|
134 editorList.sendJoinAck(channel, repCmd);
|
26
|
135 sessionmanagerGUI.setComboEditor(eid, channel);
|
21
|
136 }else{
|
23
|
137 editorList.addEditor(channel);
|
22
|
138 smList.sendJoin(repCmd);
|
30
|
139 //sessionmanagerGUI.setComboEditor(repCmd.eid, channel);
|
21
|
140 }
|
38
|
141 break;
|
31
|
142
|
1
|
143 case REP.SMCMD_JOIN_ACK:
|
31
|
144 // editorList.setEID(repCmd);
|
|
145 // editorList.sendJoinAck(repCmd);
|
|
146 // sessionmanagerGUI.setComboEditor(repCmd.eid, channel);
|
1
|
147 break;
|
31
|
148
|
0
|
149 case REP.SMCMD_PUT:
|
56
|
150 Editor editor = new Editor(channel);
|
|
151 editor.setEID(1);
|
|
152 editor.setName(repCmd.string);
|
|
153 Session session = new Session(editor);
|
|
154 sessionlist.addSession(session);
|
|
155 sessionmanagerGUI.setComboSession(session.getSID(), session.getName());
|
59
|
156 sessionmanagerGUI.setComboEditor(editor.getEID(), editor.getChannel());
|
56
|
157 session.addToRoutingTable(editor);
|
|
158 repCmd.setCMD(REP.SMCMD_PUT_ACK);
|
|
159 repCmd.setEID(1);
|
60
|
160 repCmd.setSID(session.getSID());
|
56
|
161 editor.send(repCmd);
|
|
162
|
|
163 //if(isMaster){
|
|
164 SessionXMLEncoder encoder = new SessionXMLEncoder(session);
|
|
165 REPCommand command = new REPCommand();
|
|
166 command.setSID(session.getSID());
|
|
167 command.setString(encoder.sessionListToXML());
|
|
168 if(isMaster){
|
|
169 command.setCMD(REP.SMCMD_UPDATE_ACK);
|
|
170 smList.sendToSlave(command);
|
|
171 }else{
|
|
172 command.setCMD(REP.SMCMD_UPDATE);
|
|
173 smList.sendToMaster(command);
|
31
|
174 }
|
38
|
175 break;
|
31
|
176
|
9
|
177 // case REP.SMCMD_PUT_ACK:
|
|
178 // break;
|
31
|
179
|
0
|
180 case REP.SMCMD_SELECT:
|
8
|
181 sessionlist.addEditor(channel, repCmd.sid, repCmd); //sessionlistへ追加
|
0
|
182 repCmd.setCMD(repCmd.cmd + 1);
|
|
183 REPPacketSend repSend3 = new REPPacketSend(channel);
|
9
|
184 repSend3.send(repCmd); //ACKを返す
|
8
|
185 break;
|
38
|
186
|
8
|
187 case REP.SMCMD_SELECT_ACK:
|
|
188 break;
|
38
|
189
|
1
|
190 case REP.SMCMD_SESSION:
|
|
191 break;
|
38
|
192
|
8
|
193 case REP.SMCMD_SM_JOIN:
|
31
|
194 if(isMaster){
|
38
|
195 smList.add(channel);
|
31
|
196 repCmd.setCMD(REP.SMCMD_SM_JOIN_ACK);
|
|
197 smList.sendSessionList(sessionlist, repCmd);
|
45
|
198 }else {
|
|
199
|
31
|
200 }
|
8
|
201 break;
|
38
|
202
|
8
|
203 case REP.SMCMD_SM_JOIN_ACK:
|
38
|
204 if(isMaster){
|
|
205 smList.send(channel, repCmd);
|
39
|
206 }else{
|
|
207 smList.send(channel, repCmd);
|
38
|
208 }
|
|
209
|
6
|
210 break;
|
38
|
211
|
8
|
212 case REP.SMCMD_UPDATE:
|
56
|
213 //int sessionID2 = sessionlist.addSession(channel, repCmd.string); //Sessionを作成
|
|
214 //sessionlist.addEditor(channel, sessionID2, repCmd);
|
|
215 //sessionmanagerGUI.setComboSession(sessionID2, repCmd.string); //ComboBoxにSessionを追加
|
|
216 SessionXMLDecoder decoder = new SessionXMLDecoder(repCmd.string);
|
|
217 Editor editor1 = new Editor(channel);
|
62
|
218 Session session1 = new Session(editor1);
|
|
219 sessionlist.addSession(session1);
|
58
|
220 if(isMaster){
|
|
221 repCmd.setCMD(REP.SMCMD_UPDATE_ACK);
|
|
222 smList.sendToSlave(repCmd);
|
|
223 }else{
|
|
224 repCmd.setCMD(REP.SMCMD_UPDATE);
|
|
225 smList.sendToMaster(repCmd);
|
|
226 }
|
9
|
227 break;
|
38
|
228
|
9
|
229 case REP.SMCMD_UPDATE_ACK:
|
56
|
230 smList.sendToSlave(repCmd);
|
1
|
231 break;
|
38
|
232
|
3
|
233 case REP.REPCMD_READ:
|
17
|
234 //sessionlist.sendCmd(channel, repCmd);
|
3
|
235 break;
|
38
|
236
|
0
|
237 default:
|
9
|
238 //sessionlist.sendCmd(channel, repCmd);
|
|
239 sessionlist.sendToNextEditor(channel, repCmd);
|
0
|
240 break;
|
|
241 }
|
|
242 }
|
|
243
|
22
|
244 // private void sendJoin(REPCommand repCmd) {
|
|
245 // smList.sendJoin(repCmd);
|
|
246 // }
|
21
|
247
|
|
248 private void sendSessionManagerJoinAck(SocketChannel channel, REPCommand repCmd) {
|
16
|
249 String socketstring = getSocketString(channel);
|
|
250 System.out.println(socketstring);
|
12
|
251 smList.add(channel); //SessionManagerのリストへ追加
|
|
252 repCmd.setCMD(REP.SMCMD_SM_JOIN_ACK);
|
16
|
253 repCmd.setString(repCmd.string + ":" + socketstring);
|
12
|
254 REPPacketSend repSend4 = new REPPacketSend(channel);
|
|
255 repSend4.send(repCmd); //ACK
|
10
|
256 }
|
21
|
257
|
|
258 private void sendJoinAck(REPCommand repCmd) {
|
|
259
|
|
260 }
|
10
|
261
|
21
|
262 private void sendJoinAck(SocketChannel channel, REPCommand repCmd) {
|
23
|
263 //int eid = sessionlist.getNumberOfEditor(); //eidを取得
|
|
264 int eid = editorList.addEditor(channel, repCmd);
|
17
|
265 sessionmanagerGUI.setComboEditor(eid, channel); //ComboBoxにEditorを追加
|
|
266 repCmd.setEID(eid); //eidを決定して、
|
|
267 repCmd.setCMD(REP.SMCMD_JOIN_ACK);
|
|
268 repCmd.string = sessionlist.getSessionList(); //Session一覧を
|
|
269
|
|
270 String string = getSocketString(channel);
|
|
271 StringTokenizer stn = new StringTokenizer(string, ":");
|
|
272 String host = stn.nextToken();
|
|
273 String port = stn.nextToken();
|
|
274 repCmd.setString(repCmd.string + ":" + myHost + ":" + port);
|
|
275
|
|
276 REPPacketSend repSend = new REPPacketSend(channel); //Editor側へ送信
|
|
277 repSend.send(repCmd);
|
10
|
278 }
|
|
279
|
0
|
280 public static void main(String[] args) throws InterruptedException, IOException {
|
6
|
281 int port = 8766;
|
1
|
282
|
0
|
283 if(args.length == 1){
|
39
|
284 port = Integer.parseInt(args[0]);
|
0
|
285 }
|
|
286 SessionManager sm = new SessionManager(port);
|
2
|
287 sm.openSelector();
|
|
288 sm.openWindow();
|
0
|
289 sm.sessionManagerNet(port);
|
|
290 }
|
|
291
|
2
|
292 private void openWindow() {
|
|
293 Thread th = new Thread( sessionmanagerGUI );
|
|
294 th.start();
|
|
295 System.out.println(sessionmanagerGUI.toString());
|
|
296 sessionmanagerGUI.addConnectionListener(this);
|
8
|
297 sessionmanagerGUI.addREPActionListener(this);
|
2
|
298 }
|
|
299
|
|
300 private void connectSession(String host) {
|
6
|
301 int port = 8766;
|
|
302 //SocketChannel sessionchannel;
|
2
|
303 //int port = Integer.parseInt(args[2]);
|
1
|
304 InetSocketAddress addr = new InetSocketAddress(host, port);
|
|
305 try {
|
6
|
306 SocketChannel sessionchannel = SocketChannel.open();
|
1
|
307 sessionchannel.configureBlocking(true);
|
|
308 sessionchannel.connect(addr);
|
6
|
309 REPPacketSend send = new REPPacketSend(sessionchannel);
|
|
310 while(!sessionchannel.finishConnect()){
|
|
311 System.out.print(".");
|
|
312 }
|
|
313 System.out.println("");
|
2
|
314 registerChannel(selector, sessionchannel, SelectionKey.OP_READ);
|
24
|
315 //REPCommand sm_join_com = REPCommand.SMCMD_SESSION_JOIN;
|
|
316 //String socketString = getSocketString(sessionchannel);
|
|
317 //sm_join_com.setString(sm_join_com.string + ":" + socketString);
|
45
|
318 SessionXMLEncoder encoder = new SessionXMLEncoder(sessionlist.getList());
|
|
319
|
|
320 REPCommand comm = new REPCommand();
|
|
321 comm.setCMD(REP.SMCMD_SM_JOIN);
|
|
322 comm.setString(encoder.sessionListToXML());
|
|
323 send.send(comm);
|
|
324
|
|
325 //send.send(REPCommand.SMCMD_SESSION_JOIN);
|
57
|
326 isMaster = false;
|
7
|
327 smList.add(sessionchannel);
|
31
|
328 smList.setMaster(sessionchannel);
|
1
|
329 }catch (IOException e) {
|
|
330 e.printStackTrace();
|
|
331 }
|
|
332 }
|
2
|
333
|
15
|
334 private String getSocketString(SocketChannel sessionchannel) {
|
14
|
335 SocketAddress socket = sessionchannel.socket().getRemoteSocketAddress();
|
15
|
336 //String inetAddressString = sessionchannel.socket().getInetAddress().toString();
|
|
337 StringTokenizer stn = new StringTokenizer(socket.toString(), "/");
|
|
338 String socketString = null;
|
|
339 while(stn.hasMoreTokens()){
|
|
340 socketString = stn.nextToken();
|
16
|
341 //System.out.println(socketString);
|
15
|
342 }
|
|
343 return socketString;
|
14
|
344 }
|
|
345
|
2
|
346 public void connectionOccured(ConnectionEvent event) {
|
|
347 connectSession(event.getHost());
|
|
348 }
|
8
|
349
|
|
350 public void ActionOccured(REPActionEvent event) {
|
|
351 System.out.println("Action!");
|
|
352 SocketChannel editorChannel = event.getEditorChannel();
|
|
353 int sid = event.getSID();
|
|
354 int eid = event.getEID();
|
|
355 sessionlist.addEditor(editorChannel, sid, eid);
|
|
356 REPPacketSend send = new REPPacketSend(editorChannel);
|
|
357 send.send(new REPCommand(REP.SMCMD_SELECT_ACK, sid, eid, 0,0,0,""));
|
|
358 sessionlist.sendSelect(sid);
|
|
359 }
|
0
|
360 }
|