193
|
1 package test.sematest;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.InetSocketAddress;
|
285
|
5 import java.nio.channels.SelectionKey;
|
|
6 import java.util.LinkedList;
|
193
|
7
|
|
8 import rep.REP;
|
|
9 import rep.REPCommand;
|
|
10 import rep.REPCommandPacker;
|
|
11 import rep.channel.REPLogger;
|
308
|
12 import rep.channel.REPSelectionKey;
|
285
|
13 import rep.channel.REPSelector;
|
193
|
14 import rep.channel.REPSocketChannel;
|
284
|
15 import test.Text;
|
193
|
16
|
|
17
|
297
|
18 /**
|
|
19 * @author kono
|
|
20 * Basic Temote Editor client implementation
|
|
21 * should support multi-session
|
|
22 * currently multi-session requires new channel, that is
|
|
23 * only one session for this editor.
|
|
24 */
|
193
|
25 public class TestEditor extends Thread{
|
284
|
26 private InetSocketAddress semaIP;
|
193
|
27 private REPLogger ns;
|
284
|
28 private int seq = 0;
|
313
|
29 private int prevSeq = 0;
|
284
|
30 public Text text;
|
285
|
31 public LinkedList<REPCommand> cmds;
|
286
|
32 private int eid = 0;
|
|
33 private int sid = 0;
|
285
|
34 REPSocketChannel<REPCommand> channel;
|
|
35 boolean running = true;
|
|
36 long timeout = 1;
|
297
|
37 private String name;
|
298
|
38 private boolean inputLock=false;
|
|
39 public boolean detached=false;
|
|
40 public boolean master=false;
|
303
|
41 private int syncCounter=0;
|
298
|
42 private boolean hasInputLock=true;
|
284
|
43
|
303
|
44
|
284
|
45 public TestEditor(String name, String _host,int _port, boolean master){
|
193
|
46 super(name);
|
303
|
47 LinkedList<REPCommand>cmdList = new LinkedList<REPCommand>();
|
|
48 String[] txts = {
|
|
49 "aaa", "bbb", "ccc", "ddd", "eee",
|
|
50 };
|
193
|
51 semaIP = new InetSocketAddress(_host, _port);
|
|
52 ns = REPLogger.singleton();
|
297
|
53 this.name = name;
|
303
|
54 cmds = cmdList;
|
285
|
55 if (master) {
|
298
|
56 this.master=true;
|
303
|
57 text = new Text(txts);
|
298
|
58 cmds.add(new REPCommand(REP.SMCMD_PUT,0,0,0,0,name+"-file"));
|
285
|
59 cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"m0"));
|
304
|
60 //cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"m0"));
|
|
61 //cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,""));
|
285
|
62 } else {
|
284
|
63 text = new Text(new String[0]);
|
298
|
64 cmds.add(new REPCommand(REP.SMCMD_JOIN,0,0,0,0,name));
|
304
|
65 //cmds.add(new REPCommand(REP.REPCMD_INSERT,0,0,0,0,"c0"));
|
|
66 //cmds.add(new REPCommand(REP.REPCMD_DELETE,0,0,0,0,"c0"));
|
303
|
67 }
|
|
68 }
|
|
69
|
|
70 public TestEditor(String name, String _host,int _port, boolean master,
|
|
71 String[] txts,LinkedList<REPCommand> cmdList){
|
|
72 super(name);
|
|
73 semaIP = new InetSocketAddress(_host, _port);
|
|
74 ns = REPLogger.singleton();
|
|
75 this.name = name;
|
|
76 cmds = cmdList;
|
|
77 if (master) {
|
|
78 this.master=true;
|
|
79 text = new Text(txts);
|
285
|
80 }
|
193
|
81 }
|
|
82
|
|
83 public void run(){
|
302
|
84 /*
|
|
85 * Create Socket and connect to the session manager
|
|
86 */
|
193
|
87 try {
|
|
88 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
|
285
|
89 } catch (IOException e) { return; }
|
193
|
90
|
297
|
91 ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" "+name, 1);
|
285
|
92 try {
|
193
|
93 while (!channel.connect(semaIP)){
|
|
94 ns.writeLog("SeMa not listen to socket yet, wait", 1);
|
|
95 }
|
285
|
96 } catch (IOException e) { return; }
|
297
|
97 ns.writeLog("successes to connect "+name);
|
285
|
98 /*
|
302
|
99 * Start editor main loop
|
285
|
100 * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string)
|
|
101 */
|
|
102 try {
|
|
103 mainloop();
|
193
|
104 } catch (IOException e) {
|
|
105 }
|
|
106 }
|
284
|
107
|
302
|
108 /*
|
|
109 * Editor main loop with input lock
|
|
110 */
|
285
|
111 private void mainloop() throws IOException {
|
|
112
|
|
113 channel.configureBlocking(false);
|
|
114 REPSelector<REPCommand> selector = REPSelector.create();
|
|
115 channel.register(selector, SelectionKey.OP_READ);
|
|
116 while(running) {
|
298
|
117 if (inputLock) {
|
|
118 // No user input during merge mode (optional)
|
|
119 if (selector.select(0)>0) {
|
|
120 handle(channel.read());
|
|
121 }
|
|
122 continue;
|
|
123 } else if (selector.select(timeout)<=0) {
|
|
124 if (syncCounter>0) {
|
302
|
125 syncText(); // send the master editor buffer to clients.
|
298
|
126 }
|
288
|
127 userInput();
|
308
|
128 }
|
|
129 // selector(timeout) returns 0, but it may contain readable channel..
|
|
130 for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()) {
|
|
131 REPSocketChannel<REPCommand> ch = key.channel1();
|
|
132 handle(ch.read());
|
285
|
133 }
|
|
134 }
|
|
135 }
|
284
|
136
|
300
|
137 private void syncText() {
|
302
|
138 /*
|
|
139 * Send delete/insert one at a time to synchronize
|
|
140 * all clients. SYNC is requested by the session manager.
|
|
141 */
|
300
|
142 if (syncCounter>text.size()) {
|
|
143 syncCounter=0;
|
|
144 } else {
|
|
145 int i=syncCounter-1;
|
|
146 REPCommand del = new REPCommand(REP.REPCMD_DELETE,sid,eid,0,i, text.get(i));
|
|
147 REPCommand ins = new REPCommand(REP.REPCMD_INSERT,sid,eid,0,i, text.get(i));
|
302
|
148 sendCommand(del,seq++);
|
|
149 sendCommand(ins,seq++);
|
300
|
150 syncCounter++;
|
|
151 }
|
|
152 }
|
|
153
|
302
|
154 /*
|
|
155 * Simulate User Input
|
|
156 */
|
288
|
157 private void userInput() {
|
|
158 REPCommand cmd = cmds.poll();
|
|
159 if (cmd!=null) {
|
297
|
160 switch(cmd.cmd) {
|
|
161 case REPCMD_INSERT:
|
|
162 text.insert(cmd.lineno, cmd.string);
|
302
|
163 sendCommand(cmd,seq++);
|
297
|
164 break;
|
|
165 case REPCMD_DELETE:
|
|
166 String del = text.delete(cmd.lineno);
|
|
167 cmd.setString(del);
|
302
|
168 sendCommand(cmd,seq++);
|
297
|
169 break;
|
|
170 case SMCMD_QUIT:
|
302
|
171 /*
|
|
172 * start termination phase 1 by the master editor.
|
|
173 * after this command do not send any user input.
|
|
174 * clients simply disconnect from the session manager.
|
|
175 */
|
297
|
176 cmds.clear();
|
303
|
177 sendCommand(cmd,seq++);
|
297
|
178 break;
|
299
|
179 case SMCMD_JOIN:
|
|
180 case SMCMD_PUT:
|
302
|
181 sendCommand(cmd,seq++);
|
313
|
182 prevSeq = seq;
|
302
|
183 /*
|
|
184 * To prevent confusion, stop user input until the ack
|
|
185 */
|
300
|
186 inputLock = true; // wait until ACK
|
299
|
187 break;
|
297
|
188 default:
|
|
189 assert(false);
|
|
190 }
|
288
|
191 } else {
|
|
192 // no more command to send
|
|
193 timeout = 0;
|
|
194 }
|
|
195 }
|
|
196
|
285
|
197
|
313
|
198 private void sendCommand(REPCommand cmd1,int seq) {
|
|
199 REPCommand cmd = new REPCommand(cmd1);
|
302
|
200 cmd.setSEQID(seq);
|
285
|
201 cmd.setEID(eid);
|
|
202 cmd.setSID(sid);
|
297
|
203 ns.writeLog(name +" send "+cmd);
|
285
|
204 channel.write(cmd);
|
|
205 }
|
|
206
|
|
207 private void handle(REPCommand cmd) {
|
308
|
208 if (cmd==null) return;
|
297
|
209 ns.writeLog(name +": read "+cmd);
|
285
|
210 switch(cmd.cmd) {
|
297
|
211 case REPCMD_INSERT :
|
|
212 text.insert(cmd.lineno, cmd.string);
|
313
|
213 if (cmd.eid==REP.MERGE_EID.id) break;
|
|
214 addNop();
|
302
|
215 sendCommand(cmd,cmd.seq);
|
297
|
216 break;
|
|
217 case REPCMD_INSERT_ACK :
|
298
|
218 assert(false);
|
297
|
219 break;
|
|
220 case REPCMD_DELETE :
|
|
221 String del = text.delete(cmd.lineno);
|
313
|
222 if (cmd.eid==REP.MERGE_EID.id) break;
|
|
223 addNop();
|
297
|
224 cmd.setString(del);
|
302
|
225 sendCommand(cmd,cmd.seq);
|
297
|
226 break;
|
286
|
227 case REPCMD_DELETE_ACK :
|
298
|
228 assert(false);
|
286
|
229 break;
|
313
|
230 case REPCMD_NOP :
|
|
231 if (cmd.eid==REP.MERGE_EID.id) break;
|
|
232 addNop();
|
|
233 sendCommand(cmd,cmd.seq);
|
|
234 break; case REPCMD_CLOSE :
|
286
|
235 case REPCMD_CLOSE_2 :
|
298
|
236 assert(false);
|
286
|
237 break;
|
313
|
238
|
286
|
239 case SMCMD_JOIN_ACK :
|
|
240 sid = cmd.sid;
|
|
241 eid = cmd.eid;
|
300
|
242 inputLock = false;
|
286
|
243 break;
|
|
244 case SMCMD_PUT_ACK :
|
|
245 sid = cmd.sid;
|
|
246 eid = cmd.eid;
|
300
|
247 inputLock = false;
|
286
|
248 break;
|
|
249 case SMCMD_QUIT :
|
304
|
250 if (false) {
|
303
|
251 if (cmd.eid!=eid)
|
|
252 sendCommand(cmd,cmd.seq);
|
|
253 else
|
|
254 sendCommand(new REPCommand(REP.SMCMD_QUIT_2,
|
|
255 sid, eid, seq, 0, ""),seq++);
|
304
|
256 }
|
298
|
257 cmds.clear();
|
|
258 break;
|
286
|
259 case SMCMD_QUIT_ACK :
|
298
|
260 assert(false);
|
286
|
261 break;
|
|
262 case SMCMD_START_MERGE :
|
298
|
263 // lock user input during merge (optional)
|
|
264 inputLock = hasInputLock;
|
|
265 cmd.cmd = REP.SMCMD_START_MERGE_ACK;
|
302
|
266 sendCommand(cmd,seq++);
|
298
|
267 break;
|
286
|
268 case SMCMD_START_MERGE_ACK :
|
298
|
269 assert(false);
|
286
|
270 break;
|
|
271 case SMCMD_END_MERGE :
|
298
|
272 inputLock = false;
|
286
|
273 break;
|
|
274 case SMCMD_QUIT_2 :
|
303
|
275 if (cmd.eid!=eid) {
|
|
276 sendCommand(cmd,cmd.seq);
|
|
277 }
|
286
|
278 running = false;
|
|
279 break;
|
298
|
280 case SMCMD_SYNC:
|
|
281 // start contents sync with newly joined editor
|
302
|
282 cmd.cmd = REP.SMCMD_SYNC_ACK; sendCommand(cmd,cmd.seq);
|
|
283 if (cmd.eid==eid)
|
|
284 syncCounter = 1;
|
298
|
285 break;
|
286
|
286 default:
|
|
287 assert(false);
|
|
288 break;
|
285
|
289 }
|
284
|
290 }
|
313
|
291
|
|
292 private void addNop() {
|
|
293 if (seq!=prevSeq) return;
|
|
294 // We haven't send any command, add nop before retransmition.
|
|
295 REPCommand nop = new REPCommand(REP.REPCMD_NOP, sid, eid, seq, 0, "");
|
|
296 sendCommand(nop,seq++);
|
|
297 prevSeq = seq;
|
|
298 }
|
193
|
299 }
|