view src/remoteeditor/editors/RemoteEditor.java @ 110:974e9f322959

*** empty log message ***
author pin
date Sun, 23 Dec 2007 11:50:02 +0900
parents 823a45f843a9
children 07a8d0a0f7e4
line wrap: on
line source

package remoteeditor.editors;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextListener;
import org.eclipse.jface.text.TextEvent;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;

import remoteeditor.command.REPCommand;
import remoteeditor.command.REPCommandEvent;
import remoteeditor.command.REPCommandListener;
import remoteeditor.network.REP;
import remoteeditor.network.REPPacketReceive;
import remoteeditor.network.REPPacketSend;
import remoteeditor.network.RSocketEvent;
import remoteeditor.network.RSocketListener;
import remoteeditor.ui.REPSelectWindow;
import sample.merge.Translate;


public class RemoteEditor extends TextEditor implements ITextListener, REPCommandListener{
	
	private ISourceViewer viewer;
	private IDocument document;
	
	REPPacketReceive repreceive;
	REPPacketSend repsend;
	
	List <REPCommand> userCmdList = new LinkedList<REPCommand>();
	List <REPCommand> tokenCmdList = new LinkedList<REPCommand>();
	Translate trans = new Translate(userCmdList, tokenCmdList);
	
	//Translate trans = new Translate();
	
	
	int numberOfLinesOld;
	int offset_con;
	private SocketChannel sc;
	private int myeid = 0;
	private int myseq = 0;
	private int mysid = 0;
	private String filename = "afro";
	protected boolean lock;
	private String myHostAndPort;
	
	
	//private Translate translate;
	
	public RemoteEditor() {
		super();
		//filename = this.getEditorInput().getName();

	}
	
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		viewer = getSourceViewer();
		viewer.addTextListener(this);
		document = viewer.getDocument();
		filename = this.getEditorInput().getName();
		
		int port = 8766;
		String host = "localhost";
		InetSocketAddress addr = new InetSocketAddress(host, port);
		try {
			sc = SocketChannel.open();
			sc.configureBlocking(true);
			sc.connect(addr);
			while(!sc.finishConnect()){
				System.out.println("afro");
			}
		}catch (IOException e) {
			e.printStackTrace();
		}
		getSocketString(sc);
		repreceive = new REPPacketReceive(sc);
		repsend = new REPPacketSend(sc);
		
//		IWorkbench workbench = PlatformUI.getWorkbench();
//		Display display = workbench.getDisplay();
//		REPSelectWindow selectwindow = new REPSelectWindow(display);
//		selectwindow.initWindow();
//		selectwindow.setName(this.getEditorInput().getName());
//		selectwindow.setChannel(sc);
//		selectwindow.open();
		
