38
|
1 package christie.topology.manager;
|
|
2
|
41
|
3 import christie.annotation.Peek;
|
38
|
4 import christie.codegear.CodeGear;
|
|
5 import christie.codegear.CodeGearManager;
|
|
6
|
41
|
7 import java.io.File;
|
|
8 import java.io.FileNotFoundException;
|
|
9 import java.io.FileReader;
|
|
10 import java.util.ArrayList;
|
|
11 import java.util.HashMap;
|
|
12 import java.util.LinkedList;
|
|
13
|
|
14 import org.apache.log4j.Logger;
|
|
15
|
|
16 import com.alexmerz.graphviz.ParseException;
|
|
17 import com.alexmerz.graphviz.Parser;
|
|
18 import com.alexmerz.graphviz.objects.*;
|
|
19
|
38
|
20 public class TopologyManager extends CodeGear {
|
|
21
|
41
|
22 @Peek
|
|
23 TopologyManagerConfig config;
|
|
24
|
|
25 Logger logger = Logger.getLogger(TopologyManager.class);
|
|
26
|
|
27
|
38
|
28 @Override
|
|
29 protected void run(CodeGearManager cgm) {
|
41
|
30 cgm.setup(new CheckComingHost());
|
38
|
31
|
41
|
32 // if (!conf.dynamic) は, conf.dynamic = trueの動作がわからないので, 省いた.
|
|
33
|
|
34 LinkedList<String> nodeNames = new LinkedList<String>();
|
|
35 HashMap<String, LinkedList<NodeInfo>> topology = new HashMap<String, LinkedList<NodeInfo>>();
|
|
36 int nodeNum = 0;
|
|
37
|
|
38 try {
|
|
39 FileReader reader = new FileReader(new File(config.confFilePath));
|
|
40 Parser parser = new Parser();
|
|
41 parser.parse(reader);
|
|
42
|
|
43
|
|
44 ArrayList<Graph> digraphs = parser.getGraphs();
|
|
45
|
|
46
|
|
47 for (Graph digraph : digraphs) {
|
|
48 ArrayList<Node> nodes = digraph.getNodes(false);
|
|
49 nodeNum = nodes.size();
|
|
50
|
|
51 for (Node node : nodes) {
|
|
52 String nodeName = node.getId().getId();
|
|
53 nodeNames.add(nodeName);
|
|
54 topology.put(nodeName, new LinkedList<NodeInfo>());
|
|
55 }
|
|
56
|
|
57 ArrayList<Edge> edges = digraph.getEdges();
|
|
58 HashMap<String, NodeInfo> hash = new HashMap<String, NodeInfo>();
|
|
59
|
|
60 String connection;
|
|
61 String source;
|
|
62 String target;
|
|
63
|
|
64 NodeInfo nodeInfo;
|
|
65
|
|
66 // まず1回グラフを読み込む
|
|
67 for (Edge edge : edges) {
|
|
68 connection = edge.getAttribute("label");
|
|
69 source = edge.getSource().getNode().getId().getId();
|
|
70 target = edge.getTarget().getNode().getId().getId();
|
|
71 nodeInfo = new NodeInfo(source, connection);
|
|
72
|
|
73 //Question: この下の2行はどんな役目?
|
|
74 //LinkedList<NodeInfo> sources = topology.get(target);
|
|
75 //sources.add(nodeInfo); // addしてその後の使い道は...?
|
|
76
|
|
77 hash.put(source + "," + target, nodeInfo);
|
|
78 }
|
|
79
|
|
80 // hash.get(target + "," + source); をして, グラフを逆にたどって, reverseNameにlabelを入れてる.
|
|
81 for (Edge edge : edges) {
|
|
82 connection = edge.getAttribute("label");
|
|
83 source = edge.getSource().getNode().getId().getId();
|
|
84 target = edge.getTarget().getNode().getId().getId();
|
|
85 nodeInfo = hash.get(target + "," + source);
|
|
86
|
|
87 if (nodeInfo != null) {
|
|
88 nodeInfo.reverseName = connection;
|
|
89 }
|
|
90
|
|
91 }
|
|
92 }
|
|
93
|
|
94 } catch (FileNotFoundException e) {
|
|
95 logger.error("File not found: " + config.confFilePath);
|
|
96 e.printStackTrace();
|
|
97 } catch (ParseException e) {
|
|
98 logger.error("File format error: " + config.confFilePath);
|
|
99 e.printStackTrace();
|
|
100 }
|
|
101
|
|
102 // for recode topology information
|
|
103 // cookie List
|
|
104 getLocalDGM().put("running", false);
|
|
105 getLocalDGM().put("resultParse", topology);
|
|
106 getLocalDGM().put("nodeNames", nodeNames);
|
|
107
|
|
108 cgm.setup(new IncomingHosts());
|
|
109
|
|
110
|
|
111
|
|
112 // Question: この処理何をやっているのかわからない. 一応, その下にそれっぽいコードを書いた.
|
|
113 // ConfigWaiter cs3 = new ConfigWaiter(nodeNum);
|
|
114 // cs3.done.setKey("local", "done");
|
|
115
|
|
116 cgm.setup(new ConfigWaiter());
|
|
117 getLocalDGM().put("nodeNum", nodeNum);
|
|
118 getLocalDGM().put("done", false);
|
38
|
119
|
|
120 }
|
|
121
|
41
|
122
|
38
|
123 }
|