annotate src/main/java/alice/topology/manager/CreateHash.java @ 602:8a9fd716c335 dispose

change topology class extends CodeSegment from MetaCodeSegment, Chat minor fix
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Tue, 03 May 2016 20:09:45 +0900
parents 3284428f525e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
1 package alice.topology.manager;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
2
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
3 import java.security.MessageDigest;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
4 import java.security.NoSuchAlgorithmException;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
5 import java.util.LinkedList;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
6
599
3284428f525e add MetaCodeSegment & MetaDSM
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 584
diff changeset
7 import alice.codesegment.MetaCodeSegment;
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
8 import org.apache.commons.lang3.RandomStringUtils;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
9
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
10 import alice.codesegment.CodeSegment;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
11 import alice.datasegment.CommandType;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
12 import alice.datasegment.Receiver;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
13
584
8f765e3eed24 add Topology Manager Number to Topology Node Key
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 466
diff changeset
14 /**
8f765e3eed24 add Topology Manager Number to Topology Node Key
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 466
diff changeset
15 * create host cookie
8f765e3eed24 add Topology Manager Number to Topology Node Key
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 466
diff changeset
16 */
602
8a9fd716c335 change topology class extends CodeSegment from MetaCodeSegment, Chat minor fix
Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
parents: 599
diff changeset
17 public class CreateHash extends CodeSegment {
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
18 // this CodeSegment change raw string to MD5
466
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
19
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
20 private Receiver info = ids.create(CommandType.TAKE); // order to create
466
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
21 private Receiver info1 = ids.create(CommandType.TAKE);
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
22
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
23 public CreateHash(){
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
24 info.setKey("orderHash");
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
25 info1.setKey("createdList");
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
26 }
466
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
27
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
28 @Override
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
29 public void run() {
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
30 @SuppressWarnings("unchecked")
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
31 LinkedList<String> createdRawList = this.info1.asClass(LinkedList.class);
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
32 boolean checkNewStr = false;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
33 String raw = null;
466
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
34
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
35 while (!checkNewStr){
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
36 raw = RandomStringUtils.randomAscii(10);
466
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
37 // checking raw String has already created
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
38 int count = 0;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
39 for (String str : createdRawList) {
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
40 if (raw.equals(str)) break;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
41 count++;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
42 }
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
43 if (count == createdRawList.size())
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
44 checkNewStr = true;
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
45 }
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
46 createdRawList.add(raw);
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
47 ods.put(info1.key, createdRawList);
466
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
48
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
49 try { // convert to MD5
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
50 String MD5 = convertMD5(raw);
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
51
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
52 ods.put("MD5", MD5);
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
53 } catch (NoSuchAlgorithmException e) {
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
54 }
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
55 new CreateHash();
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
56 }
466
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
57
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
58 private String convertMD5(String raw) throws NoSuchAlgorithmException{
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
59 MessageDigest md = MessageDigest.getInstance("MD5");
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
60 md.update(raw.getBytes());
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
61 byte[] hash = md.digest();
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
62 StringBuilder builder = new StringBuilder();
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
63 for (int idx = 0; idx < hash.length; idx++) {
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
64 if ((0xff & hash[idx]) < 0x10) {
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
65 builder.append("0" + Integer.toHexString((0xff & hash[idx])));
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
66 } else {
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
67 builder.append(Integer.toHexString((0xff & hash[idx])));
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
68 }
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
69 }
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
70 return builder.toString();
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
71 }
466
268b1280dc18 remove debug message
sugi
parents: 435
diff changeset
72
431
0239c1633012 add CodeSegment for creating MD5
sugi
parents:
diff changeset
73 }