		//REPCommand temp = repreceive.unpack();
		//myeid = temp.eid; mysid = temp.sid;
		repreceive.addCommandListener(this);
		//repsend.send(new REPCommand(REP.REPCMD_READ, mysid, myeid, myseq, 0, 0, ""));
		//joinPart();
		if(document.getLength() < 1){
			joinPart();
		}else {
			putPart();
		}
	}
	
	private void getSocketString(SocketChannel sc2) {
		String socketString = sc2.socket().getLocalSocketAddress().toString();
		System.out.println(socketString);
		String[] str = socketString.split("/");
		//String String str2;
		for(String str2: str){
			this.myHostAndPort = str2;
		}
		//this.myHostAndPort = socketString;
	}

	public void dispose() {
		//rep.dispose();
		super.dispose();
	}
	public void textChanged(TextEvent event) {
		String replacedText = event.getReplacedText();
		String inputText = event.getText();
				
		// �y�[�W�擪����̕�����(��s��܂�)�ɂ��W
		int textOffset = event.getOffset();
		
		System.out.println("replace = " + replacedText);
		System.out.println("input = " + inputText + " : " + inputText.length());
		
		int line = 0;
		int offset = 0;
		int length = 0;
		int cmd = 0;
		int numberOfLinesNew = 0;
		String lineText = null;
		try {
			line = document.getNumberOfLines(0, textOffset); // lineno ��擾���Ă܂��B
			offset = document.getLineOffset(line-1);
			length = document.getLineLength(line-1);
			lineText = document.get(offset, length);
			numberOfLinesNew = document.getNumberOfLines();
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}
		if(numberOfLinesNew > numberOfLinesOld){
			//insert, delete, replace �� �s���Ŕ��f
			cmd = REP.REPCMD_INSERT;
			lineText = lineText + "\n";
			//line += 1;
		}else if(numberOfLinesNew == numberOfLinesOld){
			cmd = REP.REPCMD_REPLACE;
		}else {
			cmd = REP.REPCMD_DELETE;
		}
		//rep.sendCmd(cmd, line, length, lineText);
		if(!lock){
			REPCommand usercmd = new REPCommand(cmd, mysid, myeid, myseq, line, lineText.length(), lineText);
			repsend.send(usercmd);
			//translate.addUserList(usercmd);
			trans.addUserList(usercmd);
			//System.out.println("User List" + userCmdList.toString());
			//System.out.println("Token List" + tokenCmdList.toString());
		}else{
			
		}
		numberOfLinesOld = numberOfLinesNew;
	}
	
	public void changeText(int kindOfCmd, int lineNo, int LineLength, String text) throws Exception{
		final int offset = document.getLineOffset(lineNo-1);
		final String changedText = text;

			final int replaceLength = document.getLineLength(lineNo-1);
		
		viewer.getTextWidget().getDisplay().syncExec(new Runnable() {

			public void run() {
				try {
					lock = true;
					//document.replace(offset, replaceLength, changedText);
					document.replace(offset, replaceLength, changedText);
					lock = false;
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		});
	}
/*
//	public void packetReceived(final RSocketEvent evt) {
//		final int lineNo = evt.getLineNo();
//		final int Linelength = evt.getLength();
//		final String text = evt.getText();
//		try {
//			changeText(evt.getCmd(), lineNo, Linelength, text);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}*/
	
	public void joinPart(){
		//String string = "test.txt";
		repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, 0, 0, 0, myHostAndPort));
	}
	
	public void putPart(){
		if(this.getEditorInput() != null) filename = this.getEditorInput().getName();
		repsend.send(new REPCommand(REP.SMCMD_PUT, mysid, myeid, myseq, 0, filename.length(), filename));
	}
	
	
	public void selectPart(){
		repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, 0, ""));
	}

	public void CommandReceived(REPCommandEvent event) {
		REPCommand receivedCommand = event.getCommand();
		if(receivedCommand.eid == myeid){
			System.out.println("my REP Command.");
		}else{
			tokenCmdList.add(receivedCommand);
		}
		//System.out.println("received command : " + receivedCommand.toString());
		commandManager(receivedCommand);
	}

	private void commandManager(REPCommand command) {
		switch(command.cmd){
		case REP.SMCMD_JOIN_ACK:
			mysid = command.sid;
			myeid = command.eid;
			trans.myeid = myeid;
			REPCommand sendCommand = new REPCommand(REP.REPCMD_READ, mysid, myeid, 0, 0, 0, "");
			repsend.send(sendCommand);
			//putPart();
			break;
		case REP.SMCMD_PUT_ACK:
			mysid = command.sid;
			myeid = command.eid;
			//selectPart();
			break;
		case REP.SMCMD_SELECT_ACK:
			mysid = command.sid;
			break;
		case REP.REPCMD_INSERT:
			try {
					if(command.eid == myeid) break;	//���ƂŏB�
					textInsert(command.lineno, command.len, command.string);
				} catch (Exception e) {
					e.printStackTrace();
				}
			break;
		case REP.REPCMD_REPLACE:
			try {
					if(command.eid == myeid) break;//���ƂŏB�
					repsend.send(command);
					changeText(command.cmd, command.lineno, command.len, command.string);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			break;
		case REP.REPCMD_DELETE:
			try {
					if(command.eid == myeid) break;//���ƂŏB�
					textDelete(command.lineno, command.len, command.string);
				} catch (BadLocationException e1) {
					e1.printStackTrace();
				}
			break;
		case REP.REPCMD_READ:
			try {
					receiveReadCMD(command.eid);
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			break;
		case REP.REPCMD_READ_ACK:
			try {
				if(command.sid == mysid && command.eid == myeid){
					read(command);
				}else{
					repsend.send(command);
				}
				
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			break;
		}
	}



	private void read(REPCommand command) throws BadLocationException {
		final int offset = document.getLineOffset(command.lineno);
		final String string = command.string;
		
		viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
			public void run() {
				try {
					lock = true;
					//document.replace(offset, replaceLength, changedText);
					document.replace(offset, 0, string);
					lock = false;
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void textDelete(int lineNo, int len, String string) throws BadLocationException {
		final int offset = document.getLineOffset(lineNo);
		//final String changedText = string;

			final int replaceLength = document.getLineLength(lineNo);
		
		viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
			public void run() {
				try {
					lock = true;
					//document.replace(offset, replaceLength, changedText);
					document.replace(offset, replaceLength, "");
					lock = false;
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void textInsert(int lineNo, int j, String text) throws BadLocationException {
		final int offset = document.getLineOffset(lineNo);
		final String changedText = text;

			//final int replaceLength = document.getLineLength(lineNo);
		
		viewer.getTextWidget().getDisplay().syncExec(new Runnable() {
			public void run() {
				try {
					lock = true;
					//document.replace(offset, replaceLength, changedText);
					document.replace(offset, 0, changedText);
					lock = false;
				} catch (BadLocationException e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void receiveReadCMD(int desteid) throws BadLocationException {
		for(int i = 0; i < document.getNumberOfLines(); i++){
			int offset = document.getLineOffset(i);
			int length = document.getLineLength(i);
			String text = document.get(offset, length);
			repsend.send(new REPCommand(REP.REPCMD_READ_ACK, mysid, desteid, myseq, i, text.length(), text));
		}
	}
}