Mercurial > hg > Members > you > TreeVNC
changeset 31:da007a84f111
update
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 21 Feb 2012 00:03:36 +0900 |
parents | a4ef63d95304 |
children | 3ad9ad996465 |
files | src/CuiMyVncClient.MF src/treeVnc/AcceptClient.java src/treeVnc/MyRfbProtoClient.java src/treeVnc/MyRfbProtoProxy.java src/treeVnc/RfbProto.java src/treeVnc/TextBoxClient.java src/treeVnc/VncCanvas.java |
diffstat | 7 files changed, 361 insertions(+), 318 deletions(-) [+] |
line wrap: on
line diff
--- a/src/CuiMyVncClient.MF Tue Feb 07 14:23:25 2012 +0900 +++ b/src/CuiMyVncClient.MF Tue Feb 21 00:03:36 2012 +0900 @@ -1,4 +1,4 @@ Manifest-Version: 1.0 Ant-Version: Apache Ant 1.8.2 Created-By: 1.6.0_24-b07-334-10M3326 (Apple Inc.) -Main-Class: myVncClient.CuiMyVncClient +Main-Class: treeVnc.CuiMyVncClient
--- a/src/treeVnc/AcceptClient.java Tue Feb 07 14:23:25 2012 +0900 +++ b/src/treeVnc/AcceptClient.java Tue Feb 21 00:03:36 2012 +0900 @@ -42,9 +42,9 @@ String port = is.readLine(); myAddress = getMyAddress(); if ("1".equals(line)||"3".equals(line)) { + String treeNumber = is.readLine(); // reply to Parents lost node - replyLeaderNode(os, is, port); - replyNormalChildren(os, is, port); + checkWait(os,is,port,Integer.parseInt(treeNumber)); } else if ("2".equals(line)) { // reply to not Found Parents @@ -111,8 +111,8 @@ } private void checkParameter(int parent, int counter, String leaderflag) { - System.out.println("pの値=" + parentnum); - System.out.println("iの値=" + counter); + System.out.println("number p =" + parentnum); + System.out.println("number i =" + counter); System.out.println("leaderflag=" + leaderflag + "\n"); } @@ -126,7 +126,7 @@ private void displyLinkedList( LinkedList<String> ls) { int g = 0; for (String bs : ls) { - System.out.println(g + "番目" + bs); + System.out.println(g + "number" + bs); g++; } } @@ -154,58 +154,43 @@ parentnum = (Integer.parseInt(port) - 1) / treebranch; newparent = ls.get(parentnum); sendleaderflag = decisionLeader(Integer.parseInt(port), treebranch); - leaderflag = decisionLeader(Integer.parseInt(port),treebranch); + //leaderflag = decisionLeader(Integer.parseInt(port),treebranch); } - private void replyLeaderNode(PrintStream os,BufferedReader is,String port) throws IOException, InterruptedException { - String checkRepetition = is.readLine(); // checkRepetition is TreeNumber - String tempLastNode = null; + private void replyLeaderNode(PrintStream os,BufferedReader is,String port,String treeNumber) throws IOException, InterruptedException { os.println(ls.getLast()); replyNodeInformation(port); counter--; passCheck = 1; - System.out.println("接続要求"+checkRepetition); - if(!((ls.size()-1)==Integer.parseInt(checkRepetition))) { - checkWait(Integer.parseInt(checkRepetition)); - reportLastNode(ls.getLast(), newparent,port, String.valueOf(parentnum),sendleaderflag); - } else { - tempLastNode = ls.getLast(); - } - System.out.println("接続します"+checkRepetition); + reportLastNode(ls.getLast(), newparent,port, String.valueOf(parentnum),sendleaderflag); listupdate(port); - if(!(ls.size()==Integer.parseInt(checkRepetition))) - checkWait(Integer.parseInt(checkRepetition)); - else - reportLastNode(tempLastNode, newparent,port, String.valueOf(parentnum),sendleaderflag); + waitThread(); + /* + if (Integer.parseInt(treeNumber)==ls.size()-1) + return; + */ displyLinkedList(ls); os.println(port); - runflag = true; - leaderflag = decisionLeader(Integer.parseInt(checkRepetition),treebranch); - if (Integer.parseInt(checkRepetition) == counter + 1) { - checkRepetition = "stop"; - } else { - checkRepetition = "go"; - } - os.println(checkRepetition); + leaderflag = decisionLeader(Integer.parseInt(treeNumber),treebranch); + os.println(treeNumber); Thread.sleep(intv_time); is.close(); os.close(); } - private void replyNormalChildren(PrintStream os,BufferedReader is,String port) throws IOException, InterruptedException { - String checkRepetition = is.readLine(); - if (Integer.parseInt(checkRepetition)==ls.size()-1) + private void replyNormalChildren(PrintStream os,BufferedReader is,String port,String treeNumber,boolean flag) throws IOException, InterruptedException { + if(flag) + notifyThread(); + else + waitThread(); + if (Integer.parseInt(treeNumber)==ls.size()) return; - System.out.println("接続要求"+checkRepetition); - checkWait(Integer.parseInt(checkRepetition)); - System.out.println("接続します"+checkRepetition); - checkWait(Integer.parseInt(checkRepetition)); os.println(ls.get(Integer.parseInt(port))); os.println(port); - if(ls.size()-1+passCheck == Integer.parseInt(checkRepetition)) - checkRepetition = "skip"; + if(ls.size()-1+passCheck == Integer.parseInt(treeNumber)) + treeNumber = "skip"; passCheck = 0; - os.println(checkRepetition); + os.println(treeNumber); System.out.println("num4="+ ls.get(Integer.parseInt(port))); runflag = false; is.close(); @@ -250,8 +235,7 @@ // echoSocket = new Socket(newchild, 10001 + (i + 1));// // i+1は実験中に同じマシーンを使っていたのでportを変えて対応、本番時には取り除く予定。 echoSocket = new Socket(newchild, 10001); - DataOutputStream os = new DataOutputStream( - echoSocket.getOutputStream()); + DataOutputStream os = new DataOutputStream(echoSocket.getOutputStream()); os.writeBytes(newparent + "\n"); os.writeBytes(newpnum + "\n"); os.writeBytes(newtreenum + "\n"); @@ -280,28 +264,28 @@ } - synchronized void checkWait(int treeNum) throws InterruptedException { + synchronized void checkWait(PrintStream os,BufferedReader is,String port,int treeNum) throws InterruptedException, IOException { final int TIMEOUT = 3000; if (passNumber == 0) { passNumber++; numberZone = ((treeNum - 1) / treebranch); System.out.println(treeNum + "--wait--"); - wait(TIMEOUT); + replyLeaderNode(os,is,port,String.valueOf(treeNum)); notifyAll(); passNumber = 0; } else if (numberZone == ((treeNum - 1) / treebranch)) { if (++passNumber == treebranch) { System.out.println(treeNum + "--notify--"); passNumber = 0; - notifyAll(); + replyNormalChildren(os,is,port,String.valueOf(treeNum),true); } else { - passNumber++; System.out.println(treeNum + "--waityobi--"); + replyNormalChildren(os,is,port,String.valueOf(treeNum),false); wait(TIMEOUT); } } else { wait(); - checkWait(treeNum); + checkWait(os,is,port,treeNum); } }
--- a/src/treeVnc/MyRfbProtoClient.java Tue Feb 07 14:23:25 2012 +0900 +++ b/src/treeVnc/MyRfbProtoClient.java Tue Feb 21 00:03:36 2012 +0900 @@ -574,7 +574,6 @@ g.dispose(); return bimg; } - void readSpeedCheck() throws IOException { byte[] b = new byte[1]; @@ -885,8 +884,8 @@ ByteBuffer header = bufs.get(inputIndex); if (header==null) continue; if (header.get(0)==RfbProto.CheckDelay) { - writeToClient(os, bufs, inputIndex); - continue; + System.out.println("--------------------"); + //writeToClient(os, bufs, inputIndex); } if (header.get(0)==RfbProto.FramebufferUpdate) { // System.out.println("client "+ myId);
--- a/src/treeVnc/MyRfbProtoProxy.java Tue Feb 07 14:23:25 2012 +0900 +++ b/src/treeVnc/MyRfbProtoProxy.java Tue Feb 21 00:03:36 2012 +0900 @@ -32,11 +32,10 @@ import java.util.zip.Inflater; import java.io.OutputStream; -public -class MyRfbProtoProxy extends RfbProto implements MyRfbProto { +public class MyRfbProtoProxy extends RfbProto implements MyRfbProto { final static String versionMsg_3_855 = "RFB 003.855\n"; /** - * CheckMillis is one of new msgType for RFB 3.855. + * CheckMillis is one of new msgType for RFB 3.855. */ final static byte SpeedCheckMillis = 4; @@ -46,9 +45,7 @@ // Supported authentication types final static int AuthAccess = 32; - - - private static final int INFLATE_BUFSIZE = 1024*100; + private static final int INFLATE_BUFSIZE = 1024 * 100; boolean printStatusFlag = false; long startCheckTime; private int messageType; @@ -63,7 +60,7 @@ private ServerSocket servSock; protected int acceptPort; - //private byte initData[]; + // private byte initData[]; byte initData[]; private LinkedList<Socket> cliListTmp; private LinkedList<Socket> cliList; @@ -74,26 +71,26 @@ byte[] pngBytes; - // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MostRecentMultiCast<LinkedList<ByteBuffer>>(10); + // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new + // MostRecentMultiCast<LinkedList<ByteBuffer>>(10); private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>(); private int clients = 0; private Inflater inflater = new Inflater(); private Deflater deflater = new Deflater(); private CreateThread geth; -// private Thread requestThread; + // private Thread requestThread; private RequestScreenThread rThread; private Thread requestThread; - - public - MyRfbProtoProxy() throws IOException { + + public MyRfbProtoProxy() throws IOException { } - + MyRfbProtoProxy(String h, int p, VncViewer v) throws IOException { super(h, p, v); - rThread = new RequestScreenThread(this); + rThread = new RequestScreenThread(this); requestThread = new Thread(rThread); -// requestThread = new Thread(new RequestScreenThread(this)); + // requestThread = new Thread(new RequestScreenThread(this)); } MyRfbProtoProxy(String h, int p, CreateThread geth) throws IOException { @@ -101,25 +98,25 @@ this.geth = geth; proxyFlag = true; - rThread = new RequestScreenThread(this); + rThread = new RequestScreenThread(this); requestThread = new Thread(rThread); -// requestThread = new Thread(new RequestScreenThread(this)); + // requestThread = new Thread(new RequestScreenThread(this)); } - + MyRfbProtoProxy(String h, int p) throws IOException { super(h, p); - rThread = new RequestScreenThread(this); + rThread = new RequestScreenThread(this); requestThread = new Thread(rThread); -// requestThread = new Thread(new RequestScreenThread(this)); + // requestThread = new Thread(new RequestScreenThread(this)); } - + // over write void writeVersionMsg() throws IOException { clientMajor = 3; if (serverMinor == 855) { - clientMinor = 855; - os.write(versionMsg_3_855.getBytes()); + clientMinor = 855; + os.write(versionMsg_3_855.getBytes()); } else if (serverMajor > 3 || serverMinor >= 8) { clientMinor = 8; os.write(versionMsg_3_8.getBytes()); @@ -138,19 +135,19 @@ servSock = new ServerSocket(port); acceptPort = port; } - + void authenticationRequestAccess() throws IOException { byte[] headBuf = new byte[2]; is.read(headBuf); - if(headBuf[1] == 2) { + if (headBuf[1] == 2) { byte[] b = new byte[258]; is.read(b); byte[] outBuf = new byte[256]; os.write(outBuf); os.flush(); - }else if(headBuf[1] == 23) { + } else if (headBuf[1] == 23) { byte[] b = new byte[130]; is.read(b); byte[] outBuf = new byte[192]; @@ -159,18 +156,19 @@ } int result = readU32(); - if(result != 0) { + if (result != 0) { System.out.println("faild authentication "); throw new IOException(); } - + } /* - * default port number is 5999. + * default port number is 5999. */ public void selectPort(int p) { - if(servSock != null ) return ; + if (servSock != null) + return; int port = p; while (true) { try { @@ -258,7 +256,7 @@ } void sendRfbVersion(OutputStream os) throws IOException { -// os.write(versionMsg_3_8.getBytes()); + // os.write(versionMsg_3_8.getBytes()); os.write(versionMsg_3_855.getBytes()); } @@ -282,28 +280,32 @@ if (rfbMajor < 3) { throw new IOException( - "RFB server does not support protocol version 3"); + "RFB server does not support protocol version 3"); } if (rfbMinor == 855) { sendProxyFlag(os); - if(proxyFlag)sendPortNumber(os); + if (proxyFlag) + sendPortNumber(os); } return rfbMinor; } + void sendProxyFlag(OutputStream os) throws IOException { - if(proxyFlag) os.write(1); - else os.write(0); + if (proxyFlag) + os.write(1); + else + os.write(0); } - - boolean readProxyFlag() throws IOException{ + + boolean readProxyFlag() throws IOException { int flag = readU8(); - if(flag == 1) + if (flag == 1) return true; else return false; } - + void sendPortNumber(OutputStream os) throws IOException { byte[] b = new byte[4]; b = castIntByte(geth.port); @@ -317,14 +319,10 @@ // 1:None os.write(1); -/* - os.write(4); - os.write(30); - os.write(31); - os.write(32); - os.write(35); - os.flush(); -*/ + /* + * os.write(4); os.write(30); os.write(31); os.write(32); os.write(35); + * os.flush(); + */ } void readSecType(InputStream is) throws IOException { @@ -332,26 +330,43 @@ is.read(b); } - void readSecType(InputStream is, OutputStream os) throws IOException { byte[] b = new byte[1]; is.read(b); int count = 260; - int[] data = {0, 2, 0, -128, -1, -1, -1, -1, -1, -1, -1, -1, -55, 15, -38, -94, 33, 104, -62, 52, -60, -58, 98, -117, -128, -36, 28, -47, 41, 2, 78, 8, -118, 103, -52, 116, 2, 11, -66, -90, 59, 19, -101, 34, 81, 74, 8, 121, -114, 52, 4, -35, -17, -107, 25, -77, -51, 58, 67, 27, 48, 43, 10, 109, -14, 95, 20, 55, 79, -31, 53, 109, 109, 81, -62, 69, -28, -123, -75, 118, 98, 94, 126, -58, -12, 76, 66, -23, -90, 55, -19, 107, 11, -1, 92, -74, -12, 6, -73, -19, -18, 56, 107, -5, 90, -119, -97, -91, -82, -97, 36, 17, 124, 75, 31, -26, 73, 40, 102, 81, -20, -26, 83, -127, -1, -1, -1, -1, -1, -1, -1, -1, -111, 73, -29, 30, 57, -67, -75, -77, -49, -50, -99, -76, -80, -80, 14, 65, 57, -105, -103, -54, -102, 3, 39, -44, 39, 35, 118, -84, -64, 37, -117, -21, 89, -31, -68, 70, 5, 122, -92, -119, 9, 121, 63, -112, -60, 122, -46, -69, -36, 92, -103, -92, 74, 92, -73, 87, 120, -8, 116, -47, 111, 20, -41, 110, 122, -3, -94, 14, 42, -51, -59, 48, -54, -125, 117, 60, 77, -52, -31, 98, 32, -2, -102, -15, -29, 58, -14, -106, -116, -32, -86, 50, -32, -16, -3, -123, 87, 88, -118, 10, 120, -107, -37, 125, -110, 59, 87, 93, -24, 124, -99, 18, 78, -13, -49, -34, -24, -27, 1, 114, -67, -98, -56, -3, 85, -67, -126, 77}; - for(int i=0; i<count; i++ ) { - os.write((byte)data[i]); + int[] data = { 0, 2, 0, -128, -1, -1, -1, -1, -1, -1, -1, -1, -55, 15, + -38, -94, 33, 104, -62, 52, -60, -58, 98, -117, -128, -36, 28, + -47, 41, 2, 78, 8, -118, 103, -52, 116, 2, 11, -66, -90, 59, + 19, -101, 34, 81, 74, 8, 121, -114, 52, 4, -35, -17, -107, 25, + -77, -51, 58, 67, 27, 48, 43, 10, 109, -14, 95, 20, 55, 79, + -31, 53, 109, 109, 81, -62, 69, -28, -123, -75, 118, 98, 94, + 126, -58, -12, 76, 66, -23, -90, 55, -19, 107, 11, -1, 92, -74, + -12, 6, -73, -19, -18, 56, 107, -5, 90, -119, -97, -91, -82, + -97, 36, 17, 124, 75, 31, -26, 73, 40, 102, 81, -20, -26, 83, + -127, -1, -1, -1, -1, -1, -1, -1, -1, -111, 73, -29, 30, 57, + -67, -75, -77, -49, -50, -99, -76, -80, -80, 14, 65, 57, -105, + -103, -54, -102, 3, 39, -44, 39, 35, 118, -84, -64, 37, -117, + -21, 89, -31, -68, 70, 5, 122, -92, -119, 9, 121, 63, -112, + -60, 122, -46, -69, -36, 92, -103, -92, 74, 92, -73, 87, 120, + -8, 116, -47, 111, 20, -41, 110, 122, -3, -94, 14, 42, -51, + -59, 48, -54, -125, 117, 60, 77, -52, -31, 98, 32, -2, -102, + -15, -29, 58, -14, -106, -116, -32, -86, 50, -32, -16, -3, + -123, 87, 88, -118, 10, 120, -107, -37, 125, -110, 59, 87, 93, + -24, 124, -99, 18, 78, -13, -49, -34, -24, -27, 1, 114, -67, + -98, -56, -3, 85, -67, -126, 77 }; + for (int i = 0; i < count; i++) { + os.write((byte) data[i]); os.flush(); } - + byte[] c = new byte[256]; is.read(c); - + System.out.println(new String(c)); } - void sendSecResult(OutputStream os) throws IOException { byte[] b = castIntByte(0); os.write(b); @@ -366,7 +381,6 @@ os.write(initData); } - void sendPngImage() { try { for (Socket cli : cliListTmp) { @@ -397,23 +411,23 @@ System.out.println("numBytesRead=" + numBytesRead); } - - void regiFramebufferUpdate() throws IOException { is.mark(20); - messageType = readU8(); // 0 - skipBytes(1); // 1 - rectangles = readU16(); // 2 - rectX = readU16(); // 4 - rectY = readU16(); // 6 - rectW = readU16(); // 8 - rectH = readU16(); // 10 - encoding = readU32(); // 12 -// System.out.println("encoding = "+encoding); - if (encoding == EncodingZRLE|| encoding==EncodingZRLEE||encoding==EncodingZlib) + messageType = readU8(); // 0 + skipBytes(1); // 1 + rectangles = readU16(); // 2 + rectX = readU16(); // 4 + rectY = readU16(); // 6 + rectW = readU16(); // 8 + rectH = readU16(); // 10 + encoding = readU32(); // 12 + // System.out.println("encoding = "+encoding); + if (encoding == EncodingZRLE || encoding == EncodingZRLEE + || encoding == EncodingZlib) zLen = readU32(); else zLen = 0; + System.out.println(zLen); is.reset(); } @@ -423,46 +437,45 @@ switch (encoding) { case RfbProto.EncodingRaw: dataLen = rectW * rectH * 4 + 16; -// is.mark(dataLen); + // is.mark(dataLen); break; case RfbProto.EncodingCopyRect: dataLen = 16 + 4; -// is.mark(dataLen); + // is.mark(dataLen); break; case RfbProto.EncodingRRE: case RfbProto.EncodingCoRRE: case RfbProto.EncodingHextile: case RfbProto.EncodingTight: dataLen = zLen + 20; -// is.mark(dataLen); + // is.mark(dataLen); break; case RfbProto.EncodingZlib: case RfbProto.EncodingZRLE: case RfbProto.EncodingZRLEE: dataLen = zLen + 20; - // is.mark(dataLen); + // is.mark(dataLen); break; case RfbProto.EncodingXCursor: case RfbProto.EncodingRichCursor: int pixArray = rectW * rectH * 4; - int u8Array = (int)Math.floor((rectW + 7)/8) * rectH; + int u8Array = (int) Math.floor((rectW + 7) / 8) * rectH; dataLen = pixArray + u8Array; printFramebufferUpdate(); -// is.mark(dataLen); + // is.mark(dataLen); break; default: dataLen = 1000000; -// is.mark(dataLen); + // is.mark(dataLen); } return dataLen; } - void sendDataToClient() throws Exception { regiFramebufferUpdate(); printFramebufferUpdate(); int dataLen = checkAndMark(); - readSendData(dataLen); + readSendData(dataLen); } BufferedImage createBufferedImage(Image img) { @@ -517,12 +530,12 @@ void printFramebufferUpdate() { /* - System.out.println("messageType=" + messageType); - System.out.println("rectangles=" + rectangles); - System.out.println("encoding=" + encoding); - System.out.println("rectX = "+rectX+": rectY = "+rectY); - System.out.println("rectW = "+rectW+": rectH = "+rectH); - */ + * System.out.println("messageType=" + messageType); + * System.out.println("rectangles=" + rectangles); + * System.out.println("encoding=" + encoding); + * System.out.println("rectX = "+rectX+": rectY = "+rectY); + * System.out.println("rectW = "+rectW+": rectH = "+rectH); + */ switch (encoding) { case RfbProto.EncodingRaw: System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 @@ -531,23 +544,23 @@ default: } } + int returnMsgtype() { return messageType; } - - + void readSpeedCheck() throws IOException { byte[] b = new byte[1]; readFully(b); } - + void startSpeedCheck() { ByteBuffer b = ByteBuffer.allocate(10); - b.put((byte)SpeedCheckMillis); + b.put((byte) SpeedCheckMillis); b.flip(); startCheckTime = System.currentTimeMillis(); - System.out.println("startChckTime = "+ startCheckTime); - LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); + System.out.println("startChckTime = " + startCheckTime); + LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); bufs.add(b); multicastqueue.put(bufs); } @@ -558,258 +571,291 @@ System.out.println("checkMillis: " + time); } - synchronized void changeStatusFlag() { printStatusFlag = true; } void printMills() { - if(printStatusFlag) { + if (printStatusFlag) { changeStatusFlag(); } else { changeStatusFlag(); } } - + void speedCheckMillis() { - Runnable stdin = new Runnable() { + Runnable stdin = new Runnable() { public void run() { int c; try { - while( (c = System.in.read()) != -1 ) { - switch(c) { - case 's': - break; - default: - startSpeedCheck(); - break; + while ((c = System.in.read()) != -1) { + switch (c) { + case 's': + break; + default: + startSpeedCheck(); + break; } } - }catch(IOException e){ + } catch (IOException e) { System.out.println(e); } } }; - + new Thread(stdin).start(); } - + void requestThreadStart() { requestThread.start(); } - public synchronized void requestThreadNotify() { rThread.reStart(); } - /** * gzip byte arrays + * * @param deflater * @param inputs * byte data[] - * @param inputIndex + * @param inputIndex * @param outputs * byte data[] - * @return byte length in last byte array + * @return byte length in last byte array * @throws IOException */ - public int zip(Deflater deflater,LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { + public int zip(Deflater deflater, LinkedList<ByteBuffer> inputs, + int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException { int len = 0; - ByteBuffer c1= ByteBuffer.allocate(INFLATE_BUFSIZE); - while(inputIndex < inputs.size() ) { + ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); + while (inputIndex < inputs.size()) { ByteBuffer b1 = inputs.get(inputIndex++); - deflater.setInput(b1.array(),b1.position(),b1.remaining()); + deflater.setInput(b1.array(), b1.position(), b1.remaining()); /** - * If we finish() stream and reset() it, Deflater start new gzip stream, this makes continuous zlib reader unhappy. - * if we remove finish(), Deflater.deflate() never flushes its output. The original zlib deflate has flush flag. I'm pretty - * sure this a kind of bug of Java library. + * If we finish() stream and reset() it, Deflater start new gzip + * stream, this makes continuous zlib reader unhappy. if we remove + * finish(), Deflater.deflate() never flushes its output. The + * original zlib deflate has flush flag. I'm pretty sure this a kind + * of bug of Java library. */ - if (inputIndex==inputs.size()) + if (inputIndex == inputs.size()) deflater.finish(); int len1 = 0; do { - len1 = deflater.deflate(c1.array(),c1.position(),c1.remaining()); - if (len1>0) { + len1 = deflater.deflate(c1.array(), c1.position(), + c1.remaining()); + if (len1 > 0) { len += len1; - c1.position(c1.position()+len1); - if (c1.remaining()==0) { - c1.flip(); outputs.addLast(c1); + c1.position(c1.position() + len1); + if (c1.remaining() == 0) { + c1.flip(); + outputs.addLast(c1); c1 = ByteBuffer.allocate(INFLATE_BUFSIZE); } } - } while (len1 >0 || !deflater.needsInput()); // &&!deflater.finished()); + } while (len1 > 0 || !deflater.needsInput()); // &&!deflater.finished()); } - if (c1.position()!=0) { - c1.flip(); outputs.addLast(c1); + if (c1.position() != 0) { + c1.flip(); + outputs.addLast(c1); } deflater.reset(); return len; } - + /** * gunzip byte arrays + * * @param inflater * @param inputs * byte data[] * @param outputs * byte data[] - *@return number of total bytes + * @return number of total bytes * @throws IOException */ - public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, int inputIndex, LinkedList<ByteBuffer> outputs,int bufSize) - throws DataFormatException { - int len=0; + public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs, + int inputIndex, LinkedList<ByteBuffer> outputs, int bufSize) + throws DataFormatException { + int len = 0; ByteBuffer buf = ByteBuffer.allocate(bufSize); while (inputIndex < inputs.size()) { ByteBuffer input = inputs.get(inputIndex++); - inflater.setInput(input.array(),input.position(),input.limit()); -// if (inputIndex==inputs.size()) if inflater/deflater has symmetry, we need this -// inflater.end(); but this won't work + inflater.setInput(input.array(), input.position(), input.limit()); + // if (inputIndex==inputs.size()) if inflater/deflater has symmetry, + // we need this + // inflater.end(); but this won't work do { - int len0 = inflater.inflate(buf.array(),buf.position(),buf.remaining()); - if (len0>0) { - buf.position(buf.position()+len0); + int len0 = inflater.inflate(buf.array(), buf.position(), + buf.remaining()); + if (len0 > 0) { + buf.position(buf.position() + len0); len += len0; - if (buf.remaining()==0) { + if (buf.remaining() == 0) { buf.flip(); outputs.addLast(buf); buf = ByteBuffer.allocate(bufSize); } } } while (!inflater.needsInput()); - } - if (buf.position()!=0) { + } + if (buf.position() != 0) { buf.flip(); outputs.addLast(buf); } return len; } - float maxMag = 1; + /** * send data to clients + * * @param dataLen * @throws IOException * @throws DataFormatException * - * Zlibed packet is compressed in context dependent way, that is, it have to send from the beginning. But this is - * impossible. So we have to compress it again for each clients. Separate deflater for each clients is necessary. + * Zlibed packet is compressed in context dependent way, that + * is, it have to send from the beginning. But this is + * impossible. So we have to compress it again for each clients. + * Separate deflater for each clients is necessary. * - * Java's deflater does not support flush. This means to get the result, we have to finish the compression. Reseting - * start new compression, but it is not accepted well in zlib continuous reading. So we need new Encoding ZRLEE - * which reset decoder for each packet. ZRLEE can be invisible from user, but it have to be implemented in the clients. - * ZRLEE compression is not context dependent, so no recompression is necessary. + * Java's deflater does not support flush. This means to get the + * result, we have to finish the compression. Reseting start new + * compression, but it is not accepted well in zlib continuous + * reading. So we need new Encoding ZRLEE which reset decoder + * for each packet. ZRLEE can be invisible from user, but it + * have to be implemented in the clients. ZRLEE compression is + * not context dependent, so no recompression is necessary. */ void sendDataCheckDelay() { - LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); + LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); ByteBuffer b = ByteBuffer.allocate(1); - b.put((byte)CheckDelay); + b.put((byte) CheckDelay); b.position(0); bufs.add(b); multicastqueue.put(bufs); } - + void readSendData(int dataLen) throws IOException, DataFormatException { - LinkedList<ByteBuffer>bufs = new LinkedList<ByteBuffer>(); + LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>(); ByteBuffer header = ByteBuffer.allocate(16); - readFully(header.array(),0,16); + readFully(header.array(), 0, 16); header.limit(16); - if (header.get(0)==RfbProto.FramebufferUpdate) { + if (header.get(0) == RfbProto.FramebufferUpdate) { int encoding = header.getInt(12); - if (encoding==RfbProto.EncodingZRLE||encoding==RfbProto.EncodingZlib) { // ZRLEE is already recompressed + if (encoding == RfbProto.EncodingZRLE + || encoding == RfbProto.EncodingZlib) { // ZRLEE is already + // recompressed ByteBuffer len = ByteBuffer.allocate(4); - readFully(len.array(),0,4); len.limit(4); - ByteBuffer inputData = ByteBuffer.allocate(dataLen-20); + readFully(len.array(), 0, 4); + len.limit(4); + ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20); startTiming(); - readFully(inputData.array(),0,inputData.capacity()); inputData.limit(dataLen-20); + readFully(inputData.array(), 0, inputData.capacity()); + inputData.limit(dataLen - 20); stopTiming(); - LinkedList<ByteBuffer>inputs = new LinkedList<ByteBuffer>(); + LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>(); inputs.add(inputData); - header.putInt(12, RfbProto.EncodingZRLEE); // means recompress every time - // using new Deflecter every time is incompatible with the protocol, clients have to be modified. + header.putInt(12, RfbProto.EncodingZRLEE); // means recompress + // every time + // using new Deflecter every time is incompatible with the + // protocol, clients have to be modified. Deflater nDeflater = deflater; // new Deflater(); LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); - unzip(inflater, inputs, 0 , out, INFLATE_BUFSIZE); + unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE); // dump32(inputs); int len2 = zip(nDeflater, out, 0, bufs); - ByteBuffer blen = ByteBuffer.allocate(4); blen.putInt(len2); blen.flip(); + ByteBuffer blen = ByteBuffer.allocate(4); + blen.putInt(len2); + blen.flip(); bufs.addFirst(blen); bufs.addFirst(header); multicastqueue.put(bufs); -// is.reset(); + // is.reset(); -/* - System.out.println("ZRLE = "+dataLen); - System.out.println("ZRLEE = "+(len2+20)); - float mag = (float)dataLen / (float)(len2 + 20); - System.out.println("ZRLE / ZRLEE = "+ mag); - if(mag > maxMag) maxMag = mag; - System.out.println("maxMag = "+maxMag); -*/ - return ; + /* + * System.out.println("ZRLE = "+dataLen); + * System.out.println("ZRLEE = "+(len2+20)); float mag = + * (float)dataLen / (float)(len2 + 20); + * System.out.println("ZRLE / ZRLEE = "+ mag); if(mag > maxMag) + * maxMag = mag; System.out.println("maxMag = "+maxMag); + */ + return; } - bufs.add(header); - if (dataLen>16) { - ByteBuffer b = ByteBuffer.allocate(dataLen-16); - startTiming(); - readFully(b.array(),0,dataLen-16); b.limit(dataLen-16); - stopTiming(); - bufs.add(b); + bufs.add(header); + if (dataLen > 16) { + ByteBuffer b = ByteBuffer.allocate(dataLen - 16); + startTiming(); + readFully(b.array(), 0, dataLen - 16); + b.limit(dataLen - 16); + stopTiming(); + bufs.add(b); + } + multicastqueue.put(bufs); + // is.reset(); + return; } - multicastqueue.put(bufs); -// is.reset(); - return ; - } - is.reset(); + is.reset(); - // It may be compressed. We can inflate here to avoid repeating clients decompressing here, - // but it may generate too many large data. It is better to do it in each client. - // But we have do inflation for all input data, so we have to do it here. + // It may be compressed. We can inflate here to avoid repeating clients + // decompressing here, + // but it may generate too many large data. It is better to do it in + // each client. + // But we have do inflation for all input data, so we have to do it + // here. } public void newClient(AcceptThread acceptThread, final Socket newCli, final OutputStream os, final InputStream is) throws IOException { // createBimgFlag = true; // rfb.addSockTmp(newCli); - // addSock(newCli); - final int myId = clients; - final MulticastQueue.Client <LinkedList<ByteBuffer>> c = multicastqueue.newClient(); + // addSock(newCli); + final int myId = clients; + final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue + .newClient(); final AtomicInteger writerRunning = new AtomicInteger(); writerRunning.set(1); /** - * Timeout thread. If a client is suspended, it has top of queue indefinitely, which caused memory - * overflow. After the timeout, we poll the queue and discard it. Start long wait if writer is running. + * Timeout thread. If a client is suspended, it has top of queue + * indefinitely, which caused memory overflow. After the timeout, we + * poll the queue and discard it. Start long wait if writer is running. */ final Runnable timer = new Runnable() { public void run() { int count = 0; - for(;;) { - long timeout = 50000/8; + for (;;) { + long timeout = 50000 / 8; try { - synchronized(this) { - int state,flag; + synchronized (this) { + int state, flag; writerRunning.set(0); wait(timeout); flag = 0; - while((state=writerRunning.get())==0) { + while ((state = writerRunning.get()) == 0) { c.poll(); // discard, should be timeout count++; - if (flag==0) { - System.out.println("Discarding "+myId + " count="+ count); flag = 1; + if (flag == 0) { + System.out.println("Discarding " + myId + + " count=" + count); + flag = 1; } - wait(10); // if this is too short, writer cannot take the poll, if this is too long, memory will overflow... + wait(10); // if this is too short, writer cannot + // take the poll, if this is too + // long, memory will overflow... } - if (flag==1) System.out.println("Resuming "+myId + " count="+count); - if (state!=1) { - System.out.println("Client died "+myId); + if (flag == 1) + System.out.println("Resuming " + myId + + " count=" + count); + if (state != 1) { + System.out.println("Client died " + myId); break; } } @@ -825,10 +871,11 @@ final Runnable reader = new Runnable() { public void run() { byte b[] = new byte[4096]; - for(;;) { + for (;;) { try { int c = is.read(b); - if (c<=0) throw new IOException(); + if (c <= 0) + throw new IOException(); // System.out.println("client read "+c); } catch (IOException e) { try { @@ -850,36 +897,46 @@ writerRunning.set(1); try { requestThreadNotify(); - //rThread.checkDelay(); + // rThread.checkDelay(); /** - * initial connection of RFB protocol + * initial connection of RFB protocol */ sendRfbVersion(os); -// readVersionMsg(is); - int rfbMinor = readVersionMsg(is,os); + // readVersionMsg(is); + int rfbMinor = readVersionMsg(is, os); sendSecurityType(os); readSecType(is); sendSecResult(os); readClientInit(is); sendInitData(os); - new Thread(reader).start(); // discard incoming packet here after. -// writeFramebufferUpdateRequest(0,0, framebufferWidth, framebufferHeight, false ); + new Thread(reader).start(); // discard incoming packet here + // after. + // writeFramebufferUpdateRequest(0,0, framebufferWidth, + // framebufferHeight, false ); + int i = 0; for (;;) { LinkedList<ByteBuffer> bufs = c.poll(); int inputIndex = 0; ByteBuffer header = bufs.get(inputIndex); - if (header==null) continue; - if (header.get(0)==RfbProto.CheckDelay) { + if (header == null) + continue; + if (header.get(0) == RfbProto.CheckDelay) { writeToClient(os, bufs, inputIndex); - //System.out.println("データ来たよ"); + // System.out.println("データ来たよ"); continue; } - if (header.get(0)==RfbProto.FramebufferUpdate) { + if (header.get(0) == RfbProto.FramebufferUpdate) { // System.out.println("client "+ myId); } + /* + if(i%20==0){ + sendDataCheckDelay(); + } + i++; + */ writeToClient(os, bufs, inputIndex); - writerRunning.set(1); // yes my client is awaking. + writerRunning.set(1); // yes my client is awaking. } } catch (IOException e) { try { @@ -887,14 +944,14 @@ os.close(); } catch (IOException e1) { } - /* if socket closed cliList.remove(newCli); */ + /* if socket closed cliList.remove(newCli); */ } } public void writeToClient(final OutputStream os, LinkedList<ByteBuffer> bufs, int inputIndex) throws IOException { - while(inputIndex < bufs.size()) { + while (inputIndex < bufs.size()) { ByteBuffer b = bufs.get(inputIndex++); os.write(b.array(), b.position(), b.limit()); } @@ -906,20 +963,20 @@ } - - public void dump32(LinkedList<ByteBuffer>bufs) { - int len =0; - for(ByteBuffer b: bufs) len += b.remaining(); + public void dump32(LinkedList<ByteBuffer> bufs) { + int len = 0; + for (ByteBuffer b : bufs) + len += b.remaining(); ByteBuffer top = bufs.getFirst(); ByteBuffer end = bufs.getLast(); - System.err.println("length: "+len); + System.err.println("length: " + len); System.err.print("head 0: "); - for(int i = 0; i<16 && i < top.remaining(); i++) { - System.err.print(" "+ top.get(i)); - } + for (int i = 0; i < 16 && i < top.remaining(); i++) { + System.err.print(" " + top.get(i)); + } System.err.print("tail 0: "); - for(int i = 0; i<16 && i < end.remaining(); i++) { - System.err.print(" "+end.get(i)); + for (int i = 0; i < 16 && i < end.remaining(); i++) { + System.err.print(" " + end.get(i)); } System.err.println(); } @@ -930,17 +987,17 @@ LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>(); LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>(); LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>(); -// if (false) { -// for(int i=0;i<10;i++) { -// in.add(ByteBuffer.wrap("test1".getBytes())); -// in.add(ByteBuffer.wrap("test2".getBytes())); -// in.add(ByteBuffer.wrap("test3".getBytes())); -// in.add(ByteBuffer.wrap("test44".getBytes())); -// } -// } else + // if (false) { + // for(int i=0;i<10;i++) { + // in.add(ByteBuffer.wrap("test1".getBytes())); + // in.add(ByteBuffer.wrap("test2".getBytes())); + // in.add(ByteBuffer.wrap("test3".getBytes())); + // in.add(ByteBuffer.wrap("test44".getBytes())); + // } + // } else { String t = ""; - for(int i=0;i<10;i++) { + for (int i = 0; i < 10; i++) { t += "test1"; t += "test2"; t += "test3"; @@ -948,55 +1005,54 @@ } in.add(ByteBuffer.wrap(t.getBytes())); } - + LinkedList<ByteBuffer> in1 = clone(in); Deflater deflater = new Deflater(); - zip(deflater,in,0,out); - // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend on deflator's state - unzip(inflater, out, 0,out2, INFLATE_BUFSIZE); - // inflater.reset(); + zip(deflater, in, 0, out); + // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend + // on deflator's state + unzip(inflater, out, 0, out2, INFLATE_BUFSIZE); + // inflater.reset(); equalByteBuffers(in1, out2); LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>(); deflater = new Deflater(); - zip(deflater,out2,0,out4); + zip(deflater, out2, 0, out4); LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>(); - unzip(inflater,out4,0, out5, INFLATE_BUFSIZE); - int len = equalByteBuffers(in1,out5); - - System.out.println("Test Ok. "+len); + unzip(inflater, out4, 0, out5, INFLATE_BUFSIZE); + int len = equalByteBuffers(in1, out5); + + System.out.println("Test Ok. " + len); } catch (Exception e) { - assertEquals(0,1); + assertEquals(0, 1); } } private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) { LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>(); - for(ByteBuffer b: in) { + for (ByteBuffer b : in) { ByteBuffer c = b.duplicate(); copy.add(c); } return copy; } - - public int equalByteBuffers(LinkedList<ByteBuffer> in, LinkedList<ByteBuffer> out2) { int len = 0; Iterable<Byte> i = byteBufferIterator(in); Iterator<Byte> o = byteBufferIterator(out2).iterator(); - for(int b: i) { - len ++; + for (int b : i) { + len++; if (o.hasNext()) { int c = o.next(); - assertEquals(b,c); - } else - assertEquals(0,1); + assertEquals(b, c); + } else + assertEquals(0, 1); } - if (o.hasNext()) - assertEquals(0,1); + if (o.hasNext()) + assertEquals(0, 1); // System.out.println(); return len; } @@ -1007,25 +1063,31 @@ return new Iterator<Byte>() { int bytes = 0; int buffers = 0; + public boolean hasNext() { - for(;;) { - if (buffers>=in.size()) return false; + for (;;) { + if (buffers >= in.size()) + return false; ByteBuffer b = in.get(buffers); - if (! (bytes<b.remaining())) { - buffers ++; bytes=0; - } else return true; + if (!(bytes < b.remaining())) { + buffers++; + bytes = 0; + } else + return true; } } + public Byte next() { - ByteBuffer bf =in.get(buffers); - byte b = bf.get(bytes++); - if (bf.remaining()<=bytes) { + ByteBuffer bf = in.get(buffers); + byte b = bf.get(bytes++); + if (bf.remaining() <= bytes) { buffers++; bytes = 0; } // System.out.print(b); return b; } + public void remove() { } }; @@ -1034,5 +1096,3 @@ } } - -
--- a/src/treeVnc/RfbProto.java Tue Feb 07 14:23:25 2012 +0900 +++ b/src/treeVnc/RfbProto.java Tue Feb 21 00:03:36 2012 +0900 @@ -368,10 +368,9 @@ // Find first supported security type. for (int i = 0; i < nSecTypes; i++) { // if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth) { - if (secTypes[i] == SecTypeNone || secTypes[i] == SecTypeVncAuth - || secTypes[i] == MyRfbProtoProxy.SecTypeReqAccess ) { - secType = secTypes[i]; + || secTypes[i] == MyRfbProtoProxy.SecTypeReqAccess) { + secType = secTypes[i]; break; } }
--- a/src/treeVnc/TextBoxClient.java Tue Feb 07 14:23:25 2012 +0900 +++ b/src/treeVnc/TextBoxClient.java Tue Feb 21 00:03:36 2012 +0900 @@ -86,7 +86,7 @@ counter++; } - void setButton() { + public void setButton() { panel.add(button); // panel.setBackground(Color.blue); button.addActionListener(this);
--- a/src/treeVnc/VncCanvas.java Tue Feb 07 14:23:25 2012 +0900 +++ b/src/treeVnc/VncCanvas.java Tue Feb 21 00:03:36 2012 +0900 @@ -417,6 +417,7 @@ switch (msgType) { case MyRfbProtoClient.CheckDelay: System.out.println("---------------into data------------"); + //rfbc.readSpeedCheck(); break; case MyRfbProtoClient.SpeedCheckMillis: rfbc.readSpeedCheck();