annotate src/main/java/jp/ac/u_ryukyu/treevnc/server/MyRfbProtoProxy.java @ 12:12c3a73be47f

rename package
author one
date Tue, 21 Aug 2012 14:24:38 +0900
parents src/main/java/ac/ryukyu/treevnc/server/MyRfbProtoProxy.java@57ae9fbb1245
children ff01665d26b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
12c3a73be47f rename package
one
parents: 11
diff changeset
1 package jp.ac.u_ryukyu.treevnc.server;
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 import static org.junit.Assert.*;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 import java.awt.Graphics;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 import java.awt.Image;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 import java.awt.image.BufferedImage;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 import java.io.BufferedOutputStream;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 import java.io.BufferedReader;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 import java.io.ByteArrayInputStream;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 import java.io.ByteArrayOutputStream;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 import java.io.IOException;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 import java.io.InputStream;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 import java.io.InputStreamReader;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 import java.net.BindException;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 import java.net.ServerSocket;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 import java.net.Socket;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 import java.nio.ByteBuffer;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 import java.util.Iterator;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 import java.util.LinkedList;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 import javax.imageio.ImageIO;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 import org.junit.Test;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 //import myVncProxy.MulticastQueue.Client;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 import java.util.concurrent.ExecutorService;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 import java.util.concurrent.atomic.AtomicInteger;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 import java.util.zip.DataFormatException;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 import java.util.zip.Deflater;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 import java.util.zip.Inflater;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 import java.io.OutputStream;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
12
12c3a73be47f rename package
one
parents: 11
diff changeset
36 import jp.ac.u_ryukyu.treevnc.MulticastQueue;
12c3a73be47f rename package
one
parents: 11
diff changeset
37
11
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 public class MyRfbProtoProxy {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 final static String versionMsg_3_855 = "RFB 003.855\n";
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 * CheckMillis is one of new msgType for RFB 3.855.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 final static byte SpeedCheckMillis = 4;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 // Secyrity type of OS X
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 final static int SecTypeReqAccess = 32;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 // Supported authentication types
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 final static int AuthAccess = 32;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 private static final int INFLATE_BUFSIZE = 1024 * 100;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 boolean printStatusFlag = false;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 long startCheckTime;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 private int messageType;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 private int rectangles;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 private int rectX;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 private int rectY;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 private int rectW;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 private int rectH;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 private int encoding;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 private int zLen;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 private boolean clicomp = false;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 private ServerSocket servSock;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 protected int acceptPort;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 // private byte initData[];
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 byte initData[];
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 private LinkedList<Socket> cliListTmp;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 private LinkedList<Socket> cliList;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 boolean createBimgFlag;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 boolean proxyFlag = true;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 ExecutorService executor;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 byte[] pngBytes;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 // private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 // MostRecentMultiCast<LinkedList<ByteBuffer>>(10);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 private MulticastQueue<LinkedList<ByteBuffer>> multicastqueue = new MulticastQueue<LinkedList<ByteBuffer>>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 private int clients = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 private Inflater inflater = new Inflater();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 private Deflater deflater = new Deflater();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 private CreateThread geth;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 // private Thread requestThread;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 private RequestScreenThread rThread;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 private Thread requestThread;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 public MyRfbProtoProxy() throws IOException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 void initServSock(int port) throws IOException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 servSock = new ServerSocket(port);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 acceptPort = port;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 void authenticationRequestAccess() throws IOException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 byte[] headBuf = new byte[2];
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 is.read(headBuf);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 if (headBuf[1] == 2) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 byte[] b = new byte[258];
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 is.read(b);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 byte[] outBuf = new byte[256];
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 os.write(outBuf);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 os.flush();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 } else if (headBuf[1] == 23) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 byte[] b = new byte[130];
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 is.read(b);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 byte[] outBuf = new byte[192];
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 os.write(outBuf);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 os.flush();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 int result = readU32();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 if (result != 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 System.out.println("faild authentication ");
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 throw new IOException();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 /*
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 * default port number is 5999.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 public void selectPort(int p) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 if (servSock != null)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 return;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 int port = p;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 while (true) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 try {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 initServSock(port);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 break;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 } catch (BindException e) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 port++;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 continue;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 } catch (IOException e) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 System.out.println("accept port = " + port);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 int getAcceptPort() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 return acceptPort;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
148
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 void setSoTimeout(int num) throws IOException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 servSock.setSoTimeout(num);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
152
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 public Socket accept() throws IOException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 return servSock.accept();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
156
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 void addSock(Socket sock) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 cliList.add(sock);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
160
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 void addSockTmp(Socket sock) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 System.out.println("connected " + sock.getInetAddress());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 cliListTmp.add(sock);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
165
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 boolean markSupported() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 return is.markSupported();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 synchronized void changeStatusFlag() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 printStatusFlag = true;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 void printMills() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 if (printStatusFlag) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 changeStatusFlag();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 } else {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 changeStatusFlag();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
182
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
183
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 void requestThreadStart() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 requestThread.start();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
187
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 public synchronized void requestThreadNotify() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 rThread.reStart();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
191
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 * gzip byte arrays
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 *
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 * @param deflater
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 * @param inputs
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 * byte data[]
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 * @param inputIndex
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 * @param outputs
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 * byte data[]
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 * @return byte length in last byte array
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 * @throws IOException
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 public int zip(Deflater deflater, LinkedList<ByteBuffer> inputs,
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 int inputIndex, LinkedList<ByteBuffer> outputs) throws IOException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 int len = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 ByteBuffer c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 while (inputIndex < inputs.size()) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 ByteBuffer b1 = inputs.get(inputIndex++);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 deflater.setInput(b1.array(), b1.position(), b1.remaining());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 * If we finish() stream and reset() it, Deflater start new gzip
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 * stream, this makes continuous zlib reader unhappy. if we remove
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 * finish(), Deflater.deflate() never flushes its output. The
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 * original zlib deflate has flush flag. I'm pretty sure this a kind
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 * of bug of Java library.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 if (inputIndex == inputs.size())
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 deflater.finish();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 int len1 = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 do {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 len1 = deflater.deflate(c1.array(), c1.position(),
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 c1.remaining());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 if (len1 > 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 len += len1;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 c1.position(c1.position() + len1);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 if (c1.remaining() == 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 c1.flip();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 outputs.addLast(c1);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 c1 = ByteBuffer.allocate(INFLATE_BUFSIZE);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 } while (len1 > 0 || !deflater.needsInput()); // &&!deflater.finished());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 if (c1.position() != 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 c1.flip();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 outputs.addLast(c1);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 deflater.reset();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 return len;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
242
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 * gunzip byte arrays
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 *
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 * @param inflater
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 * @param inputs
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 * byte data[]
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 * @param outputs
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 * byte data[]
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 * @return number of total bytes
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 * @throws IOException
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 public int unzip(Inflater inflater, LinkedList<ByteBuffer> inputs,
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 int inputIndex, LinkedList<ByteBuffer> outputs, int bufSize)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 throws DataFormatException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 int len = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 ByteBuffer buf = ByteBuffer.allocate(bufSize);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 while (inputIndex < inputs.size()) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 ByteBuffer input = inputs.get(inputIndex++);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 inflater.setInput(input.array(), input.position(), input.limit());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 // if (inputIndex==inputs.size()) if inflater/deflater has symmetry,
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 // we need this
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 // inflater.end(); but this won't work
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 do {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 int len0 = inflater.inflate(buf.array(), buf.position(),
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 buf.remaining());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 if (len0 > 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 buf.position(buf.position() + len0);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 len += len0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 if (buf.remaining() == 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 buf.flip();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 outputs.addLast(buf);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 buf = ByteBuffer.allocate(bufSize);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 } while (!inflater.needsInput());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 if (buf.position() != 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 buf.flip();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 outputs.addLast(buf);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 return len;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
285
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 float maxMag = 1;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
287
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 * send data to clients
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 *
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 * @param dataLen
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 * @throws IOException
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 * @throws DataFormatException
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 *
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 * Zlibed packet is compressed in context dependent way, that
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 * is, it have to send from the beginning. But this is
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 * impossible. So we have to compress it again for each clients.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 * Separate deflater for each clients is necessary.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 *
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 * Java's deflater does not support flush. This means to get the
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 * result, we have to finish the compression. Reseting start new
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 * compression, but it is not accepted well in zlib continuous
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 * reading. So we need new Encoding ZRLEE which reset decoder
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 * for each packet. ZRLEE can be invisible from user, but it
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 * have to be implemented in the clients. ZRLEE compression is
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 * not context dependent, so no recompression is necessary.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
308
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 void readSendData(int dataLen) throws IOException, DataFormatException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 LinkedList<ByteBuffer> bufs = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 ByteBuffer header = ByteBuffer.allocate(16);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 readFully(header.array(), 0, 16);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 header.limit(16);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 if (header.get(0) == RfbProto.FramebufferUpdate) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 int encoding = header.getInt(12);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 if (encoding == RfbProto.EncodingZRLE
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 || encoding == RfbProto.EncodingZlib) { // ZRLEE is already
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 // recompressed
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 ByteBuffer len = ByteBuffer.allocate(4);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 readFully(len.array(), 0, 4);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 len.limit(4);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 ByteBuffer inputData = ByteBuffer.allocate(dataLen - 20);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
323
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 startTiming();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 readFully(inputData.array(), 0, inputData.capacity());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 // System.out.println(dataLen);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 inputData.limit(dataLen - 20);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 stopTiming();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
329
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 LinkedList<ByteBuffer> inputs = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 inputs.add(inputData);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
332
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 header.putInt(12, RfbProto.EncodingZRLEE); // means recompress
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 // every time
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 // using new Deflecter every time is incompatible with the
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 // protocol, clients have to be modified.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 Deflater nDeflater = deflater; // new Deflater();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 unzip(inflater, inputs, 0, out, INFLATE_BUFSIZE);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 // dump32(inputs);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 int len2 = zip(nDeflater, out, 0, bufs);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 ByteBuffer blen = ByteBuffer.allocate(4);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 blen.putInt(len2);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 blen.flip();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 bufs.addFirst(blen);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
346
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 bufs.addFirst(header);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
348 // if(dataLen<=64000)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 multicastqueue.put(bufs);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 // is.reset();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
351
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 /*
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 * System.out.println("ZRLE = "+dataLen);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 * System.out.println("ZRLEE = "+(len2+20)); float mag =
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 * (float)dataLen / (float)(len2 + 20);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 * System.out.println("ZRLE / ZRLEE = "+ mag); if(mag > maxMag)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 * maxMag = mag; System.out.println("maxMag = "+maxMag);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
358 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 return;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
360 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
361 bufs.add(header);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 if (dataLen > 16) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 ByteBuffer b = ByteBuffer.allocate(dataLen - 16);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 startTiming();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 readFully(b.array(), 0, dataLen - 16);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 b.limit(dataLen - 16);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
367 stopTiming();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 bufs.add(b);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 multicastqueue.put(bufs);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
371 // is.reset();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 return;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 is.reset();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
375
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 // It may be compressed. We can inflate here to avoid repeating clients
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 // decompressing here,
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 // but it may generate too many large data. It is better to do it in
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 // each client.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 // But we have do inflation for all input data, so we have to do it
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 // here.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
382 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
383
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
384 public void newClient(AcceptThread acceptThread, final Socket newCli,
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 final OutputStream os, final InputStream is) throws IOException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
386 // createBimgFlag = true;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
387 // rfb.addSockTmp(newCli);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
388 // addSock(newCli);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 final int myId = clients;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 final MulticastQueue.Client<LinkedList<ByteBuffer>> c = multicastqueue
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 .newClient();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
392 final AtomicInteger writerRunning = new AtomicInteger();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 writerRunning.set(1);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 * Timeout thread. If a client is suspended, it has top of queue
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 * indefinitely, which caused memory overflow. After the timeout, we
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 * poll the queue and discard it. Start long wait if writer is running.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
398 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
399 final Runnable timer = new Runnable() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 public void run() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 int count = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 for (;;) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
403 long timeout = 50000 / 8;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
404 try {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
405 synchronized (this) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
406 int state, flag;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
407 writerRunning.set(0);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
408 wait(timeout);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 flag = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 while ((state = writerRunning.get()) == 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 c.poll(); // discard, should be timeout
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
412 count++;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 if (flag == 0) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
414 System.out.println("Discarding " + myId
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
415 + " count=" + count);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
416 flag = 1;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
417 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
418 wait(10); // if this is too short, writer cannot
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
419 // take the poll, if this is too
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
420 // long, memory will overflow...
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
421 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 if (flag == 1)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
423 System.out.println("Resuming " + myId
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
424 + " count=" + count);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
425 if (state != 1) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
426 System.out.println("Client died " + myId);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
427 break;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
428 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
429 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
430 } catch (InterruptedException e) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
431 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
432 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
433 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
434 };
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
435 new Thread(timer).start();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
436 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
437 * discard all incoming from clients
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
438 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
439 final Runnable reader = new Runnable() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
440 public void run() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
441 byte b[] = new byte[4096];
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
442 for (;;) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
443 try {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
444 int c = is.read(b);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
445 if (c <= 0)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
446 throw new IOException();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
447 // System.out.println("client read "+c);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
448 } catch (IOException e) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
449 try {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
450 writerRunning.set(2);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
451 os.close();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
452 is.close();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 } catch (IOException e1) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
454 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
455 return;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
459 };
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 * send packets to a client
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
462 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
463 Runnable sender = new Runnable() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
464 public void run() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
465 writerRunning.set(1);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
466 try {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
467 requestThreadNotify();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
468 // rThread.checkDelay();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
469
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
470 /**
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
471 * initial connection of RFB protocol
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
472 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
473 sendRfbVersion(os);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
474 // readVersionMsg(is);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
475 int rfbMinor = readVersionMsg(is, os);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
476 sendSecurityType(os);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
477 readSecType(is);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
478 sendSecResult(os);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
479 readClientInit(is);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
480 sendInitData(os);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
481 new Thread(reader).start(); // discard incoming packet here
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
482 // after.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
483 // writeFramebufferUpdateRequest(0,0, framebufferWidth,
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
484 // framebufferHeight, false );
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
485 int i = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
486 for (;;) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
487 LinkedList<ByteBuffer> bufs = c.poll();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
488 int inputIndex = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
489 ByteBuffer header = bufs.get(inputIndex);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
490 if (header == null)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
491 continue;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
492 else if (header.get(0) == RfbProto.CheckDelay) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 writeToClient(os, bufs, inputIndex);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
494 continue;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
495 } else if (header.get(0) == RfbProto.FramebufferUpdate) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
496 // System.out.println("client "+ myId);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
497 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
498 /*
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
499 if(i%20==0){
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
500 sendDataCheckDelay();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
501 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
502 i++;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
503 */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
504 writeToClient(os, bufs, inputIndex);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 writerRunning.set(1); // yes my client is awaking.
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
506 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
507 } catch (IOException e) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
508 try {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
509 writerRunning.set(2);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
510 os.close();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
511 } catch (IOException e1) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
512 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
513 /* if socket closed cliList.remove(newCli); */
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
514 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
515 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
516
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
517 public void writeToClient(final OutputStream os,
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
518 LinkedList<ByteBuffer> bufs, int inputIndex)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
519 throws IOException {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
520 while (inputIndex < bufs.size()) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 ByteBuffer b = bufs.get(inputIndex++);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
522 os.write(b.array(), b.position(), b.limit());
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
523 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
524 os.flush();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
525 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
526 };
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
527 clients++;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
528 new Thread(sender).start();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
529
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
530 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
531
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
532 public void dump32(LinkedList<ByteBuffer> bufs) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
533 int len = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 for (ByteBuffer b : bufs)
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
535 len += b.remaining();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 ByteBuffer top = bufs.getFirst();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
537 ByteBuffer end = bufs.getLast();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
538 System.err.println("length: " + len);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
539 System.err.print("head 0: ");
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
540 for (int i = 0; i < 16 && i < top.remaining(); i++) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
541 System.err.print(" " + top.get(i));
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
542 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
543 System.err.print("tail 0: ");
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
544 for (int i = 0; i < 16 && i < end.remaining(); i++) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
545 System.err.print(" " + end.get(i));
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
546 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
547 System.err.println();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
548 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
549
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
550 @Test
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
551 public void test1() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
552 try {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
553 LinkedList<ByteBuffer> in = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
554 LinkedList<ByteBuffer> out = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
555 LinkedList<ByteBuffer> out2 = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
556 // if (false) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
557 // for(int i=0;i<10;i++) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
558 // in.add(ByteBuffer.wrap("test1".getBytes()));
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
559 // in.add(ByteBuffer.wrap("test2".getBytes()));
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
560 // in.add(ByteBuffer.wrap("test3".getBytes()));
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
561 // in.add(ByteBuffer.wrap("test44".getBytes()));
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
562 // }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
563 // } else
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
564 {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
565 String t = "";
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
566 for (int i = 0; i < 10; i++) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
567 t += "test1";
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
568 t += "test2";
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
569 t += "test3";
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
570 t += "test44";
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
571 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
572 in.add(ByteBuffer.wrap(t.getBytes()));
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
573 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
574
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
575 LinkedList<ByteBuffer> in1 = clone(in);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
576
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
577 Deflater deflater = new Deflater();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
578 zip(deflater, in, 0, out);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
579 // LinkedList<ByteBuffer> out3 = clone(out); zipped result is depend
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
580 // on deflator's state
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
581 unzip(inflater, out, 0, out2, INFLATE_BUFSIZE);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
582 // inflater.reset();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
583 equalByteBuffers(in1, out2);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
584 LinkedList<ByteBuffer> out4 = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
585 deflater = new Deflater();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
586 zip(deflater, out2, 0, out4);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
587 LinkedList<ByteBuffer> out5 = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
588 unzip(inflater, out4, 0, out5, INFLATE_BUFSIZE);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
589 int len = equalByteBuffers(in1, out5);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
590
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
591 System.out.println("Test Ok. " + len);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
592 } catch (Exception e) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
593 assertEquals(0, 1);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
594 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
595 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
596
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
597 private LinkedList<ByteBuffer> clone(LinkedList<ByteBuffer> in) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
598 LinkedList<ByteBuffer> copy = new LinkedList<ByteBuffer>();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
599 for (ByteBuffer b : in) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
600 ByteBuffer c = b.duplicate();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
601 copy.add(c);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
602 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
603 return copy;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
604 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
605
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
606 public int equalByteBuffers(LinkedList<ByteBuffer> in,
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
607 LinkedList<ByteBuffer> out2) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
608 int len = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
609 Iterable<Byte> i = byteBufferIterator(in);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
610 Iterator<Byte> o = byteBufferIterator(out2).iterator();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
611
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
612 for (int b : i) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
613 len++;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
614 if (o.hasNext()) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
615 int c = o.next();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
616 assertEquals(b, c);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
617 } else
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
618 assertEquals(0, 1);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
619 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
620 if (o.hasNext())
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
621 assertEquals(0, 1);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
622 // System.out.println();
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
623 return len;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
624 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
625
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
626 private Iterable<Byte> byteBufferIterator(final LinkedList<ByteBuffer> in) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
627 return new Iterable<Byte>() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
628 public Iterator<Byte> iterator() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
629 return new Iterator<Byte>() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
630 int bytes = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
631 int buffers = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
632
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
633 public boolean hasNext() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
634 for (;;) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
635 if (buffers >= in.size())
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
636 return false;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
637 ByteBuffer b = in.get(buffers);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
638 if (!(bytes < b.remaining())) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
639 buffers++;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
640 bytes = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
641 } else
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
642 return true;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
643 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
644 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
645
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
646 public Byte next() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
647 ByteBuffer bf = in.get(buffers);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
648 byte b = bf.get(bytes++);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
649 if (bf.remaining() <= bytes) {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
650 buffers++;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
651 bytes = 0;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
652 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
653 // System.out.print(b);
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
654 return b;
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
655 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
656
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
657 public void remove() {
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
658 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
659 };
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
660 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
661 };
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
662 }
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
663
57ae9fbb1245 add files
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
664 }