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(){
|
|
70 try {
|
|
71 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
|
285
|
72 } catch (IOException e) { return; }
|
193
|
73
|
297
|
74 ns.writeLog("try to connect to SessionManager whose ip is "+semaIP+" "+name, 1);
|
285
|
75 try {
|
193
|
76 while (!channel.connect(semaIP)){
|
|
77 ns.writeLog("SeMa not listen to socket yet, wait", 1);
|
|
78 }
|
285
|
79 } catch (IOException e) { return; }
|
297
|
80 ns.writeLog("successes to connect "+name);
|
285
|
81 /*
|
|
82 * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string)
|
|
83 */
|
|
84 try {
|
|
85 mainloop();
|
193
|
86 } catch (IOException e) {
|
|
87 }
|
|
88 }
|
284
|
89
|
285
|
90 private void mainloop() throws IOException {
|
|
91
|
|
92 channel.configureBlocking(false);
|
|
93 REPSelector<REPCommand> selector = REPSelector.create();
|
|
94 channel.register(selector, SelectionKey.OP_READ);
|
|
95 while(running) {
|
298
|
96 if (inputLock) {
|
|
97 // No user input during merge mode (optional)
|
|
98 if (selector.select(0)>0) {
|
|
99 handle(channel.read());
|
|
100 }
|
|
101 continue;
|
|
102 } else if (selector.select(timeout)<=0) {
|
|
103 if (syncCounter>0) {
|
|
104 if (syncCounter>text.size()) {
|
|
105 syncCounter=0;
|
|
106 } else {
|
|
107 int i=syncCounter-1;
|
|
108 REPCommand del = new REPCommand(REP.REPCMD_DELETE,sid,eid,0,i, text.get(i));
|
|
109 REPCommand ins = new REPCommand(REP.REPCMD_INSERT,sid,eid,0,i, text.get(i));
|
|
110 sendCommand(del);
|
|
111 sendCommand(ins);
|
|
112 syncCounter++;
|
|
113 }
|
|
114 }
|
288
|
115 userInput();
|
285
|
116 } else {
|
|
117 handle(channel.read());
|
|
118 }
|
|
119 }
|
|
120 }
|
284
|
121
|
288
|
122 private void userInput() {
|
|
123 REPCommand cmd = cmds.poll();
|
|
124 if (cmd!=null) {
|
297
|
125 switch(cmd.cmd) {
|
|
126 case REPCMD_INSERT:
|
|
127 text.insert(cmd.lineno, cmd.string);
|
|
128 sendCommand(cmd);
|
|
129 break;
|
|
130 case REPCMD_DELETE:
|
|
131 String del = text.delete(cmd.lineno);
|
|
132 cmd.setString(del);
|
|
133 sendCommand(cmd);
|
|
134 break;
|
|
135 case SMCMD_QUIT:
|
|
136 cmds.clear();
|
|
137 sendCommand(cmd);
|
|
138 break;
|
|
139 default:
|
|
140 assert(false);
|
|
141 }
|
288
|
142 } else {
|
|
143 // no more command to send
|
|
144 timeout = 0;
|
|
145 }
|
|
146 }
|
|
147
|
285
|
148
|
|
149 private void sendCommand(REPCommand cmd) {
|
|
150 cmd.setSEQID(seq++);
|
|
151 cmd.setEID(eid);
|
|
152 cmd.setSID(sid);
|
297
|
153 ns.writeLog(name +" send "+cmd);
|
285
|
154 channel.write(cmd);
|
|
155 }
|
|
156
|
|
157 private void handle(REPCommand cmd) {
|
297
|
158 ns.writeLog(name +": read "+cmd);
|
285
|
159 switch(cmd.cmd) {
|
297
|
160 case REPCMD_INSERT :
|
|
161 text.insert(cmd.lineno, cmd.string);
|
|
162 sendCommand(cmd);
|
298
|
163 sendCommand(nop);
|
297
|
164 break;
|
|
165 case REPCMD_INSERT_ACK :
|
298
|
166 assert(false);
|
297
|
167 break;
|
|
168 case REPCMD_DELETE :
|
|
169 String del = text.delete(cmd.lineno);
|
|
170 cmd.setString(del);
|
|
171 sendCommand(cmd);
|
298
|
172 sendCommand(nop);
|
297
|
173 break;
|
286
|
174 case REPCMD_DELETE_ACK :
|
298
|
175 assert(false);
|
286
|
176 break;
|
|
177 case REPCMD_CLOSE :
|
|
178 case REPCMD_CLOSE_2 :
|
298
|
179 assert(false);
|
286
|
180 break;
|
|
181 case REPCMD_NOP :
|
298
|
182 sendCommand(cmd);
|
|
183 sendCommand(nop);
|
286
|
184 break;
|
|
185 case SMCMD_JOIN_ACK :
|
|
186 sid = cmd.sid;
|
|
187 eid = cmd.eid;
|
|
188 break;
|
|
189 case SMCMD_PUT_ACK :
|
|
190 sid = cmd.sid;
|
|
191 eid = cmd.eid;
|
|
192 break;
|
|
193 case SMCMD_QUIT :
|
298
|
194 sendCommand(cmd);
|
|
195 cmds.clear();
|
|
196 break;
|
286
|
197 case SMCMD_QUIT_ACK :
|
298
|
198 assert(false);
|
286
|
199 break;
|
|
200 case SMCMD_START_MERGE :
|
298
|
201 // lock user input during merge (optional)
|
|
202 inputLock = hasInputLock;
|
|
203 cmd.cmd = REP.SMCMD_START_MERGE_ACK;
|
|
204 sendCommand(cmd);
|
|
205 break;
|
286
|
206 case SMCMD_START_MERGE_ACK :
|
298
|
207 assert(false);
|
286
|
208 break;
|
|
209 case SMCMD_END_MERGE :
|
298
|
210 inputLock = false;
|
286
|
211 break;
|
|
212 case SMCMD_QUIT_2 :
|
298
|
213 sendCommand(cmd);
|
286
|
214 running = false;
|
|
215 break;
|
298
|
216 case SMCMD_SYNC:
|
|
217 // start contents sync with newly joined editor
|
|
218 cmd.cmd = REP.SMCMD_SYNC_ACK; sendCommand(cmd);
|
|
219 syncCounter = 1;
|
|
220 break;
|
286
|
221 default:
|
|
222 assert(false);
|
|
223 break;
|
285
|
224 }
|
284
|
225 }
|
193
|
226 }
|