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