Mercurial > hg > Applications > TreeVNC
annotate src/main/java/jp/ac/u_ryukyu/treevnc/TreeManagement.java @ 223:3189e210a7ed
indent fix.
author | oc |
---|---|
date | Sun, 06 Jul 2014 12:46:20 +0900 |
parents | f1d7cb4a1868 |
children | 0e0e6744432c |
rev | line source |
---|---|
206 | 1 package jp.ac.u_ryukyu.treevnc; |
11 | 2 |
153 | 3 |
4 import java.io.IOException; | |
208 | 5 import java.net.InetAddress; |
153 | 6 import java.util.LinkedList; |
11 | 7 |
153 | 8 public class TreeManagement { |
188 | 9 |
223 | 10 private LinkedList<TreeVNCNode> nodeList = new LinkedList<TreeVNCNode>(); |
11 boolean runflag = false; | |
12 private final int treebranch = 2; | |
13 private boolean showTreeNode = false; | |
14 private byte[] netmask; | |
15 private byte[] netaddress; | |
188 | 16 |
223 | 17 public TreeManagement(String hostName, int vncport, boolean showTree) { |
18 TreeVNCNode me = new TreeVNCNode(hostName, vncport,"localhost"); | |
19 showTreeNode = showTree; | |
20 me.setTreeNum(0); | |
21 nodeList.add(me); | |
22 } | |
23 | |
24 // | |
25 // private boolean checkAddress(String line) { | |
26 // String test[] = line.split("\\."); | |
27 // int a = Integer.parseInt(test[0]); | |
28 // int b = Integer.parseInt(test[1]); | |
29 // if ((192 == a && b == 168) || (172 == a && (b > 15 || b < 32)) | |
30 // || 10 == a) { | |
31 // return true; | |
32 // } else { | |
33 // return false; | |
34 // } | |
35 // } | |
11 | 36 |
138 | 37 |
223 | 38 /** |
39 * a parent is lost, remove from the list and move last one into here | |
40 * @param nodeNum | |
41 * parent value | |
42 */ | |
43 private void moveLastNodeToLostNodePosition(int nodeNum) { | |
44 nodeList.remove(nodeNum); | |
45 TreeVNCNode node = nodeList.removeLast(); | |
46 node.setTreeNum(nodeNum); | |
47 nodeList.add(nodeNum, node) ; | |
48 } | |
11 | 49 |
223 | 50 private void checkParameter(int parent, int counter, int leaderflag2) { |
51 System.out.print("number p =" + parent); | |
52 System.out.print(" number i =" + counter); | |
53 System.out.println(" leaderflag=" + leaderflag2 + "\n"); | |
54 } | |
11 | 55 |
223 | 56 private TreeVNCNode getParentNode(int nodeNum) { |
57 int parentnum = (nodeNum - 1) / treebranch; | |
135
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
58 return nodeList.get(parentnum); |
223 | 59 } |
60 | |
61 | |
62 public TreeVNCNode getChildNode(TreeVNCNode newparent, int i) { | |
63 int child = newparent.getTreeNum() * treebranch + i + 1; | |
64 if (child >= nodeList.size()) { | |
65 return(null); | |
66 } else { | |
67 return(nodeList.get(child)); | |
68 } | |
69 | |
70 } | |
71 | |
72 /** | |
73 * A parent is lost, move last node which has no child to the position | |
74 * @param hostname | |
75 * @param port | |
76 * @param myHostName | |
77 */ | |
195 | 78 public void fixLostParent(String hostname, int port, String myHostName) { |
136 | 79 TreeVNCNode lostParentNode = lookup(nodeList,hostname,port); |
80 if (lostParentNode == null ) return; // some thing wrong | |
81 int treeNumber = lostParentNode.getTreeNum(); | |
172 | 82 TreeVNCNode deadParent = getParentNode(treeNumber); |
223 | 83 TreeVNCNode me = nodeList.getFirst(); |
84 me.setHostName(myHostName); | |
173 | 85 if (deadParent.getTreeNum() == 0) { |
223 | 86 // if dead root, connect me. |
87 connectTo(me, lostParentNode); | |
88 return; | |
173 | 89 } |
90 if (getChildNode(deadParent, 0) == null) return; | |
172 | 91 moveLastNodeToLostNodePosition(deadParent.getTreeNum()); |
92 lostNodeConnection(deadParent); | |
136 | 93 // if lostParentNode is the last one, we don't need reconnection |
94 // Thread.sleep(intv_time); | |
95 } | |
54 | 96 |
136 | 97 /** |
98 * send reconnect to all children | |
99 * @param newparent | |
100 */ | |
172 | 101 private void lostNodeConnection(TreeVNCNode oldParent) { |
223 | 102 TreeVNCNode newparent = nodeList.get(oldParent.getTreeNum()); |
103 TreeVNCNode grandfather = getParentNode(newparent.getTreeNum()); | |
104 | |
105 connectTo(grandfather, newparent); | |
106 | |
173 | 107 for(int i=0; i < treebranch; i++) { |
223 | 108 TreeVNCNode child = getChildNode(newparent, i); |
173 | 109 if (child != null) { |
110 connectTo(newparent, child); | |
136 | 111 } |
112 } | |
223 | 113 |
188 | 114 if (showTreeNode) { |
223 | 115 showTreeNode(); |
188 | 116 } |
136 | 117 } |
54 | 118 |
173 | 119 |
120 | |
223 | 121 public void connectTo(TreeVNCNode newparent, TreeVNCNode n) { |
122 TreeVncProtocol vc1 = new TreeVncProtocol(n.getHostname(),n.getPort()); | |
123 try { | |
124 short nodeId = (short) n.getTreeNum(); | |
125 vc1.connectTo(newparent.getHostname(), newparent.getPort(), isLeader(n), nodeId); | |
126 } catch (IOException e) { | |
127 // log | |
128 } | |
129 } | |
173 | 130 |
136 | 131 private int isLeader(TreeVNCNode n) { |
132 return ( n.getTreeNum() % treebranch == 1) ? 1 : 0; | |
133 } | |
134 | |
135 private TreeVNCNode lookup(LinkedList<TreeVNCNode> list, String hostname, int port) { | |
223 | 136 for (TreeVNCNode r : list) { |
137 if (r.getHostname().equals(hostname) && r.getPort() == port ) | |
138 return r; | |
139 } | |
140 return null; | |
136 | 141 } |
135
ada4d850a820
lostParent and notFoundParenet
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
134
diff
changeset
|
142 |
223 | 143 /** |
144 * Determine tree topology and send CONNECT_TO command | |
145 * @param hostname | |
146 * @param port | |
147 */ | |
148 public void decideWhereToConnect(String hostname, int port, String localhostname) { | |
147 | 149 TreeVNCNode node = new TreeVNCNode(hostname,port,localhostname); |
136 | 150 node.setTreeNum(nodeList.size()); |
151 nodeList.add(node); | |
223 | 152 if (nodeList.size() >= treebranch + 1) { |
153 TreeVNCNode parent = getParentNode(node.getTreeNum()); | |
154 checkParameter(parent.getTreeNum(), nodeList.size(), isLeader(node)); | |
155 connectTo(parent, node); | |
156 } else { | |
157 // connect to me | |
158 TreeVNCNode me = nodeList.getFirst(); | |
159 me.setHostName(localhostname); | |
160 connectTo(me, node); | |
161 } | |
162 if (showTreeNode) { | |
163 showTreeNode(); | |
164 } | |
165 } | |
166 | |
167 /** | |
168 * show Tree Node. | |
169 */ | |
170 public void showTreeNode() { | |
171 int nl = 0, pow = 2; | |
163 | 172 for (int i=0; i<nodeList.size(); i++) { |
223 | 173 TreeVNCNode treeNode = nodeList.get(i); |
174 System.out.print(treeNode.getTreeNum() + ":" + treeNode.getPort() + ":" + treeNode.getHostname()); | |
175 if (i==nl) { | |
176 System.out.println(); | |
177 nl = nl + pow; | |
178 pow = pow * pow; | |
179 } else { | |
180 System.out.print(" "); | |
181 } | |
172 | 182 |
163 | 183 } |
188 | 184 System.out.println(); |
223 | 185 } |
11 | 186 |
223 | 187 public LinkedList<TreeVNCNode> getList() { |
188 return nodeList; | |
189 } | |
54 | 190 |
223 | 191 public void setList(LinkedList<TreeVNCNode> _ls) { |
192 nodeList = _ls; | |
193 } | |
54 | 194 |
223 | 195 public int getTreeBranch() { |
196 return treebranch; | |
197 } | |
136 | 198 |
223 | 199 public void setNetMask(byte[] netmask,byte[] netaddress) { |
200 this.netmask = netmask; | |
201 this.netaddress = netaddress; | |
202 } | |
203 | |
204 public boolean onTheSameNetwork(InetAddress adr ) { | |
205 byte [] byteadr = adr.getAddress(); | |
206 if (byteadr.length != netmask.length) return false; | |
207 for(int i=0; i < netmask.length; i++) { | |
208 if ((netmask[i] & byteadr[i])!=netaddress[i]) return false; | |
209 } | |
210 return true; | |
211 } | |
208 | 212 |
54 | 213 } |