From d4b67256993d155c374170b283fd3d6f0eb64cf7 Mon Sep 17 00:00:00 2001 From: dimgrichr <32580033+dimgrichr@users.noreply.github.com> Date: Tue, 15 Jan 2019 14:27:31 +0200 Subject: [PATCH 1/4] Cyclic redundancy check Algorithm Implementation of an CRC Algorithm, used in order to examine already received packets/messages. --- Others/CRCAlgorithm.java | 209 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 Others/CRCAlgorithm.java diff --git a/Others/CRCAlgorithm.java b/Others/CRCAlgorithm.java new file mode 100644 index 000000000..f26763db8 --- /dev/null +++ b/Others/CRCAlgorithm.java @@ -0,0 +1,209 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package crcalgorithm; + +import java.util.ArrayList; +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + +/** + * + * @author dimig + */ +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 Date: Tue, 15 Jan 2019 14:28:47 +0200 Subject: [PATCH 2/4] Delete CRCAlgorithm.java --- Others/CRCAlgorithm.java | 209 --------------------------------------- 1 file changed, 209 deletions(-) delete mode 100644 Others/CRCAlgorithm.java diff --git a/Others/CRCAlgorithm.java b/Others/CRCAlgorithm.java deleted file mode 100644 index f26763db8..000000000 --- a/Others/CRCAlgorithm.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package crcalgorithm; - -import java.util.ArrayList; -import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; - -/** - * - * @author dimig - */ -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 Date: Tue, 15 Jan 2019 14:32:01 +0200 Subject: [PATCH 3/4] 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 000000000..f932a9b74 --- /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 Date: Sun, 17 Feb 2019 20:56:47 +0800 Subject: [PATCH 4/4] Update CRCAlgorithm.java --- Others/CRCAlgorithm.java | 135 +++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 68 deletions(-) diff --git a/Others/CRCAlgorithm.java b/Others/CRCAlgorithm.java index f932a9b74..d53e3a02c 100644 --- a/Others/CRCAlgorithm.java +++ b/Others/CRCAlgorithm.java @@ -1,53 +1,53 @@ -package crcalgorithm; +package Others; 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 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 + * @param ber The Bit Error Rate */ - public CRCAlgorithm(String str, int size, double ber){ - messageChanged=false; + 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. + * 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 + * if true, it is checked + * otherwise, it is not */ - public void divideMessageWithP(boolean check){ + 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){ + if (!check) { + for (int z : dividedMessage) { message.add(z); } - } - else{ - if(dividedMessage.contains(1) && messageChanged){ + } else { + if (dividedMessage.contains(1) && messageChanged) { wrongMessCaught++; - } - else if(!dividedMessage.contains(1) && messageChanged){ + } else if (!dividedMessage.contains(1) && messageChanged) { wrongMessNotCaught++; - } - else if(!messageChanged){ + } 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, @@ -180,25 +181,23 @@ public class CRCAlgorithm { * Based on these changes. the boolean variable messageChanged, gets the value: * true, or false. */ - public void changeMess(){ - for(int y : message){ + public void changeMess() { + for (int y : message) { double x = randomGenerator.nextDouble(); - while(x<0.0000 || x>1.00000){ + while (x < 0.0000 || x > 1.00000) { x = randomGenerator.nextDouble(); } - if(x