view src/main/java/jp/ac/u_ryukyu/treevnc/AcceptThread.java @ 39:6a34a5220a01

add files
author one
date Tue, 02 Oct 2012 18:13:13 +0900
parents c2f0b6907448
children 3c072f2f39bb
line wrap: on
line source

package jp.ac.u_ryukyu.treevnc;
import java.net.Socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.glavsoft.transport.Reader;
import com.glavsoft.transport.Writer;

public class AcceptThread implements Runnable {
        public MyRfbProto rfb = null;
        byte[] imageBytes;
        int port;
        public boolean flag = false;
        
        public AcceptThread(MyRfbProto _rfb) {
                rfb = _rfb;
        }


        public AcceptThread(MyRfbProto _rfb, int p) {
            rfb = _rfb;
            port = p;
        }
       
	public void changeRfb(MyRfbProto _rfb) {
		rfb = _rfb;
	}

	public void run() {
		rfb.selectPort(port);

		while (true) {
			try {
				Socket newCli = rfb.accept();
				if(flag) throw new IOException();
				OutputStream os = newCli.getOutputStream();
				InputStream is = newCli.getInputStream();
//				if(confirmHalt(is)) break;
				rfb.newClient(this, newCli, new Writer(os), new Reader(is));
			} catch (IOException e) {
				break;
			}
		}
	}
	
	
	/**
	 * Instruction stop if you come from if WaitReply.
	 * @return if return true. This Thread halt.
	 */
	private boolean confirmHalt(InputStream is) throws IOException {
		byte[] b = new byte[4];
		is.mark(4);
		is.read(b);
		if(String.valueOf(b).equals("halt")) return true;
		is.reset();
		return false;
	}
}