|
| 1 | +package Others; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Random; |
| 5 | +import java.util.concurrent.ThreadLocalRandom; |
| 6 | + |
| 7 | +/** |
| 8 | + * @author dimgrichr |
| 9 | + */ |
| 10 | +public class CRCAlgorithm { |
| 11 | + |
| 12 | + private int correctMess; |
| 13 | + |
| 14 | + private int wrongMess; |
| 15 | + |
| 16 | + private int wrongMessCaught; |
| 17 | + |
| 18 | + private int wrongMessNotCaught; |
| 19 | + |
| 20 | + private int messSize; |
| 21 | + |
| 22 | + private double ber; |
| 23 | + |
| 24 | + private boolean messageChanged; |
| 25 | + |
| 26 | + private ArrayList<Integer> message; |
| 27 | + |
| 28 | + private ArrayList<Integer> dividedMessage; |
| 29 | + |
| 30 | + private ArrayList<Integer> p; |
| 31 | + |
| 32 | + private Random randomGenerator; |
| 33 | + |
| 34 | + |
| 35 | + /** |
| 36 | + * The algorithm's main constructor. |
| 37 | + * The most significant variables, used in the algorithm, |
| 38 | + * are set in their initial values. |
| 39 | + * |
| 40 | + * @param str The binary number P, in a string form, which is used by the CRC algorithm |
| 41 | + * @param size The size of every transmitted message |
| 42 | + * @param ber The Bit Error Rate |
| 43 | + */ |
| 44 | + public CRCAlgorithm(String str, int size, double ber) { |
| 45 | + messageChanged = false; |
| 46 | + message = new ArrayList<>(); |
| 47 | + messSize = size; |
| 48 | + dividedMessage = new ArrayList<>(); |
| 49 | + p = new ArrayList<>(); |
| 50 | + for (int i = 0; i < str.length(); i++) { |
| 51 | + p.add(Character.getNumericValue(str.charAt(i))); |
| 52 | + } |
| 53 | + randomGenerator = new Random(); |
| 54 | + correctMess = 0; |
| 55 | + wrongMess = 0; |
| 56 | + wrongMessCaught = 0; |
| 57 | + wrongMessNotCaught = 0; |
| 58 | + this.ber = ber; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the counter wrongMess |
| 64 | + * |
| 65 | + * @return wrongMess, the number of Wrong Messages |
| 66 | + */ |
| 67 | + public int getWrongMess() { |
| 68 | + return wrongMess; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the counter wrongMessCaught |
| 73 | + * |
| 74 | + * @return wrongMessCaught, the number of wrong messages, which are caught by the CRC algoriithm |
| 75 | + */ |
| 76 | + public int getWrongMessCaught() { |
| 77 | + return wrongMessCaught; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the counter wrongMessNotCaught |
| 82 | + * |
| 83 | + * @return wrongMessNotCaught, the number of wrong messages, which are not caught by the CRC algorithm |
| 84 | + */ |
| 85 | + public int getWrongMessNotCaught() { |
| 86 | + return wrongMessNotCaught; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns the counter correctMess |
| 91 | + * |
| 92 | + * @return correctMess, the number of the Correct Messages |
| 93 | + */ |
| 94 | + public int getCorrectMess() { |
| 95 | + return correctMess; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Resets some of the object's values, used on the main function, |
| 100 | + * so that it can be re-used, in order not to waste too much memory and time, |
| 101 | + * by creating new objects. |
| 102 | + */ |
| 103 | + public void refactor() { |
| 104 | + messageChanged = false; |
| 105 | + message = new ArrayList<>(); |
| 106 | + dividedMessage = new ArrayList<>(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Random messages, consisted of 0's and 1's, |
| 111 | + * are generated, so that they can later be transmitted |
| 112 | + */ |
| 113 | + public void generateRandomMess() { |
| 114 | + for (int i = 0; i < messSize; i++) { |
| 115 | + int x = ThreadLocalRandom.current().nextInt(0, 2); |
| 116 | + message.add(x); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * The most significant part of the CRC algorithm. |
| 122 | + * The message is divided by P, so the dividedMessage ArrayList<Integer> is created. |
| 123 | + * If check == true, the dividedMessaage is examined, in order to see if it contains any 1's. |
| 124 | + * If it does, the message is considered to be wrong by the receiver,so the variable wrongMessCaught changes. |
| 125 | + * If it does not, it is accepted, so one of the variables correctMess, wrongMessNotCaught, changes. |
| 126 | + * If check == false, the diviided Message is added at the end of the ArrayList<integer> message. |
| 127 | + * |
| 128 | + * @param check the variable used to determine, if the message is going to be checked from the receiver |
| 129 | + * if true, it is checked |
| 130 | + * otherwise, it is not |
| 131 | + */ |
| 132 | + public void divideMessageWithP(boolean check) { |
| 133 | + ArrayList<Integer> x = new ArrayList<>(); |
| 134 | + ArrayList<Integer> k = (ArrayList<Integer>) message.clone(); |
| 135 | + if (!check) { |
| 136 | + for (int i = 0; i < p.size() - 1; i++) { |
| 137 | + k.add(0); |
| 138 | + } |
| 139 | + } |
| 140 | + while (!k.isEmpty()) { |
| 141 | + while (x.size() < p.size() && !k.isEmpty()) { |
| 142 | + x.add(k.get(0)); |
| 143 | + k.remove(0); |
| 144 | + } |
| 145 | + if (x.size() == p.size()) { |
| 146 | + for (int i = 0; i < p.size(); i++) { |
| 147 | + if (x.get(i) == p.get(i)) { |
| 148 | + x.set(i, 0); |
| 149 | + } else { |
| 150 | + x.set(i, 1); |
| 151 | + } |
| 152 | + } |
| 153 | + for (int i = 0; i < x.size() && x.get(i) != 1; i++) { |
| 154 | + x.remove(0); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + dividedMessage = (ArrayList<Integer>) x.clone(); |
| 159 | + if (!check) { |
| 160 | + for (int z : dividedMessage) { |
| 161 | + message.add(z); |
| 162 | + } |
| 163 | + } else { |
| 164 | + if (dividedMessage.contains(1) && messageChanged) { |
| 165 | + wrongMessCaught++; |
| 166 | + } else if (!dividedMessage.contains(1) && messageChanged) { |
| 167 | + wrongMessNotCaught++; |
| 168 | + } else if (!messageChanged) { |
| 169 | + correctMess++; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Once the message is transmitted, some of it's elements, |
| 176 | + * is possible to change from 1 to 0, or from 0 to 1, |
| 177 | + * because of the Bit Error Rate (ber). |
| 178 | + * For every element of the message, a random double number is created. |
| 179 | + * If that number is smaller than ber, then the spesific element changes. |
| 180 | + * On the other hand, if it's bigger than ber, it does not. |
| 181 | + * Based on these changes. the boolean variable messageChanged, gets the value: |
| 182 | + * true, or false. |
| 183 | + */ |
| 184 | + public void changeMess() { |
| 185 | + for (int y : message) { |
| 186 | + double x = randomGenerator.nextDouble(); |
| 187 | + while (x < 0.0000 || x > 1.00000) { |
| 188 | + x = randomGenerator.nextDouble(); |
| 189 | + } |
| 190 | + if (x < ber) { |
| 191 | + messageChanged = true; |
| 192 | + if (y == 1) { |
| 193 | + message.set(message.indexOf(y), 0); |
| 194 | + } else { |
| 195 | + message.set(message.indexOf(y), 1); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + if (messageChanged) { |
| 200 | + wrongMess++; |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments