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