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