Mercurial > hg > FederatedLinda
view src/fdl/test/debug/ConfigurationManagerEngine.java @ 114:3b877c9a44f5
gather old packages
author | kazz |
---|---|
date | Mon, 11 Oct 2010 14:29:37 +0900 |
parents | src/fdl/test/debug2/ConfigurationManagerEngine.java@0ea086f0e96f |
children |
line wrap: on
line source
package fdl.test.debug; import java.io.IOException; import java.nio.ByteBuffer; import fdl.*; public class ConfigurationManagerEngine implements MetaEngine { public static final int DEFAULTPORT = 10000; public int nodeNum = 0; public int relayNum = 0; public int relaySize = 1; private MetaLinda ml; private NodeInfo[] nodes; private boolean running = true; private class AcceptNewNode implements PSXCallback { int count = 0; public void callback(ByteBuffer reply) { String[] hostData = new String(reply.array()).split(":"); String hostName = hostData[0]; int port = DEFAULTPORT; if (hostData.length > 1) port = Integer.parseInt(hostData[1]); nodes[count] = new NodeInfo(hostName, port); try { nodes[count].linda = ml.open(hostName, port); } catch (IOException e) { e.printStackTrace(); } if (++count < nodeNum) { ml.in(TupleId.MANAGE.id, this); } else { linkNodes(); ml.in(TupleId.MANAGE.id, new ConfirmConnectionNode()); } } } private class ConfirmConnectionNode implements PSXCallback { int count = 0; public void callback(ByteBuffer reply) { if (++count < nodeNum) { ml.in(TupleId.MANAGE.id, this); } else { routingNodes(); ml.in(TupleId.MANAGE.id, new ConfirmRoutingNode()); } } } private class ConfirmRoutingNode implements PSXCallback { int count = 0; public void callback(ByteBuffer reply) { if (++count < nodeNum) { ml.in(TupleId.MANAGE.id, this); } else { print("All link configured!"); // TREE実験開始を通知 nodes[0].linda.out(TupleId.START.id, ByteBuffer.wrap("0".getBytes())); // DebugRing 開始を通知 //nodes[0].linda.out(DEBUGSTART, ByteBuffer.wrap(("print," + BODY + ",").getBytes())); ml.in(TupleId.START.id, new ConfirmFinish()); } } } private class ConfirmFinish implements PSXCallback { public void callback(ByteBuffer reply) { System.out.println(new String(reply.array())); nodes[0].linda.out(TupleId.DEBUGSTART.id, ByteBuffer.wrap("shutdown".getBytes())); print("Finish token"); try { nodes[0].linda.sync(1); } catch (IOException e) { e.printStackTrace(); } running = false; } } private class RingLoop implements PSXCallback { int counter = 0; public void callback(ByteBuffer reply) { nodes[0].linda.out(TupleId.DEBUGSTART.id, ByteBuffer.wrap(("print," + TupleId.BODY.id + ",").getBytes())); print("ring=" + counter + ": \n" + new String(reply.array())); counter++; ml.in(TupleId.DEBUG.id, this); } } public ConfigurationManagerEngine(MetaLinda metaLinda, int nodeNum, int relayNum, int relaySize) { // initialize this.ml = metaLinda; this.nodeNum = nodeNum; this.relayNum = relayNum; this.relaySize = relaySize; nodes = new NodeInfo[nodeNum]; } public void mainLoop(MetaLinda ml1) { // resist poll tuple id ml.in(TupleId.MANAGE.id, new AcceptNewNode()); ml.in(TupleId.DEBUG.id, new RingLoop()); while (running) ml.sync(0); } private void linkNodes() { for (int i = 0; i < nodes.length; i++) { // XML for Tree Topology ConnectionXMLBuilder tree = new ConnectionXMLBuilder(i); int k; if (i != 0) { // TOP k = (i-1)/2; tree.appendConnection(TupleId.TREETOP, nodes[k].host, nodes[k].port, (i%2 == 0) ? TupleId.TREERIGHT : TupleId.TREELEFT); } if ((k = 2*i+1) < nodes.length) // LEFT tree.appendConnection(TupleId.TREELEFT, nodes[k].host, nodes[k].port, TupleId.TREETOP); if ((k = 2*i+2) < nodes.length) // RIGHT tree.appendConnection(TupleId.TREERIGHT, nodes[k].host, nodes[k].port, TupleId.TREETOP); nodes[i].connectionXML = tree.createXML(); nodes[i].linda.out(TupleId.MANAGE.id, ByteBuffer.wrap(nodes[i].connectionXML.getBytes())); print(nodes[i].connectionXML); // XML for Ring Debug Topology ConnectionXMLBuilder debug = new ConnectionXMLBuilder(i); int left = (nodes.length + i - 1) % nodes.length; int right = (i + 1) % nodes.length; debug.appendConnection(TupleId.RINGLEFT, nodes[left].host, nodes[left].port, TupleId.RINGRIGHT); debug.appendConnection(TupleId.RINGRIGHT, nodes[right].host, nodes[right].port, TupleId.RINGLEFT); nodes[i].debugConnectionXML = debug.createXML(); nodes[i].linda.out(TupleId.DEBUG.id, ByteBuffer.wrap(nodes[i].debugConnectionXML.getBytes())); print(nodes[i].debugConnectionXML); } } private void routingNodes() { for (int i = 0; i < nodes.length; i++) { RoutingXMLBuilder tree = new RoutingXMLBuilder(); if (i != 0) { // TOP if (2*i+1 < nodes.length) { // LEFT tree.appendRoutingTable(TupleId.TREETOP, TupleId.TREELEFT); tree.appendRoutingTable(TupleId.TREELEFT, TupleId.TREETOP); } if (2*i+2 < nodes.length) { // RIGHT tree.appendRoutingTable(TupleId.TREETOP, TupleId.TREERIGHT); tree.appendRoutingTable(TupleId.TREERIGHT, TupleId.TREETOP); } } nodes[i].routingXML = tree.createXML(); nodes[i].linda.out(TupleId.MANAGE.id, ByteBuffer.wrap(nodes[i].routingXML.getBytes())); print(nodes[i].routingXML); RoutingXMLBuilder debug = new RoutingXMLBuilder(); debug.appendRoutingTable(TupleId.RINGLEFT, TupleId.RINGRIGHT); debug.appendRoutingTable(TupleId.RINGRIGHT, TupleId.RINGLEFT); nodes[i].debugRoutingXML = debug.createXML(); nodes[i].linda.out(TupleId.DEBUG.id, ByteBuffer.wrap(nodes[i].debugRoutingXML.getBytes())); print(nodes[i].debugRoutingXML); } } void print(String str) { System.err.println("[DEBUG] ConfigurationManager: " + str); } }