Skip to content

Commit 31bf10f

Browse files
authored
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.
1 parent 8bd630f commit 31bf10f

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

Others/CRCAlgorithm.java

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

0 commit comments

Comments
 (0)