2019-02-17 20:56:47 +08:00
|
|
|
package Others;
|
2019-01-15 20:32:01 +08:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author dimgrichr
|
|
|
|
*/
|
|
|
|
public class CRCAlgorithm {
|
|
|
|
|
|
|
|
private int correctMess;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private int wrongMess;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private int wrongMessCaught;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private int wrongMessNotCaught;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private int messSize;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private double ber;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private boolean messageChanged;
|
|
|
|
|
|
|
|
private ArrayList<Integer> message;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private ArrayList<Integer> dividedMessage;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private ArrayList<Integer> p;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
private Random randomGenerator;
|
2019-02-17 20:56:47 +08:00
|
|
|
|
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* The algorithm's main constructor.
|
|
|
|
* The most significant variables, used in the algorithm,
|
|
|
|
* are set in their initial values.
|
2019-02-17 20:56:47 +08:00
|
|
|
*
|
|
|
|
* @param str The binary number P, in a string form, which is used by the CRC algorithm
|
2019-01-15 20:32:01 +08:00
|
|
|
* @param size The size of every transmitted message
|
2019-02-17 20:56:47 +08:00
|
|
|
* @param ber The Bit Error Rate
|
2019-01-15 20:32:01 +08:00
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public CRCAlgorithm(String str, int size, double ber) {
|
|
|
|
messageChanged = false;
|
2019-01-15 20:32:01 +08:00
|
|
|
message = new ArrayList<>();
|
|
|
|
messSize = size;
|
|
|
|
dividedMessage = new ArrayList<>();
|
|
|
|
p = new ArrayList<>();
|
2019-02-17 20:56:47 +08:00
|
|
|
for (int i = 0; i < str.length(); i++) {
|
2019-01-15 20:32:01 +08:00
|
|
|
p.add(Character.getNumericValue(str.charAt(i)));
|
|
|
|
}
|
|
|
|
randomGenerator = new Random();
|
|
|
|
correctMess = 0;
|
|
|
|
wrongMess = 0;
|
|
|
|
wrongMessCaught = 0;
|
|
|
|
wrongMessNotCaught = 0;
|
|
|
|
this.ber = ber;
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
|
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* Returns the counter wrongMess
|
2019-02-17 20:56:47 +08:00
|
|
|
*
|
2019-01-15 20:32:01 +08:00
|
|
|
* @return wrongMess, the number of Wrong Messages
|
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public int getWrongMess() {
|
2019-01-15 20:32:01 +08:00
|
|
|
return wrongMess;
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* Returns the counter wrongMessCaught
|
2019-02-17 20:56:47 +08:00
|
|
|
*
|
2019-01-15 20:32:01 +08:00
|
|
|
* @return wrongMessCaught, the number of wrong messages, which are caught by the CRC algoriithm
|
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public int getWrongMessCaught() {
|
2019-01-15 20:32:01 +08:00
|
|
|
return wrongMessCaught;
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* Returns the counter wrongMessNotCaught
|
2019-02-17 20:56:47 +08:00
|
|
|
*
|
2019-01-15 20:32:01 +08:00
|
|
|
* @return wrongMessNotCaught, the number of wrong messages, which are not caught by the CRC algorithm
|
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public int getWrongMessNotCaught() {
|
2019-01-15 20:32:01 +08:00
|
|
|
return wrongMessNotCaught;
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* Returns the counter correctMess
|
2019-02-17 20:56:47 +08:00
|
|
|
*
|
2019-01-15 20:32:01 +08:00
|
|
|
* @return correctMess, the number of the Correct Messages
|
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public int getCorrectMess() {
|
2019-01-15 20:32:01 +08:00
|
|
|
return correctMess;
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* Resets some of the object's values, used on the main function,
|
|
|
|
* so that it can be re-used, in order not to waste too much memory and time,
|
|
|
|
* by creating new objects.
|
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public void refactor() {
|
2019-01-15 20:32:01 +08:00
|
|
|
messageChanged = false;
|
|
|
|
message = new ArrayList<>();
|
|
|
|
dividedMessage = new ArrayList<>();
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* Random messages, consisted of 0's and 1's,
|
|
|
|
* are generated, so that they can later be transmitted
|
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public void generateRandomMess() {
|
|
|
|
for (int i = 0; i < messSize; i++) {
|
|
|
|
int x = ThreadLocalRandom.current().nextInt(0, 2);
|
2019-01-15 20:32:01 +08:00
|
|
|
message.add(x);
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* The most significant part of the CRC algorithm.
|
|
|
|
* The message is divided by P, so the dividedMessage ArrayList<Integer> is created.
|
|
|
|
* If check == true, the dividedMessaage is examined, in order to see if it contains any 1's.
|
|
|
|
* If it does, the message is considered to be wrong by the receiver,so the variable wrongMessCaught changes.
|
|
|
|
* If it does not, it is accepted, so one of the variables correctMess, wrongMessNotCaught, changes.
|
2019-02-17 20:56:47 +08:00
|
|
|
* If check == false, the diviided Message is added at the end of the ArrayList<integer> message.
|
|
|
|
*
|
2019-01-15 20:32:01 +08:00
|
|
|
* @param check the variable used to determine, if the message is going to be checked from the receiver
|
2019-02-17 20:56:47 +08:00
|
|
|
* if true, it is checked
|
|
|
|
* otherwise, it is not
|
2019-01-15 20:32:01 +08:00
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public void divideMessageWithP(boolean check) {
|
2019-01-15 20:32:01 +08:00
|
|
|
ArrayList<Integer> x = new ArrayList<>();
|
|
|
|
ArrayList<Integer> k = (ArrayList<Integer>) message.clone();
|
2019-02-17 20:56:47 +08:00
|
|
|
if (!check) {
|
|
|
|
for (int i = 0; i < p.size() - 1; i++) {
|
2019-01-15 20:32:01 +08:00
|
|
|
k.add(0);
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
while (!k.isEmpty()) {
|
|
|
|
while (x.size() < p.size() && !k.isEmpty()) {
|
2019-01-15 20:32:01 +08:00
|
|
|
x.add(k.get(0));
|
|
|
|
k.remove(0);
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
if (x.size() == p.size()) {
|
|
|
|
for (int i = 0; i < p.size(); i++) {
|
|
|
|
if (x.get(i) == p.get(i)) {
|
2019-01-15 20:32:01 +08:00
|
|
|
x.set(i, 0);
|
2019-02-17 20:56:47 +08:00
|
|
|
} else {
|
2019-01-15 20:32:01 +08:00
|
|
|
x.set(i, 1);
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
for (int i = 0; i < x.size() && x.get(i) != 1; i++) {
|
2019-01-15 20:32:01 +08:00
|
|
|
x.remove(0);
|
2019-02-17 20:56:47 +08:00
|
|
|
}
|
2019-01-15 20:32:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dividedMessage = (ArrayList<Integer>) x.clone();
|
2019-02-17 20:56:47 +08:00
|
|
|
if (!check) {
|
|
|
|
for (int z : dividedMessage) {
|
2019-01-15 20:32:01 +08:00
|
|
|
message.add(z);
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
} else {
|
|
|
|
if (dividedMessage.contains(1) && messageChanged) {
|
2019-01-15 20:32:01 +08:00
|
|
|
wrongMessCaught++;
|
2019-02-17 20:56:47 +08:00
|
|
|
} else if (!dividedMessage.contains(1) && messageChanged) {
|
2019-01-15 20:32:01 +08:00
|
|
|
wrongMessNotCaught++;
|
2019-02-17 20:56:47 +08:00
|
|
|
} else if (!messageChanged) {
|
2019-01-15 20:32:01 +08:00
|
|
|
correctMess++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
|
2019-01-15 20:32:01 +08:00
|
|
|
/**
|
|
|
|
* Once the message is transmitted, some of it's elements,
|
|
|
|
* is possible to change from 1 to 0, or from 0 to 1,
|
|
|
|
* because of the Bit Error Rate (ber).
|
|
|
|
* For every element of the message, a random double number is created.
|
|
|
|
* If that number is smaller than ber, then the spesific element changes.
|
|
|
|
* On the other hand, if it's bigger than ber, it does not.
|
|
|
|
* Based on these changes. the boolean variable messageChanged, gets the value:
|
|
|
|
* true, or false.
|
|
|
|
*/
|
2019-02-17 20:56:47 +08:00
|
|
|
public void changeMess() {
|
|
|
|
for (int y : message) {
|
2019-01-15 20:32:01 +08:00
|
|
|
double x = randomGenerator.nextDouble();
|
2019-02-17 20:56:47 +08:00
|
|
|
while (x < 0.0000 || x > 1.00000) {
|
2019-01-15 20:32:01 +08:00
|
|
|
x = randomGenerator.nextDouble();
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
if (x < ber) {
|
2019-01-15 20:32:01 +08:00
|
|
|
messageChanged = true;
|
2019-02-17 20:56:47 +08:00
|
|
|
if (y == 1) {
|
2019-01-15 20:32:01 +08:00
|
|
|
message.set(message.indexOf(y), 0);
|
2019-02-17 20:56:47 +08:00
|
|
|
} else {
|
2019-01-15 20:32:01 +08:00
|
|
|
message.set(message.indexOf(y), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 20:56:47 +08:00
|
|
|
if (messageChanged) {
|
2019-01-15 20:32:01 +08:00
|
|
|
wrongMess++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|