Mercurial > hg > Database > Alice
view src/main/java/alice/topology/manager/IncomingHosts.java @ 430:c7c57f8d7538 dispose
use cookie MD5
author | sugi |
---|---|
date | Sun, 03 Aug 2014 22:58:55 +0900 |
parents | 1b32ea1263f3 |
children | 0239c1633012 |
line wrap: on
line source
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; import alice.datasegment.CommandType; import alice.datasegment.DataSegment; import alice.datasegment.Receiver; import alice.topology.HostMessage; public class IncomingHosts extends CodeSegment { HashMap<String, LinkedList<NodeInfo>> topology; LinkedList<String> nodeNames; private Receiver host = ids.create(CommandType.TAKE); private Receiver nodes = ids.create(CommandType.TAKE); public IncomingHosts(HashMap<String, LinkedList<NodeInfo>> topology, LinkedList<String> nodeNames) { this.topology = topology; this.nodeNames = nodeNames; this.host.setKey("host"); this.nodes.setKey("nodes"); } @Override public void run() { HostMessage host = this.host.asClass(HostMessage.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)) { match = true; System.out.println("cookie is match"); host.absName = node.absName; ods.put("reconnect", host); ods.put(this.nodes.key, nodes); new SearchHostName(); } } } if (!match) { System.out.println("new node come"); // not have or match cookie String nodeName = nodeNames.poll(); // Manager connect to Node DataSegment.connect(nodeName, "", host.name, host.port); ods.put(nodeName, "host", nodeName); String cookie = createCookie(nodeName); ods.put(nodeName, "cookie", cookie); LinkedList<NodeInfo> nodes = topology.get(nodeName); for (NodeInfo nodeInfo : nodes) { HostMessage newHost = new HostMessage(host.name, host.port, nodeInfo.connectionName, nodeInfo.reverseName); newHost.absName = nodeName; newHost.remoteAbsName = nodeInfo.sourceNodeName; ods.put("nodeInfo", newHost); ods.put(nodeInfo.sourceNodeName, newHost); new RecodeTopology(); } if (nodeNames.isEmpty()) { // configuration finish for (String key : topology.keySet()) { ods.put("local", key, ValueFactory.createNilValue()); } } } 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(); } }