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