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