417
|
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 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;
|
419
|
30 private boolean hasInputLock = false;
|
417
|
31 private boolean master;
|
|
32 private boolean syncEnable = true;
|
|
33 private LogTarget logTarget;
|
|
34
|
|
35 public REPEditor(REPText repText, boolean master){
|
|
36 this.repText = repText;
|
|
37 this.master = master;
|
|
38 repText.addTextListener(this);
|
|
39 if(master){
|
|
40 userCommand.add(new REPCommand(REP.SMCMD_PUT,0,0,0,0,name +"-file"));
|
|
41 }else{
|
|
42 userCommand.add(new REPCommand(REP.SMCMD_JOIN, 0, 0, 0, 0, name));
|
|
43 }
|
|
44 }
|
|
45
|
|
46 public void textDeleted(REPTextEvent event) {
|
|
47 Logger.print(event.getText());
|
|
48 addUserInput(new REPCommand(REP.REPCMD_DELETE_USER, 0, 0, 0, event.getLineno(), event.getText()));
|
|
49 }
|
|
50
|
|
51 public void textInserted(REPTextEvent event) {
|
|
52 Logger.print(event.getText());
|
|
53 addUserInput(new REPCommand(REP.REPCMD_INSERT_USER, 0, 0, 0, event.getLineno(), event.getText()));
|
|
54 }
|
|
55
|
419
|
56 public void addUserInput(final REPCommand command) {
|
417
|
57 Runnable runner = new Runnable(){
|
|
58 public void run(){
|
|
59 userCommand.add(command);
|
|
60 timeout = 1;
|
|
61 }
|
|
62 };
|
|
63 synchronized(runners){
|
|
64 runners.add(runner);
|
|
65 }
|
|
66 if(selector != null){
|
|
67 selector.wakeup();
|
|
68 }
|
|
69 }
|
|
70
|
|
71 public void run(){
|
|
72 /*
|
|
73 * Create Socket and connect to the session manager
|
|
74 */
|
|
75 try {
|
|
76 channel = REPSocketChannel.<REPCommand>create(new REPCommandPacker());
|
|
77 } catch (IOException e) {
|
|
78 e.printStackTrace();
|
|
79 return;
|
|
80 }
|
|
81 try {
|
|
82 InetSocketAddress semaIP = new InetSocketAddress("localhost", 8766);
|
|
83 while (!channel.connect(semaIP)){
|
|
84 Logger.print("SeMa not listen to socket yet, wait");
|
|
85 }
|
|
86 } catch (IOException e) {
|
|
87 e.printStackTrace();
|
|
88 return;
|
|
89 }
|
|
90 /*
|
|
91 * Start editor main loop
|
|
92 * public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string)
|
|
93 */
|
|
94 try {
|
|
95 mainloop();
|
|
96 } catch (IOException e) {
|
|
97 }
|
|
98 }
|
|
99
|
|
100 /*
|
|
101 * Editor main loop with input lock
|
|
102 */
|
|
103 private void mainloop() throws IOException {
|
|
104
|
|
105 channel.configureBlocking(false);
|
|
106 selector = REPSelector.create();
|
|
107 channel.register(selector, SelectionKey.OP_READ);
|
|
108 while(running) {
|
|
109
|
|
110 synchronized(runners){
|
|
111 for(Runnable runner : runners){
|
|
112 runner.run();
|
|
113 }
|
|
114 runners.clear();
|
|
115 }
|
|
116
|
|
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) {
|
|
125 syncText(); // send the master editor buffer to clients.
|
|
126 }
|
|
127 userInput();
|
|
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());
|
|
133 }
|
|
134 }
|
|
135 }
|
|
136
|
|
137 private void handle(REPCommand command) {
|
|
138 Logger.print(logTarget, command);
|
419
|
139 // if(inputLock) Logger.print(logTarget, command);
|
417
|
140 if(command == null) return;
|
|
141 switch(command.cmd){
|
|
142 case REPCMD_DELETE:
|
|
143 if(command.eid != eid){
|
|
144 String del = repText.delete(command.lineno);
|
|
145 command.setString(del);
|
|
146 }
|
|
147 forward(command);
|
|
148 break;
|
|
149 case REPCMD_INSERT:
|
|
150 if(command.eid != eid){
|
|
151 repText.insert(command.lineno, command.string);
|
|
152 }
|
|
153 forward(command);
|
|
154 break;
|
|
155 case REPCMD_NOP:
|
|
156 case REPCMD_INSERT_ACK:
|
|
157 case REPCMD_DELETE_ACK:
|
|
158 forward(command);
|
|
159 break;
|
|
160 case SMCMD_PUT_ACK:
|
|
161 sid = command.sid;
|
|
162 eid = command.eid;
|
|
163 name += "(eid="+eid+",sid="+sid+")";
|
|
164 inputLock = false;
|
|
165 break;
|
|
166 case SMCMD_JOIN_ACK :
|
|
167 sid = command.sid;
|
|
168 eid = command.eid;
|
|
169 name += "(eid="+eid+",sid="+sid+")";
|
|
170 inputLock = false;
|
|
171 break;
|
|
172 case SMCMD_START_MERGE :
|
|
173 // lock user input during merge (optional)
|
|
174 inputLock = hasInputLock;
|
|
175 command.cmd = REP.SMCMD_START_MERGE_ACK;
|
|
176 send(command);
|
|
177 break;
|
|
178 case SMCMD_END_MERGE :
|
|
179 inputLock = false;
|
|
180 break;
|
|
181 case SMCMD_SYNC:
|
|
182 // start contents sync with newly joined editor
|
|
183 command.cmd = REP.SMCMD_SYNC_ACK;
|
|
184 forward(command);
|
|
185 //if (cmd.eid==eid) {
|
|
186 if (master && syncEnable ) {
|
|
187 syncCounter = 1;
|
|
188 timeout = 1;
|
|
189 }
|
|
190 break;
|
|
191 }
|
|
192 }
|
|
193
|
|
194 private void userInput() {
|
|
195 Logger.print();
|
|
196 REPCommand command = userCommand.poll();
|
|
197 if(command != null){
|
|
198 switch(command.cmd){
|
|
199 case REPCMD_DELETE_USER:
|
|
200 send(command);
|
|
201 break;
|
|
202 case REPCMD_INSERT_USER:
|
|
203 send(command);
|
|
204 break;
|
|
205 case SMCMD_PUT:
|
|
206 case SMCMD_JOIN:
|
|
207 send(command);
|
|
208 break;
|
|
209 }
|
|
210 }else{
|
|
211 if(syncCounter == 0){
|
|
212 timeout = 0;
|
|
213 }
|
|
214 }
|
|
215 }
|
|
216
|
|
217 private void forward(REPCommand command) {
|
|
218 REPCommand cmd = new REPCommand(command);
|
|
219 channel.write(cmd);
|
|
220 }
|
|
221
|
|
222 private void send(REPCommand command) {
|
|
223 REPCommand cmd = new REPCommand(command);
|
|
224 cmd.setSEQID(seq++);
|
|
225 cmd.setEID(eid);
|
|
226 cmd.setSID(sid);
|
|
227 channel.write(cmd);
|
|
228 }
|
|
229
|
|
230 private void syncText() {
|
|
231 if(syncCounter>repText.size()){
|
|
232 syncCounter = 0;
|
|
233 }else {
|
|
234 if(inputLock) return;
|
|
235 int i = syncCounter - 1;
|
|
236 REPCommand del = new REPCommand(REP.REPCMD_DELETE_USER, sid, eid, 0, i, repText.get(i));
|
|
237 REPCommand ins = new REPCommand(REP.REPCMD_INSERT_USER, sid, eid, 0, i, repText.get(i));
|
|
238 send(del);
|
|
239 send(ins);
|
|
240 syncCounter++;
|
|
241 }
|
|
242 }
|
|
243
|
|
244 public void setLogTarget(LogTarget target){
|
|
245 logTarget = target;
|
|
246 }
|
|
247
|
419
|
248 public REPText getREPText() {
|
|
249 return repText;
|
|
250 }
|
|
251
|
417
|
252 }
|