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