changeset 431:0239c1633012 dispose

add CodeSegment for creating MD5
author sugi
date Mon, 04 Aug 2014 08:40:28 +0900
parents c7c57f8d7538
children 59e84cd75403
files src/main/java/alice/topology/manager/CreateHash.java src/main/java/alice/topology/manager/IncomingHosts.java src/main/java/alice/topology/manager/StartTopologyManager.java
diffstat 3 files changed, 94 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/topology/manager/CreateHash.java	Mon Aug 04 08:40:28 2014 +0900
@@ -0,0 +1,70 @@
+package alice.topology.manager;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.LinkedList;
+
+import org.apache.commons.lang3.RandomStringUtils;
+
+import alice.codesegment.CodeSegment;
+import alice.datasegment.CommandType;
+import alice.datasegment.Receiver;
+
+public class CreateHash extends CodeSegment{
+    // this CodeSegment change raw string to MD5
+    
+    private Receiver info = ids.create(CommandType.TAKE); // order to create
+    private Receiver info1 = ids.create(CommandType.TAKE); 
+    
+    public CreateHash(){
+        info.setKey("orderHash");
+        info1.setKey("createdList");
+    }
+    
+    @Override
+    public void run() {
+        @SuppressWarnings("unchecked")
+        LinkedList<String> createdRawList = this.info1.asClass(LinkedList.class);
+        boolean checkNewStr = false;
+        String raw = null;
+        
+        while (!checkNewStr){
+            raw = RandomStringUtils.randomAscii(10);
+         // checking raw String has already created 
+            int count = 0;
+            for (String str : createdRawList) {
+                if (raw.equals(str)) break;
+                count++;
+            }
+            if (count == createdRawList.size())
+                checkNewStr = true;
+        }
+        createdRawList.add(raw);
+        ods.put(info1.key, createdRawList);
+        
+        try { // convert to MD5
+            String MD5 = convertMD5(raw);
+
+            ods.put("MD5", MD5);
+        } catch (NoSuchAlgorithmException e) {
+        }
+        System.out.println("put hash");
+        new CreateHash();
+    }
+    
+    private String convertMD5(String raw) throws NoSuchAlgorithmException{
+        MessageDigest md = MessageDigest.getInstance("MD5");
+        md.update(raw.getBytes());
+        byte[] hash = md.digest();
+        StringBuilder builder = new StringBuilder();
+        for (int idx = 0; idx < hash.length; idx++) {
+            if ((0xff & hash[idx]) < 0x10) {
+                builder.append("0" + Integer.toHexString((0xff & hash[idx])));
+            } else {
+                builder.append(Integer.toHexString((0xff & hash[idx])));
+            }
+        }
+        return builder.toString();
+    }
+    
+}
--- a/src/main/java/alice/topology/manager/IncomingHosts.java	Sun Aug 03 22:58:55 2014 +0900
+++ b/src/main/java/alice/topology/manager/IncomingHosts.java	Mon Aug 04 08:40:28 2014 +0900
@@ -1,11 +1,8 @@
 package alice.topology.manager;
 
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
 import java.util.LinkedList;
 
-import org.apache.commons.lang3.RandomStringUtils;
 import org.msgpack.type.ValueFactory;
 
 import alice.codesegment.CodeSegment;
@@ -18,34 +15,36 @@
 
     HashMap<String, LinkedList<NodeInfo>> topology;
     LinkedList<String> nodeNames;
