Mercurial > hg > Database > Christie
changeset 83:2314c55534ef
add TreeTopology
author | akahori |
---|---|
date | Sat, 15 Sep 2018 09:24:36 +0900 |
parents | a02f44b709c0 |
children | c0c29f989af3 |
files | src/main/java/christie/topology/manager/ComingServiceHosts.java src/main/java/christie/topology/manager/CreateTreeTopology.java src/main/java/christie/topology/manager/Parent.java src/main/java/christie/topology/manager/ParentManager.java src/main/java/christie/topology/manager/TopologyManager.java |
diffstat | 5 files changed, 252 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/topology/manager/ComingServiceHosts.java Sat Sep 15 09:24:36 2018 +0900 @@ -0,0 +1,28 @@ +package christie.topology.manager; + + +import christie.annotation.Peek; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.topology.HostMessage; + +public class ComingServiceHosts extends CodeGear{ + // このクラスそもそもいらない説. CreateTreeTopologyだけでいいかも. + + // Peekなの? + //private Receiver info = ids.create(CommandType.PEEK); + + @Peek + HostMessage newHost; + + public ComingServiceHosts(){ + + } + + @Override + protected void run(CodeGearManager cgm) { + cgm.setup(new CreateTreeTopology()); + cgm.setup(new ComingServiceHosts()); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/topology/manager/CreateTreeTopology.java Sat Sep 15 09:24:36 2018 +0900 @@ -0,0 +1,85 @@ +package christie.topology.manager; + + +import christie.annotation.Peek; +import christie.annotation.Take; +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; +import christie.topology.HostMessage; + +import java.util.HashMap; + +public class CreateTreeTopology extends CodeGear{ + + @Take + HostMessage newHost; + + @Take + int hostCount; + + @Peek + HashMap<String, HostMessage> nameTable; + + @Take + String MD5; + + @Peek + HashMap<String, String> absCookieTable; + + @Peek + ParentManager parentManager; + + public CreateTreeTopology(){ + } + + @Override + protected void run(CodeGearManager cgm) { + System.out.println("cookie:" + MD5); + + String nodeName = "node" + hostCount; + // Manager connect to Node + + cgm.createRemoteDGM(nodeName, newHost.hostName, newHost.port); + + getDGM(nodeName).put("nodeName", nodeName); + getDGM(nodeName).put("cookie", MD5); + + absCookieTable.put(MD5, nodeName); + getLocalDGM().put("hostCount", hostCount + 1); + + newHost.alive = true; + nameTable.put(nodeName, newHost); + parentManager.register(nodeName); + + if (hostCount == 0) { + // どこにも繋がれるところがないので, ルートのとき. + // ルートなので, connectNodeNumもreverseCountも0でいい. + getDGM(nodeName).put("connectNodeNum", 0); + getDGM(nodeName).put("reverseCount", 0); + getLocalDGM().put("start", true); + + } else { + // 親のみにつながればいいので1 + getDGM(nodeName).put("connectNodeNum", 1); + // put parent information own + String parentNodeName = parentManager.getMyParent(); + HostMessage parent = nameTable.get(parentNodeName); + int num = parentManager.getMyNumber(); + + HostMessage newHost = new HostMessage(parent.hostName, parent.port, "parent", "child" + num); + newHost.nodeName = parentNodeName; + newHost.remoteNodeName = nodeName; // address + + getLocalDGM().put("nodeInfo", newHost); + cgm.setup(new RecordTopology()); + + // put own information parent + newHost = new HostMessage(newHost.hostName, newHost.port, "child" + num, "parent"); + newHost.nodeName = nodeName; + newHost.remoteNodeName = parentNodeName; + + getLocalDGM().put("nodeInfo", newHost); + cgm.setup(new RecordTopology()); + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/topology/manager/Parent.java Sat Sep 15 09:24:36 2018 +0900 @@ -0,0 +1,35 @@ +package christie.topology.manager; + +public class Parent { + private String name; + private int children = 0; + + public Parent(String name) { + this.name = name; + } + + public int children() { + return children; + } + + public void setChildren(int num) { + children = num; + } + + public String getName() { + return name; + } + + public void setName(String n) { + name = n; + } + + public void increment() { + children++; + } + + public void decrement() { + children--; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/topology/manager/ParentManager.java Sat Sep 15 09:24:36 2018 +0900 @@ -0,0 +1,92 @@ +package christie.topology.manager; + +import java.util.LinkedList; + +public class ParentManager { + + private int maxChildren; + private int position = 0; + private LinkedList<Parent> list; + public ParentManager(int hasChildren){ + list = new LinkedList<Parent>(); + maxChildren = hasChildren; + } + + public String getMyParent() { + checkChildNumber(); + return list.get(position).getName(); + } + + public int getMyNumber() { + checkChildNumber(); + int num = list.get(position).children(); + list.get(position).increment(); + return num; + } + + private void checkChildNumber() { + for (;;next()) { + if (list.get(position).children() < maxChildren) + break; + } + } + + public void register(String name) { + Parent p = new Parent(name); + list.add(p); + } + + public void next() { + position++; + } + + public void previous() { + position--; + } + + public void replaceAndRemove(String remove, String replace) { + Parent removeNode = find(remove); + remove(replace); + removeNode.setName(replace); + } + + public void remove(String remove) { + Parent removeNode = find(remove); + list.remove(removeNode); + } + + public Parent find(String name) { + boolean found = false; + int i = 0; + for (;i<list.size();i++) { + if (list.get(i).getName().equals(name)) { + found = true; + break; + } + } + if (found) { + return list.get(i); + } else { + return null; + } + } + + public Parent getLastNode(){ + return list.getLast(); + } + + public void show() { + int counter = 0; + System.out.print("| "); + for (Parent p :list) { + if (counter==position) + System.out.print("P "); + System.out.print(p.getName()+" "+p.children()+" | "); + counter++; + } + } + + public LinkedList<Parent> getList() { + return list; + } +}
--- a/src/main/java/christie/topology/manager/TopologyManager.java Sat Sep 15 09:12:42 2018 +0900 +++ b/src/main/java/christie/topology/manager/TopologyManager.java Sat Sep 15 09:24:36 2018 +0900 @@ -23,10 +23,21 @@ getLocalDGM().put("absCookieTable", new HashMap<String, String>()); if(!topologyManagerConfig.dynamic) { + getLocalDGM().put("running", false); cgm.setup(new FileParser()); cgm.setup(new IncomingHosts()); cgm.setup(new ConfigWaiter()); - getLocalDGM().put("running", false); + }else{ + getLocalDGM().put("running", true); + + if (topologyManagerConfig.type == TopologyType.Tree) { + getLocalDGM().put("parentManager", new ParentManager(topologyManagerConfig.hasChild)); + getLocalDGM().put("nameTable", new HashMap<String, HostMessage>()); + getLocalDGM().put("hostCount", 0); + + cgm.setup(new ComingServiceHosts()); + //cgm.setup(new ReceiveDisconnectMessage()); + } } cgm.setup(new CreateHash());