annotate rep/SessionManager.java @ 62:c8b66e3d13d1

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