changeset 186:00e031baf065

merge
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Mon, 07 Nov 2011 12:48:16 +0900
parents 950ffe24f72e (diff) 1f583d6e0d1a (current diff)
children e7cc8bcf261d
files src/myVncProxy/MyRfbProto.java
diffstat 7 files changed, 279 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/src/myVncProxy/AcceptClient.java	Tue Oct 25 17:33:47 2011 +0900
+++ b/src/myVncProxy/AcceptClient.java	Mon Nov 07 12:48:16 2011 +0900
@@ -39,8 +39,10 @@
 				String port = is.readLine();
 				myAddress = getMyAddress();
 				if ("1".equals(line)) {
+					//Parents lost
 					replyLeaderNode(os, is, port);
 				} else if ("2".equals(line)) {
+					//not Found Parents
 					replyNodeInformation(port);
 					listupdate(port, newparent);
 					outputStream(os, newparent, String.valueOf(parentnum),
@@ -48,11 +50,18 @@
 					os.close();
 					is.close();
 				} else if ("3".equals(line)) {
+					//lost time send to not leader children
 					replyNormalChildren(os, is, port);
 					line = null;
 				} else {
-					if(replyCreateTree(os, is, port, line)) {
-						break;
+					//connection First time
+					if(checkAddress(line)){
+						outputStream(os, ls.getFirst(),"0",
+								"0", "0");
+					}else {
+						if(replyCreateTree(os, port, line)) {
+							break;
+						}
 					}
 				}
 			}
@@ -62,24 +71,35 @@
 			e.printStackTrace();
 		}
 	}
+	
+	private boolean checkAddress(String line){
+		String test[] = line.split("\\.");
+		int a = Integer.parseInt(test[0]);
+		int b = Integer.parseInt(test[1]);
+		if((192==a&&b==168)||(172==a&&(b>15||b<32))||10==a) {
+			return true;
+		} else{
+			return false;		
+		}
+	}
 
 	/**
 	 * @param port
 	 *            parent value
 	 */
