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