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)
|
424
|
129 selector.select(0);
|
298
|
130 } else if (selector.select(timeout)<=0) {
|
|
131 if (syncCounter>0) {
|
302
|
132 syncText(); // send the master editor buffer to clients.
|
298
|
133 }
|
288
|
134 userInput();
|
308
|
135 }
|
|
136 // selector(timeout) returns 0, but it may contain readable channel..
|
|
137 for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()) {
|
|
138 REPSocketChannel<REPCommand> ch = key.channel1();
|
|
139 handle(ch.read());
|
285
|
140 }
|
|
141 }
|
|
142 }
|
284
|
143
|
300
|
144 private void syncText() {
|
302
|
145 /*
|
|
146 * Send delete/insert one at a time to synchronize
|
|
147 * all clients. SYNC is requested by the session manager.
|
|
148 */
|
300
|
149 if (syncCounter>text.size()) {
|
407
|
150 SessionManager.logger.writeLog("Sync Completed.");
|
300
|
151 syncCounter=0;
|
|
152 } else {
|
411
|
153 if (inputLock) return;
|
300
|
154 int i=syncCounter-1;
|
400
|
155 REPCommand del = new REPCommand(REP.REPCMD_DELETE_USER,sid,eid,0,i, text.get(i));
|
|
156 REPCommand ins = new REPCommand(REP.REPCMD_INSERT_USER,sid,eid,0,i, text.get(i));
|
315
|
157 sendCommand(del);
|
|
158 sendCommand(ins);
|
300
|
159 syncCounter++;
|
|
160 }
|
|
161 }
|
|
162
|
302
|
163 /*
|
|
164 * Simulate User Input
|
|
165 */
|
288
|
166 private void userInput() {
|
|
167 REPCommand cmd = cmds.poll();
|
|
168 if (cmd!=null) {
|
297
|
169 switch(cmd.cmd) {
|
400
|
170 case REPCMD_INSERT_USER:
|
297
|
171 text.insert(cmd.lineno, cmd.string);
|
315
|
172 sendCommand(cmd);
|
297
|
173 break;
|
400
|
174 case REPCMD_DELETE_USER:
|
297
|
175 String del = text.delete(cmd.lineno);
|
|
176 cmd.setString(del);
|
315
|
177 sendCommand(cmd);
|
297
|
178 break;
|
|
179 case SMCMD_QUIT:
|
302
|
180 /*
|
|
181 * start termination phase 1 by the master editor.
|
|
182 * after this command do not send any user input.
|
|
183 * clients simply disconnect from the session manager.
|
|
184 */
|
297
|
185 cmds.clear();
|
413
|
186 cmd.eid = -1;
|
387
|
187 quit = cmd;
|
297
|
188 break;
|
299
|
189 case SMCMD_JOIN:
|
|
190 case SMCMD_PUT:
|
315
|
191 sendCommand(cmd);
|
302
|
192 /*
|
|
193 * To prevent confusion, stop user input until the ack
|
|
194 */
|
300
|
195 inputLock = true; // wait until ACK
|
299
|
196 break;
|
297
|
197 default:
|
|
198 assert(false);
|
|
199 }
|
288
|
200 } else {
|
387
|
201 if(syncCounter==0) {
|
386
|
202 // no more command to send, and we don't have syncCounter
|
|
203 timeout = 0;
|
387
|
204 if (quit!=null) {
|
413
|
205 if (quit.eid==-1)
|
|
206 sendCommand(quit);
|
|
207 else
|
|
208 forwardCommand(quit);
|
387
|
209 quit=null;
|
|
210 }
|
|
211 }
|
288
|
212 }
|
|
213 }
|
|
214
|
285
|
215
|
315
|
216 private void sendCommand(REPCommand cmd1) {
|
313
|
217 REPCommand cmd = new REPCommand(cmd1);
|
315
|
218 cmd.setSEQID(seq++);
|
285
|
219 cmd.setEID(eid);
|
|
220 cmd.setSID(sid);
|
297
|
221 ns.writeLog(name +" send "+cmd);
|
285
|
222 channel.write(cmd);
|
|
223 }
|
|
224
|
315
|
225 private void forwardCommand(REPCommand cmd1) {
|
|
226 REPCommand cmd = new REPCommand(cmd1);
|
|
227 ns.writeLog(name +" forward "+cmd);
|
|
228 channel.write(cmd);
|
|
229 }
|
|
230
|
285
|
231 private void handle(REPCommand cmd) {
|
308
|
232 if (cmd==null) return;
|
407
|
233 ns.writeLog(name +": read "+cmd + " textsize="+text.size());
|
285
|
234 switch(cmd.cmd) {
|
297
|
235 case REPCMD_INSERT :
|
405
|
236 if (cmd.eid!=eid) {
|
|
237 text.insert(cmd.lineno, cmd.string);
|
|
238 }
|
315
|
239 forwardCommand(cmd);
|
297
|
240 break;
|
|
241 case REPCMD_DELETE :
|
405
|
242 if (cmd.eid!=eid) {
|
|
243 String del="";
|
407
|
244 if(cmd.lineno<text.size()) {
|
405
|
245 del = text.delete(cmd.lineno);
|
|
246 }
|
|
247 cmd.setString(del);
|
333
|
248 }
|
315
|
249 forwardCommand(cmd);
|
297
|
250 break;
|
405
|
251 case REPCMD_NOP :
|
|
252 case REPCMD_INSERT_ACK :
|
|
253 case REPCMD_DELETE_ACK :
|
|
254 forwardCommand(cmd);
|
|
255 break;
|
|
256 case REPCMD_CLOSE :
|
|
257 case REPCMD_CLOSE_2 :
|
|
258 assert(false);
|
|
259 break;
|
313
|
260
|
405
|
261 case SMCMD_JOIN_ACK :
|
|
262 sid = cmd.sid;
|
|
263 eid = cmd.eid;
|
|
264 name += "(eid="+eid+",sid="+sid+")";
|
|
265 inputLock = false;
|
|
266 break;
|
|
267 case SMCMD_PUT_ACK :
|
|
268 sid = cmd.sid;
|
|
269 eid = cmd.eid;
|
|
270 name += "(eid="+eid+",sid="+sid+")";
|
|
271 inputLock = false;
|
|
272 break;
|
|
273 case SMCMD_QUIT :
|
|
274 if (cmd.eid!=eid)
|
413
|
275 quit = cmd;
|
|
276 else // eid=-1 means do not forward but send it.
|
|
277 quit = new REPCommand(REP.SMCMD_QUIT_2,
|
|
278 sid, -1, seq, 0, "");
|
|
279 timeout=1;
|
405
|
280 // stop input processing after this command
|
|
281 cmds.clear();
|
|
282 break;
|
|
283 case SMCMD_START_MERGE :
|
|
284 // lock user input during merge (optional)
|
|
285 inputLock = hasInputLock;
|
|
286 cmd.cmd = REP.SMCMD_START_MERGE_ACK;
|
|
287 sendCommand(cmd);
|
|
288 break;
|
|
289 case SMCMD_END_MERGE :
|
|
290 inputLock = false;
|
|
291 break;
|
|
292 case SMCMD_QUIT_2 :
|
|
293 if (cmd.eid!=eid) {
|
|
294 forwardCommand(cmd);
|
|
295 } else {
|
|
296 cmd.cmd = REP.SMCMD_QUIT_2_ACK;
|
|
297 sendCommand(cmd);
|
|
298 }
|
|
299 running = false;
|
421
|
300 dumpText();
|
405
|
301 break;
|
|
302 case SMCMD_SYNC:
|
|
303 // start contents sync with newly joined editor
|
|
304 cmd.cmd = REP.SMCMD_SYNC_ACK;
|
|
305 forwardCommand(cmd);
|
|
306 //if (cmd.eid==eid) {
|
|
307 if (master && syncEnable ) {
|
|
308 syncCounter = 1;
|
|
309 timeout = 1;
|
|
310 }
|
|
311 break;
|
|
312 default:
|
|
313 assert(false);
|
|
314 break;
|
285
|
315 }
|
284
|
316 }
|
313
|
317
|
334
|
318
|
421
|
319 private void dumpText() {
|
|
320 ns.writeLog("Final "+text);
|
|
321 }
|
|
322
|
334
|
323 public int getPort() {
|
|
324 return port;
|
|
325 }
|
342
|
326
|
|
327 public synchronized void setCommand(LinkedList<REPCommand> cmds) {
|
|
328 this.cmds = cmds;
|
|
329 timeout=1;
|
|
330 if(selector!=null) selector.wakeup();
|
|
331 }
|
193
|
332 }
|