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