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