Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | package de.viathinksoft.distributed.apps.immortal.deepcheck; |
2 | |||
3 | import java.io.BufferedReader; |
||
4 | import java.io.BufferedWriter; |
||
5 | import java.io.FileReader; |
||
6 | import java.io.FileWriter; |
||
7 | import java.io.IOException; |
||
8 | import java.io.Serializable; |
||
9 | import java.math.BigInteger; |
||
10 | import java.security.NoSuchAlgorithmException; |
||
11 | |||
12 | import de.viathinksoft.distributed.apps.immortal.exception.FileContentsBadException; |
||
13 | import de.viathinksoft.utils.security.MD5; |
||
14 | |||
15 | public class DeepImmortalCheckState implements Serializable { |
||
16 | |||
17 | private static final long serialVersionUID = -813320779387593520L; |
||
18 | |||
19 | private boolean immortal; |
||
20 | private BigInteger count; |
||
21 | private BigInteger initialNumber; |
||
22 | // Warning: There will be no check that continueAt is a valid |
||
23 | // part of the last result which myint did create. |
||
24 | private BigInteger lastResult; |
||
25 | |||
26 | public boolean isImmortal() { |
||
27 | return immortal; |
||
28 | } |
||
29 | |||
30 | public BigInteger getCount() { |
||
31 | return count; |
||
32 | } |
||
33 | |||
34 | public BigInteger getInitialNumber() { |
||
35 | return initialNumber; |
||
36 | } |
||
37 | |||
38 | public BigInteger getLastResult() { |
||
39 | return lastResult; |
||
40 | } |
||
41 | |||
42 | public DeepImmortalCheckState(boolean immortal, BigInteger count, BigInteger initialNumber, BigInteger lastResult) { |
||
43 | super(); |
||
44 | this.immortal = immortal; |
||
45 | this.count = count; |
||
46 | this.initialNumber = initialNumber; |
||
47 | this.lastResult = lastResult; |
||
48 | } |
||
49 | |||
50 | public DeepImmortalCheckState(String filename) throws IOException, NoSuchAlgorithmException, FileContentsBadException { |
||
51 | super(); |
||
52 | loadFromFile(filename); |
||
53 | } |
||
54 | |||
55 | @Override |
||
56 | public String toString() { |
||
57 | return "["+initialNumber+"] " + immortal + " (#" + count + ": " + lastResult + ")"; |
||
58 | } |
||
59 | |||
60 | // TODO equals, clone |
||
61 | |||
62 | private final String SIGNATURE = "Immortal Result Set V001"; |
||
63 | private final String SALT = "omrtjsdflmaeafuinalafm4iosaFFA$§Eeqoqw09kfslmflafnASFmgalm092k928jlamrnln"; |
||
64 | |||
65 | public void saveToFile(String filename) throws IOException, NoSuchAlgorithmException { |
||
66 | BufferedWriter x = new BufferedWriter(new FileWriter(filename)); |
||
67 | |||
68 | String s; |
||
69 | x.write(SIGNATURE); |
||
70 | x.write("\r\n"); |
||
71 | |||
72 | s = this.initialNumber.toString(); |
||
73 | x.write(s); |
||
74 | x.write("\r\n"); |
||
75 | x.write(MD5.digest(s, SALT)); |
||
76 | x.write("\r\n"); |
||
77 | |||
78 | s = Boolean.toString(this.immortal); |
||
79 | x.write(s); |
||
80 | x.write("\r\n"); |
||
81 | x.write(MD5.digest(s, SALT)); |
||
82 | x.write("\r\n"); |
||
83 | |||
84 | s = this.count.toString(); |
||
85 | x.write(s); |
||
86 | x.write("\r\n"); |
||
87 | x.write(MD5.digest(s, SALT)); |
||
88 | x.write("\r\n"); |
||
89 | |||
90 | s = this.lastResult.toString(); |
||
91 | x.write(s); |
||
92 | x.write("\r\n"); |
||
93 | x.write(MD5.digest(s, SALT)); |
||
94 | x.write("\r\n"); |
||
95 | |||
96 | x.close(); |
||
97 | } |
||
98 | |||
99 | public void loadFromFile(String filename) throws IOException, NoSuchAlgorithmException, FileContentsBadException { |
||
100 | BufferedReader r = new BufferedReader(new FileReader(filename)); |
||
101 | |||
102 | String f_sig = r.readLine(); |
||
103 | if ((f_sig == null) || (!f_sig.equals(SIGNATURE))) { |
||
104 | throw new FileContentsBadException(); |
||
105 | } |
||
106 | |||
107 | String f_initialnumber = r.readLine(); |
||
108 | if ((f_initialnumber == null) || (!MD5.digest(f_initialnumber, SALT).equals(r.readLine()))) { |
||
109 | throw new FileContentsBadException(); |
||
110 | } |
||
111 | |||
112 | String f_immortal = r.readLine(); |
||
113 | if ((f_immortal == null) || (!MD5.digest(f_immortal, SALT).equals(r.readLine()))) { |
||
114 | throw new FileContentsBadException(); |
||
115 | } |
||
116 | |||
117 | String f_count = r.readLine(); |
||
118 | if ((f_count == null) || (!MD5.digest(f_count, SALT).equals(r.readLine()))) { |
||
119 | throw new FileContentsBadException(); |
||
120 | } |
||
121 | |||
122 | String f_lastresult = r.readLine(); |
||
123 | if ((f_lastresult == null) || (!MD5.digest(f_lastresult, SALT).equals(r.readLine()))) { |
||
124 | throw new FileContentsBadException(); |
||
125 | } |
||
126 | |||
127 | r.close(); |
||
128 | |||
129 | this.initialNumber = new BigInteger(f_initialnumber); |
||
130 | this.immortal = Boolean.valueOf(f_immortal); |
||
131 | this.count = new BigInteger(f_count); |
||
132 | this.lastResult = new BigInteger(f_lastresult); |
||
133 | } |
||
134 | |||
135 | } |