-    private Receiver host = ids.create(CommandType.TAKE);
-    private Receiver nodes = ids.create(CommandType.TAKE);
+    private Receiver host = ids.create(CommandType.TAKE); //node name
+    private Receiver messageList = ids.create(CommandType.TAKE); // HostMessage List
+    private Receiver cookie = ids.create(CommandType.TAKE); // MD5
 
     public IncomingHosts(HashMap<String, LinkedList<NodeInfo>> topology,
             LinkedList<String> nodeNames) {
         this.topology = topology;
         this.nodeNames = nodeNames;
         this.host.setKey("host");
-        this.nodes.setKey("nodes");
+        this.messageList.setKey("messageList");
+        this.cookie.setKey("MD5");
     }
 
     @Override
     public void run() {
         HostMessage host = this.host.asClass(HostMessage.class);
+        @SuppressWarnings("unchecked")
+        LinkedList<HostMessage> messageList = this.messageList.asClass(LinkedList.class);
         boolean match = false;
         // check cookie
         if (host.cookie != null) {
-            ;
-            @SuppressWarnings("unchecked")
-            LinkedList<HostMessage> nodes = this.nodes.asClass(LinkedList.class);
-            for (HostMessage node : nodes) {
-                if (host.cookie.equals(node.cookie)) {
+            for (HostMessage hostMessage : messageList) {
+                if (host.cookie.equals(hostMessage.cookie)) {
                     match = true;
                     System.out.println("cookie is match");
-                    host.absName = node.absName;
+                    host.absName = hostMessage.absName;
                     ods.put("reconnect", host);
-                    ods.put(this.nodes.key, nodes);
+                    ods.put(this.messageList.key, messageList);
                     new SearchHostName();
+                    break;
                 }
             }
         }
@@ -58,7 +57,13 @@
             DataSegment.connect(nodeName, "", host.name, host.port);
             ods.put(nodeName, "host", nodeName);
 
-            String cookie = createCookie(nodeName);
+            String cookie = this.cookie.asString();
+            HostMessage record = new HostMessage();
+            record.absName = nodeName;
+            record.cookie = cookie;
+            messageList.add(record);
+            ods.put(this.messageList.key, messageList);
+            
             ods.put(nodeName, "cookie", cookie);
 
             LinkedList<NodeInfo> nodes = topology.get(nodeName);
@@ -79,53 +84,7 @@
                 }
             }
         }
-        
+        ods.put("orderHash", "order");
         new IncomingHosts(topology, nodeNames);
     }
-
-    private String createCookie(String hostName) {
-        @SuppressWarnings("unchecked")
-        LinkedList<HostMessage> nodes = this.nodes.asClass(LinkedList.class);
-        boolean checkNewCookie = false;
-        String hash = null;
-        while (!checkNewCookie){
-            // create random string
-            hash = RandomStringUtils.randomAscii(10);
-            try { // convert to MD5
-                MessageDigest md = MessageDigest.getInstance("MD5");
-                md.update(hash.getBytes());
-                hash = hashByteToMD5(md.digest());
-            } catch (NoSuchAlgorithmException e) {
-            }
-            
-            // checking  MD5 has already created 
-            int count = 0;
-            for (HostMessage node : nodes) {
-                if (hash.equals(node.cookie)) break;
-                count++;
-            }
-            if (count == nodes.size())
-                checkNewCookie = true;
-        }
-        HostMessage table = new HostMessage();
-        table.absName = hostName;
-        table.cookie = hash;
-        nodes.add(table);
-        ods.put(this.nodes.key, nodes);
-        System.out.println(hash);
-        return hash;
-    }
-    
-    private String hashByteToMD5(byte[] hash){
-        StringBuilder builder = new StringBuilder();
-        for (int idx = 0; idx < hash.length; idx++) {
-            if ((0xff & hash[idx]) < 0x10) {
-                builder.append("0" + Integer.toHexString((0xff & hash[idx])));
-            } else {
-                builder.append(Integer.toHexString((0xff & hash[idx])));
-            }
-        }
-        return builder.toString();
-    }
-
 }
--- a/src/main/java/alice/topology/manager/StartTopologyManager.java	Sun Aug 03 22:58:55 2014 +0900
+++ b/src/main/java/alice/topology/manager/StartTopologyManager.java	Mon Aug 04 08:40:28 2014 +0900
@@ -79,8 +79,11 @@
             // for recode topology information
             ods.put("topology", new HashMap<String, LinkedList<HostMessage>>());
             // cookie List
-            ods.put("nodes", new LinkedList<String>());
+            ods.put("createdList", new LinkedList<String>());
+            ods.put("messageList", new LinkedList<HostMessage>());
+            ods.put("orderHash", "order");
             
+            new CreateHash();
             new IncomingHosts(topology, nodeNames);
 
             ConfigWaiter cs3 = new ConfigWaiter(nodeNum);