view test/editortest/REPCommandReceiver.java @ 417:267f9748e826

(no commit message)
author one
date Wed, 31 Dec 2008 14:52:45 +0900
parents
children
line wrap: on
line source

package test.editortest;

import java.io.IOException;
import java.nio.channels.SelectionKey;

import rep.REPCommand;
import rep.channel.REPSelectionKey;
import rep.channel.REPSelector;
import rep.channel.REPSocketChannel;

public class REPCommandReceiver extends Thread{
	
	REPSocketChannel<REPCommand> channel;
	boolean running = true;
	private boolean inputLock=false;
	long timeout = 1;
	private int syncCounter=0;

	public REPCommandReceiver(REPSocketChannel<REPCommand> channel, REPSelector<REPCommand> selector){
		this.channel = channel;
	}
	
	public void run(){
		try {
			mainloop();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void mainloop() throws IOException {
		
		channel.configureBlocking(false);
		REPSelector<REPCommand> selector = REPSelector.create();
		channel.register(selector, SelectionKey.OP_READ);
		while(running) {
			if (inputLock) {
				// No user input during merge mode (optional)
				if (selector.select(0)>0) {
					handle(channel.read());
				}
				continue;
			} else if (selector.select(timeout)<=0) {
				if (syncCounter>0) {
					syncText(); // send the master editor buffer to clients. 
				}
				userInput();
			}
			// selector(timeout) returns 0, but it may contain readable channel..
			for(REPSelectionKey<REPCommand> key : selector.selectedKeys1()) {
				REPSocketChannel<REPCommand> ch = key.channel1();
				handle(ch.read());
			}
		}
	}

	private void userInput() {
		// TODO Auto-generated method stub
		
	}

	private void syncText() {
		// TODO Auto-generated method stub
		
	}

	private void handle(REPCommand read) {
		// TODO Auto-generated method stub
		
	}
}