0
|
1 package remoteeditor.editors;
|
|
2
|
34
|
3 import java.io.IOException;
|
|
4 import java.net.InetAddress;
|
|
5 import java.net.InetSocketAddress;
|
64
|
6 import java.net.NetworkInterface;
|
|
7 import java.net.SocketException;
|
85
|
8 import java.nio.CharBuffer;
|
34
|
9 import java.nio.channels.SocketChannel;
|
64
|
10 import java.util.Enumeration;
|
34
|
11 import java.util.LinkedList;
|
|
12 import java.util.List;
|
|
13
|
0
|
14 import org.eclipse.jface.text.BadLocationException;
|
|
15 import org.eclipse.jface.text.IDocument;
|
|
16 import org.eclipse.jface.text.ITextListener;
|
|
17 import org.eclipse.jface.text.TextEvent;
|
|
18 import org.eclipse.jface.text.source.ISourceViewer;
|
|
19 import org.eclipse.swt.widgets.Composite;
|
37
|
20 import org.eclipse.swt.widgets.Display;
|
|
21 import org.eclipse.swt.widgets.Shell;
|
|
22 import org.eclipse.ui.IWorkbench;
|
|
23 import org.eclipse.ui.PlatformUI;
|
1
|
24 import org.eclipse.ui.editors.text.TextEditor;
|
0
|
25
|
34
|
26 import remoteeditor.command.REPCommand;
|
|
27 import remoteeditor.command.REPCommandEvent;
|
|
28 import remoteeditor.command.REPCommandListener;
|
0
|
29 import remoteeditor.network.REP;
|
34
|
30 import remoteeditor.network.REPPacketReceive;
|
|
31 import remoteeditor.network.REPPacketSend;
|
0
|
32 import remoteeditor.network.RSocketEvent;
|
|
33 import remoteeditor.network.RSocketListener;
|
37
|
34 import remoteeditor.ui.REPSelectWindow;
|
34
|
35 import sample.merge.Translate;
|
0
|
36
|
|
37
|
34
|
38 public class RemoteEditor extends TextEditor implements ITextListener, REPCommandListener{
|
0
|
39
|
|
40 private ISourceViewer viewer;
|
|
41 private IDocument document;
|
34
|
42
|
|
43 REPPacketReceive repreceive;
|
|
44 REPPacketSend repsend;
|
56
|
45
|
|
46 List <REPCommand> userCmdList = new LinkedList<REPCommand>();
|
|
47 List <REPCommand> tokenCmdList = new LinkedList<REPCommand>();
|
|
48 Translate trans = new Translate(userCmdList, tokenCmdList);
|
|
49
|
|
50 //Translate trans = new Translate();
|
34
|
51
|
|
52
|
21
|
53 int numberOfLinesOld;
|
26
|
54 int offset_con;
|
34
|
55 private SocketChannel sc;
|
35
|
56 private int myeid = 0;
|
|
57 private int myseq = 0;
|
|
58 private int mysid = 0;
|
|
59 private String filename = "afro";
|
39
|
60 protected boolean lock;
|
65
|
61 private String myHostAndPort;
|
|
62
|
|
63
|
56
|
64 //private Translate translate;
|
37
|
65
|
21
|
66 public RemoteEditor() {
|
0
|
67 super();
|
35
|
68 //filename = this.getEditorInput().getName();
|
36
|
69
|
|
70 }
|
|
71
|
|
72 public void createPartControl(Composite parent) {
|
|
73 super.createPartControl(parent);
|
|
74 viewer = getSourceViewer();
|
|
75 viewer.addTextListener(this);
|
|
76 document = viewer.getDocument();
|
|
77 filename = this.getEditorInput().getName();
|
138
|
78 numberOfLinesOld = document.getNumberOfLines();
|
103
|
79
|
61
|
80 int port = 8766;
|
34
|
81 String host = "localhost";
|
|
82 InetSocketAddress addr = new InetSocketAddress(host, port);
|
|
83 try {
|
|
84 sc = SocketChannel.open();
|
|
85 sc.configureBlocking(true);
|
|
86 sc.connect(addr);
|
|
87 while(!sc.finishConnect()){
|
|
88 System.out.println("afro");
|
|
89 }
|
|
90 }catch (IOException e) {
|
|
91 e.printStackTrace();
|
|
92 }
|
65
|
93 getSocketString(sc);
|
34
|
94 repreceive = new REPPacketReceive(sc);
|
37
|
95 repsend = new REPPacketSend(sc);
|
|
96
|
62
|
97 // IWorkbench workbench = PlatformUI.getWorkbench();
|
|
98 // Display display = workbench.getDisplay();
|
|
99 // REPSelectWindow selectwindow = new REPSelectWindow(display);
|
|
100 // selectwindow.initWindow();
|
|
101 // selectwindow.setName(this.getEditorInput().getName());
|
|
102 // selectwindow.setChannel(sc);
|
|
103 // selectwindow.open();
|
37
|
104
|
62
|
105 //REPCommand temp = repreceive.unpack();
|
|
106 //myeid = temp.eid; mysid = temp.sid;
|
34
|
107 repreceive.addCommandListener(this);
|
62
|
108 //repsend.send(new REPCommand(REP.REPCMD_READ, mysid, myeid, myseq, 0, 0, ""));
|
86
|
109 //joinPart();
|
103
|
110 if(document.getLength() < 1){
|
|
111 joinPart();
|
|
112 }else {
|
|
113 putPart();
|
|
114 }
|
0
|
115 }
|
|
116
|
65
|
117 private void getSocketString(SocketChannel sc2) {
|
|
118 String socketString = sc2.socket().getLocalSocketAddress().toString();
|
|
119 System.out.println(socketString);
|
|
120 String[] str = socketString.split("/");
|
|
121 //String String str2;
|
|
122 for(String str2: str){
|
|
123 this.myHostAndPort = str2;
|
|
124 }
|
|
125 //this.myHostAndPort = socketString;
|
|
126 }
|
|
127
|
0
|
128 public void dispose() {
|
34
|
129 //rep.dispose();
|
0
|
130 super.dispose();
|
|
131 }
|
|
132 public void textChanged(TextEvent event) {
|
140
|
133 if(lock) {
|
|
134 numberOfLinesOld = document.getNumberOfLines();
|
|
135 return;
|
|
136 }
|
0
|
137 String replacedText = event.getReplacedText();
|
|
138 String inputText = event.getText();
|
21
|
139
|
117
|
140 //ページ先頭からの文字数による座標(改行を含む)
|
21
|
141 int textOffset = event.getOffset();
|
0
|
142
|
|
143 System.out.println("replace = " + replacedText);
|
18
|
144 System.out.println("input = " + inputText + " : " + inputText.length());
|
1
|
145
|
12
|
146 int line = 0;
|
26
|
147 int offset = 0;
|
12
|
148 int length = 0;
|
21
|
149 int cmd = 0;
|
|
150 int numberOfLinesNew = 0;
|
12
|
151 String lineText = null;
|
0
|
152 try {
|
117
|
153 line = document.getNumberOfLines(0, textOffset); // lineno を取得してます。
|
12
|
154 offset = document.getLineOffset(line-1);
|
|
155 length = document.getLineLength(line-1);
|
|
156 lineText = document.get(offset, length);
|
21
|
157 numberOfLinesNew = document.getNumberOfLines();
|
11
|
158 } catch (BadLocationException e1) {
|
|
159 e1.printStackTrace();
|
|
160 }
|
21
|
161 if(numberOfLinesNew > numberOfLinesOld){
|
117
|
162 //insert, delete, replace 行数で判断
|
64
|
163 cmd = REP.REPCMD_INSERT;
|
118
|
164 try {
|
|
165 sendInsert(line);
|
135
|
166 createUndoCommand(cmd, textOffset - offset, replacedText, inputText, line);
|
118
|
167 } catch (BadLocationException e) {
|
|
168 e.printStackTrace();
|
112
|
169 }
|
127
|
170
|
21
|
171 }else if(numberOfLinesNew == numberOfLinesOld){
|
64
|
172 cmd = REP.REPCMD_REPLACE;
|
127
|
173 try {
|
|
174 sendReplace(line);
|
135
|
175 createUndoCommand(cmd, textOffset - offset, replacedText, inputText, line);
|
127
|
176 } catch (BadLocationException e) {
|
|
177 e.printStackTrace();
|
|
178 }
|
21
|
179 }else {
|
64
|
180 cmd = REP.REPCMD_DELETE;
|
127
|
181 try {
|
|
182 sendDelete(line);
|
142
|
183 createUndoCommand(cmd, textOffset - offset, replacedText, inputText, line);
|
127
|
184 } catch (BadLocationException e) {
|
|
185 e.printStackTrace();
|
|
186 }
|
21
|
187 }
|
34
|
188 //rep.sendCmd(cmd, line, length, lineText);
|
127
|
189 // if(!lock){
|
|
190 // REPCommand usercmd = new REPCommand(cmd, mysid, myeid, myseq, line, lineText.length(), lineText);
|
|
191 // repsend.send(usercmd);
|
|
192 // //translate.addUserList(usercmd);
|
|
193 // trans.addUserList(usercmd);
|
|
194 // //System.out.println("User List" + userCmdList.toString());
|
|
195 // //System.out.println("Token List" + tokenCmdList.toString());
|
|
196 // }else{ }
|
139
|
197 //numberOfLinesOld = numberOfLinesNew;
|
|
198 numberOfLinesOld = document.getNumberOfLines();
|
21
|
199 }
|
|
200
|
135
|
201 private void createUndoCommand(int cmd, int offset, String replacedText, String inputText, int line) {
|
134
|
202 int undoCmd = 0;
|
|
203 String undoString = null;
|
|
204 REPCommand undoCommand;
|
|
205 REPPacketSend send = new REPPacketSend(sc);
|
135
|
206
|
131
|
207 if(cmd == REP.REPCMD_INSERT){
|
132
|
208 undoCmd = REP.REPCMD_DELETE;
|
|
209 undoString = "";
|
135
|
210
|
132
|
211 }else if(cmd == REP.REPCMD_REPLACE){
|
|
212 undoCmd = REP.REPCMD_REPLACE;
|
135
|
213 String lineText = null;
|
|
214 try {
|
|
215 int lineOffset = document.getLineOffset(line-1);
|
|
216 int length = document.getLineLength(line-1);
|
|
217 lineText = document.get(lineOffset, length);
|
|
218 } catch (BadLocationException e) {
|
|
219 e.printStackTrace();
|
|
220 }
|
136
|
221 StringBuffer sb = new StringBuffer(lineText);
|
|
222 sb.delete(offset, offset + inputText.length());
|
138
|
223 //undoString = sb.toString();
|
137
|
224 if(replacedText != null){
|
|
225 sb.insert(offset, replacedText);
|
|
226 }
|
|
227 undoString = sb.toString();
|
135
|
228 //sb.insert(offset, inputText);
|
|
229 //if(replacedText == null){
|
|
230 // sb.delete(offset, inputText.length());
|
|
231 //}else {
|
|
232 //sb.replace(offset, inputText.length(), replacedText);
|
|
233 //}
|
|
234 //undoString = sb.toString();
|
|
235
|
132
|
236 }else if(cmd == REP.REPCMD_DELETE){
|
|
237 undoCmd = REP.REPCMD_INSERT;
|
|
238 undoString = replacedText;
|
131
|
239 }
|
135
|
240
|
134
|
241 undoCommand = new REPCommand(undoCmd, mysid, 0, -1, line, undoString.length(), undoString);
|
|
242 send.send(undoCommand);
|
131
|
243 }
|
|
244
|
127
|
245 private void sendDelete(int line) throws BadLocationException {
|
|
246 if(!lock){
|
|
247 int offset = document.getLineOffset(line-1);
|
|
248 int length = document.getLineLength(line-1);
|
|
249 String lineText = document.get(offset, length);
|
129
|
250 REPCommand usercmd = new REPCommand(REP.REPCMD_REPLACE, mysid, myeid, myseq, line, lineText.length(), lineText);
|
|
251 repsend.send(usercmd);
|
|
252
|
|
253 offset = document.getLineOffset(line);
|
|
254 length = document.getLineLength(line);
|
|
255 lineText = document.get(offset, length);
|
|
256 usercmd = new REPCommand(REP.REPCMD_DELETE, mysid, myeid, myseq, line, lineText.length(), lineText);
|
127
|
257 repsend.send(usercmd);
|
|
258 }
|
|
259 }
|
|
260
|
|
261 private void sendReplace(int line) throws BadLocationException {
|
|
262 if(!lock){
|
|
263 int offset = document.getLineOffset(line-1);
|
|
264 int length = document.getLineLength(line-1);
|
|
265 String lineText = document.get(offset, length);
|
|
266 REPCommand usercmd = new REPCommand(REP.REPCMD_REPLACE, mysid, myeid, myseq, line, lineText.length(), lineText);
|
|
267 repsend.send(usercmd);
|
|
268 }
|
|
269 }
|
|
270
|
118
|
271 private void sendInsert(int line) throws BadLocationException {
|
120
|
272 if(!lock){
|
121
|
273 int offset = document.getLineOffset(line-1);
|
|
274 int length = document.getLineLength(line-1);
|
118
|
275 String lineText = document.get(offset, length);
|
119
|
276 REPCommand usercmd = new REPCommand(REP.REPCMD_REPLACE, mysid, myeid, myseq, line, lineText.length(), lineText);
|
118
|
277 repsend.send(usercmd);
|
|
278
|
121
|
279 offset = document.getLineOffset(line);
|
|
280 length = document.getLineLength(line);
|
118
|
281 lineText = document.get(offset, length);
|
|
282 usercmd = new REPCommand(REP.REPCMD_INSERT, mysid, myeid, myseq, line, lineText.length(), lineText);
|
|
283 repsend.send(usercmd);
|
120
|
284 }
|
118
|
285 }
|
|
286
|
135
|
287 private void changeText(int kindOfCmd, int lineNo, int LineLength, String text) throws Exception{
|
|
288 System.out.println("Replace Text : " + text);
|
52
|
289 final int offset = document.getLineOffset(lineNo-1);
|
21
|
290 final String changedText = text;
|
43
|
291
|
52
|
292 final int replaceLength = document.getLineLength(lineNo-1);
|
43
|
293
|
21
|
294 viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
|
43
|
295
|
21
|
296 public void run() {
|
|
297 try {
|
39
|
298 lock = true;
|
40
|
299 //document.replace(offset, replaceLength, changedText);
|
50
|
300 document.replace(offset, replaceLength, changedText);
|
39
|
301 lock = false;
|
21
|
302 } catch (BadLocationException e) {
|
|
303 e.printStackTrace();
|
|
304 }
|
18
|
305 }
|
21
|
306 });
|
|
307 }
|
65
|
308 /*
|
35
|
309 // public void packetReceived(final RSocketEvent evt) {
|
|
310 // final int lineNo = evt.getLineNo();
|
|
311 // final int Linelength = evt.getLength();
|
|
312 // final String text = evt.getText();
|
|
313 // try {
|
|
314 // changeText(evt.getCmd(), lineNo, Linelength, text);
|
|
315 // } catch (Exception e) {
|
|
316 // e.printStackTrace();
|
|
317 // }
|
65
|
318 // }*/
|
35
|
319
|
|
320 public void joinPart(){
|
|
321 //String string = "test.txt";
|
65
|
322 repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, 0, 0, 0, myHostAndPort));
|
0
|
323 }
|
34
|
324
|
35
|
325 public void putPart(){
|
|
326 if(this.getEditorInput() != null) filename = this.getEditorInput().getName();
|
64
|
327 repsend.send(new REPCommand(REP.SMCMD_PUT, mysid, myeid, myseq, 0, filename.length(), filename));
|
35
|
328 }
|
|
329
|
|
330
|
|
331 public void selectPart(){
|
64
|
332 repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, 0, ""));
|
34
|
333 }
|
|
334
|
|
335 public void CommandReceived(REPCommandEvent event) {
|
35
|
336 REPCommand receivedCommand = event.getCommand();
|
56
|
337 if(receivedCommand.eid == myeid){
|
|
338 System.out.println("my REP Command.");
|
|
339 }else{
|
|
340 tokenCmdList.add(receivedCommand);
|
|
341 }
|
35
|
342 //System.out.println("received command : " + receivedCommand.toString());
|
|
343 commandManager(receivedCommand);
|
|
344 }
|
|
345
|
|
346 private void commandManager(REPCommand command) {
|
|
347 switch(command.cmd){
|
64
|
348 case REP.SMCMD_JOIN_ACK:
|
92
|
349 mysid = command.sid;
|
35
|
350 myeid = command.eid;
|
56
|
351 trans.myeid = myeid;
|
103
|
352 REPCommand sendCommand = new REPCommand(REP.REPCMD_READ, mysid, myeid, 0, 0, 0, "");
|
|
353 repsend.send(sendCommand);
|
89
|
354 //putPart();
|
35
|
355 break;
|
64
|
356 case REP.SMCMD_PUT_ACK:
|
87
|
357 mysid = command.sid;
|
|
358 myeid = command.eid;
|
62
|
359 //selectPart();
|
35
|
360 break;
|
64
|
361 case REP.SMCMD_SELECT_ACK:
|
63
|
362 mysid = command.sid;
|
35
|
363 break;
|
64
|
364 case REP.REPCMD_INSERT:
|
37
|
365 try {
|
117
|
366 if(command.eid == myeid) break; //mergerを導入する時に消す
|
48
|
367 textInsert(command.lineno, command.len, command.string);
|
37
|
368 } catch (Exception e) {
|
|
369 e.printStackTrace();
|
|
370 }
|
|
371 break;
|
64
|
372 case REP.REPCMD_REPLACE:
|
43
|
373 try {
|
117
|
374 if(command.eid == myeid) break;//mergerを導入する時に消す
|
135
|
375 if(command.seq > 0) repsend.send(command);
|
43
|
376 changeText(command.cmd, command.lineno, command.len, command.string);
|
|
377 } catch (Exception e1) {
|
|
378 e1.printStackTrace();
|
|
379 }
|
|
380 break;
|
64
|
381 case REP.REPCMD_DELETE:
|
53
|
382 try {
|
117
|
383 if(command.eid == myeid) break;//mergerを導入する時に消す
|
53
|
384 textDelete(command.lineno, command.len, command.string);
|
|
385 } catch (BadLocationException e1) {
|
|
386 e1.printStackTrace();
|
|
387 }
|
|
388 break;
|
38
|
389 case REP.REPCMD_READ:
|
|
390 try {
|
65
|
391 receiveReadCMD(command.eid);
|
38
|
392 } catch (BadLocationException e) {
|
|
393 e.printStackTrace();
|
|
394 }
|
|
395 break;
|
106
|
396 case REP.REPCMD_READ_ACK:
|
|
397 try {
|
107
|
398 if(command.sid == mysid && command.eid == myeid){
|
106
|
399 read(command);
|
107
|
400 }else{
|
|
401 repsend.send(command);
|
|
402 }
|
|
403
|
106
|
404 } catch (BadLocationException e) {
|
|
405 e.printStackTrace();
|
|
406 }
|
|
407 break;
|
38
|
408 }
|
|
409 }
|
|
410
|
53
|
411
|
|
412
|
106
|
413 private void read(REPCommand command) throws BadLocationException {
|
|
414 final int offset = document.getLineOffset(command.lineno);
|
107
|
415 final String string = command.string;
|
106
|
416
|
|
417 viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
|
|
418 public void run() {
|
|
419 try {
|
|
420 lock = true;
|
|
421 //document.replace(offset, replaceLength, changedText);
|
|
422 document.replace(offset, 0, string);
|
|
423 lock = false;
|
|
424 } catch (BadLocationException e) {
|
|
425 e.printStackTrace();
|
|
426 }
|
|
427 }
|
|
428 });
|
|
429 }
|
|
430
|
53
|
431 private void textDelete(int lineNo, int len, String string) throws BadLocationException {
|
54
|
432 final int offset = document.getLineOffset(lineNo);
|
53
|
433 //final String changedText = string;
|
|
434
|
54
|
435 final int replaceLength = document.getLineLength(lineNo);
|
53
|
436
|
|
437 viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
|
|
438 public void run() {
|
|
439 try {
|
|
440 lock = true;
|
|
441 //document.replace(offset, replaceLength, changedText);
|
|
442 document.replace(offset, replaceLength, "");
|
|
443 lock = false;
|
|
444 } catch (BadLocationException e) {
|
|
445 e.printStackTrace();
|
|
446 }
|
|
447 }
|
|
448 });
|
|
449 }
|
|
450
|
48
|
451 private void textInsert(int lineNo, int j, String text) throws BadLocationException {
|
49
|
452 final int offset = document.getLineOffset(lineNo);
|
48
|
453 final String changedText = text;
|
|
454
|
|
455 //final int replaceLength = document.getLineLength(lineNo);
|
|
456
|
|
457 viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
|
|
458 public void run() {
|
|
459 try {
|
|
460 lock = true;
|
|
461 //document.replace(offset, replaceLength, changedText);
|
|
462 document.replace(offset, 0, changedText);
|
|
463 lock = false;
|
|
464 } catch (BadLocationException e) {
|
|
465 e.printStackTrace();
|
|
466 }
|
|
467 }
|
|
468 });
|
|
469 }
|
|
470
|
65
|
471 private void receiveReadCMD(int desteid) throws BadLocationException {
|
38
|
472 for(int i = 0; i < document.getNumberOfLines(); i++){
|
|
473 int offset = document.getLineOffset(i);
|
|
474 int length = document.getLineLength(i);
|
|
475 String text = document.get(offset, length);
|
65
|
476 repsend.send(new REPCommand(REP.REPCMD_READ_ACK, mysid, desteid, myseq, i, text.length(), text));
|
35
|
477 }
|
34
|
478 }
|
0
|
479 }
|