comparison src/main/java/alice/topology/manager/IncomingHosts.java @ 431:0239c1633012 dispose

add CodeSegment for creating MD5
author sugi
date Mon, 04 Aug 2014 08:40:28 +0900
parents c7c57f8d7538
children e565d481c52e
comparison
equal deleted inserted replaced
430:c7c57f8d7538 431:0239c1633012
1 package alice.topology.manager; 1 package alice.topology.manager;
2 2
3 import java.security.MessageDigest;
4 import java.security.NoSuchAlgorithmException;
5 import java.util.HashMap; 3 import java.util.HashMap;
6 import java.util.LinkedList; 4 import java.util.LinkedList;
7 5
8 import org.apache.commons.lang3.RandomStringUtils;
9 import org.msgpack.type.ValueFactory; 6 import org.msgpack.type.ValueFactory;
10 7
11 import alice.codesegment.CodeSegment; 8 import alice.codesegment.CodeSegment;
12 import alice.datasegment.CommandType; 9 import alice.datasegment.CommandType;
13 import alice.datasegment.DataSegment; 10 import alice.datasegment.DataSegment;
16 13
17 public class IncomingHosts extends CodeSegment { 14 public class IncomingHosts extends CodeSegment {
18 15
19 HashMap<String, LinkedList<NodeInfo>> topology; 16 HashMap<String, LinkedList<NodeInfo>> topology;
20 LinkedList<String> nodeNames; 17 LinkedList<String> nodeNames;
21 private Receiver host = ids.create(CommandType.TAKE); 18 private Receiver host = ids.create(CommandType.TAKE); //node name
22 private Receiver nodes = ids.create(CommandType.TAKE); 19 private Receiver messageList = ids.create(CommandType.TAKE); // HostMessage List
20 private Receiver cookie = ids.create(CommandType.TAKE); // MD5
23 21
24 public IncomingHosts(HashMap<String, LinkedList<NodeInfo>> topology, 22 public IncomingHosts(HashMap<String, LinkedList<NodeInfo>> topology,
25 LinkedList<String> nodeNames) { 23 LinkedList<String> nodeNames) {
26 this.topology = topology; 24 this.topology = topology;
27 this.nodeNames = nodeNames; 25 this.nodeNames = nodeNames;
28 this.host.setKey("host"); 26 this.host.setKey("host");
29 this.nodes.setKey("nodes"); 27 this.messageList.setKey("messageList");
28 this.cookie.setKey("MD5");
30 } 29 }
31 30
32 @Override 31 @Override
33 public void run() { 32 public void run() {
34 HostMessage host = this.host.asClass(HostMessage.class); 33 HostMessage host = this.host.asClass(HostMessage.class);
34 @SuppressWarnings("unchecked")
35 LinkedList<HostMessage> messageList = this.messageList.asClass(LinkedList.class);
35 boolean match = false; 36 boolean match = false;
36 // check cookie 37 // check cookie
37 if (host.cookie != null) { 38 if (host.cookie != null) {
38 ; 39 for (HostMessage hostMessage : messageList) {
39 @SuppressWarnings("unchecked") 40 if (host.cookie.equals(hostMessage.cookie)) {
40 LinkedList<HostMessage> nodes = this.nodes.asClass(LinkedList.class);
41 for (HostMessage node : nodes) {
42 if (host.cookie.equals(node.cookie)) {
43 match = true; 41 match = true;
44 System.out.println("cookie is match"); 42 System.out.println("cookie is match");
45 host.absName = node.absName; 43 host.absName = hostMessage.absName;
46 ods.put("reconnect", host); 44 ods.put("reconnect", host);
47 ods.put(this.nodes.key, nodes); 45 ods.put(this.messageList.key, messageList);
48 new SearchHostName(); 46 new SearchHostName();
47 break;
49 } 48 }
50 } 49 }
51 } 50 }
52 51
53 if (!match) { 52 if (!match) {
56 String nodeName = nodeNames.poll(); 55 String nodeName = nodeNames.poll();
57 // Manager connect to Node 56 // Manager connect to Node
58 DataSegment.connect(nodeName, "", host.name, host.port); 57 DataSegment.connect(nodeName, "", host.name, host.port);
59 ods.put(nodeName, "host", nodeName); 58 ods.put(nodeName, "host", nodeName);
60 59
61 String cookie = createCookie(nodeName); 60 String cookie = this.cookie.asString();
61 HostMessage record = new HostMessage();
62 record.absName = nodeName;
63 record.cookie = cookie;
64 messageList.add(record);
65 ods.put(this.messageList.key, messageList);
66
62 ods.put(nodeName, "cookie", cookie); 67 ods.put(nodeName, "cookie", cookie);
63 68
64 LinkedList<NodeInfo> nodes = topology.get(nodeName); 69 LinkedList<NodeInfo> nodes = topology.get(nodeName);
65 for (NodeInfo nodeInfo : nodes) { 70 for (NodeInfo nodeInfo : nodes) {
66 HostMessage newHost = new HostMessage(host.name, host.port, 71 HostMessage newHost = new HostMessage(host.name, host.port,
77 for (String key : topology.keySet()) { 82 for (String key : topology.keySet()) {
78 ods.put("local", key, ValueFactory.createNilValue()); 83 ods.put("local", key, ValueFactory.createNilValue());
79 } 84 }
80 } 85 }
81 } 86 }
82 87 ods.put("orderHash", "order");
83 new IncomingHosts(topology, nodeNames); 88 new IncomingHosts(topology, nodeNames);
84 } 89 }
85
86 private String createCookie(String hostName) {
87 @SuppressWarnings("unchecked")
88 LinkedList<HostMessage> nodes = this.nodes.asClass(LinkedList.class);
89 boolean checkNewCookie = false;
90 String hash = null;
91 while (!checkNewCookie){
92 // create random string
93 hash = RandomStringUtils.randomAscii(10);
94 try { // convert to MD5
95 MessageDigest md = MessageDigest.getInstance("MD5");
96 md.update(hash.getBytes());
97 hash = hashByteToMD5(md.digest());
98 } catch (NoSuchAlgorithmException e) {
99 }
100
101 // checking MD5 has already created
102 int count = 0;
103 for (HostMessage node : nodes) {
104 if (hash.equals(node.cookie)) break;
105 count++;
106 }
107 if (count == nodes.size())
108 checkNewCookie = true;
109 }
110 HostMessage table = new HostMessage();
111 table.absName = hostName;
112 table.cookie = hash;
113 nodes.add(table);
114 ods.put(this.nodes.key, nodes);
115 System.out.println(hash);
116 return hash;
117 }
118
119 private String hashByteToMD5(byte[] hash){
120 StringBuilder builder = new StringBuilder();
121 for (int idx = 0; idx < hash.length; idx++) {
122 if ((0xff & hash[idx]) < 0x10) {
123 builder.append("0" + Integer.toHexString((0xff & hash[idx])));
124 } else {
125 builder.append(Integer.toHexString((0xff & hash[idx])));
126 }
127 }
128 return builder.toString();
129 }
130
131 } 90 }