Skip to content

Commit d4b6725

Browse files
authored
Cyclic redundancy check Algorithm
Implementation of an CRC Algorithm, used in order to examine already received packets/messages.
1 parent 0a37179 commit d4b6725

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed

Others/CRCAlgorithm.java

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

0 commit comments

Comments
 (0)