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;
|
|
180 case SMCMD_QUIT:
|
302
|
181 /*
|
|
182 * start termination phase 1 by the master editor.
|
|
183 * after this command do not send any user input.
|
|
184 * clients simply disconnect from the session manager.
|
|
185 */
|
297
|
186 cmds.clear();
|
413
|
187 cmd.eid = -1;
|
387
|
188 quit = cmd;
|
297
|
189 break;
|
299
|
190 case SMCMD_JOIN:
|
|
191 case SMCMD_PUT:
|
315
|
192 sendCommand(cmd);
|
302
|
193 /*
|
|
194 * To prevent confusion, stop user input until the ack
|
|
195 */
|
300
|
196 inputLock = true; // wait until ACK
|
299
|
197 break;
|
297
|
198 default:
|
|
199 assert(false);
|
|
200 }
|
288
|
201 } else {
|
387
|
202 if(syncCounter==0) {
|
386
|
203 // no more command to send, and we don't have syncCounter
|
|
204 timeout = 0;
|
387
|
205 if (quit!=null) {
|
413
|
206 if (quit.eid==-1)
|
|
207 sendCommand(quit);
|
|
208 else
|
|
209 forwardCommand(quit);
|
387
|
210 quit=null;
|
|
211 }
|
|
212 }
|
288
|
213 }
|
|
214 }
|
|
215
|
285
|
216
|
315
|
217 private void sendCommand(REPCommand cmd1) {
|
313
|
218 REPCommand cmd = new REPCommand(cmd1);
|
315
|
219 cmd.setSEQID(seq++);
|
285
|
220 cmd.setEID(eid);
|
|
221 cmd.setSID(sid);
|
297
|
222 ns.writeLog(name +" send "+cmd);
|
285
|
223 channel.write(cmd);
|
|
224 }
|
|
225
|
315
|
226 private void forwardCommand(REPCommand cmd1) {
|
|
227 REPCommand cmd = new REPCommand(cmd1);
|
445
|
228 ns.writeLog(name +" forward "+cmd);
|
315
|
229 channel.write(cmd);
|
|
230 }
|
|
231
|
285
|
232 private void handle(REPCommand cmd) {
|
308
|
233 if (cmd==null) return;
|
407
|
234 ns.writeLog(name +": read "+cmd + " textsize="+text.size());
|
285
|
235 switch(cmd.cmd) {
|
297
|
236 case REPCMD_INSERT :
|
405
|
237 if (cmd.eid!=eid) {
|
|
238 text.insert(cmd.lineno, cmd.string);
|
|
239 }
|
315
|
240 forwardCommand(cmd);
|
297
|
241 break;
|
|
242 case REPCMD_DELETE :
|
405
|
243 if (cmd.eid!=eid) {
|
|
244 String del="";
|
407
|
245 if(cmd.lineno<text.size()) {
|
405
|
246 del = text.delete(cmd.lineno);
|
|
247 }
|
|
248 cmd.setString(del);
|
333
|
249 }
|
315
|
250 forwardCommand(cmd);
|
297
|
251 break;
|
405
|
252 case REPCMD_NOP :
|
|
253 case REPCMD_INSERT_ACK :
|
|
254 case REPCMD_DELETE_ACK :
|
|
255 forwardCommand(cmd);
|
|
256 break;
|
|
257 case REPCMD_CLOSE :
|
|
258 case REPCMD_CLOSE_2 :
|
|
259 assert(false);
|
|
260 break;
|
313
|
261
|
405
|
262 case SMCMD_JOIN_ACK :
|
|
263 sid = cmd.sid;
|
|
264 eid = cmd.eid;
|
431
|
265 setName(name+eid);
|
|
266 name += "(sid="+sid+")";
|
405
|
267 inputLock = false;
|
|
268 break;
|
|
269 case SMCMD_PUT_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_QUIT :
|
|
277 if (cmd.eid!=eid)
|
413
|
278 quit = cmd;
|
|
279 else // eid=-1 means do not forward but send it.
|
|
280 quit = new REPCommand(REP.SMCMD_QUIT_2,
|
|
281 sid, -1, seq, 0, "");
|
|
282 timeout=1;
|
405
|
283 // stop input processing after this command
|
|
284 cmds.clear();
|
|
285 break;
|
|
286 case SMCMD_START_MERGE :
|
|
287 // lock user input during merge (optional)
|
|
288 inputLock = hasInputLock;
|
|
289 cmd.cmd = REP.SMCMD_START_MERGE_ACK;
|
427
|
290 ns.writeLog("BeforeMerge "+text);
|
405
|
291 sendCommand(cmd);
|
|
292 break;
|
|
293 case SMCMD_END_MERGE :
|
|
294 inputLock = false;
|
427
|
295 ns.writeLog("AfterMerge "+text);
|
405
|
296 break;
|
|
297 case SMCMD_QUIT_2 :
|
|
298 if (cmd.eid!=eid) {
|
|
299 forwardCommand(cmd);
|
|
300 } else {
|
|
301 cmd.cmd = REP.SMCMD_QUIT_2_ACK;
|
|
302 sendCommand(cmd);
|
|
303 }
|
|
304 running = false;
|
421
|
305 dumpText();
|
405
|
306 break;
|
|
307 case SMCMD_SYNC:
|
|
308 // start contents sync with newly joined editor
|
|
309 cmd.cmd = REP.SMCMD_SYNC_ACK;
|
|
310 forwardCommand(cmd);
|
|
311 //if (cmd.eid==eid) {
|
|
312 if (master && syncEnable ) {
|
|
313 syncCounter = 1;
|
|
314 timeout = 1;
|
|
315 }
|
|
316 break;
|
|
317 default:
|
|
318 assert(false);
|
|
319 break;
|
285
|
320 }
|
284
|
321 }
|
313
|
322
|
334
|
323
|
421
|
324 private void dumpText() {
|
|
325 ns.writeLog("Final "+text);
|
|
326 }
|
|
327
|
334
|
328 public int getPort() {
|
|
329 return port;
|
|
330 }
|
342
|
331
|
|
332 public synchronized void setCommand(LinkedList<REPCommand> cmds) {
|
|
333 this.cmds = cmds;
|
|
334 timeout=1;
|
|
335 if(selector!=null) selector.wakeup();
|
|
336 }
|
193
|
337 }
|