193
|
1 package remoteeditor.editors;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.InetSocketAddress;
|
|
5 import java.nio.channels.SelectionKey;
|
|
6 import java.util.LinkedList;
|
|
7 import rep.REP;
|
|
8 import rep.REPCommand;
|
|
9 import rep.REPCommandPacker;
|
|
10 import rep.channel.REPSelectionKey;
|
|
11 import rep.channel.REPSelector;
|
|
12 import rep.channel.REPSocketChannel;
|
|
13
|
|
14
|
|
15 public class REPEditor extends Thread implements REPTextListener{
|
|
16
|
|
17 private REPSocketChannel<REPCommand> channel;
|
|
18 REPSelector<REPCommand> selector;
|
|
19 private boolean running = true;
|
|
20 private boolean inputLock = false;
|
|
21 private long timeout = 1;
|
|
22 private int syncCounter = 0;
|
|
23 private LinkedList<REPCommand> userCommand = new LinkedList<REPCommand>();
|
|
24 private LinkedList<Runnable> runners = new LinkedList<Runnable>();
|
|
25 private String name = "test";
|
|
26 private int seq;
|
|
27 private int eid;
|
|
28 private int sid;
|
|
29 private REPText repText;
|
196
|
30 private boolean hasInputLock = true;
|
193
|
31 private boolean master;
|
|
32 private boolean syncEnable = true;
|
209
|
33 private REPCommand quit = null;
|
193
|
34
|
|
35 public REPEditor(REPText repText, boolean master){
|
|
36 this.repText = repText;
|
|
37 this.master = master;
|
|
38 repText.addTextListener(this);
|
|
39 }
|
|
40
|
|
41 public void textDeleted(REPTextEvent event) {
|
|
42 Logger.print(event.getText());
|
|
43 addUserInput(new REPCommand(REP.REPCMD_DELETE_USER, 0, 0, 0, event.getLineno(), event.getText()));
|
|
44 }
|
|
45
|
|
46 public void textInserted(REPTextEvent event) {
|
|
47 Logger.print(event.getText());
|
|
48 addUserInput(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, event.getLineno(), event.getText()));
|
|
49 }
|
|
50
|
|
51 private void addUserInput(final REPCommand command) {
|
|
52 Runnable runner = new Runnable(){
|
|
53 public void run(){
|
|
54 userCommand.add(command);
|
|
55 timeout = 1;
|
|
56 }
|
|
57 };
|
|
58 synchronized(runners){
|
|
59 runners.add(runner);
|
|
60 }
|
|
61 if(selector != null){
|
|
62 selector.wakeup();
|
|
63 }
|
|
64 }
|
|
65
|
|
66 public void run(){
|
|
67 /*
|
|
68 * Create Socket and connect to the session manager
|
|
69 */
|
|
70 try {
|
|
71 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
|
|
72 } catch (IOException e) {
|
|
73 e.printStackTrace();
|
|
74 return;
|
|
75 }
|
|
76 try {
|
|
77 InetSocketAddress semaIP = new InetSocketAddress("localhost", 8766);
|
|
78 while (!channel.connect(semaIP)){
|
|
79 Logger.print("SeMa not listen to socket yet, wait");
|
|
80 }
|
|
81 } catch (IOException e) {
|
|
82 e.printStackTrace();
|
|
83 return;
|
|
84 }
|
|
85 /*
|
|
86 * Start editor main loop
|
|
87 * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string)
|
|
88 */
|
|
89 try {
|
|
90 mainloop();
|
|
91 } catch (IOException e) {
|
|
92 }
|
|
93 }
|
|
94
|
|
95 /*
|
|
96 * Editor main loop with input lock
|
|
97 */
|
196
|
98 void mainloop() throws IOException {
|
193
|
99
|
|
100 channel.configureBlocking(false);
|
|
101 selector = REPSelector.create();
|
|
102 channel.register(selector, SelectionKey.OP_READ);
|
|
103 while(running) {
|
|
104
|
|
105 synchronized(runners){
|
|
106 for(Runnable runner : runners){
|
|
107 runner.run();
|
|
108 }
|
|
109 runners.clear();
|
|
110 }
|
|
111
|
196
|
112 // if(inputLock){
|
|
113 if (repText.isMerging()) {
|
193
|
114 // No user input during merge mode (optional)
|
|
115 if (selector.select(0)>0) {
|
|
116 handle(channel.read());
|
|
117 }
|
|
118 continue;
|
|
119 } else if (selector.select(timeout)<=0) {
|
|
120 if (syncCounter>0) {
|
|
121 syncText(); // send the master editor buffer to clients.
|
|
122 }
|
|
123 userInput();
|
|
124 }
|
|
125 // selector(timeout) returns 0, but it may contain readable channel..
|
|
126 for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()) {
|
|
127 REPSocketChannel<REPCommand> ch = key.channel1();
|
|
128 handle(ch.read());
|
|
129 }
|
|
130 }
|
|
131 }
|
|
132
|
209
|
133
|
193
|
134
|
|
135 private void userInput() {
|
|
136 REPCommand command = userCommand.poll();
|
|
137 if(command != null){
|
|
138 switch(command.cmd){
|
|
139 case REPCMD_DELETE_USER:
|
209
|
140 sendCommand(command);
|
193
|
141 break;
|
|
142 case REPCMD_INSERT_USER:
|
209
|
143 sendCommand(command);
|
193
|
144 break;
|
|
145 case SMCMD_PUT:
|
|
146 case SMCMD_JOIN:
|
209
|
147 sendCommand(command);
|
193
|
148 break;
|
|
149 }
|
|
150 }else{
|
|
151 if(syncCounter == 0){
|
|
152 timeout = 0;
|
|
153 }
|
|
154 }
|
|
155 }
|
|
156
|
209
|
157 private void handle(REPCommand cmd) {
|
|
158 if (cmd==null) return;
|
|
159 switch(cmd.cmd) {
|
|
160 case REPCMD_INSERT :
|
|
161 if (cmd.eid!=eid) {
|
|
162 repText.insert(cmd.lineno, cmd.string);
|
|
163 }
|
|
164 forwardCommand(cmd);
|
|
165 break;
|
|
166 case REPCMD_DELETE :
|
|
167 if (cmd.eid!=eid) {
|
|
168 String del="";
|
|
169 if(cmd.lineno<repText.size()) {
|
|
170 del = repText.delete(cmd.lineno);
|
|
171 }
|
|
172 cmd.setString(del);
|
|
173 }
|
|
174 forwardCommand(cmd);
|
|
175 break;
|
|
176 case REPCMD_NOP :
|
|
177 case REPCMD_MERGE_MARK :
|
|
178 case REPCMD_INSERT_ACK :
|
|
179 case REPCMD_DELETE_ACK :
|
|
180 forwardCommand(cmd);
|
|
181 break;
|
|
182 case REPCMD_CLOSE :
|
|
183 case REPCMD_CLOSE_2 :
|
|
184 assert(false);
|
|
185 break;
|
|
186
|
|
187 case SMCMD_JOIN_ACK :
|
|
188 sid = cmd.sid;
|
|
189 eid = cmd.eid;
|
|
190 setName(name+eid);
|
|
191 name += "(sid="+sid+")";
|
|
192 inputLock = false;
|
|
193 break;
|
|
194 case SMCMD_PUT_ACK :
|
|
195 sid = cmd.sid;
|
|
196 eid = cmd.eid;
|
|
197 setName(name+eid);
|
|
198 name += "(sid="+sid+")";
|
|
199 inputLock = false;
|
|
200 break;
|
|
201 case SMCMD_QUIT :
|
|
202 if (cmd.eid!=eid)
|
|
203 quit = cmd;
|
|
204 else // eid=-1 means do not forward but send it.
|
|
205 quit = new REPCommand(REP.SMCMD_QUIT_2,
|
|
206 sid, -1, seq, 0, "");
|
|
207 timeout=1;
|
|
208 if (quit.eid==-1)
|
|
209 sendCommand(quit);
|
|
210 else
|
|
211 forwardCommand(quit);
|
|
212 quit=null;
|
|
213 //close connection user
|
|
214
|
|
215 break;
|
|
216 case SMCMD_START_MERGE :
|
|
217 // lock user input during merge (optional)
|
|
218 inputLock = hasInputLock;
|
|
219 cmd.cmd = REP.SMCMD_START_MERGE_ACK;
|
|
220 sendCommand(cmd);
|
|
221 break;
|
|
222 case SMCMD_END_MERGE :
|
|
223 inputLock = false;
|
|
224 break;
|
|
225 // master editor changes QUIT_2 to QUIT_2_ACK
|
|
226 // Session manager should do this
|
|
227 case SMCMD_QUIT_2 :
|
|
228 if (cmd.eid!=eid) {
|
|
229 forwardCommand(cmd);
|
|
230 } else {
|
|
231 cmd.cmd = REP.SMCMD_QUIT_2_ACK;
|
|
232 sendCommand(cmd);
|
|
233 }
|
|
234 running = false;
|
|
235 break;
|
|
236 case SMCMD_SYNC:
|
|
237 // start contents sync with newly joined editor
|
|
238 cmd.cmd = REP.SMCMD_SYNC_ACK;
|
|
239 forwardCommand(cmd);
|
|
240 //if (cmd.eid==eid) {
|
|
241 if (master && syncEnable ) {
|
|
242 syncCounter = 1;
|
|
243 timeout = 1;
|
|
244 }
|
|
245 break;
|
|
246 default:
|
|
247 assert(false);
|
|
248 break;
|
|
249 }
|
|
250 }
|
|
251
|
|
252 private void forwardCommand(REPCommand command) {
|
193
|
253 REPCommand cmd = new REPCommand(command);
|
|
254 channel.write(cmd);
|
|
255 }
|
|
256
|
209
|
257 private void sendCommand(REPCommand command) {
|
193
|
258 REPCommand cmd = new REPCommand(command);
|
|
259 cmd.setSEQID(seq++);
|
|
260 cmd.setEID(eid);
|
|
261 cmd.setSID(sid);
|
|
262 channel.write(cmd);
|
|
263 }
|
|
264
|
|
265 private void syncText() {
|
|
266 Logger.print();
|
|
267 if(syncCounter>repText.size()){
|
|
268 syncCounter = 0;
|
|
269 }else {
|
|
270 if(inputLock) return;
|
|
271 int i = syncCounter - 1;
|
|
272 REPCommand del = new REPCommand(REP.REPCMD_DELETE_USER, sid, eid, 0, i, repText.get(i));
|
|
273 REPCommand ins = new REPCommand(REP.REPCMD_INSERT_USER, sid, eid, 0, i, repText.get(i));
|
209
|
274 sendCommand(del);
|
|
275 sendCommand(ins);
|
193
|
276 syncCounter++;
|
|
277 }
|
|
278 }
|
|
279
|
|
280 }
|