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;
|
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 = {
|
386
|
52 "aaa", "bbb", // "ccc", "ddd", "eee",
|
303
|
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"));
|
387
|
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);
|
386
|
85 } else {
|
|
86 text = new Text(new String[0]);
|
285
|
87 }
|
193
|
88 }
|
|
89
|
|
90 public void run(){
|
302
|
91 /*
|
|
92 * Create Socket and connect to the session manager
|
|
93 */
|
193
|
94 try {
|
|
95 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
|
285
|
96 } catch (IOException e) { return; }
|
193
|
97
|
297
|
98 ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" "+name, 1);
|
285
|
99 try {
|
193
|
100 while (!channel.connect(semaIP)){
|
|
101 ns.writeLog("SeMa not listen to socket yet, wait", 1);
|
|
102 }
|
285
|
103 } catch (IOException e) { return; }
|
297
|
104 ns.writeLog("successes to connect "+name);
|
285
|
105 /*
|
302
|
106 * Start editor main loop
|
285
|
107 * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string)
|
|
108 */
|
|
109 try {
|
|
110 mainloop();
|
193
|
111 } catch (IOException e) {
|
|
112 }
|
|
113 }
|
284
|
114
|
302
|
115 /*
|
|
116 * Editor main loop with input lock
|
|
117 */
|
285
|
118 private void mainloop() throws IOException {
|
|
119
|
|
120 channel.configureBlocking(false);
|
|
121 REPSelector<REPCommand> selector = REPSelector.create();
|
|
122 channel.register(selector, SelectionKey.OP_READ);
|
342
|
123 this.selector = selector;
|
285
|
124 while(running) {
|
298
|
125 if (inputLock) {
|
|
126 // No user input during merge mode (optional)
|
|
127 if (selector.select(0)>0) {
|
|
128 handle(channel.read());
|
|
129 }
|
|
130 continue;
|
|
131 } else if (selector.select(timeout)<=0) {
|
|
132 if (syncCounter>0) {
|
302
|
133 syncText(); // send the master editor buffer to clients.
|
298
|
134 }
|
288
|
135 userInput();
|
308
|
136 }
|
|
137 // selector(timeout) returns 0, but it may contain readable channel..
|
|
138 for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()) {
|
|
139 REPSocketChannel<REPCommand> ch = key.channel1();
|
|
140 handle(ch.read());
|
285
|
141 }
|
|
142 }
|
|
143 }
|
284
|
144
|
300
|
145 private void syncText() {
|
302
|
146 /*
|
|
147 * Send delete/insert one at a time to synchronize
|
|
148 * all clients. SYNC is requested by the session manager.
|
|
149 */
|
300
|
150 if (syncCounter>text.size()) {
|
|
151 syncCounter=0;
|
|
152 } else {
|
|
153 int i=syncCounter-1;
|
|
154 REPCommand del = new REPCommand(REP.REPCMD_DELETE,sid,eid,0,i, text.get(i));
|
|
155 REPCommand ins = new REPCommand(REP.REPCMD_INSERT,sid,eid,0,i, text.get(i));
|
315
|
156 sendCommand(del);
|
|
157 sendCommand(ins);
|
300
|
158 syncCounter++;
|
|
159 }
|
|
160 }
|
|
161
|
302
|
162 /*
|
|
163 * Simulate User Input
|
|
164 */
|
288
|
165 private void userInput() {
|
|
166 REPCommand cmd = cmds.poll();
|
|
167 if (cmd!=null) {
|
297
|
168 switch(cmd.cmd) {
|
|
169 case REPCMD_INSERT:
|
|
170 text.insert(cmd.lineno, cmd.string);
|
315
|
171 sendCommand(cmd);
|
297
|
172 break;
|
|
173 case REPCMD_DELETE:
|
|
174 String del = text.delete(cmd.lineno);
|
|
175 cmd.setString(del);
|
315
|
176 sendCommand(cmd);
|
297
|
177 break;
|
|
178 case SMCMD_QUIT:
|
302
|
179 /*
|
|
180 * start termination phase 1 by the master editor.
|
|
181 * after this command do not send any user input.
|
|
182 * clients simply disconnect from the session manager.
|
|
183 */
|
297
|
184 cmds.clear();
|
387
|
185 quit = cmd;
|
297
|
186 break;
|
299
|
187 case SMCMD_JOIN:
|
|
188 case SMCMD_PUT:
|
315
|
189 sendCommand(cmd);
|
302
|
190 /*
|
|
191 * To prevent confusion, stop user input until the ack
|
|
192 */
|
300
|
193 inputLock = true; // wait until ACK
|
299
|
194 break;
|
297
|
195 default:
|
|
196 assert(false);
|
|
197 }
|
288
|
198 } else {
|
387
|
199 if(syncCounter==0) {
|
386
|
200 // no more command to send, and we don't have syncCounter
|
|
201 timeout = 0;
|
387
|
202 if (quit!=null) {
|
|
203 sendCommand(quit);
|
|
204 quit=null;
|
|
205 }
|
|
206 }
|
288
|
207 }
|
|
208 }
|
|
209
|
285
|
210
|
315
|
211 private void sendCommand(REPCommand cmd1) {
|
313
|
212 REPCommand cmd = new REPCommand(cmd1);
|
315
|
213 cmd.setSEQID(seq++);
|
285
|
214 cmd.setEID(eid);
|
|
215 cmd.setSID(sid);
|
297
|
216 ns.writeLog(name +" send "+cmd);
|
285
|
217 channel.write(cmd);
|
|
218 }
|
|
219
|
315
|
220 private void forwardCommand(REPCommand cmd1) {
|
|
221 REPCommand cmd = new REPCommand(cmd1);
|
|
222 ns.writeLog(name +" forward "+cmd);
|
|
223 channel.write(cmd);
|
|
224 }
|
|
225
|
285
|
226 private void handle(REPCommand cmd) {
|
308
|
227 if (cmd==null) return;
|
297
|
228 ns.writeLog(name +": read "+cmd);
|
285
|
229 switch(cmd.cmd) {
|
297
|
230 case REPCMD_INSERT :
|
|
231 text.insert(cmd.lineno, cmd.string);
|
315
|
232 forwardCommand(cmd);
|
297
|
233 break;
|
|
234 case REPCMD_DELETE :
|
333
|
235 String del="";
|
|
236 if(cmd.lineno>text.size()) {
|
|
237 del = text.delete(cmd.lineno);
|
|
238 }
|
297
|
239 cmd.setString(del);
|
315
|
240 forwardCommand(cmd);
|
297
|
241 break;
|
313
|
242 case REPCMD_NOP :
|
395
|
243 case REPCMD_INSERT_ACK :
|
|
244 case REPCMD_DELETE_ACK :
|
315
|
245 forwardCommand(cmd);
|
387
|
246 break;
|
|
247 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_START_MERGE :
|
298
|
274 // lock user input during merge (optional)
|
|
275 inputLock = hasInputLock;
|
|
276 cmd.cmd = REP.SMCMD_START_MERGE_ACK;
|
315
|
277 sendCommand(cmd);
|
298
|
278 break;
|
286
|
279 case SMCMD_END_MERGE :
|
298
|
280 inputLock = false;
|
286
|
281 break;
|
|
282 case SMCMD_QUIT_2 :
|
303
|
283 if (cmd.eid!=eid) {
|
315
|
284 forwardCommand(cmd);
|
341
|
285 } else {
|
|
286 cmd.cmd = REP.SMCMD_QUIT_2_ACK;
|
|
287 sendCommand(cmd);
|
303
|
288 }
|
286
|
289 running = false;
|
|
290 break;
|
298
|
291 case SMCMD_SYNC:
|
|
292 // start contents sync with newly joined editor
|
315
|
293 cmd.cmd = REP.SMCMD_SYNC_ACK;
|
|
294 forwardCommand(cmd);
|
386
|
295 //if (cmd.eid==eid) {
|
|
296 if (master) {
|
302
|
297 syncCounter = 1;
|
386
|
298 timeout = 1;
|
|
299 }
|
298
|
300 break;
|
286
|
301 default:
|
|
302 assert(false);
|
|
303 break;
|
285
|
304 }
|
284
|
305 }
|
313
|
306
|
334
|
307
|
|
308 public int getPort() {
|
|
309 return port;
|
|
310 }
|
342
|
311
|
|
312 public synchronized void setCommand(LinkedList<REPCommand> cmds) {
|
|
313 this.cmds = cmds;
|
|
314 timeout=1;
|
|
315 if(selector!=null) selector.wakeup();
|
|
316 }
|
193
|
317 }
|