Mercurial > hg > FederatedLinda
comparison src/fdl/test/debug2/ConfigurationManagerEngine.java @ 92:ea4ee892baf5
commit
author | kazz <kazz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 22 Apr 2010 16:13:03 +0900 |
parents | |
children | 0ea086f0e96f |
comparison
equal
deleted
inserted
replaced
91:4df1d50df52a | 92:ea4ee892baf5 |
---|---|
1 package fdl.test.debug2; | |
2 | |
3 import java.io.IOException; | |
4 import java.nio.ByteBuffer; | |
5 | |
6 import fdl.*; | |
7 | |
8 public class ConfigurationManagerEngine implements MetaEngine { | |
9 public static final int DEFAULTPORT = 10000; | |
10 | |
11 private int nodeNum = 0; | |
12 private int relayNum = 0; | |
13 private int relaySize = 1; | |
14 private MetaLinda ml; | |
15 private NodeInfo[] nodes; | |
16 private boolean running = true; | |
17 | |
18 private class AcceptNewNode implements PSXCallback { | |
19 int count = 0; | |
20 public void callback(ByteBuffer reply) { | |
21 String[] hostData = new String(reply.array()).split(":"); | |
22 String hostName = hostData[0]; | |
23 int port = DEFAULTPORT; | |
24 if (hostData.length > 1) | |
25 port = Integer.parseInt(hostData[1]); | |
26 nodes[count] = new NodeInfo(hostName, port); | |
27 try { | |
28 nodes[count].linda = ml.open(hostName, port); | |
29 } catch (IOException e) { | |
30 e.printStackTrace(); | |
31 } | |
32 if (++count < nodeNum) { | |
33 ml.in(TupleId.MANAGE.id, this); | |
34 } else { | |
35 linkNodes(); | |
36 ml.in(TupleId.MANAGE.id, new ConfirmConnectionNode()); | |
37 } | |
38 } | |
39 } | |
40 | |
41 private class ConfirmConnectionNode implements PSXCallback { | |
42 int count = 0; | |
43 public void callback(ByteBuffer reply) { | |
44 if (++count < nodeNum) { | |
45 ml.in(TupleId.MANAGE.id, this); | |
46 } else { | |
47 routingNodes(); | |
48 ml.in(TupleId.MANAGE.id, new ConfirmRoutingNode()); | |
49 } | |
50 } | |
51 | |
52 } | |
53 | |
54 private class ConfirmRoutingNode implements PSXCallback { | |
55 int count = 0; | |
56 public void callback(ByteBuffer reply) { | |
57 if (++count < nodeNum) { | |
58 ml.in(TupleId.MANAGE.id, this); | |
59 } else { | |
60 print("All link configured!"); | |
61 // TREE実験開始を通知 | |
62 nodes[0].linda.out(TupleId.START.id, ByteBuffer.wrap("0".getBytes())); | |
63 // DebugRing 開始を通知 | |
64 //nodes[0].linda.out(DEBUGSTART, ByteBuffer.wrap(("print," + BODY + ",").getBytes())); | |
65 ml.in(TupleId.START.id, new ConfirmFinish()); | |
66 } | |
67 } | |
68 | |
69 } | |
70 | |
71 private class ConfirmFinish implements PSXCallback { | |
72 public void callback(ByteBuffer reply) { | |
73 System.out.println(new String(reply.array())); | |
74 nodes[0].linda.out(TupleId.DEBUGSTART.id, ByteBuffer.wrap("shutdown".getBytes())); | |
75 print("Finish token"); | |
76 try { | |
77 nodes[0].linda.sync(1); | |
78 } catch (IOException e) { | |
79 e.printStackTrace(); | |
80 } | |
81 running = false; | |
82 } | |
83 | |
84 } | |
85 | |
86 private class RingLoop implements PSXCallback { | |
87 int counter = 0; | |
88 public void callback(ByteBuffer reply) { | |
89 nodes[0].linda.out(TupleId.DEBUGSTART.id, ByteBuffer.wrap(("print," + TupleId.BODY.id + ",").getBytes())); | |
90 print("ring=" + counter + ": \n" + new String(reply.array())); | |
91 counter++; | |
92 ml.in(TupleId.DEBUG.id, this); | |
93 } | |
94 | |
95 } | |
96 | |
97 public ConfigurationManagerEngine(MetaLinda metaLinda, int nodeNum, int relayNum, int relaySize) { | |
98 // initialize | |
99 this.ml = metaLinda; | |
100 this.nodeNum = nodeNum; | |
101 this.relayNum = relayNum; | |
102 this.relaySize = relaySize; | |
103 nodes = new NodeInfo[nodeNum]; | |
104 } | |
105 | |
106 public void mainLoop() { | |
107 // resist poll tuple id | |
108 ml.in(TupleId.MANAGE.id, new AcceptNewNode()); | |
109 ml.in(TupleId.DEBUG.id, new RingLoop()); | |
110 while (running) | |
111 ml.sync(); | |
112 } | |
113 | |
114 private void linkNodes() { | |
115 for (int i = 0; i < nodes.length; i++) { | |
116 // XML for Tree Topology | |
117 ConnectionXMLBuilder tree = new ConnectionXMLBuilder(i); | |
118 int k; | |
119 if (i != 0) { // TOP | |
120 k = (i-1)/2; | |
121 tree.appendConnection(TupleId.TREETOP, nodes[k].host, nodes[k].port, (i%2 == 0) ? TupleId.TREERIGHT : TupleId.TREELEFT); | |
122 } | |
123 if ((k = 2*i+1) < nodes.length) // LEFT | |
124 tree.appendConnection(TupleId.TREELEFT, nodes[k].host, nodes[k].port, TupleId.TREETOP); | |
125 if ((k = 2*i+2) < nodes.length) // RIGHT | |
126 tree.appendConnection(TupleId.TREERIGHT, nodes[k].host, nodes[k].port, TupleId.TREETOP); | |
127 nodes[i].connectionXML = tree.createXML(); | |
128 nodes[i].linda.out(TupleId.MANAGE.id, ByteBuffer.wrap(nodes[i].connectionXML.getBytes())); | |
129 print(nodes[i].connectionXML); | |
130 | |
131 // XML for Ring Debug Topology | |
132 ConnectionXMLBuilder debug = new ConnectionXMLBuilder(i); | |
133 int left = (nodes.length + i - 1) % nodes.length; | |
134 int right = (i + 1) % nodes.length; | |
135 debug.appendConnection(TupleId.RINGLEFT, nodes[left].host, nodes[left].port, TupleId.RINGRIGHT); | |
136 debug.appendConnection(TupleId.RINGRIGHT, nodes[right].host, nodes[right].port, TupleId.RINGLEFT); | |
137 nodes[i].debugConnectionXML = debug.createXML(); | |
138 nodes[i].linda.out(TupleId.DEBUG.id, ByteBuffer.wrap(nodes[i].debugConnectionXML.getBytes())); | |
139 print(nodes[i].debugConnectionXML); | |
140 } | |
141 } | |
142 | |
143 private void routingNodes() { | |
144 for (int i = 0; i < nodes.length; i++) { | |
145 RoutingXMLBuilder tree = new RoutingXMLBuilder(); | |
146 if (i != 0) { // TOP | |
147 if (2*i+1 < nodes.length) { // LEFT | |
148 tree.appendRoutingTable(TupleId.TREETOP, TupleId.TREELEFT); | |
149 tree.appendRoutingTable(TupleId.TREELEFT, TupleId.TREETOP); | |
150 } | |
151 if (2*i+2 < nodes.length) { // RIGHT | |
152 tree.appendRoutingTable(TupleId.TREETOP, TupleId.TREERIGHT); | |
153 tree.appendRoutingTable(TupleId.TREERIGHT, TupleId.TREETOP); | |
154 } | |
155 } | |
156 nodes[i].routingXML = tree.createXML(); | |
157 nodes[i].linda.out(TupleId.MANAGE.id, ByteBuffer.wrap(nodes[i].routingXML.getBytes())); | |
158 print(nodes[i].routingXML); | |
159 | |
160 RoutingXMLBuilder debug = new RoutingXMLBuilder(); | |
161 debug.appendRoutingTable(TupleId.RINGLEFT, TupleId.RINGRIGHT); | |
162 debug.appendRoutingTable(TupleId.RINGRIGHT, TupleId.RINGLEFT); | |
163 nodes[i].debugRoutingXML = debug.createXML(); | |
164 nodes[i].linda.out(TupleId.DEBUG.id, ByteBuffer.wrap(nodes[i].debugRoutingXML.getBytes())); | |
165 print(nodes[i].debugRoutingXML); | |
166 } | |
167 } | |
168 | |
169 void print(String str) { | |
170 System.err.println("[DEBUG] ConfigurationManager: " + str); | |
171 } | |
172 | |
173 } |