view src/remoteeditor/network/REP.java @ 15:535f124c2fc3

*** empty log message ***
author pin
date Mon, 23 Oct 2006 02:50:36 +0900
parents 0ac009f1c69e
children b8f407692ecf
line wrap: on
line source

package remoteeditor.network;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;



public class REP implements Runnable{
	
	SocketChannel sc;
	int cmd;
	int eid;
	int lineno;
	int sid;
	int seqid;
	int textsiz;
	
	public static final int REP_INSERT_CMD = 6;
	static final int REP_INSERT_ACK_CMD = 7;
	static final int REP_JOIN_CMD = 41;
	static final int REP_JOIN_ACK_CMD = 42;
	static final int REP_PUT_CMD = 45;
	static final int REP_PUT_ACK_CMD = 46;
	static final int REP_SELECT_CMD = 47;
	static final int REP_SELECT_ACK_CMD = 48;
	static final int REP_QUIT_CMD = 53;
	static final int REP_QUIT_ACK_CMD = 54;
	
	ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
	ByteBuffer read_buffer = ByteBuffer.allocateDirect(1024);
	
	String string;
	private RSocketListener socketListener;
	private Shell shell;
	
	List myCmdList = new LinkedList();
	
	public REP() throws Exception {
		String host = "localhost";
		int port = 8080;
		
		InputDialog dialog = new InputDialog(shell, "REP", "host", "localhost", null);
		if(dialog.open() == Window.OK){
			host = dialog.getValue();
		}
		
		dialog = new InputDialog(shell, "REP", "port", "8080", null);
		if (dialog.open() == Window.OK) {
			try {
				port = Integer.parseInt(dialog.getValue());
			} catch (NumberFormatException x) {
			}
		}
		
		
		InetSocketAddress addr = new InetSocketAddress(host, port);
		//SocketChannel sc;
		sc = SocketChannel.open();
		sc.configureBlocking(true);
		sc.connect(addr);
		while(!sc.finishConnect()){
			System.out.println("afro");
		}
		join();
		
		dialog = new InputDialog(shell, "repput", "read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string, "hugo", null);
		if(dialog.open() == Window.OK){
			put();
		}
		
		dialog = new InputDialog(shell, "repselect", "read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string ,Integer.toString(sid), null);
		if(dialog.open() == Window.OK){
			try {
				sid = (byte) Integer.parseInt(dialog.getValue());
			} catch (NumberFormatException x) {
			}
			select();
		}
	}
	
	public void join() throws IOException{
		sc.write(pack(buffer, REP_JOIN_CMD, sid, eid, seqid, lineno, "afro"));
		unpack(read_buffer);
		System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
	}
	
	public void put() throws Exception {
		sc.write(pack(buffer, REP_PUT_CMD, sid, eid, seqid, lineno, "filename"));
		unpack(read_buffer);
		System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
	}
	
	public void select() throws Exception {
		sc.write(pack(buffer, REP_SELECT_CMD, sid, eid, seqid, lineno, "afro"));
		unpack(read_buffer);
		System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
	}
	
	public void insert(int offset, int length, String text) throws IOException {
		seqid = (byte)offset;
		sc.write(pack(buffer, REP_INSERT_CMD, sid, eid, seqid, lineno, text));
	}
	
    public ByteBuffer pack(ByteBuffer buffer,int cmd, int sid, int eid, int seqid, int lineno, String text ) {	


    	
    	//ByteBuffer buffer = ByteBuffer.allocateDirect(24+textsiz);
    	buffer.clear();  // position = 0 
    	buffer.putInt(cmd); buffer.putInt(sid); buffer.putInt(eid);
    	buffer.putInt(seqid); buffer.putInt(lineno); 
    	buffer.putInt(text.length()*2); 
    	//buffer.putString(text);
    	System.out.println("pack:" + cmd +", "+ "length="+ text.length() +" "+ sid +", "+ eid +", "+ seqid +", "+ lineno);
    	
    	for(int i=0;i<text.length();i++) {
			buffer.putChar(text.charAt(i));
		}	
    	buffer.flip();    // limit = current position, position = 0
    	return buffer;
    }

	
    static final int HEADER_SIZE = 24;
    ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
    
	public void unpack(ByteBuffer buffer) throws IOException{
		long len;
		header.clear();
		len = sc.read(header);  // limit = read length
		if (len !=HEADER_SIZE) {
			// this can't happen
		}
		header.rewind();  // position = 0
		/*for(int i = 0; i < 24; i++){
			readbyte[i] = header.get();
			//System.out.println(readbyte[i]);
		}*/
		String text = "";
		cmd = header.getInt();
		sid = header.getInt();
		eid = header.getInt();
		seqid = header.getInt();
		lineno = header.getInt();
		textsiz = header.getInt()/2;
		
		buffer.limit(textsiz*2);
		buffer.rewind();
		len = sc.read(buffer);  // limit = read length
		if (len !=HEADER_SIZE) {
			// this can't happen
		}
		buffer.rewind();
		for(int i=0;i<textsiz;i++) {
			text +=buffer.getChar();
		}
		/*for(int i = 0; i < textsiz; i++){
			readbyte[i] = buffer.get();
		}
		string = new String(readbyte, 0, textsiz);*/
		string = text;
	}
	
	public void addSocketListener(RSocketListener socketListener){
		//Runnable prt = new PacketReceiveThread(sc, socketListener);
		//Thread th = new Thread(prt);
		//th.start();
		Thread th = new Thread(this);
		th.start();
		this.socketListener = socketListener;
	}

	public void run() {
		while(sc.isConnected()){

			try {
				unpack(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
			socketListener.packetReceived(new RSocketEvent(cmd, sid, eid, seqid, lineno, textsiz, string));
		}	
		
	}

	public void dispose() {
		try {
			sc.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}