Mercurial > hg > FederatedLinda
changeset 76:e8b757d7d0af
(no commit message)
author | one |
---|---|
date | Mon, 16 Nov 2009 23:16:40 +0900 |
parents | 5b1f099da593 |
children | 5336bafaaf48 |
files | src/fdl/test/topology/FDLindaNode.java src/fdl/test/topology/MetaProtocolEngine.java src/fdl/test/topology/NodeManager.java src/fdl/test/topology/RingTopologyManager.java src/fdl/test/topology/TopologyManager.java src/fdl/test/topology/TopologyManagerEngine.java src/fdl/test/topology/ring/FDLindaRingNode.java src/fdl/test/topology/ring/RingMetaProtocolEngine.java |
diffstat | 8 files changed, 458 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/FDLindaNode.java Mon Nov 16 23:16:40 2009 +0900 @@ -0,0 +1,62 @@ +package fdl.test.topology; + +import java.io.IOException; + +import fdl.FDLindaServ; +import fdl.MetaLinda; +import fdl.MetaEngine; + +/** +* FDLindaNode +* +* @author Kazuki Akamine +* +* Federated Linda の Topology Node +* 接続する機能までを実装 MetaEngine は外部から提供される +* +*/ + +public class FDLindaNode extends FDLindaServ { + // Fields + private MetaLinda ml; + private static int localPort = 10000; + private static String managerHostName; + private final static String usageString + = "Usage: FDLindaNode -manager SERVERNAME"; + + // Constructor + public FDLindaNode(int port) throws IOException { + super(port); + this.ml = new MetaLinda(tupleSpace, this); + } + + @Override public void mainLoop() { + me.mainLoop(); + } + public void setMetaEngine(MetaEngine me) { + this.me = me; + } + public MetaLinda getMetaLinda() { + return ml; + } + + // main routine + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-manager".equals(args[i])) { + managerHostName = args[++i]; + } else { + System.err.println(usageString); + } + } + try { + FDLindaNode node = new FDLindaNode(localPort); + MetaEngine me = new MetaProtocolEngine(node.getMetaLinda(), managerHostName); + node.setMetaEngine(me); + node.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/MetaProtocolEngine.java Mon Nov 16 23:16:40 2009 +0900 @@ -0,0 +1,115 @@ +package fdl.test.topology; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import java.nio.ByteBuffer; +import java.util.ArrayList; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + +/** +* MetaProtocolEngine +* +* @author Kazuki Akamine +* +* 接続する機能までを実装した MetaEngine +* これを継承して、具体的な処理を書く +* +*/ + +public class MetaProtocolEngine implements MetaEngine { + // Fields + private static String lastToken = "null"; + private static int port = 10000; + private static int manageId = 60000; + private PSXLinda manager, psxLocal; + private ArrayList<PSXLinda> psxSendServers; + private MetaLinda fdlMeta; + private String managerHostName; + private String localHostName; + + // Constructor + public MetaProtocolEngine(MetaLinda ml, String managerHostName) { + this.fdlMeta = ml; + this.managerHostName = managerHostName; + try { + this.localHostName = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + this.psxSendServers = new ArrayList<PSXLinda>(); + } + + public void mainLoop() { + psxLocal = fdlMeta; + initTopologyManager(); + initSendServer(); + } + + private void initSendServer() { + sendLocalHostName(); + connectSendServers(); + } + + private void initTopologyManager() { + // Connect to TopologyManager Server + try { + manager = fdlMeta.open(managerHostName, port); + } catch (IOException e) { + // manager は先に立ち上げてから実行する。 + // while で回して再接続 loop にすべき? + e.printStackTrace(); + } + } + + private void sendLocalHostName() { + // TopologyManager に自分のホストネームを送信して、起動を伝える + ByteBuffer local; + local = ByteBuffer.wrap(localHostName.getBytes()); + manager.out(manageId, local); + try { + manager.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void connectSendServers() { + // TopologyManager から、送られてくる ConnectServer の hostName を取得して接続 + System.out.println("[DEBUG] MethodCall connectSendServers()"); + while (true) { + PSXReply reply; + reply = psxLocal.in(manageId); + do { + try { + psxLocal.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } while (!reply.ready()); + ByteBuffer data = reply.getData(); + String hostName = new String(data.array()); + System.out.println("[DEBUG] GetReply " + hostName); + if (hostName.equals(lastToken)) + break; + connectSendServer(hostName); + } + } + + private void connectSendServer(String hostName) { + PSXLinda linda; + try { + linda = fdlMeta.open(hostName, port); + psxSendServers.add(linda); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println("Connect to " + hostName); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/NodeManager.java Mon Nov 16 23:16:40 2009 +0900 @@ -0,0 +1,82 @@ +package fdl.test.topology; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; + +import fdl.PSXLinda; +import fdl.MetaLinda; + +public class NodeManager { + public String hostName; + public PSXLinda linda; + private MetaLinda mLinda; + private int port; + private int manageId; + public ArrayList<NodeManager> waitingNodes; + public ArrayList<NodeManager> waitedNodes; + public ArrayList<NodeManager> sendNodes; + + public NodeManager(MetaLinda ml, int port, int manageId) { + this.port = port; + this.manageId = manageId; + this.mLinda = ml; + hostName = null; + waitingNodes = new ArrayList<NodeManager>(); + waitedNodes = new ArrayList<NodeManager>(); + sendNodes = new ArrayList<NodeManager>(); + } + + public void addConnection(NodeManager node) { + waitingNodes.add(node); + node.waitedNodes.add(this); + } + + public void finishConnection(NodeManager node) { + waitingNodes.remove(node); + node.waitedNodes.remove(this); + sendNodes.add(node); + } + + public void startUp(String hostName) { + // 起動 + this.hostName = hostName; + try { + linda = mLinda.open(hostName, port); + } catch (IOException e) { + e.printStackTrace(); + } + // 起動待ちのノードに通知 + connectWaitedNodes(); + } + + public void connectWaitedNodes() { + for (int i = 0; i < waitedNodes.size(); i++) { + NodeManager node = waitedNodes.get(i); + if (node.isRunning()) { + node.connect(this); + node.finishConnection(this); + if (waitingNodes.contains(node)) { + connect(node); + finishConnection(node); + } + } + } + } + + public void connect(NodeManager node) { + ByteBuffer data = ByteBuffer.wrap(node.hostName.getBytes()); + linda.out(manageId, data); + try { + linda.sync(1); + System.out.println("[DEBUG] SendConnectTo: " + hostName + " to " + node.hostName); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean isRunning() { + return hostName != null; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/RingTopologyManager.java Mon Nov 16 23:16:40 2009 +0900 @@ -0,0 +1,15 @@ +package fdl.test.topology; + +import java.io.IOException; + +public class RingTopologyManager extends TopologyManager { + public RingTopologyManager(int port) throws IOException { + //super(port); + } + + void makeTopology() { + + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/TopologyManager.java Mon Nov 16 23:16:40 2009 +0900 @@ -0,0 +1,40 @@ +package fdl.test.topology; + +import java.io.IOException; + +import fdl.MetaEngine; + +/** +* TopologyManager +* +* @author Kazuki Akamine +* +* FDLindaNode の Topology を管理する Server +* +*/ + +public class TopologyManager { + private static int localPort = 10000; + private static int nodeNum = 2; + private static String usageString + = "TopologyManager -nodes NODENUM"; + + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-nodes".equals(args[i])) { + nodeNum = Integer.parseInt(args[++i]); + } else { + System.err.println(usageString); + } + } + try { + FDLindaNode manager = new FDLindaNode(localPort); + MetaEngine me = new TopologyManagerEngine(manager.getMetaLinda(), nodeNum); + manager.setMetaEngine(me); + manager.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/TopologyManagerEngine.java Mon Nov 16 23:16:40 2009 +0900 @@ -0,0 +1,79 @@ +package fdl.test.topology; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import fdl.MetaEngine; +import fdl.MetaLinda; +import fdl.PSXLinda; +import fdl.PSXReply; + +/** +* TopologyManagerEngine +* +* @author Kazuki Akamine +* +* FDLindaNode の Topology を管理する Server の MetaEngine +* TopologyManager の具体的な実装 +* +*/ + +public class TopologyManagerEngine implements MetaEngine { + private static String lastToken = "null"; + private static int port = 10000; + private static int manageId = 60000; + private PSXLinda manager; + private MetaLinda fdlMeta; + private int nodeNum; + private NodeManager[] nodes; + + // Constructor + public TopologyManagerEngine(MetaLinda ml, int nodeNum) { + this.fdlMeta = ml; + this.nodeNum = nodeNum; + this.nodes = new NodeManager[this.nodeNum]; + for (int i = 0; i < nodes.length; i++) { + this.nodes[i] = new NodeManager(fdlMeta, port, manageId); + } + } + + public void mainLoop() { + manager = fdlMeta; + makeTopology(); + } + + private void makeTopology() { + // 接続を定義 + nodes[0].addConnection(nodes[1]); + nodes[1].addConnection(nodes[0]); + // 起動状況を確認しつつ、接続命令を送信 + for (int i = 0; i < nodes.length; i++) { + PSXReply reply; + reply = manager.in(manageId); + do { + try { + manager.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } while (!reply.ready()); + ByteBuffer data = reply.getData(); + String hostName = new String(data.array()); + System.out.println("[DEBUG] GetNodeName: " + hostName); + nodes[i].startUp(hostName); + } + ByteBuffer lastTokenBB = ByteBuffer.wrap(lastToken.getBytes()); + for (int i = 0; i < nodes.length; i++) { + // TODO: 参加ノードに実験開始を告知する ("null"を送る) + nodes[i].linda.out(manageId, lastTokenBB); + System.out.println("[DEBUG] SendMsg: " + lastToken + " to " + nodes[i].hostName); + + try { + nodes[i].linda.sync(1); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/ring/FDLindaRingNode.java Mon Nov 16 23:16:40 2009 +0900 @@ -0,0 +1,43 @@ +package fdl.test.topology.ring; + +import java.io.IOException; +import fdl.MetaEngine; +import fdl.test.topology.*; + +/** +* FDLindaRingNode +* +* @author Kazuki Akamine +* +* Federated Linda の Ring 用 Node +* Ring 用の MetaEngine を持たせる。 +* +*/ + +public class FDLindaRingNode { + // Fields + private static int localPort = 10000; + private static String managerHostName; + private final static String usageString + = "Usage: FDLindaRingNode -manager SERVERNAME"; + + // main routine + public static void main(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-manager".equals(args[i])) { + managerHostName = args[++i]; + } else { + System.err.println(usageString); + } + } + try { + FDLindaNode node = new FDLindaNode(localPort); + MetaEngine me = new MetaProtocolEngine(node.getMetaLinda(), managerHostName); + node.setMetaEngine(me); + node.mainLoop(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fdl/test/topology/ring/RingMetaProtocolEngine.java Mon Nov 16 23:16:40 2009 +0900 @@ -0,0 +1,22 @@ +package fdl.test.topology.ring; + +import fdl.MetaLinda; +import fdl.test.topology.MetaProtocolEngine; + +/** +* MetaProtocolEngine +* +* @author Kazuki Akamine +* +* Ring 接続実験用の MetaProtocolEngine +* Ring 接続実験の具体的な処理を記述する。 +* +*/ + +public class RingMetaProtocolEngine extends MetaProtocolEngine { + + public RingMetaProtocolEngine(MetaLinda ml, String managerHostName) { + super(ml, managerHostName); + } + +}