changeset 1:1d5cebe3bca0

update
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Thu, 23 Feb 2012 16:18:42 +0900
parents 756bfaf731f3
children 5d72f4c7371d
files src/treeVnc/MyRfbProtoClient.java src/treeVnc/MyRfbProtoProxy.java src/treeVnc/MyVncClient.java src/treeVnc/RfbProto.java
diffstat 4 files changed, 87 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/treeVnc/MyRfbProtoClient.java	Tue Feb 21 04:10:12 2012 +0900
+++ b/src/treeVnc/MyRfbProtoClient.java	Thu Feb 23 16:18:42 2012 +0900
@@ -14,6 +14,9 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.BindException;
+import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.MulticastSocket;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.nio.ByteBuffer;
@@ -54,6 +57,7 @@
 	private int encoding;
 	private int zLen;
 	//private boolean clicomp = false;
+	private byte[] broadCastBuf = new byte[64000];
 
 	private ServerSocket servSock;
 	private int acceptPort;
@@ -161,8 +165,7 @@
 		port = p;
 		
 		sock = new Socket(host, port);
-		is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
-				16384));
+		is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),16384));
 		os = sock.getOutputStream();
 
 		timing = false;
@@ -404,8 +407,54 @@
 	}
 
 
+	Runnable sendr = new Runnable() {
+		public void run() {
+			broadCastCommnication();	
+		}
+	};
+		
+	void mcastStart(){
+		new Thread(sendr).start();
+	}
+	
+		
+	void broadCastCommnication() {
+		String mcastAddr = "224.0.0.1";
+		int port = 8192;
+		int bufSize = 1024;
+
+        String recvData;
+        is = new DataInputStream(new ByteArrayInputStream(broadCastBuf));
+        
+        try {
+            InetAddress mAddr = InetAddress.getByName(mcastAddr);
+            MulticastSocket soc = new MulticastSocket(port);
+            DatagramPacket recvPacket = new DatagramPacket(broadCastBuf, bufSize);
+
+            soc.joinGroup(mAddr);
+            //System.out.println("マルチキャスト"+mcastAddr+"に参加します");
+
+            while(true) {
+                soc.receive(recvPacket);
+                recvData =new String(recvPacket.getData());
+                recvData = recvData.trim();
+                if(recvData.equals("quit")) {
+                    break;
+                }
+                // System.out.println(recvData);
+            }
+            soc.leaveGroup(mAddr);
+            soc.close();
+        } catch(IOException e) {
+            e.printStackTrace();
+        }
+	}
+	
+	
+	
 	
 	void regiFramebufferUpdate() throws IOException {
+		mcastStart();
 		is.mark(20);
 		messageType = readU8();   // 0
 		skipBytes(1);                    // 1
@@ -786,6 +835,7 @@
 
 	public void newClient(AcceptThread acceptThread, final Socket newCli,
 			final OutputStream os, final InputStream is) throws IOException {
+		mcastStart();
 		// createBimgFlag = true;
 		// rfb.addSockTmp(newCli);
 		//		addSock(newCli);
--- a/src/treeVnc/MyRfbProtoProxy.java	Tue Feb 21 04:10:12 2012 +0900
+++ b/src/treeVnc/MyRfbProtoProxy.java	Thu Feb 23 16:18:42 2012 +0900
@@ -13,6 +13,9 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.BindException;
+import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.MulticastSocket;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.nio.ByteBuffer;
@@ -757,7 +760,7 @@
 
 				startTiming();
 				readFully(inputData.array(), 0, inputData.capacity());
-				System.out.println(dataLen);
+				//System.out.println(dataLen);
 				inputData.limit(dataLen - 20);
 				stopTiming();
 
@@ -947,13 +950,37 @@
 					/* if socket closed cliList.remove(newCli); */
 				}
 			}
+			
+			void broadCastCommunication(byte[] buf,int len) {
+				String mcastAddr = "224.0.0.1";
+				int port = 8192;
+						
+				try {
+					InetAddress mAddr = InetAddress.getByName(mcastAddr);
+					MulticastSocket soc = new MulticastSocket();
+					DatagramPacket sendPacket =null;
+					soc.setTimeToLive(100);
+					
+//					System.out.println("Multicast = " + mcastAddr);
+					//while(buf != null) {
+//						int len = System.in.read(buf);
+					sendPacket = new DatagramPacket(buf,len,mAddr,port);
+					soc.send(sendPacket);
+//					}
+					soc.close();
+				} catch(IOException e) {
+					e.printStackTrace();
+				}
+				
+			}
 
 			public void writeToClient(final OutputStream os,
 					LinkedList<ByteBuffer> bufs, int inputIndex)
 					throws IOException {
 				while (inputIndex < bufs.size()) {
 					ByteBuffer b = bufs.get(inputIndex++);
-					os.write(b.array(), b.position(), b.limit());
+					broadCastCommunication(b.array(),b.limit()-b.position());
+//					os.write(b.array(), b.position(), b.limit());
 				}
 				os.flush();
 			}
--- a/src/treeVnc/MyVncClient.java	Tue Feb 21 04:10:12 2012 +0900
+++ b/src/treeVnc/MyVncClient.java	Thu Feb 23 16:18:42 2012 +0900
@@ -184,8 +184,8 @@
 
 			if (first) {
 				connectAndAuthenticate();
-				accThread = new Thread(new AcceptThread(rfb, 5999));
-				accThread.start();
+				//accThread = new Thread(new AcceptThread(rfb, 5999));
+				//accThread.start();
 				first = false;
 			} else {
 				System.out.println("reConnectAndAuthenticate() ");
--- a/src/treeVnc/RfbProto.java	Tue Feb 21 04:10:12 2012 +0900
+++ b/src/treeVnc/RfbProto.java	Thu Feb 23 16:18:42 2012 +0900
@@ -178,6 +178,8 @@
 	// If true, informs that the RFB socket was closed.
 //	private boolean closed;
 	protected boolean closed;
+	
+	private byte[] broadCastBuf = new byte[64000];
 
 	//
 	// Constructor. Make TCP connection to RFB server.
@@ -217,8 +219,8 @@
 		port = p;
 
 		sock = new Socket(host, port);
-		is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),
-				16384));
+
+		is = new DataInputStream(new BufferedInputStream(sock.getInputStream(),16384));
 		os = sock.getOutputStream();
 
 		timing = false;