-	void listupdate(String port) {
+	private void listupdate(String port) {
 		ls.remove(Integer.parseInt(port));
 		ls.add(Integer.parseInt(port), ls.getLast());
 		ls.removeLast();
 	}
 	
-	void listupdate(String port,String myaddr) {
+	private void listupdate(String port,String myaddr) {
 		ls.remove(Integer.parseInt(port));
 		ls.add(Integer.parseInt(port), myaddr);
 		ls.removeLast();
 	}
 
-	void outputStream(PrintStream os, String request, String parentnum,
+	private void outputStream(PrintStream os, String request, String parentnum,
 			String treenum, String leaderflag) {
 		os.println(request);
 		os.println(parentnum);
@@ -87,20 +107,20 @@
 		os.println(leaderflag);
 	}
 
-	void checkParameter(int parent, int counter, String leaderflag) {
+	private void checkParameter(int parent, int counter, String leaderflag) {
 		System.out.println("pの値=" + parentnum);
 		System.out.println("iの値=" + counter);
 		System.out.println("leaderflag=" + leaderflag + "\n");
 	}
 
-	void addClientAdress(String line, LinkedList<String> ls) {
+	private void addClientAdress(String line, LinkedList<String> ls) {
 		if (line != null) {
 			ls.add(line);
 		}
 		displyLinkedList(ls);
 	}
 	
-	void displyLinkedList( LinkedList<String> ls) {
+	private void displyLinkedList( LinkedList<String> ls) {
 		int g = 0;
 		for (String bs : ls) {
 			System.out.println(g + "番目" + bs);
@@ -108,7 +128,7 @@
 		}
 	}
 
-	String decisionLeader(int counter, int treebranch) {
+	private String decisionLeader(int counter, int treebranch) {
 		if ((counter - 1) % treebranch == 1) { // children in most young treenum
 												// have leaderflag 1 other 0
 			return "0";
@@ -117,19 +137,19 @@
 		}
 	}
 	
-	String getMyAddress () throws UnknownHostException {
+	private String getMyAddress () throws UnknownHostException {
 		InetAddress addr = InetAddress.getLocalHost();
 		return new String(addr.getHostAddress());
 	}
 	
-	void replyNodeInformation (String port) {
+	private void replyNodeInformation (String port) {
 		parentnum = (Integer.parseInt(port) - 1) / treebranch;
 		newparent = ls.get(parentnum);
 		sendleaderflag = decisionLeader(Integer.parseInt(port), treebranch);
 		leaderflag = decisionLeader(Integer.parseInt(port),treebranch);
 	}
 	
-	void replyLeaderNode(PrintStream os,BufferedReader is,String port) throws IOException, InterruptedException {
+	private void replyLeaderNode(PrintStream os,BufferedReader is,String port) throws IOException, InterruptedException {
 		String checkRepetition = is.readLine();
 		os.println(ls.getLast());
 		replyNodeInformation(port);
@@ -156,30 +176,21 @@
 		os.close();
 	}
 	
-	void replyNormalChildren(PrintStream os,BufferedReader is,String port) throws IOException {
+	private void replyNormalChildren(PrintStream os,BufferedReader is,String port) throws IOException {
 		String checkRepetition = is.readLine();
-		System.out.println("落ちたのを確認しました");
-
 		os.println(ls.get(Integer.parseInt(port)));
 		os.println(port);
-
-		
-		if(ls.size()-1+passCheck == Integer.parseInt(checkRepetition)) {
+		if(ls.size()-1+passCheck == Integer.parseInt(checkRepetition))
 			checkRepetition = "skip";
-		}
 		passCheck = 0;
-
 		os.println(checkRepetition);
-
-		System.out.println("num4="
-				+ ls.get(Integer.parseInt(port)));
-
+		System.out.println("num4="+ ls.get(Integer.parseInt(port)));
 		runflag = false;
 		is.close();
 		os.close();
 	}
 	
-	boolean replyCreateTree(PrintStream os,BufferedReader is,String port,String line) throws InterruptedException {
+	private boolean replyCreateTree(PrintStream os,String port,String line) throws InterruptedException {
 		if (addrRegistor == true) {
 			ls.add(myAddress);
 			addrRegistor = false;
--- a/src/myVncProxy/AcceptThread.java	Tue Oct 25 17:33:47 2011 +0900
+++ b/src/myVncProxy/AcceptThread.java	Mon Nov 07 12:48:16 2011 +0900
@@ -17,6 +17,10 @@
             rfb = _rfb;
             port = p;
         }
+      
+        public void changeRfb(MyRfbProto _rfb) {
+        	rfb = _rfb;
+        }
 
         public void run() {
                 rfb.selectPort(port);
--- a/src/myVncProxy/ButtonPanel.java	Tue Oct 25 17:33:47 2011 +0900
+++ b/src/myVncProxy/ButtonPanel.java	Mon Nov 07 12:48:16 2011 +0900
@@ -37,6 +37,7 @@
   Button clipboardButton;
   Button ctrlAltDelButton;
   Button refreshButton;
+  Button changeHost;
 
   ButtonPanel(VncViewer v) {
     viewer = v;
@@ -66,6 +67,9 @@
     refreshButton.setEnabled(false);
     add(refreshButton);
     refreshButton.addActionListener(this);
+    changeHost = new Button("Change Host");
+    add(changeHost);
+    changeHost.addActionListener(this);
   }
 
   //
@@ -126,6 +130,9 @@
     } else if (evt.getSource() == clipboardButton) {
       viewer.clipboard.setVisible(!viewer.clipboard.isVisible());
 
+    }else if (evt.getSource() == changeHost) {
+      //write when click to "Change Host"
+
     } else if (evt.getSource() == ctrlAltDelButton) {
       try {
         final int modifiers = InputEvent.CTRL_MASK | InputEvent.ALT_MASK;
--- a/src/myVncProxy/MyRfbProto.java	Tue Oct 25 17:33:47 2011 +0900
+++ b/src/myVncProxy/MyRfbProto.java	Mon Nov 07 12:48:16 2011 +0900
@@ -51,7 +51,6 @@
 	private static final int INFLATE_BUFSIZE = 1024*100;
 	boolean printStatusFlag = false;
 	long startCheckTime;
-
 	private int messageType;
 	private int rectangles;
 	private int rectX;
@@ -64,7 +63,8 @@
 
 	private ServerSocket servSock;
 	protected int acceptPort;
-	private byte initData[];
+	//private byte initData[];
+	byte initData[];
 	private LinkedList<Socket> cliListTmp;
 	private LinkedList<Socket> cliList;
 	boolean createBimgFlag;
@@ -598,6 +598,7 @@
 		requestThread.start();
 	}
 
+
 	public synchronized void requestThreadNotify() {
 		rThread.reStart();
 	}
--- a/src/myVncProxy/RfbProto.java	Tue Oct 25 17:33:47 2011 +0900
+++ b/src/myVncProxy/RfbProto.java	Mon Nov 07 12:48:16 2011 +0900
@@ -28,6 +28,7 @@
 import java.io.*;
 import java.awt.event.*;
 import java.net.Socket;
+import java.net.UnknownHostException;
 import java.util.zip.*;
 
 class RfbProto {
@@ -225,6 +226,19 @@
 	public RfbProto() {
 		
 	}
+	
+	public void changeRfbProto(String h,int port) throws IOException {
+		host = h;
+		sock=null;
+		sock = new Socket(host, port);
+		is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
+				16384));
+		os = sock.getOutputStream();
+
+		timing = false;
+		timeWaitedIn100us = 5;
+		timedKbits = 0;
+	}
 
 
 
--- a/src/myVncProxy/TextBox.java	Tue Oct 25 17:33:47 2011 +0900
+++ b/src/myVncProxy/TextBox.java	Mon Nov 07 12:48:16 2011 +0900
@@ -6,15 +6,17 @@
 
 public class TextBox extends JFrame implements ActionListener {
 
-	JPanel panel = new JPanel();
-	JButton button = new JButton("Connect");
-	TextField t1;
-	TextField t2;
-	JPasswordField pw;
-	double width;
-	double height;
-	JLabel label;
-	boolean flag;
+	private JPanel panel;
+	private JButton button;
+	private JButton button1;
+	private TextField t1;
+	private TextField t2;
+	private JPasswordField pw;
+	private double width;
+	private double height;
+	private JLabel label;
+	private boolean flag;
+	private VncProxyService vps;
 
 	public void ipRegister() {
 		setSize();
@@ -22,30 +24,56 @@
 		visible();
 	}
 
-	public TextBox() {
+	public TextBox(VncProxyService _vps) {
+		vps = _vps;
 		setTitle("Informatin Connection Address");
 		setResizable(false);
 		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 	}
 
-	void setSize() {
+	private void setSize() {
 		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
 		width = (d.getWidth() / 2);
 		height = (d.getHeight() / 2);
 	}
 
-	void visible() {
+	private void visible() {
 		Point point = new Point();
 		point.setLocation(width - 250, height - 80);
 		setLocation(point.getLocation());
 		pack();
 		setVisible(true);
 	}
-
-	void setText() {
+	
+	private void init(){
+		frameInit();
+//		panel = null;
+//		button = null;
+//		label = null;
+		setVisible(false);
+	}
+	
+	public void changeHost() {
+		init();
+		button1 = new JButton("Change Host");
 		t1 = new TextField("you.st.ie.u-ryukyu.ac.jp", 30);
+		panel = new JPanel();
+		panel.add(t1);
+		panel.add(button1);
+		button1.addActionListener(this);
+		label = new JLabel();
+		Container contentPane = getContentPane();
+		contentPane.add(panel, BorderLayout.CENTER);
+		contentPane.add(label, BorderLayout.SOUTH);
+		visible();
+	}
+
+	private void setText() {
+		panel = new JPanel();
+		t1 = new TextField("you.cr.ie.u-ryukyu.ac.jp", 30);
 		t2 = new TextField("5900", 5);
-		pw = new JPasswordField("kakiku");
+		pw = new JPasswordField("Password",30);
+		button = new JButton("Connect");
 		panel.add(t1);
 		panel.add(t2);
 		panel.add(pw);
@@ -76,6 +104,15 @@
 	}
 
 	public void actionPerformed(ActionEvent e) {
-		flag = true;
+		if(e.getSource()==button)
+			flag = true;
+		if(e.getSource()==button1)
+			try {
+				init();
+				vps.host = getAddress();
+				vps.changeInit();
+			} catch (Exception e1) {
+				e1.printStackTrace();
+			}
 	}
 }
\ No newline at end of file
--- a/src/myVncProxy/VncProxyService.java	Tue Oct 25 17:33:47 2011 +0900
+++ b/src/myVncProxy/VncProxyService.java	Mon Nov 07 12:48:16 2011 +0900
@@ -6,6 +6,7 @@
 import java.net.*;
 
 public class VncProxyService implements java.lang.Runnable {
+	
 
 	public static void main(String[] argv) {
 		VncProxyService v = new VncProxyService();
@@ -25,11 +26,13 @@
 
 	// RfbProto rfb;
 	MyRfbProto rfb;
+	MyRfbProto testrfb;
 	Thread rfbThread;
 	Thread accThread;
 	Thread clientThread;
 	CreateThread geth;
 	AcceptClient acc;
+	private AcceptThread acceptThread;
 	private TextBox getHost;
 
 	Frame vncFrame;
@@ -67,6 +70,7 @@
 	int debugStatsMeasureUpdates;
 
 	int echoPort = 9999;
+	private boolean changeFlag;
 
 	String url;
 
@@ -121,7 +125,7 @@
 	//
 
 	public void init() {
-
+		
 		readParameters();
 
 		options = new OptionsNoFrame(this);
@@ -148,11 +152,15 @@
 		} catch (Exception e) {
 			
 		}
-
-		rfb.selectPort(5999);
-		rfbThread = new Thread(this);
-		accThread = new Thread(new AcceptThread(rfb, 5999));
-
+/*		
+		if(changeFlag){
+			acceptThread.changeRfb(rfb);
+		} else {*/
+			rfb.selectPort(5999);
+			rfbThread = new Thread(this);
+			acceptThread = new AcceptThread(rfb, 5999);
+			accThread = new Thread(acceptThread);
+		//}
 		/*
 		 * if(url != null) { try { client = new XmlRpc(blogId, wpUsername,
 		 * wpPassword, url ); InetAddress addr = InetAddress.getLocalHost();
@@ -167,6 +175,57 @@
 		 */
 
 	}
+	
+	
+	public void changeInit() throws Exception {
+		VncProxyService v = new VncProxyService();
+		v.changeFlag = true;
+		// v.checkArgs(argv);
+
+		v.mainArgs = new String[0];
+		v.host = host;
+		v.init();
+		v.start_threads();
+		
+		//Thread.sleep(10000);
+		rfb.is = v.rfb.is;
+		//rfb.os = v.rfb.os;
+		//rfb.initData = v.rfb.initData;
+	
+//		changeConnection();
+//		doProtocolInitialisation();
+	/*	
+		//readParameters();
+		options = new OptionsNoFrame(this);
+		recordingSync = new Object();
+
+		sessionFileName = null;
+		recordingActive = false;
+		recordingStatusChanged = false;
+		cursorUpdatesDef = null;
+		eightBitColorsDef = null;
+
+		try {
+			changeConnection();
+			doProtocolInitialisation();
+		} catch (NoRouteToHostException e) {
+			fatalError("Network error: no route to server: " + host, e);
+		} catch (UnknownHostException e) {
+			fatalError("Network error: server name unknown: " + host, e);
+		} catch (ConnectException e) {
+			fatalError("Network error: could not connect to server: " + host
+					+ ":" + port, e);
+		} catch (IOException e) {
+			fatalError("Network error: faild connect or authenticate to server");
+		} catch (Exception e) {
+			
+		}
+		
+		//acceptThread.changeRfb(rfb);
+		//vc.rfb = rfb;
+		//rfbThread.start();
+*/
+	}
 
 	/*
 	 * void setStatusXmlRpc(XmlRpc client, String title, String hostname, int
@@ -178,18 +237,16 @@
 		String description = "<h1>" + hostname + " " + port + "<h1>\n";
 		return description;
 	}
-
+	
 	public void start_threads() {
 		rfbThread.start();
 		accThread.start();
 		rfb.requestThreadStart();
-
 	}
 
 	//
 	// run() - executed by the rfbThread to deal with the RFB socket.
 	//
-
 	public void run() {
 
 		try {
@@ -270,7 +327,8 @@
 
 	void connectAndAuthenticate() throws Exception {
 		if (mainArgs.length == 0)
-			acc = new AcceptClient(getHost.getAddress());
+			acc = new AcceptClient(host);
+			//acc = new AcceptClient(getHost.getAddress());
 		// acc = new AcceptClient();
 		else
 			acc = new AcceptClient(mainArgs[0]);
@@ -326,6 +384,67 @@
 			throw new Exception("Unknown authentication scheme " + authType);
 		}
 	}
+	
+	void changeConnection() throws Exception {
+		/*
+		acc = new AcceptClient(getHost.getAddress());
+		geth = new CreateThread(acc, echoPort);
+		Thread thread = new Thread(geth);
+		thread.start();
+		*/
+		showConnectionStatus("Initializing...");
+
+		showConnectionStatus("Connecting to " + host + ", port " + port + "...");
+		
+		//rfb = null;
+		//rfb.is = null;
+
+		testrfb = new MyRfbProto(host, port);
+		showConnectionStatus("Connected to server");
+
+		testrfb.readVersionMsg();
+		showConnectionStatus("RFB server supports protocol version "
+				+ testrfb.serverMajor + "." + testrfb.serverMinor);
+
+		testrfb.writeVersionMsg();
+		showConnectionStatus("Using RFB protocol version " + testrfb.clientMajor
+				+ "." + testrfb.clientMinor);
+
+		int secType = testrfb.negotiateSecurity();
+		int authType;
+		if (secType == RfbProto.SecTypeTight) {
+			showConnectionStatus("Enabling TightVNC protocol extensions");
+			testrfb.setupTunneling();
+			authType = testrfb.negotiateAuthenticationTight();
+		} else {
+			authType = secType;
+		}
+
+		switch (authType) {
+		case MyRfbProto.AuthAccess:
+			testrfb.authenticationRequestAccess();
+			System.out.println("authenticateion Request right of Acces");
+			break;
+		case RfbProto.AuthNone:
+			showConnectionStatus("No authentication needed");
+			testrfb.authenticateNone();
+			break;
+		case RfbProto.AuthVNC:
+			showConnectionStatus("Performing standard VNC authentication");
+			if (passwordParam != null) {
+				testrfb.authenticateVNC(passwordParam);
+			} else {
+				String pw = askPassword();
+				testrfb.authenticateVNC(pw);
+			}
+			break;
+		default:
+			throw new Exception("Unknown authentication scheme " + authType);
+		}
+		rfb.is = testrfb.is;
+		rfb.os = testrfb.os;
+		//rfb = testrfb;
+	}
 
 	//
 	// Show a message describing the connection status.
@@ -601,22 +720,29 @@
 	// param_name/param_value pairs where the names and values correspond to
 	// those expected in the html applet tag source.
 	//
-
+	
 	void readParameters() {
-		if (mainArgs.length == 0) {
-			getHost = new TextBox();
-			getHost.ipRegister();
-			host = getHost.getAddress();
+		if (!(changeFlag)) {
+			if (mainArgs.length == 0) {
+				getHost = new TextBox(this);
+				getHost.ipRegister();
+				host = getHost.getAddress();
+			} else {
+				host = mainArgs[0];
+			}
+
+			if (mainArgs.length < 2) {
+				port = Integer.parseInt(getHost.getPort());
+				// port = 5900;
+			} else {
+				port = Integer.parseInt(mainArgs[1]);
+			}
 		} else {
-			host = mainArgs[0];
+			getHost = new TextBox(this);
+			port = 5900;
 		}
-
-		if (mainArgs.length < 2) {
-			port = Integer.parseInt(getHost.getPort());
-			// port = 5900;
-		} else {
-			port = Integer.parseInt(mainArgs[1]);
-		}
+		
+		//getHost.changeHost();
 
 		// Read "ENCPASSWORD" or "PASSWORD" parameter if specified.
 		// readPasswordParameters();
@@ -693,20 +819,22 @@
 	}
 
 	public String readParameter(String name, boolean required) {
-		for (int i = 0; i < mainArgs.length; i += 2) {
-			if (mainArgs[i].equalsIgnoreCase(name)) {
-				try {
-					return mainArgs[i + 1];
-				} catch (Exception e) {
-					if (required) {
-						fatalError(name + " parameter not specified");
+		if(mainArgs!=null){
+			for (int i = 0; i < mainArgs.length; i += 2) {
+				if (mainArgs[i].equalsIgnoreCase(name)) {
+					try {
+						return mainArgs[i + 1];
+					} catch (Exception e) {
+						if (required) {
+							fatalError(name + " parameter not specified");
+						}
+						return null;
 					}
-					return null;
 				}
 			}
-		}
-		if (required) {
-			fatalError(name + " parameter not specified");
+			if (required) {
+				fatalError(name + " parameter not specified");
+			}
 		}
 		return null;
 	}