Mercurial > hg > Members > nobuyasu > tightVNCProxy
comparison src/myVncProxy/AcceptClient.java @ 169:4ee9cfadd8dc
update AcceptClient
author | one |
---|---|
date | Mon, 12 Sep 2011 16:36:40 +0900 |
parents | 0d9a824dd12f |
children | 57904d72fc09 |
comparison
equal
deleted
inserted
replaced
148:0d9a824dd12f | 169:4ee9cfadd8dc |
---|---|
1 package myVncProxy; | 1 package myVncProxy; |
2 | 2 |
3 import java.net.Socket; | 3 import java.net.Socket; |
4 import java.net.UnknownHostException; | |
4 import java.io.*; | 5 import java.io.*; |
5 import java.net.*; | 6 import java.net.*; |
6 import java.util.*; | 7 import java.util.*; |
7 | 8 |
8 public class AcceptClient extends Thread { | 9 public class AcceptClient extends Thread { |
10 LinkedList<String> ls = new LinkedList<String>(); | 11 LinkedList<String> ls = new LinkedList<String>(); |
11 boolean runflag = false; | 12 boolean runflag = false; |
12 private String name; | 13 private String name; |
13 boolean addrRegistor = true; | 14 boolean addrRegistor = true; |
14 int passCheck = 0; | 15 int passCheck = 0; |
15 | 16 int treebranch = 2;// treeの子ノードの数 |
16 public AcceptClient(String _name) { | 17 String newparent,request,myAddress; |
17 name = _name; | 18 String leaderflag = "0", sendleaderflag = "0"; |
19 int intv_time = 100; | |
20 | |
21 | |
22 public AcceptClient(String name) { | |
23 this.name = name; | |
18 } | 24 } |
19 | 25 |
20 public AcceptClient() { | 26 public AcceptClient() { |
21 new CreateThread(this); | 27 new CreateThread(this); |
22 } | 28 } |
23 | 29 |
24 // public synchronized void transferParentAddrerss(BufferedReader | 30 // public synchronized void transferParentAddrerss(BufferedReader |
25 // is,PrintStream os) { | 31 // is,PrintStream os) { |
26 public synchronized void transferParentAddrerss(BufferedReader is,PrintStream os) { | 32 public synchronized void transferParentAddrerss(BufferedReader is,PrintStream os) { |
27 String line, port; | 33 // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す |
28 int intv_time = 100; | 34 try { |
29 String request; | 35 while (true) { |
30 int treebranch = 2;// treeの子ノードの数 | 36 String line = is.readLine(); |
31 String leaderflag = "0", sendleaderflag = "0"; | 37 String port = is.readLine(); |
32 | 38 myAddress = getMyAddress(); |
33 // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す | 39 if ("1".equals(line)) { |
34 try { | 40 replyLeaderNode(os, is, port); |
35 while (true) { | 41 } else if ("2".equals(line)) { |
36 line = is.readLine(); | 42 replyNodeInformation(port); |
37 port = is.readLine(); | 43 listupdate(port, newparent); |
38 | 44 outputStream(os, newparent, String.valueOf(parentnum), |
39 System.out.println("データーを受信しましたlin=" + line | 45 port, leaderflag); |
40 + " port=" + port); | 46 os.close(); |
41 // 自分の IPADRESSを取得する | 47 is.close(); |
42 InetAddress addr = InetAddress.getLocalHost(); | 48 } else if ("3".equals(line)) { |
43 String add = new String(addr.getHostAddress()); | 49 replyNormalChildren(os, is, port); |
44 | 50 line = null; |
45 if ("1".equals(line)) { | 51 } else { |
46 System.out.println("親が落ちましたmessage" + port); | 52 if(replyCreateTree(os, is, port, line)) { |
47 String checkRepetition = is.readLine(); | 53 break; |
48 os.println(ls.getLast()); | |
49 parentnum = (Integer.parseInt(port) - 1) | |
50 / treebranch; | |
51 String newparent = ls.get(parentnum); | |
52 | |
53 counter--; | |
54 runflag = true; | |
55 | |
56 sendleaderflag = decisionLeader(Integer.parseInt(port), treebranch); | |
57 | |
58 Child report = new Child(); | |
59 | |
60 passCheck = 1; | |
61 report.reportLastNode(ls.getLast(), newparent,port, | |
62 String.valueOf(parentnum),sendleaderflag, counter); | |
63 | |
64 listupdate(port); | |
65 | |
66 int g = 0; | |
67 for (String bs : ls) { | |
68 System.out.println(g + "番目" + bs); | |
69 g++; | |
70 } | |
71 | |
72 os.println(port); | |
73 // os.println(leaderflag); | |
74 | |
75 leaderflag = decisionLeader( | |
76 Integer.parseInt(checkRepetition), | |
77 treebranch); | |
78 | |
79 if (Integer.parseInt(checkRepetition) == counter + 1) { | |
80 checkRepetition = "stop"; | |
81 } else { | |
82 checkRepetition = "go"; | |
83 } | |
84 os.println(checkRepetition); | |
85 | |
86 // os.println(leaderflag); | |
87 Thread.sleep(intv_time); | |
88 is.close(); | |
89 os.close(); | |
90 | |
91 } else if ("2".equals(line)) { | |
92 parentnum = (Integer.parseInt(port) - 1) / treebranch; | |
93 String newparent = ls.get(parentnum); | |
94 leaderflag = decisionLeader(Integer.parseInt(port),treebranch); | |
95 listupdate(port,newparent); | |
96 outputStream(os, newparent,String.valueOf(parentnum),port,leaderflag); | |
97 os.close(); | |
98 is.close(); | |
99 } else if ("3".equals(line)) { | |
100 String checkRepetition = is.readLine(); | |
101 System.out.println("落ちたのを確認しました"); | |
102 | |
103 os.println(ls.get(Integer.parseInt(port))); | |
104 os.println(port); | |
105 | |
106 if (checkRepetition.equals(ls.getLast())) { | |
107 checkRepetition = "stop"; | |
108 } else { | |
109 //checkRepetition = "go"; | |
110 if(ls.size()-1+passCheck == Integer.parseInt(checkRepetition)) { | |
111 checkRepetition = "skip"; | |
112 } | |
113 passCheck = 0; | |
114 } | |
115 os.println(checkRepetition); | |
116 | |
117 System.out.println("num4=" | |
118 + ls.get(Integer.parseInt(port))); | |
119 line = null; | |
120 runflag = false; | |
121 is.close(); | |
122 os.close(); | |
123 } else { | |
124 if (addrRegistor == true) { | |
125 ls.add(add); | |
126 addrRegistor = false; | |
127 } | |
128 | |
129 if (line != null) { | |
130 addClientAdress(line, ls); | |
131 counter++; | |
132 } else { | |
133 break; | |
134 } | |
135 | |
136 if (counter >= treebranch + 1) { | |
137 leaderflag = decisionLeader(counter, treebranch); | |
138 parentnum = (counter - 1) / treebranch; | |
139 request = ls.get(parentnum); | |
140 System.out.println(parentnum); | |
141 outputStream(os, request,String.valueOf(parentnum), | |
142 String.valueOf(counter), leaderflag); | |
143 checkParameter(parentnum, counter, leaderflag); | |
144 } else { | |
145 // treeの親ノードに接続する人に接続する人を教える | |
146 outputStream(os, add, "0", | |
147 String.valueOf(counter), leaderflag); | |
148 } | |
149 Thread.sleep(intv_time); | |
150 } | |
151 } | 54 } |
152 | |
153 } catch (IOException e) { | |
154 System.out.println(e); | |
155 } | 55 } |
156 | 56 } |
157 catch (InterruptedException e) { | 57 } catch (IOException e) { |
158 e.printStackTrace(); | 58 System.out.println(e); |
159 } | 59 } catch (InterruptedException e) { |
160 | 60 e.printStackTrace(); |
161 | 61 } |
162 } | 62 } |
163 | 63 |
164 /** | 64 /** |
165 * @param port | 65 * @param port |
166 * parent value | 66 * parent value |
190 System.out.println("iの値=" + counter); | 90 System.out.println("iの値=" + counter); |
191 System.out.println("leaderflag=" + leaderflag + "\n"); | 91 System.out.println("leaderflag=" + leaderflag + "\n"); |
192 } | 92 } |
193 | 93 |
194 void addClientAdress(String line, LinkedList<String> ls) { | 94 void addClientAdress(String line, LinkedList<String> ls) { |
195 int g = 0; | |
196 if (line != null) { | 95 if (line != null) { |
197 ls.add(line); | 96 ls.add(line); |
198 } | 97 } |
98 displyLinkedList(ls); | |
99 } | |
100 | |
101 void displyLinkedList( LinkedList<String> ls) { | |
102 int g = 0; | |
199 for (String bs : ls) { | 103 for (String bs : ls) { |
200 System.out.println(g + "番目" + bs); | 104 System.out.println(g + "番目" + bs); |
201 g++; | 105 g++; |
202 } | 106 } |
203 } | 107 } |
208 if ((counter - 1) % treebranch == 1)// children in most young treenum | 112 if ((counter - 1) % treebranch == 1)// children in most young treenum |
209 return "0"; | 113 return "0"; |
210 else | 114 else |
211 return "1"; | 115 return "1"; |
212 } | 116 } |
117 | |
118 String getMyAddress () throws UnknownHostException { | |
119 InetAddress addr = InetAddress.getLocalHost(); | |
120 return new String(addr.getHostAddress()); | |
121 } | |
122 | |
123 void replyNodeInformation (String port) { | |
124 parentnum = (Integer.parseInt(port) - 1) / treebranch; | |
125 newparent = ls.get(parentnum); | |
126 sendleaderflag = decisionLeader(Integer.parseInt(port), treebranch); | |
127 leaderflag = decisionLeader(Integer.parseInt(port),treebranch); | |
128 } | |
129 | |
130 void replyLeaderNode(PrintStream os,BufferedReader is,String port) throws IOException, InterruptedException { | |
131 String checkRepetition = is.readLine(); | |
132 os.println(ls.getLast()); | |
133 replyNodeInformation(port); | |
134 counter--; | |
135 passCheck = 1; | |
136 Child report = new Child(); | |
137 report.reportLastNode(ls.getLast(), newparent,port, String.valueOf(parentnum), | |
138 sendleaderflag, counter); | |
139 listupdate(port); | |
140 displyLinkedList(ls); | |
141 os.println(port); | |
142 runflag = true; | |
143 leaderflag = decisionLeader( | |
144 Integer.parseInt(checkRepetition), | |
145 treebranch); | |
146 if (Integer.parseInt(checkRepetition) == counter + 1) { | |
147 checkRepetition = "stop"; | |
148 } else { | |
149 checkRepetition = "go"; | |
150 } | |
151 os.println(checkRepetition); | |
152 Thread.sleep(intv_time); | |
153 is.close(); | |
154 os.close(); | |
155 } | |
156 | |
157 void replyNormalChildren(PrintStream os,BufferedReader is,String port) throws IOException { | |
158 String checkRepetition = is.readLine(); | |
159 System.out.println("落ちたのを確認しました"); | |
160 | |
161 os.println(ls.get(Integer.parseInt(port))); | |
162 os.println(port); | |
163 | |
164 | |
165 if(ls.size()-1+passCheck == Integer.parseInt(checkRepetition)) { | |
166 checkRepetition = "skip"; | |
167 } | |
168 passCheck = 0; | |
169 | |
170 os.println(checkRepetition); | |
171 | |
172 System.out.println("num4=" | |
173 + ls.get(Integer.parseInt(port))); | |
174 | |
175 runflag = false; | |
176 is.close(); | |
177 os.close(); | |
178 } | |
179 | |
180 boolean replyCreateTree(PrintStream os,BufferedReader is,String port,String line) throws InterruptedException { | |
181 if (addrRegistor == true) { | |
182 ls.add(myAddress); | |
183 addrRegistor = false; | |
184 } | |
185 | |
186 if (line != null) { | |
187 addClientAdress(line, ls); | |
188 counter++; | |
189 } else { | |
190 return true; | |
191 } | |
192 | |
193 if (counter >= treebranch + 1) { | |
194 leaderflag = decisionLeader(counter, treebranch); | |
195 parentnum = (counter - 1) / treebranch; | |
196 request = ls.get(parentnum); | |
197 System.out.println(parentnum); | |
198 outputStream(os, request,String.valueOf(parentnum), | |
199 String.valueOf(counter), leaderflag); | |
200 checkParameter(parentnum, counter, leaderflag); | |
201 } else { | |
202 // treeの親ノードに接続する人に接続する人を教える | |
203 outputStream(os, myAddress, "0", | |
204 String.valueOf(counter), leaderflag); | |
205 } | |
206 Thread.sleep(intv_time); | |
207 return false; | |
208 } | |
213 | 209 |
214 } | 210 } |
215 | 211 |
216 class Child { | 212 class Child { |
217 | 213 |