From 31bf10f29f8f76c36585aa965337a7325598efd4 Mon Sep 17 00:00:00 2001 From: dimgrichr <32580033+dimgrichr@users.noreply.github.com> Date: Tue, 15 Jan 2019 14:32:01 +0200 Subject: [PATCH] Cyclic Redundancy Check Algorithm Implementation of a CRC algorithm, used in order to examine received messages/packets for any errors. This type of algorithms, is widely used in networks. --- Others/CRCAlgorithm.java | 204 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 Others/CRCAlgorithm.java diff --git a/Others/CRCAlgorithm.java b/Others/CRCAlgorithm.java new file mode 100644 index 00000000..f932a9b7 --- /dev/null +++ b/Others/CRCAlgorithm.java @@ -0,0 +1,204 @@ +package crcalgorithm; + +import java.util.ArrayList; +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + +/** + * + * @author dimgrichr + */ +public class CRCAlgorithm { + + private int correctMess; + + private int wrongMess; + + private int wrongMessCaught; + + private int wrongMessNotCaught; + + private int messSize; + + private double ber; + + private boolean messageChanged; + + private ArrayList message; + + private ArrayList dividedMessage; + + private ArrayList p; + + private Random randomGenerator; + + + /** + * The algorithm's main constructor. + * The most significant variables, used in the algorithm, + * are set in their initial values. + * @param str The binary number P, in a string form, which is used by the CRC algorithm + * @param size The size of every transmitted message + * @param ber The Bit Error Rate + */ + public CRCAlgorithm(String str, int size, double ber){ + messageChanged=false; + message = new ArrayList<>(); + messSize = size; + dividedMessage = new ArrayList<>(); + p = new ArrayList<>(); + for(int i=0;i(); + dividedMessage = new ArrayList<>(); + } + + /** + * Random messages, consisted of 0's and 1's, + * are generated, so that they can later be transmitted + */ + public void generateRandomMess(){ + for(int i=0;i 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. + * If check == false, the diviided Message is added at the end of the ArrayList message. + * @param check the variable used to determine, if the message is going to be checked from the receiver + * if true, it is checked + * otherwise, it is not + */ + public void divideMessageWithP(boolean check){ + ArrayList x = new ArrayList<>(); + ArrayList k = (ArrayList) message.clone(); + if(!check){ + for(int i=0;i) x.clone(); + if(!check){ + for(int z:dividedMessage){ + message.add(z); + } + } + else{ + if(dividedMessage.contains(1) && messageChanged){ + wrongMessCaught++; + } + else if(!dividedMessage.contains(1) && messageChanged){ + wrongMessNotCaught++; + } + else if(!messageChanged){ + correctMess++; + } + } + } + + /** + * 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. + */ + public void changeMess(){ + for(int y : message){ + double x = randomGenerator.nextDouble(); + while(x<0.0000 || x>1.00000){ + x = randomGenerator.nextDouble(); + } + if(x