comparison test/editortest/TestEditor2.java @ 414:784a4d67e6a5

(no commit message)
author one
date Tue, 09 Dec 2008 15:44:28 +0900
parents
children 648c676bf9df
comparison
equal deleted inserted replaced
413:1cdbdeedc5b7 414:784a4d67e6a5
1 package test.editortest;
2
3 import java.io.IOException;
4 import java.net.InetSocketAddress;
5 import java.nio.channels.SelectionKey;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import rep.REP;
10 import rep.REPCommand;
11 import rep.REPCommandPacker;
12 import rep.SessionManager;
13 import rep.channel.REPLogger;
14 import rep.channel.REPSelectionKey;
15 import rep.channel.REPSelector;
16 import rep.channel.REPSocketChannel;
17 import test.Text;
18
19
20 /**
21 * @author kono
22 * Basic Remote Editor client implementation
23 * should support multi-session
24 * currently multi-session requires new channel, that is
25 * only one session for this editor.
26 */
27 public class TestEditor2 extends Thread{
28 private InetSocketAddress semaIP;
29 private REPLogger ns;
30 private int seq = 0;
31 public Text text;
32 public LinkedList<REPCommand> cmds;
33 private int eid = 0;
34 private int sid = 0;
35 REPSocketChannel<REPCommand> channel;
36 REPCommand nop = new REPCommand(REP.REPCMD_NOP, 0, 0, 0, 0, "");
37 boolean running = true;
38 long timeout = 1;
39 private String name;
40 private boolean inputLock=false;
41 public boolean detached=false;
42 public boolean master=false;
43 REPCommand quit=null;
44 private int syncCounter=0;
45 private boolean hasInputLock=false;
46 private int port;
47 private REPSelector<REPCommand> selector;
48 private boolean syncEnable=true;
49 private LogTarget logTarget;
50 private REPText repText;
51 private REPSimpleEditor simpleEditor;
52
53
54 public TestEditor2(String name, String _host,int _port, boolean master){
55 super(name);
56 LinkedList<REPCommand>cmdList = new LinkedList<REPCommand>();
57 String[] txts = {
58 "aaa", "bbb", // "ccc", "ddd", "eee",
59 };
60 port = _port;
61 semaIP = new InetSocketAddress(_host, _port);
62 ns = REPLogger.singleton();
63 this.name = name;
64 cmds = cmdList;
65 if (master) {
66 this.master=true;
67 text = new Text(txts);
68 cmds.add(new REPCommand(REP.SMCMD_PUT,0,0,0,0,name+"-file"));
69 cmds.add(new REPCommand(REP.REPCMD_INSERT_USER,0,0,0,0,"m0"));
70 cmds.add(new REPCommand(REP.REPCMD_DELETE_USER,0,0,0,0,"m0"));
71 cmds.add(new REPCommand(REP.SMCMD_QUIT,0,0,0,0,""));
72 } else {
73 text = new Text(new String[0]);
74 cmds.add(new REPCommand(REP.SMCMD_JOIN,0,0,0,0,name));
75 cmds.add(new REPCommand(REP.REPCMD_INSERT_USER,0,0,0,0,"c0"));
76 cmds.add(new REPCommand(REP.REPCMD_DELETE_USER,0,0,0,0,"c0"));
77 }
78
79 simpleEditor = new REPSimpleEditor();
80 simpleEditor.setDefaultCloseOperation(REPSimpleEditor.EXIT_ON_CLOSE);
81 simpleEditor.setVisible(true);
82 simpleEditor.setText(text);
83 repText = simpleEditor.getREPText();
84 logTarget = simpleEditor;
85 }
86
87 public TestEditor2(String name, String _host,int _port, boolean master,
88 String[] txts,LinkedList<REPCommand> cmdList){
89 super(name);
90 port = _port;
91 semaIP = new InetSocketAddress(_host, _port);
92 ns = REPLogger.singleton();
93 this.name = name;
94 cmds = cmdList;
95 if (master) {
96 this.master=true;
97 text = new Text(txts);
98 } else {
99 text = new Text(new String[0]);
100 }
101 }
102
103 public void run(){
104 /*
105 * Create Socket and connect to the session manager
106 */
107 try {
108 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
109 } catch (IOException e) { return; }
110
111 ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" "+name, 1);
112 try {
113 while (!channel.connect(semaIP)){
114 ns.writeLog("SeMa not listen to socket yet, wait", 1);
115 }
116 } catch (IOException e) { return; }
117 ns.writeLog("successes to connect "+name);
118 /*
119 * Start editor main loop
120 * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string)
121 */
122 try {
123 mainloop();
124 } catch (IOException e) {
125 }
126 }
127
128 /*
129 * Editor main loop with input lock
130 */
131 private void mainloop() throws IOException {
132
133 channel.configureBlocking(false);
134 REPSelector<REPCommand> selector = REPSelector.create();
135 channel.register(selector, SelectionKey.OP_READ);
136 this.selector = selector;
137 while(running) {
138 if (inputLock) {
139 // No user input during merge mode (optional)
140 if (selector.select(0)>0) {
141 handle(channel.read());
142 }
143 continue;
144 } else if (selector.select(timeout)<=0) {
145 if (syncCounter>0) {
146 syncText(); // send the master editor buffer to clients.
147 }
148 userInput();
149 }
150 // selector(timeout) returns 0, but it may contain readable channel..
151 for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()) {
152 REPSocketChannel<REPCommand> ch = key.channel1();
153 handle(ch.read());
154 }
155 }
156 }
157
158 private void syncText() {
159 /*
160 * Send delete/insert one at a time to synchronize
161 * all clients. SYNC is requested by the session manager.
162 */
163 if (syncCounter>text.size()) {
164 SessionManager.logger.writeLog("Sync Completed.");
165 syncCounter=0;
166 } else {
167 if (inputLock) return;
168 int i=syncCounter-1;
169 REPCommand del = new REPCommand(REP.REPCMD_DELETE_USER,sid,eid,0,i, text.get(i));
170 REPCommand ins = new REPCommand(REP.REPCMD_INSERT_USER,sid,eid,0,i, text.get(i));
171 sendCommand(del);
172 sendCommand(ins);
173 syncCounter++;
174 }
175 }
176
177 /*
178 * Simulate User Input
179 */
180 private void userInput() {
181 REPCommand cmd = cmds.poll();
182 if (cmd!=null) {
183 switch(cmd.cmd) {
184 case REPCMD_INSERT_USER:
185 text.insert(cmd.lineno, cmd.string);
186 repText.insert(cmd.lineno, cmd.string);
187 sendCommand(cmd);
188 break;
189 case REPCMD_DELETE_USER:
190 String del = text.delete(cmd.lineno);
191 del = repText.delete(cmd.lineno, cmd.string);
192 cmd.setString(del);
193 sendCommand(cmd);
194 break;
195 case SMCMD_QUIT:
196 /*
197 * start termination phase 1 by the master editor.
198 * after this command do not send any user input.
199 * clients simply disconnect from the session manager.
200 */
201 cmds.clear();
202 cmd.eid = -1;
203 quit = cmd;
204 break;
205 case SMCMD_JOIN:
206 case SMCMD_PUT:
207 sendCommand(cmd);
208 /*
209 * To prevent confusion, stop user input until the ack
210 */
211 inputLock = true; // wait until ACK
212 break;
213 default:
214 assert(false);
215 }
216 } else {
217 if(syncCounter==0) {
218 // no more command to send, and we don't have syncCounter
219 timeout = 0;
220 if (quit!=null) {
221 if (quit.eid==-1)
222 sendCommand(quit);
223 else
224 forwardCommand(quit);
225 quit=null;
226 }
227 }
228 }
229 }
230
231
232 private void sendCommand(REPCommand cmd1) {
233 REPCommand cmd = new REPCommand(cmd1);
234 cmd.setSEQID(seq++);
235 cmd.setEID(eid);
236 cmd.setSID(sid);
237 ns.writeLog(name +" send "+cmd);
238 channel.write(cmd);
239 }
240
241 private void forwardCommand(REPCommand cmd1) {
242 REPCommand cmd = new REPCommand(cmd1);
243 ns.writeLog(name +" forward "+cmd);
244 Logger.print(logTarget, "write : " + cmd);
245 channel.write(cmd);
246 }
247
248 private void handle(REPCommand cmd) {
249 if (cmd==null) return;
250 ns.writeLog(name +": read "+cmd + " textsize="+text.size());
251 Logger.print(logTarget, "read : " + cmd);
252 switch(cmd.cmd) {
253 case REPCMD_INSERT :
254 if (cmd.eid!=eid) {
255 text.insert(cmd.lineno, cmd.string);
256 repText.insert(cmd.lineno, cmd.string);
257 }
258 forwardCommand(cmd);
259 break;
260 case REPCMD_DELETE :
261 if (cmd.eid!=eid) {
262 String del="";
263 if(cmd.lineno<text.size()) {
264 del = text.delete(cmd.lineno);
265 del = repText.delete(cmd.lineno, cmd.string);
266 }
267 cmd.setString(del);
268 }
269 forwardCommand(cmd);
270 break;
271 case REPCMD_NOP :
272 case REPCMD_INSERT_ACK :
273 case REPCMD_DELETE_ACK :
274 forwardCommand(cmd);
275 break;
276 case REPCMD_CLOSE :
277 case REPCMD_CLOSE_2 :
278 assert(false);
279 break;
280
281 case SMCMD_JOIN_ACK :
282 sid = cmd.sid;
283 eid = cmd.eid;
284 name += "(eid="+eid+",sid="+sid+")";
285 inputLock = false;
286 break;
287 case SMCMD_PUT_ACK :
288 sid = cmd.sid;
289 eid = cmd.eid;
290 name += "(eid="+eid+",sid="+sid+")";
291 inputLock = false;
292 break;
293 case SMCMD_QUIT :
294 if (cmd.eid!=eid)
295 quit = cmd;
296 else // eid=-1 means do not forward but send it.
297 quit = new REPCommand(REP.SMCMD_QUIT_2,
298 sid, -1, seq, 0, "");
299 timeout=1;
300 // stop input processing after this command
301 cmds.clear();
302 break;
303 case SMCMD_START_MERGE :
304 // lock user input during merge (optional)
305 inputLock = hasInputLock;
306 cmd.cmd = REP.SMCMD_START_MERGE_ACK;
307 sendCommand(cmd);
308 break;
309 case SMCMD_END_MERGE :
310 inputLock = false;
311 break;
312 case SMCMD_QUIT_2 :
313 if (cmd.eid!=eid) {
314 forwardCommand(cmd);
315 } else {
316 cmd.cmd = REP.SMCMD_QUIT_2_ACK;
317 sendCommand(cmd);
318 }
319 running = false;
320 break;
321 case SMCMD_SYNC:
322 // start contents sync with newly joined editor
323 cmd.cmd = REP.SMCMD_SYNC_ACK;
324 forwardCommand(cmd);
325 //if (cmd.eid==eid) {
326 if (master && syncEnable ) {
327 syncCounter = 1;
328 timeout = 1;
329 }
330 break;
331 default:
332 assert(false);
333 break;
334 }
335 }
336
337
338 public int getPort() {
339 return port;
340 }
341
342 public synchronized void setCommand(LinkedList<REPCommand> cmds) {
343 this.cmds = cmds;
344 timeout=1;
345 if(selector!=null) selector.wakeup();
346 }
347
348 public void setText(List<String> list) {
349 text = new Text(list);
350 }
351
352 public void setLogTarget(LogTarget target) {
353 logTarget = target;
354 }
355
356 public void setREPText(REPText repText) {
357 this.repText = repText;
358 }
359 }