changeset 94:87a203c99177

update HostMessage refactor
author akahori
date Sat, 15 Sep 2018 18:59:12 +0900
parents 7abfe041b75c
children 5cd99bac9abd
files src/main/java/christie/topology/HostMessage.java src/main/java/christie/topology/manager/CheckComingHost.java src/main/java/christie/topology/manager/CreateTreeTopology.java src/main/java/christie/topology/manager/FileParser.java src/main/java/christie/topology/manager/IncomingHosts.java src/main/java/christie/topology/manager/RecordTopology.java src/main/java/christie/topology/manager/SearchHostName.java src/main/java/christie/topology/node/IncomingConnectionInfo.java src/main/java/christie/topology/node/TopologyNode.java
diffstat 9 files changed, 115 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/christie/topology/HostMessage.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/HostMessage.java	Sat Sep 15 18:59:12 2018 +0900
@@ -3,34 +3,64 @@
 
 import org.msgpack.annotation.Message;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
 @Message
 public class HostMessage {
-    public String hostName;
-    public int port;
+    private String hostName;
+    private int port;
+    private String nodeName; // this is nodeName which have these IP and port.
+    private String connectionName;
+    private String remoteNodeName;
 
-    public String connectionName;
-    public String nodeName; // this is nodeName which have these IP and port.
+    private String cookie; // MD5
 
-    public String remoteNodeName;
-    public String cookie; // MD5
-
-    public boolean alive;
+    private boolean alive;
 
     public HostMessage() { }
-    public HostMessage(String name, int port) {
-        this.hostName = name;
+
+    public void setLocalHostAndPort(int port){
+        try {
+            this.hostName = InetAddress.getLocalHost().getHostAddress();
+            this.port = port;
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void setHostAndPort(String hostName, int port) {
+        this.hostName = hostName;
         this.port = port;
     }
 
-    public HostMessage(String name, int port, String connectionName) {
-        this.hostName = name;
-        this.port = port;
+    public void setNodeInfo(String nodeName, String connectionName, String remoteNodeName){
+        this.nodeName = nodeName;
         this.connectionName = connectionName;
+        this.remoteNodeName = remoteNodeName;
+    }
+
+    public void setNodeName(String nodeName) {
+        this.nodeName = nodeName;
     }
 
-    public boolean isAlive() {
-        return alive;
-    }
+    public String getHostName() { return hostName; }
+
+    public int getPort() { return port; }
+
+    public String getNodeName() { return nodeName; }
+
+    public String getConnectionName() { return connectionName; }
+
+    public String getRemoteNodeName() { return remoteNodeName; }
+
+    public void setAlive(boolean alive) { this.alive = alive; }
+
+    public boolean isAlive() { return alive; }
+
+    public void setCookie(String cookie) { this.cookie = cookie; }
+
+    public String getCookie() { return cookie; }
 
     public String toString() {
         return "HostMessage : name = " + hostName + ", port = " + Integer.toString(port) + " connectionName = " +
--- a/src/main/java/christie/topology/manager/CheckComingHost.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/manager/CheckComingHost.java	Sat Sep 15 18:59:12 2018 +0900
@@ -22,10 +22,11 @@
     @Override
     protected void run(CodeGearManager cgm) {
 
+        String cookie = hostMessage.getCookie();
         // check cookie
-        if (hostMessage.cookie != null) {
-            if (absCookieTable.containsKey(hostMessage.cookie)) {
-                hostMessage.nodeName = absCookieTable.get(hostMessage.cookie);
+        if (cookie != null) {
+            if (absCookieTable.containsKey(cookie)) {
+                hostMessage.setNodeName(cookie);
                 System.out.println("match");
                 // coming host has ever joined this App
                 getLocalDGM().put("reconnectHost", hostMessage);
--- a/src/main/java/christie/topology/manager/CreateTreeTopology.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/manager/CreateTreeTopology.java	Sat Sep 15 18:59:12 2018 +0900
@@ -39,7 +39,10 @@
         String nodeName = "node" + hostCount;
         // Manager connect to Node
 
-        cgm.createRemoteDGM(nodeName, newHost.hostName, newHost.port);
+        String newHostName = newHost.getHostName();
+        int newHostPort = newHost.getPort();
+
+        cgm.createRemoteDGM(nodeName, newHostName, newHostPort);
 
         getDGM(nodeName).put("nodeName", nodeName);
         getDGM(nodeName).put("cookie", MD5);
@@ -47,7 +50,7 @@
         absCookieTable.put(MD5, nodeName);
         getLocalDGM().put("hostCount", hostCount + 1);
 
-        newHost.alive = true;
+        newHost.setAlive(true);
         nameTable.put(nodeName, newHost);
         parentManager.register(nodeName);
 
@@ -62,21 +65,16 @@
             getDGM(nodeName).put("connectNodeNum", 1);
             // put parent information own
             String parentNodeName = parentManager.getMyParent();
-            HostMessage parent = nameTable.get(parentNodeName);
+            HostMessage parentHost = nameTable.get(parentNodeName);
 
-            HostMessage newParentHost = new HostMessage(parent.hostName, parent.port, "parent");
-            newParentHost.nodeName = parentNodeName;
-            newParentHost.remoteNodeName = nodeName; // address
-
-            getLocalDGM().put("nodeInfo", newParentHost);
+            // 相手からhostNameとportはもらっているので, nodeの情報だけ与えれば良い.
+            parentHost.setNodeInfo(parentNodeName, "parent", nodeName);
+            getLocalDGM().put("nodeInfo", parentHost);
             cgm.setup(new RecordTopology());
 
-            // put own information parent
-            HostMessage newChildHost = new HostMessage(newHost.hostName, newHost.port, "child" + parentManager.getMyNumber());
-            newChildHost.nodeName = nodeName;
-            newChildHost.remoteNodeName = parentNodeName;
-
-            getLocalDGM().put("nodeInfo", newChildHost);
+            // newChildHost, newHostも同じ
+            newHost.setNodeInfo(nodeName, "child" + parentManager.getMyNumber(), parentNodeName);
+            getLocalDGM().put("nodeInfo", newHost);
             cgm.setup(new RecordTopology());
         }
 
--- a/src/main/java/christie/topology/manager/FileParser.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/manager/FileParser.java	Sat Sep 15 18:59:12 2018 +0900
@@ -11,6 +11,7 @@
 import java.util.HashMap;
 import java.util.LinkedList;
 
+import christie.topology.HostMessage;
 import com.alexmerz.graphviz.ParseException;
 import com.alexmerz.graphviz.Parser;
 import com.alexmerz.graphviz.objects.*;
@@ -23,7 +24,7 @@
     @Override
     protected void run(CodeGearManager cgm) {
         LinkedList<String> nodeNames = new LinkedList<>();
-        HashMap<String, LinkedList<NodeInfo>> resultParse = new HashMap<>();
+        HashMap<String, LinkedList<HostMessage>> resultParse = new HashMap<>();
         int nodeNum = 0;
 
         try {
@@ -47,20 +48,13 @@
 
                 ArrayList<Edge> edges = digraph.getEdges();
 
-                String connection;
-                String source;
-                String target;
-
-                NodeInfo nodeInfo;
+                for (Edge edge : edges) {
+                    HostMessage nodeInfo = new HostMessage();
 
-                for (Edge edge : edges) {
-                    connection = edge.getAttribute("label");
-                    source = edge.getSource().getNode().getId().getId();
-                    target = edge.getTarget().getNode().getId().getId();
-                    nodeInfo = new NodeInfo(source, connection, target);
-
-                    resultParse.get(source).add(nodeInfo);
-
+                    nodeInfo.setNodeInfo(edge.getSource().getNode().getId().getId(),
+                                         edge.getAttribute("label"),
+                                         edge.getTarget().getNode().getId().getId());
+                    resultParse.get(nodeInfo.getNodeName()).add(nodeInfo);
                 }
             }
 
--- a/src/main/java/christie/topology/manager/IncomingHosts.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/manager/IncomingHosts.java	Sat Sep 15 18:59:12 2018 +0900
@@ -13,7 +13,7 @@
 public class IncomingHosts extends CodeGear {
 
     @Peek // Topology from parse file
-    HashMap<String, LinkedList<NodeInfo>> resultParse;
+    HashMap<String, LinkedList<HostMessage>> resultParse;
     @Peek // nodeName list
     LinkedList<String> nodeNames;
     @Peek
@@ -33,22 +33,23 @@
         // not have or match cookie
         String nodeName = nodeNames.poll();
 
+        String newHostName = newHost.getHostName();
+        int newHostPort = newHost.getPort();
+
         // Manager connect to Node
-        cgm.createRemoteDGM(nodeName, newHost.hostName, newHost.port);
+        cgm.createRemoteDGM(nodeName, newHostName, newHostPort);
 
         absCookieTable.put(MD5, nodeName);
 
         getDGM(nodeName).put( "nodeName", nodeName);
         getDGM(nodeName).put("cookie", MD5);
 
-        LinkedList<NodeInfo> nodeInfoList = resultParse.get(nodeName);
+        LinkedList<HostMessage> nodeInfoList = resultParse.get(nodeName);
         getDGM(nodeName).put("connectNodeNum", nodeInfoList.size());
-        for (NodeInfo nodeInfo : nodeInfoList) {
-            HostMessage hostMessage = new HostMessage(newHost.hostName, newHost.port, nodeInfo.connectionName);
-            hostMessage.nodeName = nodeName;
-            hostMessage.remoteNodeName = nodeInfo.targetNodeName;
+        for (HostMessage nodeInfo : nodeInfoList) {
+            nodeInfo.setHostAndPort(newHostName, newHostPort);
 
-            getLocalDGM().put("nodeInfo", hostMessage);
+            getLocalDGM().put("nodeInfo", nodeInfo);
 
             cgm.setup(new RecordTopology());
         }
--- a/src/main/java/christie/topology/manager/RecordTopology.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/manager/RecordTopology.java	Sat Sep 15 18:59:12 2018 +0900
@@ -21,8 +21,8 @@
 
     @Override
     protected void run(CodeGearManager cgm) {
-        String nodeName = nodeInfo.nodeName;
-        String remoteNodeName = nodeInfo.remoteNodeName;
+        String nodeName = nodeInfo.getNodeName();
+        String remoteNodeName = nodeInfo.getRemoteNodeName();
 
         // topologyにつながりを保存する.
         if (topology.containsKey(nodeName)) {
@@ -30,8 +30,8 @@
 
         } else {
             HashMap<String, HostMessage> connections = new HashMap<String, HostMessage>();
-            connections.put(nodeInfo.remoteNodeName, nodeInfo);
-            topology.put(nodeInfo.nodeName, connections);
+            connections.put(remoteNodeName, nodeInfo);
+            topology.put(nodeName, connections);
         }
 
         // topologyNodeにつながる相手の情報を送る
--- a/src/main/java/christie/topology/manager/SearchHostName.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/manager/SearchHostName.java	Sat Sep 15 18:59:12 2018 +0900
@@ -34,24 +34,28 @@
         // Question: remove処理 どうしようか. いらない気もする.
         // DataSegment.remove(hostInfo.absName);
 
-        cgm.createRemoteDGM(reconnectHost.nodeName, reconnectHost.hostName, reconnectHost.port);
-        getDGM(reconnectHost.nodeName).put("host", reconnectHost.nodeName);
+        String nodeName = reconnectHost.getNodeName();
+        String hostName = reconnectHost.getHostName();
+        int port = reconnectHost.getPort();
+
+        cgm.createRemoteDGM(nodeName, hostName, port);
+        getDGM(nodeName).put("nodeName", nodeName);
 
         // put Host dataSegment on reconnect node
-        if (topology.containsKey(reconnectHost.nodeName)) {
+        if (topology.containsKey(nodeName)) {
 
             // Question: これはバグ...?
             // ods.put(reconnectHost.absName, "dummy"); // this is bug
 
             if (running){
-                getLocalDGM().put(reconnectHost.nodeName, ValueFactory.createNilValue());
+                getLocalDGM().put(nodeName, ValueFactory.createNilValue());
             }
 
 
-            LinkedList<HostMessage> hostList = topology.get(reconnectHost.nodeName);
+            LinkedList<HostMessage> hostList = topology.get(nodeName);
             for (HostMessage host : hostList){
-                getLocalDGM().put(reconnectHost.nodeName, host);
-                System.out.println("put data in "+ reconnectHost.nodeName);
+                getLocalDGM().put(nodeName, host);
+                System.out.println("put data in "+ nodeName);
             }
         }
 
@@ -60,13 +64,12 @@
             for (HostMessage host : list){
 
                 // find and update old info
-                if (!reconnectHost.nodeName.equals(host.nodeName)) continue;
+                if (!nodeName.equals(host.getNodeName())) continue;
 
-                if (!reconnectHost.hostName.equals(host.hostName) || reconnectHost.port != host.port){
-                    host.hostName = reconnectHost.hostName;
-                    host.port = reconnectHost.port;
+                if (!hostName.equals(host.getHostName()) || port != host.getPort()){
+                    host.setHostAndPort(hostName, port);
 
-                    getLocalDGM().put(host.remoteNodeName, host);
+                    getLocalDGM().put(host.getNodeName(), host);
 
                 } else {
                     // nothing to do ?
@@ -75,9 +78,13 @@
         }
 
         for (LinkedList<HostMessage> list :topology.values()){
-            System.out.print(list.get(0).remoteNodeName+" : ");
+            System.out.print(list.get(0).getRemoteNodeName()+" : ");
             for (HostMessage host : list){
-                System.out.print("[ "+host.nodeName+" "+host.hostName+" "+host.port+" "+host.connectionName+" "+host.remoteNodeName+" ]");
+                System.out.print("[ "+host.getNodeName()      +" "
+                                     +host.getHostName()      +" "
+                                     +host.getPort()          +" "
+                                     +host.getConnectionName()+" "
+                                     +host.getRemoteNodeName()+" ]");
             }
             System.out.println();
         }
--- a/src/main/java/christie/topology/node/IncomingConnectionInfo.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/node/IncomingConnectionInfo.java	Sat Sep 15 18:59:12 2018 +0900
@@ -20,10 +20,14 @@
 
     @Override
     protected void run(CodeGearManager cgm) {
-        cgm.createRemoteDGM(remoteNodeInfo.connectionName, remoteNodeInfo.hostName, remoteNodeInfo.port);
-        getDGM(remoteNodeInfo.connectionName).put("reverseNodeName", nodeName);
+        String connectionName = remoteNodeInfo.getConnectionName();
 
-        getLocalDGM().put("cMember", remoteNodeInfo.connectionName);
+        cgm.createRemoteDGM(connectionName,
+                            remoteNodeInfo.getHostName(),
+                            remoteNodeInfo.getPort());
+        getDGM(connectionName).put("reverseNodeName", nodeName);
+
+        getLocalDGM().put("cMember", connectionName);
         cgm.setup(new CreateConnectionList());
         cgm.setup(new IncomingConnectionInfo());
     }
--- a/src/main/java/christie/topology/node/TopologyNode.java	Sat Sep 15 17:41:14 2018 +0900
+++ b/src/main/java/christie/topology/node/TopologyNode.java	Sat Sep 15 18:59:12 2018 +0900
@@ -24,18 +24,11 @@
                             topologyNodeConfig.getManagerHostName(),
                             topologyNodeConfig.getManagerPort());
 
+        HostMessage hostMessage = new HostMessage();
+        hostMessage.setLocalHostAndPort(cgm.localPort);
+        hostMessage.setCookie(topologyNodeConfig.cookie);
 
-        String localHostName = null;
-        try {
-            localHostName = InetAddress.getLocalHost().getHostAddress();
-        } catch (UnknownHostException e) {
-            e.printStackTrace();
-        }
-
-        HostMessage host = new HostMessage(localHostName, cgm.localPort);
-        host.cookie = topologyNodeConfig.cookie;
-
-        getDGM(topologyNodeConfig.getManagerKey()).put("hostMessage", host);
+        getDGM(topologyNodeConfig.getManagerKey()).put("hostMessage", hostMessage);
 
         getLocalDGM().put("_CLIST", new ArrayList<String>());
         getLocalDGM().put("reverseCount", 0);