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